Skip to content

Commit 6c84dc2

Browse files
committed
fix(plugin): let's see if it works
1 parent 72b30ad commit 6c84dc2

File tree

2 files changed

+86
-16
lines changed

2 files changed

+86
-16
lines changed

libs/plugin/src/generators/gltf/files/__fileName__.ts__tmpl__

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,26 @@ export class <%= className %> {
7070
modelRef = viewChild<ElementRef<Group>>('model');
7171

7272
protected gltf = injectGLTF<<%= gltfResultTypeName %>>(() => ${options.importattribute && !url.startsWith("http") ? gltfName : `"${url}"`}${gltfOptions ? `, ${JSON.stringify(gltfOptions)}` : ""});
73+
protected gltf = injectGLTF<<%= gltfResultTypeName %>>(() => <% if (useImportAttribute) { %> <%= gltfName %> <% } else { %> '<%= url %>' <% } %><% if (gltfOptions) { %>, <%= gltfOptions %><% } %>);
7374

7475
constructor() {
7576
extend({ Group${ngtTypesArr.length ? ", " + ngtTypesArr.join(", ") : ""} });
77+
extend({ Group<%= threeImports %> });
7678

77-
${
78-
hasAnimations
79-
? `
79+
<% if (animations.length) { %>
8080
const animations = injectAnimations(this.gltf, this.modelRef);
8181
effect(() => {
82-
if (animations.${useNewAnimationApi ? "isReady" : "ready()"}) {
83-
this.animations.set(animations);
84-
}
85-
}${options.ngVer < 19 ? ", { allowSignalWrites: true }" : ""})
86-
`
87-
: ""
88-
}
82+
if (!animations.isReady) return;
83+
this.animations.set(animations);
84+
});
85+
<% } %>
8986

9087
const objectEvents = inject(NgtObjectEvents, { host: true });
9188
effect(() => {
9289
const model = this.modelRef()?.nativeElement;
9390
if (!model) return;
9491

9592
objectEvents.ngtObjectEvents.set(model);
96-
}${options.ngVer < 19 ? ", { allowSignalWrites: true }" : ""});
93+
});
9794
}
9895
}

libs/plugin/src/generators/gltf/gltf.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
33
import { GenerateNGT } from './utils/generate-ngt';
44

55
export interface GltfGeneratorSchema {
@@ -53,11 +53,84 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
5353

5454
const generateNGT = new GenerateNGT(analyzed, options);
5555

56-
const test = await generateNGT.generate();
56+
const scene = await generateNGT.generate();
5757

58-
console.log(test);
58+
const args = generateNGT.args;
59+
const perspective = generateNGT.ngtTypes.has('PerspectiveCamera');
60+
const orthographic = generateNGT.ngtTypes.has('OrthographicCamera');
5961

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);
61134
}
62135

63136
export default gltfGenerator;

0 commit comments

Comments
 (0)