@@ -11,18 +11,23 @@ export class GenerateNGT {
11
11
private options : GltfGeneratorSchema ,
12
12
) { }
13
13
14
+ async generate ( ) {
15
+ return this . analyzedGLTF . gltf . scene . children . map ( ( child ) => this . print ( child ) ) . join ( '\n' ) ;
16
+ }
17
+
14
18
async print ( obj : Object3D ) {
15
- const { nodeName, isRemoved, isChildless , isTargetedLight, isInstancedMesh, sanitizeName } = await import (
19
+ const { nodeName, isRemoved, isTargetedLight, isInstancedMesh, sanitizeName } = await import (
16
20
'@rosskevin/gltfjsx'
17
21
) ;
18
22
19
23
let result = '' ;
20
24
let children = '' ;
21
25
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 ;
26
31
27
32
const { bones } = this . options ;
28
33
const node = nodeName ( obj ) ;
@@ -42,11 +47,8 @@ export class GenerateNGT {
42
47
</${ ngtType } >` ;
43
48
}
44
49
45
- // Collect children
46
- if ( obj . children ) obj . children . forEach ( ( child ) => ( children += this . print ( child ) ) ) ;
47
-
48
50
// TODO: Instances are currently not supported for NGT components
49
-
51
+ //
50
52
if ( isInstancedMesh ( obj ) ) {
51
53
const geo = `gltf.${ node } .geometry` ;
52
54
const mat =
@@ -62,22 +64,56 @@ export class GenerateNGT {
62
64
}
63
65
}
64
66
67
+ let shoudSkipName = false ;
65
68
if (
66
69
obj . name . length &&
67
70
'morphTargetDictionary' in obj &&
68
71
! ! obj . morphTargetDictionary &&
69
72
this . analyzedGLTF . hasAnimations ( )
70
73
) {
74
+ shoudSkipName = true ;
71
75
result += `name="${ obj . name } " ` ;
72
76
}
73
77
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
+
76
90
return result ;
77
91
}
78
92
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 ;
81
117
}
82
118
83
119
private getType ( obj : Object3D ) {
0 commit comments