1
1
import { formatFiles , logger , names , readJson , readProjectConfiguration , Tree , workspaceRoot } from '@nx/devkit' ;
2
2
import { prompt } from 'enquirer' ;
3
+ import { readFileSync } from 'node:fs' ;
4
+ import { DRACOLoader , GLTFLoader , MeshoptDecoder } from 'three-stdlib' ;
3
5
import { addSobaGenerator } from '../add-soba/generator' ;
4
6
5
7
export interface GltfGeneratorSchema {
@@ -26,6 +28,48 @@ function buildSelector(fileName: string, prefix: string) {
26
28
return `${ prefix } -${ fileName } ` ;
27
29
}
28
30
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
+
29
73
export async function gltfGenerator ( tree : Tree , options : GltfGeneratorSchema ) {
30
74
const packageJson = readJson ( tree , 'package.json' ) ;
31
75
const hasAngularThreeSoba =
@@ -48,7 +92,7 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
48
92
// 'angular-three-soba/loaders',
49
93
// ).then((m) => m.injectGLTF);
50
94
// // 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;
52
96
53
97
const { gltfPath, project, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
54
98
@@ -66,13 +110,15 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
66
110
runtimeGltfPath = gltfPath ;
67
111
}
68
112
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
+ // });
76
122
77
123
const projectConfig = readProjectConfiguration ( tree , project ) ;
78
124
const modelNames = names ( modelName ) ;
0 commit comments