|
| 1 | +import { formatFiles, logger, names, readJson, readProjectConfiguration, Tree, workspaceRoot } from '@nx/devkit'; |
| 2 | +import { prompt } from 'enquirer'; |
| 3 | +import { readFileSync } from 'node:fs'; |
| 4 | +import { DRACOLoader, GLTFLoader, MeshoptDecoder } from 'three-stdlib'; |
| 5 | +import { addSobaGenerator } from '../add-soba/generator'; |
| 6 | + |
| 7 | +export interface GltfGeneratorSchema { |
| 8 | + gltfPath: string; |
| 9 | + project: string; |
| 10 | + console: boolean; |
| 11 | + modelName: string; |
| 12 | + meshopt: boolean; |
| 13 | + outputPath?: string; |
| 14 | + draco?: boolean | string; |
| 15 | +} |
| 16 | + |
| 17 | +function normalizeOptions(options: GltfGeneratorSchema) { |
| 18 | + let { gltfPath, project, console, modelName, outputPath, draco, meshopt } = options; |
| 19 | + |
| 20 | + if (draco == null) { |
| 21 | + draco = true; |
| 22 | + } |
| 23 | + |
| 24 | + return { gltfPath, project, console, modelName, outputPath, draco, meshopt }; |
| 25 | +} |
| 26 | + |
| 27 | +function buildSelector(fileName: string, prefix: string) { |
| 28 | + return `${prefix}-${fileName}`; |
| 29 | +} |
| 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 | + |
| 73 | +export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) { |
| 74 | + const packageJson = readJson(tree, 'package.json'); |
| 75 | + const hasAngularThreeSoba = |
| 76 | + packageJson['dependencies']['angular-three-soba'] || packageJson['devDependencies']['angular-three-soba']; |
| 77 | + |
| 78 | + if (!hasAngularThreeSoba) { |
| 79 | + logger.warn(`[NGT] angular-three-soba must be installed to use the GLTF generator`); |
| 80 | + const { initSoba } = await prompt<{ initSoba: boolean }>({ |
| 81 | + type: 'confirm', |
| 82 | + name: 'initSoba', |
| 83 | + message: 'Would you like to initialize angular-three-soba?', |
| 84 | + initial: true, |
| 85 | + }); |
| 86 | + if (!initSoba) return; |
| 87 | + await addSobaGenerator(tree); |
| 88 | + } |
| 89 | + |
| 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; |
| 96 | + |
| 97 | + const { gltfPath, project, console: toConsole, modelName, outputPath, draco, meshopt } = normalizeOptions(options); |
| 98 | + |
| 99 | + let runtimeGltfPath: string; |
| 100 | + |
| 101 | + if (!gltfPath.startsWith('http')) { |
| 102 | + const { path } = await prompt<{ path: string }>({ |
| 103 | + type: 'input', |
| 104 | + name: 'path', |
| 105 | + message: 'What is the path to the asset file to be used at runtime (with injectGLTF)?', |
| 106 | + required: true, |
| 107 | + }); |
| 108 | + runtimeGltfPath = path; |
| 109 | + } else { |
| 110 | + runtimeGltfPath = gltfPath; |
| 111 | + } |
| 112 | + |
| 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 | + // }); |
| 122 | + |
| 123 | + const projectConfig = readProjectConfiguration(tree, project); |
| 124 | + const modelNames = names(modelName); |
| 125 | + const tmpPath = `${workspaceRoot}/tmp/ngt-gltf/${modelNames.fileName}`; |
| 126 | + const output = toConsole ? tmpPath : (outputPath ?? (projectConfig.sourceRoot || `${projectConfig.root}/src`)); |
| 127 | + |
| 128 | + // NOTE: even if user passes in "console", we still generate the files. |
| 129 | + // But we generate them to a temporary destination then we'll remove them printing to console. |
| 130 | + // generateFiles(tree, 'files', output, { |
| 131 | + // tmpl: '', |
| 132 | + // fileName: modelNames.fileName, |
| 133 | + // className: modelNames.className, |
| 134 | + // selector: buildSelector( |
| 135 | + // modelNames.fileName, |
| 136 | + // 'prefix' in projectConfig && typeof projectConfig.prefix === 'string' ? projectConfig.prefix : 'app', |
| 137 | + // ), |
| 138 | + // runtimeGltfPath, |
| 139 | + // }); |
| 140 | + |
| 141 | + await formatFiles(tree); |
| 142 | + |
| 143 | + if (toConsole) { |
| 144 | + // print to console |
| 145 | + // delete the files in tmp |
| 146 | + } |
| 147 | + } catch (e) { |
| 148 | + logger.error(`[NGT] Error generating GLTF files: ${e}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +export default gltfGenerator; |
0 commit comments