|
1 |
| -import { Tree } from '@nx/devkit'; |
2 |
| -import { join } from 'node:path'; |
| 1 | +import { formatFiles, generateFiles, names, Tree } from '@nx/devkit'; |
| 2 | +import { dirname, join } from 'node:path'; |
3 | 3 | import { GenerateNGT } from './utils/generate-ngt';
|
4 | 4 |
|
5 | 5 | export interface GltfGeneratorSchema {
|
@@ -53,11 +53,84 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
|
53 | 53 |
|
54 | 54 | const generateNGT = new GenerateNGT(analyzed, options);
|
55 | 55 |
|
56 |
| - const test = await generateNGT.generate(); |
| 56 | + const scene = await generateNGT.generate(); |
57 | 57 |
|
58 |
| - console.log(test); |
| 58 | + const args = generateNGT.args; |
| 59 | + const perspective = generateNGT.ngtTypes.has('PerspectiveCamera'); |
| 60 | + const orthographic = generateNGT.ngtTypes.has('OrthographicCamera'); |
59 | 61 |
|
60 |
| - // await formatFiles(tree); |
| 62 | + const { className, fileName } = names(options.className); |
| 63 | + const gltfExtras = analyzed.gltf.parser.json.asset && analyzed.gltf.parser.json.asset.extras; |
| 64 | + const extras = gltfExtras |
| 65 | + ? Object.keys(gltfExtras) |
| 66 | + .map((key) => `${key.charAt(0).toUpperCase() + key.slice(1)}: ${gltfExtras[key]}`) |
| 67 | + .join('\n') |
| 68 | + : ''; |
| 69 | + |
| 70 | + const ngtTypesArr = Array.from(generateNGT.ngtTypes).filter( |
| 71 | + (t) => |
| 72 | + // group always is the top-level object |
| 73 | + t !== 'Group' && |
| 74 | + // we render ngts-perspective-camera instead of ngt-perspective-camera |
| 75 | + t !== 'PerspectiveCamera' && |
| 76 | + // we render ngts-orthographic-camera instead of ngt-orthographic-camera |
| 77 | + t !== 'OrthographicCamera' && |
| 78 | + // we don't render ngt-bone |
| 79 | + t !== 'Bone' && |
| 80 | + // we don't render ngt-object3D |
| 81 | + t !== 'Object3D', |
| 82 | + ); |
| 83 | + const threeImports = ngtTypesArr.length ? `, ${ngtTypesArr.join(',')}` : ''; |
| 84 | + const gltfAnimationTypeName = className + 'AnimationClips'; |
| 85 | + const gltfAnimationApiTypeName = className + 'AnimationApi'; |
| 86 | + const gltfName = className + 'GLTF'; |
| 87 | + const gltfResultTypeName = gltfName + 'GLTFResult'; |
| 88 | + |
| 89 | + const angularImports = []; |
| 90 | + |
| 91 | + if (args) { |
| 92 | + angularImports.push('NgtArgs'); |
| 93 | + } |
| 94 | + |
| 95 | + if (perspective) { |
| 96 | + angularImports.push('NgtsPerspectiveCamera'); |
| 97 | + } |
| 98 | + |
| 99 | + if (perspective) { |
| 100 | + angularImports.push('NgtsOrthographicCamera'); |
| 101 | + } |
| 102 | + |
| 103 | + const gltfOptions = options.draco ? `{ useDraco: true }` : ''; |
| 104 | + const meshes = analyzed.getMeshes(); |
| 105 | + const bones = analyzed.getBones(); |
| 106 | + const materials = analyzed.getMaterials(); |
| 107 | + |
| 108 | + generateFiles(tree, join(__dirname, 'files'), dirname(options.output), { |
| 109 | + tmpl: '', |
| 110 | + scene, |
| 111 | + fileName, |
| 112 | + className, |
| 113 | + selector: `${options.selectorPrefix}-${fileName}`, |
| 114 | + animations: analyzed.gltf.animations || [], |
| 115 | + extras, |
| 116 | + threeImports, |
| 117 | + args, |
| 118 | + perspective, |
| 119 | + orthographic, |
| 120 | + useImportAttribute: !modelPath.startsWith('http'), |
| 121 | + preload: true, |
| 122 | + gltfName, |
| 123 | + gltfAnimationTypeName, |
| 124 | + gltfAnimationApiTypeName, |
| 125 | + gltfResultTypeName, |
| 126 | + url: modelPath, |
| 127 | + gltfOptions, |
| 128 | + meshes, |
| 129 | + bones, |
| 130 | + materials, |
| 131 | + }); |
| 132 | + |
| 133 | + await formatFiles(tree); |
61 | 134 | }
|
62 | 135 |
|
63 | 136 | export default gltfGenerator;
|
0 commit comments