@@ -11,18 +11,23 @@ export class GenerateNGT {
1111 private options : GltfGeneratorSchema ,
1212 ) { }
1313
14+ async generate ( ) {
15+ return this . analyzedGLTF . gltf . scene . children . map ( ( child ) => this . print ( child ) ) . join ( '\n' ) ;
16+ }
17+
1418 async print ( obj : Object3D ) {
15- const { nodeName, isRemoved, isChildless , isTargetedLight, isInstancedMesh, sanitizeName } = await import (
19+ const { nodeName, isRemoved, isTargetedLight, isInstancedMesh, sanitizeName } = await import (
1620 '@rosskevin/gltfjsx'
1721 ) ;
1822
1923 let result = '' ;
2024 let children = '' ;
2125
22- if ( isRemoved ( obj ) && ! isChildless ( obj ) ) {
23- obj . children . forEach ( ( child ) => ( result += this . print ( child ) ) ) ;
24- return result ;
25- }
26+ // Children
27+ if ( obj . children ) obj . children . forEach ( ( child ) => ( children += this . print ( child ) ) ) ;
28+
29+ // Bail out if the object was pruned
30+ if ( isRemoved ( obj ) ) return children ;
2631
2732 const { bones } = this . options ;
2833 const node = nodeName ( obj ) ;
@@ -42,11 +47,8 @@ export class GenerateNGT {
4247</${ ngtType } >` ;
4348 }
4449
45- // Collect children
46- if ( obj . children ) obj . children . forEach ( ( child ) => ( children += this . print ( child ) ) ) ;
47-
4850 // TODO: Instances are currently not supported for NGT components
49-
51+ //
5052 if ( isInstancedMesh ( obj ) ) {
5153 const geo = `gltf.${ node } .geometry` ;
5254 const mat =
@@ -62,22 +64,56 @@ export class GenerateNGT {
6264 }
6365 }
6466
67+ let shoudSkipName = false ;
6568 if (
6669 obj . name . length &&
6770 'morphTargetDictionary' in obj &&
6871 ! ! obj . morphTargetDictionary &&
6972 this . analyzedGLTF . hasAnimations ( )
7073 ) {
74+ shoudSkipName = true ;
7175 result += `name="${ obj . name } " ` ;
7276 }
7377
74- const oldResult = result ;
75- result += this . handleAngularInputs ( obj ) ;
78+ result += this . handleAngularInputs ( obj , shoudSkipName ) ;
79+
80+ if ( children . length ) {
81+ // Add children and close the element's tag
82+ result += `>
83+ ${ children }
84+ </${ ngtType } >` ;
85+ } else {
86+ // Close this element's tag
87+ result += `/>` ;
88+ }
89+
7690 return result ;
7791 }
7892
79- private handleAngularInputs ( obj : Object3D ) {
80- return '' ;
93+ private handleAngularInputs ( obj : Object3D , skipName = false ) {
94+ const properties = this . analyzedGLTF . calculateProps ( obj ) ;
95+
96+ let propertiesString = '' ;
97+
98+ for ( const key in properties ) {
99+ const value = properties [ key ] ;
100+
101+ if ( key === 'name' ) {
102+ if ( skipName ) continue ;
103+ propertiesString += `name="${ value } " ` ;
104+ continue ;
105+ }
106+
107+ if ( value === true ) {
108+ // i.e: castShadow, receiveShadow
109+ propertiesString += `${ key } ` ;
110+ continue ;
111+ }
112+
113+ propertiesString += `[${ key } ]="${ value } " ` ;
114+ }
115+
116+ return propertiesString ;
81117 }
82118
83119 private getType ( obj : Object3D ) {
0 commit comments