1
- import { formatFiles , logger , names , readJson , readProjectConfiguration , Tree , workspaceRoot } from '@nx/devkit' ;
1
+ import {
2
+ formatFiles ,
3
+ getProjects ,
4
+ logger ,
5
+ names ,
6
+ readJson ,
7
+ readProjectConfiguration ,
8
+ Tree ,
9
+ workspaceRoot ,
10
+ } from '@nx/devkit' ;
2
11
import { prompt } from 'enquirer' ;
3
- import { readFileSync } from 'node:fs' ;
4
- import { DRACOLoader , GLTFLoader , MeshoptDecoder } from 'three-stdlib' ;
12
+ import { join } from 'node:path' ;
5
13
import { addSobaGenerator } from '../add-soba/generator' ;
14
+ import { parse } from './utils' ;
6
15
7
16
export interface GltfGeneratorSchema {
8
17
gltfPath : string ;
9
- project : string ;
10
18
console : boolean ;
11
19
modelName : string ;
12
20
meshopt : boolean ;
@@ -15,61 +23,19 @@ export interface GltfGeneratorSchema {
15
23
}
16
24
17
25
function normalizeOptions ( options : GltfGeneratorSchema ) {
18
- let { gltfPath, project , console, modelName, outputPath, draco, meshopt } = options ;
26
+ let { gltfPath, console, modelName, outputPath, draco, meshopt } = options ;
19
27
20
28
if ( draco == null ) {
21
29
draco = true ;
22
30
}
23
31
24
- return { gltfPath, project , console, modelName, outputPath, draco, meshopt } ;
32
+ return { gltfPath, console, modelName, outputPath, draco, meshopt } ;
25
33
}
26
34
27
35
function buildSelector ( fileName : string , prefix : string ) {
28
36
return `${ prefix } -${ fileName } ` ;
29
37
}
30
38
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
-
73
39
export async function gltfGenerator ( tree : Tree , options : GltfGeneratorSchema ) {
74
40
const packageJson = readJson ( tree , 'package.json' ) ;
75
41
const hasAngularThreeSoba =
@@ -87,40 +53,60 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
87
53
await addSobaGenerator ( tree ) ;
88
54
}
89
55
90
- try {
91
- // const injectGLTF = await loadEsmModule<typeof import('angular-three-soba/loaders')>(
92
- // 'angular-three-soba/loaders',
93
- // ).then((m) => m.injectGLTF);
94
- // // const injectGLTF = await import('angular-three-soba/loaders').then((m) => m.injectGLTF);
95
- // const injectGLTF = require('angular-three-soba/loaders').injectGLTF;
56
+ const projects = getProjects ( tree ) ;
57
+ const applicationProjects = Array . from ( projects . entries ( ) ) . reduce ( ( acc , [ projectName , project ] ) => {
58
+ if ( project . projectType === 'application' ) {
59
+ acc . push ( projectName ) ;
60
+ }
61
+ return acc ;
62
+ } , [ ] as string [ ] ) ;
63
+
64
+ let { project } = await prompt < { project : string } > ( {
65
+ type : 'select' ,
66
+ name : 'project' ,
67
+ message : 'What project would you like to generate the model component for?' ,
68
+ choices : [ ...applicationProjects , 'custom' ] ,
69
+ required : true ,
70
+ } ) ;
71
+
72
+ if ( project === 'custom' ) {
73
+ const { projectName } = await prompt < { projectName : string } > ( {
74
+ type : 'input' ,
75
+ name : 'projectName' ,
76
+ message : 'What is the name of the project to generate the model component for?' ,
77
+ required : true ,
78
+ } ) ;
79
+ project = projectName ;
80
+ }
96
81
97
- const { gltfPath, project, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
82
+ const projectConfig = readProjectConfiguration ( tree , project ) ;
83
+
84
+ if ( ! projectConfig ) {
85
+ logger . error ( `[NGT] ${ project } is not a project` ) ;
86
+ return ;
87
+ }
88
+
89
+ try {
90
+ const { gltfPath, console : toConsole , modelName, outputPath, draco, meshopt } = normalizeOptions ( options ) ;
98
91
99
92
let runtimeGltfPath : string ;
100
93
101
94
if ( ! gltfPath . startsWith ( 'http' ) ) {
102
95
const { path } = await prompt < { path : string } > ( {
103
96
type : 'input' ,
104
97
name : 'path' ,
105
- message : 'What is the path to the asset file to be used at runtime ( with injectGLTF) ?' ,
98
+ message : 'What is the path to the asset file to be used at runtime with injectGLTF?' ,
106
99
required : true ,
107
100
} ) ;
108
101
runtimeGltfPath = path ;
109
102
} else {
110
- runtimeGltfPath = gltfPath ;
103
+ runtimeGltfPath = join ( workspaceRoot , gltfPath ) ;
111
104
}
112
105
113
- await load ( runtimeGltfPath , draco , meshopt ) ;
106
+ const data = await parse ( runtimeGltfPath , draco , meshopt ) ;
114
107
115
- // injectGLTF.preload(() => runtimeGltfPath, {
116
- // useDraco: draco,
117
- // useMeshOpt: meshopt,
118
- // onLoad: (data) => {
119
- // console.log('data', data);
120
- // },
121
- // });
108
+ console . log ( data ) ;
122
109
123
- const projectConfig = readProjectConfiguration ( tree , project ) ;
124
110
const modelNames = names ( modelName ) ;
125
111
const tmpPath = `${ workspaceRoot } /tmp/ngt-gltf/${ modelNames . fileName } ` ;
126
112
const output = toConsole ? tmpPath : ( outputPath ?? ( projectConfig . sourceRoot || `${ projectConfig . root } /src` ) ) ;
0 commit comments