1
1
import { formatFiles , generateFiles , names , Tree } from '@nx/devkit' ;
2
- import { dirname , join } from 'node:path' ;
2
+ import { dirname , join , resolve } from 'node:path' ;
3
3
import { GenerateNGT } from './utils/generate-ngt' ;
4
4
5
5
export interface GltfGeneratorSchema {
@@ -15,30 +15,66 @@ export interface GltfGeneratorSchema {
15
15
console : boolean ;
16
16
instance : boolean ;
17
17
instanceAll : boolean ;
18
+ transform : boolean ;
19
+ degrade : string ;
20
+ degradeResolution : number ;
18
21
resolution : number ;
19
22
keepMeshes : boolean ;
20
23
keepMaterials : boolean ;
21
24
keepAttributes : boolean ;
22
25
keepNames : boolean ;
23
26
keepGroups : boolean ;
24
- format : string ;
27
+ format : 'jpeg' | 'png' | 'webp' | 'avif' ;
25
28
simplify : boolean ;
26
29
ratio : number ;
27
30
error : number ;
31
+ header : string ;
28
32
verbose : boolean ;
29
33
}
30
34
31
35
export async function gltfGenerator ( tree : Tree , options : GltfGeneratorSchema ) {
32
- const { loadGLTF, AnalyzedGLTF, gltfTransform, Log, allPruneStrategies } = await import ( '@rosskevin/gltfjsx' ) ;
36
+ const { loadGLTF, AnalyzedGLTF, gltfTransform, Log, allPruneStrategies, compareFileSizes } = await import (
37
+ '@rosskevin/gltfjsx'
38
+ ) ;
33
39
34
40
const modelPath = join ( tree . root , options . modelPath ) ;
41
+ const log = new Log ( { debug : options . verbose , silent : false } ) ;
42
+
43
+ //
44
+ // Transform the GLTF file if necessary using gltf-transform
45
+ //
46
+ let size = '' ;
47
+ let transformedModelPath : string | undefined = undefined ;
48
+ if ( options . transform ) {
49
+ transformedModelPath = resolve ( modelPath + '-transformed.glb' ) ;
50
+ await gltfTransform ( modelPath , transformedModelPath , {
51
+ format : options . format ,
52
+ degrade : options . degrade ,
53
+ degraderesolution : options . degradeResolution ,
54
+ simplify : options . simplify ? { ratio : options . ratio , error : options . error } : false ,
55
+ log,
56
+ bones : options . bones ,
57
+ meta : options . meta ,
58
+ shadows : options . shadows ,
59
+ instance : options . instance ,
60
+ instanceall : options . instanceAll ,
61
+ keepgroups : options . keepGroups ,
62
+ keepnames : options . keepNames ,
63
+ precision : options . precision ,
64
+ keepattributes : options . keepAttributes ,
65
+ keepmeshes : options . keepMeshes ,
66
+ keepmaterials : options . keepMaterials ,
67
+ resolution : options . resolution ,
68
+ } ) ;
69
+ size = compareFileSizes ( modelPath , transformedModelPath ) ;
70
+ }
35
71
36
72
const gltf = await loadGLTF ( modelPath ) ;
37
73
38
74
const analyzed = new AnalyzedGLTF (
39
75
gltf ,
40
76
{
41
- log : new Log ( { debug : options . verbose , silent : false } ) ,
77
+ log,
42
78
bones : options . bones ,
43
79
meta : options . meta ,
44
80
shadows : options . shadows ,
@@ -101,9 +137,18 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
101
137
}
102
138
103
139
const gltfOptions = options . draco ? `{ useDraco: true }` : '' ;
104
- const meshes = analyzed . getMeshes ( ) ;
105
- const bones = analyzed . getBones ( ) ;
140
+ const meshesTypes = analyzed
141
+ . getMeshes ( )
142
+ . map ( ( { name, type } ) => "\'" + name + "\'" + ': THREE.' + type )
143
+ . join ( ';\n' ) ;
144
+ const bonesTypes = analyzed
145
+ . getBones ( )
146
+ . map ( ( { name, type } ) => "\'" + name + "\'" + ': THREE.' + type )
147
+ . join ( ';\n' ) ;
106
148
const materials = analyzed . getMaterials ( ) ;
149
+ const materialsTypes = materials . map ( ( { name, type } ) => "\'" + name + "\'" + ': THREE.' + type ) . join ( ';\n' ) ;
150
+
151
+ log . debug ( materialsTypes ) ;
107
152
108
153
generateFiles ( tree , join ( __dirname , 'files' ) , dirname ( options . output ) , {
109
154
tmpl : '' ,
@@ -123,11 +168,14 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
123
168
gltfAnimationTypeName,
124
169
gltfAnimationApiTypeName,
125
170
gltfResultTypeName,
126
- url : modelPath ,
171
+ gltfPath : modelPath ,
127
172
gltfOptions,
128
- meshes,
129
- bones,
130
- materials,
173
+ meshesTypes,
174
+ bonesTypes,
175
+ materialsTypes,
176
+ angularImports,
177
+ header : options . header ,
178
+ size,
131
179
} ) ;
132
180
133
181
await formatFiles ( tree ) ;
0 commit comments