11import { formatFiles , logger , names , readJson , readProjectConfiguration , Tree , workspaceRoot } from '@nx/devkit' ;
22import { prompt } from 'enquirer' ;
3+ import { readFileSync } from 'node:fs' ;
4+ import { DRACOLoader , GLTFLoader , MeshoptDecoder } from 'three-stdlib' ;
35import { addSobaGenerator } from '../add-soba/generator' ;
46
57export interface GltfGeneratorSchema {
@@ -26,6 +28,48 @@ function buildSelector(fileName: string, prefix: string) {
2628 return `${ prefix } -${ fileName } ` ;
2729}
2830
31+ function toArrayBuffer ( buf : Buffer ) {
32+ const ab = new ArrayBuffer ( buf . length ) ;
33+ const view = new Uint8Array ( ab ) ;
34+ for ( let i = 0 ; i < buf . length ; ++ i ) view [ i ] = buf [ i ] ;
35+ return ab ;
36+ }
37+
38+ let dracoLoader : DRACOLoader | null = null ;
39+ let decoderPath = 'https://www.gstatic.com/draco/versioned/decoders/1.5.5/' ;
40+ const loader = new GLTFLoader ( ) ;
41+
42+ function load ( input : string , draco : boolean | string , meshopt : boolean ) {
43+ if ( draco ) {
44+ if ( ! dracoLoader ) {
45+ dracoLoader = new DRACOLoader ( ) ;
46+ }
47+
48+ dracoLoader . setDecoderPath ( typeof draco === 'string' ? draco : decoderPath ) ;
49+ ( loader as GLTFLoader ) . setDRACOLoader ( dracoLoader ) ;
50+ }
51+
52+ if ( meshopt ) {
53+ ( loader as GLTFLoader ) . setMeshoptDecoder ( typeof MeshoptDecoder === 'function' ? MeshoptDecoder ( ) : MeshoptDecoder ) ;
54+ }
55+
56+ const data = input . startsWith ( 'http' )
57+ ? null
58+ : ( ( ) => {
59+ const fileContent = readFileSync ( input ) ;
60+ return toArrayBuffer ( fileContent ) ;
61+ } ) ( ) ;
62+ const operationFactory = ( onLoad : ( data : any ) => void , onError : ( error : ErrorEvent ) => void ) => {
63+ return input . startsWith ( 'http' )
64+ ? loader . load . call ( loader , input , onLoad , ( ) => { } , onError )
65+ : loader . parse . call ( loader , data , input , onLoad , onError ) ;
66+ } ;
67+
68+ return new Promise ( ( resolve , reject ) => {
69+ operationFactory ( resolve , reject ) ;
70+ } ) ;
71+ }
72+
2973export async function gltfGenerator ( tree : Tree , options : GltfGeneratorSchema ) {
3074 const packageJson = readJson ( tree , 'package.json' ) ;
3175 const hasAngularThreeSoba =
@@ -48,7 +92,7 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
4892 // 'angular-three-soba/loaders',
4993 // ).then((m) => m.injectGLTF);
5094 // // const injectGLTF = await import('angular-three-soba/loaders').then((m) => m.injectGLTF);
51- const injectGLTF = require ( 'angular-three-soba/loaders' ) . injectGLTF ;
95+ // const injectGLTF = require('angular-three-soba/loaders').injectGLTF;
5296
5397 const { gltfPath, project, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
5498
@@ -66,13 +110,15 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
66110 runtimeGltfPath = gltfPath ;
67111 }
68112
69- injectGLTF . preload ( ( ) => runtimeGltfPath , {
70- useDraco : draco ,
71- useMeshOpt : meshopt ,
72- onLoad : ( data ) => {
73- console . log ( 'data' , data ) ;
74- } ,
75- } ) ;
113+ await load ( runtimeGltfPath , draco , meshopt ) ;
114+
115+ // injectGLTF.preload(() => runtimeGltfPath, {
116+ // useDraco: draco,
117+ // useMeshOpt: meshopt,
118+ // onLoad: (data) => {
119+ // console.log('data', data);
120+ // },
121+ // });
76122
77123 const projectConfig = readProjectConfiguration ( tree , project ) ;
78124 const modelNames = names ( modelName ) ;
0 commit comments