|
| 1 | +import { formatFiles, logger, names, readJson, readProjectConfiguration, Tree, workspaceRoot } from '@nx/devkit'; |
| 2 | +import { prompt } from 'enquirer'; |
| 3 | +import { addSobaGenerator } from '../add-soba/generator'; |
| 4 | + |
| 5 | +export interface GltfGeneratorSchema { |
| 6 | + gltfPath: string; |
| 7 | + project: string; |
| 8 | + console: boolean; |
| 9 | + modelName: string; |
| 10 | + meshopt: boolean; |
| 11 | + outputPath?: string; |
| 12 | + draco?: boolean | string; |
| 13 | +} |
| 14 | + |
| 15 | +function normalizeOptions(options: GltfGeneratorSchema) { |
| 16 | + let { gltfPath, project, console, modelName, outputPath, draco, meshopt } = options; |
| 17 | + |
| 18 | + if (draco == null) { |
| 19 | + draco = true; |
| 20 | + } |
| 21 | + |
| 22 | + return { gltfPath, project, console, modelName, outputPath, draco, meshopt }; |
| 23 | +} |
| 24 | + |
| 25 | +function buildSelector(fileName: string, prefix: string) { |
| 26 | + return `${prefix}-${fileName}`; |
| 27 | +} |
| 28 | + |
| 29 | +export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) { |
| 30 | + const packageJson = readJson(tree, 'package.json'); |
| 31 | + const hasAngularThreeSoba = |
| 32 | + packageJson['dependencies']['angular-three-soba'] || packageJson['devDependencies']['angular-three-soba']; |
| 33 | + |
| 34 | + if (!hasAngularThreeSoba) { |
| 35 | + logger.warn(`[NGT] angular-three-soba must be installed to use the GLTF generator`); |
| 36 | + const { initSoba } = await prompt<{ initSoba: boolean }>({ |
| 37 | + type: 'confirm', |
| 38 | + name: 'initSoba', |
| 39 | + message: 'Would you like to initialize angular-three-soba?', |
| 40 | + initial: true, |
| 41 | + }); |
| 42 | + if (!initSoba) return; |
| 43 | + await addSobaGenerator(tree); |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const injectGLTF = await import('angular-three-soba/loaders').then((m) => m.injectGLTF); |
| 48 | + |
| 49 | + const { gltfPath, project, console: toConsole, modelName, outputPath, draco, meshopt } = normalizeOptions(options); |
| 50 | + |
| 51 | + let runtimeGltfPath: string; |
| 52 | + |
| 53 | + if (!gltfPath.startsWith('http')) { |
| 54 | + const { path } = await prompt<{ path: string }>({ |
| 55 | + type: 'input', |
| 56 | + name: 'path', |
| 57 | + message: 'What is the path to the asset file to be used at runtime (with injectGLTF)?', |
| 58 | + required: true, |
| 59 | + }); |
| 60 | + runtimeGltfPath = path; |
| 61 | + } else { |
| 62 | + runtimeGltfPath = gltfPath; |
| 63 | + } |
| 64 | + |
| 65 | + injectGLTF.preload(() => runtimeGltfPath, { |
| 66 | + useDraco: draco, |
| 67 | + useMeshOpt: meshopt, |
| 68 | + onLoad: (data) => { |
| 69 | + console.log('data', data); |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + const projectConfig = readProjectConfiguration(tree, project); |
| 74 | + const modelNames = names(modelName); |
| 75 | + const tmpPath = `${workspaceRoot}/tmp/ngt-gltf/${modelNames.fileName}`; |
| 76 | + const output = toConsole ? tmpPath : (outputPath ?? (projectConfig.sourceRoot || `${projectConfig.root}/src`)); |
| 77 | + |
| 78 | + // NOTE: even if user passes in "console", we still generate the files. |
| 79 | + // But we generate them to a temporary destination then we'll remove them printing to console. |
| 80 | + // generateFiles(tree, 'files', output, { |
| 81 | + // tmpl: '', |
| 82 | + // fileName: modelNames.fileName, |
| 83 | + // className: modelNames.className, |
| 84 | + // selector: buildSelector( |
| 85 | + // modelNames.fileName, |
| 86 | + // 'prefix' in projectConfig && typeof projectConfig.prefix === 'string' ? projectConfig.prefix : 'app', |
| 87 | + // ), |
| 88 | + // runtimeGltfPath, |
| 89 | + // }); |
| 90 | + |
| 91 | + await formatFiles(tree); |
| 92 | + |
| 93 | + if (toConsole) { |
| 94 | + // print to console |
| 95 | + // delete the files in tmp |
| 96 | + } |
| 97 | + } catch (e) { |
| 98 | + logger.error(`[NGT] Error generating GLTF files: ${e}`); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export default gltfGenerator; |
0 commit comments