11import { formatFiles , generateFiles , names , Tree } from '@nx/devkit' ;
2- import { dirname , join } from 'node:path' ;
2+ import { dirname , join , resolve } from 'node:path' ;
33import { GenerateNGT } from './utils/generate-ngt' ;
44
55export interface GltfGeneratorSchema {
@@ -15,30 +15,66 @@ export interface GltfGeneratorSchema {
1515 console : boolean ;
1616 instance : boolean ;
1717 instanceAll : boolean ;
18+ transform : boolean ;
19+ degrade : string ;
20+ degradeResolution : number ;
1821 resolution : number ;
1922 keepMeshes : boolean ;
2023 keepMaterials : boolean ;
2124 keepAttributes : boolean ;
2225 keepNames : boolean ;
2326 keepGroups : boolean ;
24- format : string ;
27+ format : 'jpeg' | 'png' | 'webp' | 'avif' ;
2528 simplify : boolean ;
2629 ratio : number ;
2730 error : number ;
31+ header : string ;
2832 verbose : boolean ;
2933}
3034
3135export 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+ ) ;
3339
3440 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+ }
3571
3672 const gltf = await loadGLTF ( modelPath ) ;
3773
3874 const analyzed = new AnalyzedGLTF (
3975 gltf ,
4076 {
41- log : new Log ( { debug : options . verbose , silent : false } ) ,
77+ log,
4278 bones : options . bones ,
4379 meta : options . meta ,
4480 shadows : options . shadows ,
@@ -101,9 +137,18 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
101137 }
102138
103139 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' ) ;
106148 const materials = analyzed . getMaterials ( ) ;
149+ const materialsTypes = materials . map ( ( { name, type } ) => "\'" + name + "\'" + ': THREE.' + type ) . join ( ';\n' ) ;
150+
151+ log . debug ( materialsTypes ) ;
107152
108153 generateFiles ( tree , join ( __dirname , 'files' ) , dirname ( options . output ) , {
109154 tmpl : '' ,
@@ -123,11 +168,14 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
123168 gltfAnimationTypeName,
124169 gltfAnimationApiTypeName,
125170 gltfResultTypeName,
126- url : modelPath ,
171+ gltfPath : modelPath ,
127172 gltfOptions,
128- meshes,
129- bones,
130- materials,
173+ meshesTypes,
174+ bonesTypes,
175+ materialsTypes,
176+ angularImports,
177+ header : options . header ,
178+ size,
131179 } ) ;
132180
133181 await formatFiles ( tree ) ;
0 commit comments