import { BufferData , ComponentManager , CullableComponent , Entity , GeometryComponent , HierarchyComponent , IDENTIFIER , MaterialComponent , MeshComponent , SceneGraphSystem , TransformComponent , } from '@antv/g-webgpu-core' ; import { inject , injectable , named } from 'inversify' ; export interface IRenderable < T > { setConfig ( config ) : void ; setAttributes ( attributes ) : void ; setEntity ( entity ) : void ; setMaterial ( material ) : void ; setGeometry ( geometry ) : void ; } @ injectable ( ) export class Renderable < T = { } > implements IRenderable < T > { public static POINT = 'point' ; public static LINE = 'line' ; public static GRID = 'grid' ; protected attributes : Record < string , BufferData > = { } ; protected config : T ; @ inject ( IDENTIFIER . MeshComponentManager ) private readonly mesh : ComponentManager < MeshComponent > ; @ inject ( IDENTIFIER . CullableComponentManager ) private readonly cullable : ComponentManager < CullableComponent > ; @ inject ( IDENTIFIER . TransformComponentManager ) private readonly transform : ComponentManager < TransformComponent > ; @ inject ( IDENTIFIER . Systems ) @ named ( IDENTIFIER . SceneGraphSystem ) private readonly sceneGraphSystem : SceneGraphSystem ; private meshComponent : MeshComponent ; private transformComponent : TransformComponent ; private entity : Entity ; public getEntity ( ) { return this . entity ; } public getTransformComponent ( ) { return this . transformComponent ; } public getMeshComponent ( ) { return this . meshComponent ; } public setConfig ( config ) { this . config = config ; } public setEntity ( entity ) { this . entity = entity ; this . cullable . create ( entity ) ; this . meshComponent = this . mesh . create ( entity ) ; this . transformComponent = this . transform . create ( entity ) ; this . onEntityCreated ( ) ; } public setMaterial ( material ) { this . meshComponent . material = material ; return this ; } public setGeometry ( geometry ) { this . meshComponent . geometry = geometry ; return this ; } public setAttributes ( attributes ) { Object . keys ( attributes ) . forEach ( ( name ) => { if ( attributes [ name ] !== undefined && attributes [ name ] !== this . attributes [ name ] ) { this . onAttributeChanged ( { name , data : attributes [ name ] ! , } ) ; this . attributes [ name ] = attributes [ name ] ! ; } } ) ; } public setVisible ( visible ) { this . meshComponent . visible = visible ; this . meshComponent . children . forEach ( ( childEntity ) => { const child = this . mesh . getComponentByEntity ( childEntity ) ; if ( child ) { child . visible = visible ; } } ) ; return this ; } public isVisible ( ) { return this . meshComponent . visible ; } public attach ( parentRenderable ) { this . sceneGraphSystem . attach ( this . entity , parentRenderable . entity ) ; return this ; } public detach ( ) { this . sceneGraphSystem . detach ( this . entity ) ; return this ; } public detachChildren ( ) { this . sceneGraphSystem . detachChildren ( this . entity ) ; return this ; } protected onEntityCreated ( ) { } protected onAttributeChanged ( { name , data , } ) { if ( this . meshComponent && this . meshComponent . material ) { this . meshComponent . material . setUniform ( this . convertAttributeName2UniformName ( name ) , data ) ; } } protected convertAttributeName2UniformName ( attributeName ) { return attributeName ; } }