import { AABB , generateAABBFromVertices , GeometryComponent , } from '@antv/g-webgpu-core' ; import { vec3 } from 'gl-matrix' ; import { inject , injectable } from 'inversify' ; import { isNumber , isTypedArray } from 'lodash' ; import { Geometry } from '.' ; import { merge } from '../utils/typedarray' ; export interface IMergedGeometryParams { geometries : GeometryComponent [ ] ; } @ injectable ( ) export class Merged extends Geometry < Partial < IMergedGeometryParams > > { protected onEntityCreated ( ) { const { geometries = [ ] } = this . config ; const mergedComponent = this . getComponent ( ) ; mergedComponent . aabb = new AABB ( ) ; const mergedAttributes = [ ] ; const mergedIndices = [ ] ; let indexOffset = 0 ; geometries . forEach ( ( geometry ) => { const { aabb , indices , vertexCount , attributes } = geometry ; mergedComponent . aabb . add ( aabb ) ; mergedComponent . vertexCount += vertexCount ; if ( indices ) { mergedIndices . push ( ... indices . map ( ( index ) => index + indexOffset ) ) ; } indexOffset += vertexCount ; attributes . forEach ( ( attribute , i ) => { if ( ! mergedAttributes [ i ] ) { mergedAttributes [ i ] = attribute ; mergedAttributes [ i ] . dirty = true ; } else { if ( attribute . data ) { if ( isNumber ( attribute . data ) ) { mergedAttributes [ i ] . push ( attribute . data ) ; } else if ( isTypedArray ( attribute . data ) ) { mergedAttributes [ i ] . data = merge ( mergedAttributes [ i ] . data , attribute . data ) ; } else { mergedAttributes [ i ] . data = mergedAttributes [ i ] . data . concat ( attribute . data ) ; } } } } ) ; } ) ; mergedComponent . attributes = mergedAttributes ; mergedComponent . indices = Uint32Array . from ( mergedIndices ) ; mergedComponent . dirty = true ; } }