Skip to content

Commit c7025c5

Browse files
committed
fix(plugin): adjust
1 parent 9cdb597 commit c7025c5

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export type <%= gltfAnimationApiTypeName %> = Exclude<NgtsAnimationApi<<%= gltfA
2828
<% } %>
2929
export type <%= gltfResultTypeName %> = GLTF & {
3030
nodes: {
31-
<% meshes.map(({ name, type }) => "\'" + name + "\'" + ": THREE." + type).join(';\n') %>
32-
<% bones.map(({ name, type }) => "\'" + name + "\'" + ": THREE." + type).join(';\n') %>
31+
<%- meshesTypes %>
32+
<%- bonesTypes %>
3333
};
3434
materials: {
35-
<% materials.map(({ name, type }) => "\'" + name + "\'" + ": THREE." + type).join(';\n') %>
35+
<%- materialsTypes %>
3636
};<% if (animations.length) { %>
3737
animations: <%= gltfAnimationTypeName %>[];<% } %>
3838
};
@@ -69,11 +69,9 @@ export class <%= className %> {
6969

7070
modelRef = viewChild<ElementRef<Group>>('model');
7171

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

7574
constructor() {
76-
extend({ Group${ngtTypesArr.length ? ", " + ngtTypesArr.join(", ") : ""} });
7775
extend({ Group<%= threeImports %> });
7876

7977
<% if (animations.length) { %>

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

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { formatFiles, generateFiles, names, Tree } from '@nx/devkit';
2-
import { dirname, join } from 'node:path';
2+
import { dirname, join, resolve } from 'node:path';
33
import { GenerateNGT } from './utils/generate-ngt';
44

55
export interface GltfGeneratorSchema {
@@ -15,30 +15,66 @@ export interface GltfGeneratorSchema {
1515
console: boolean;
1616
instance: boolean;
1717
instanceAll: boolean;
18+
transform: boolean;
19+
degrade: string;
20+
degradeResolution: number;
1821
resolution: number;
1922
keepMeshes: boolean;
2023
keepMaterials: boolean;
2124
keepAttributes: boolean;
2225
keepNames: boolean;
2326
keepGroups: boolean;
24-
format: string;
27+
format: 'jpeg' | 'png' | 'webp' | 'avif';
2528
simplify: boolean;
2629
ratio: number;
2730
error: number;
31+
header: string;
2832
verbose: boolean;
2933
}
3034

3135
export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
32-
const { loadGLTF, AnalyzedGLTF, gltfTransform, Log, allPruneStrategies } = await import('@rosskevin/gltfjsx');
36+
const { loadGLTF, AnalyzedGLTF, gltfTransform, Log, allPruneStrategies, compareFileSizes } = await import(
37+
'@rosskevin/gltfjsx'
38+
);
3339

3440
const modelPath = join(tree.root, options.modelPath);
41+
const log = new Log({ debug: options.verbose, silent: false });
42+
43+
//
44+
// Transform the GLTF file if necessary using gltf-transform
45+
//
46+
let size = '';
47+
let transformedModelPath: string | undefined = undefined;
48+
if (options.transform) {
49+
transformedModelPath = resolve(modelPath + '-transformed.glb');
50+
await gltfTransform(modelPath, transformedModelPath, {
51+
format: options.format,
52+
degrade: options.degrade,
53+
degraderesolution: options.degradeResolution,
54+
simplify: options.simplify ? { ratio: options.ratio, error: options.error } : false,
55+
log,
56+
bones: options.bones,
57+
meta: options.meta,
58+
shadows: options.shadows,
59+
instance: options.instance,
60+
instanceall: options.instanceAll,
61+
keepgroups: options.keepGroups,
62+
keepnames: options.keepNames,
63+
precision: options.precision,
64+
keepattributes: options.keepAttributes,
65+
keepmeshes: options.keepMeshes,
66+
keepmaterials: options.keepMaterials,
67+
resolution: options.resolution,
68+
});
69+
size = compareFileSizes(modelPath, transformedModelPath);
70+
}
3571

3672
const gltf = await loadGLTF(modelPath);
3773

3874
const analyzed = new AnalyzedGLTF(
3975
gltf,
4076
{
41-
log: new Log({ debug: options.verbose, silent: false }),
77+
log,
4278
bones: options.bones,
4379
meta: options.meta,
4480
shadows: options.shadows,
@@ -101,9 +137,18 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
101137
}
102138

103139
const gltfOptions = options.draco ? `{ useDraco: true }` : '';
104-
const meshes = analyzed.getMeshes();
105-
const bones = analyzed.getBones();
140+
const meshesTypes = analyzed
141+
.getMeshes()
142+
.map(({ name, type }) => "\'" + name + "\'" + ': THREE.' + type)
143+
.join(';\n');
144+
const bonesTypes = analyzed
145+
.getBones()
146+
.map(({ name, type }) => "\'" + name + "\'" + ': THREE.' + type)
147+
.join(';\n');
106148
const materials = analyzed.getMaterials();
149+
const materialsTypes = materials.map(({ name, type }) => "\'" + name + "\'" + ': THREE.' + type).join(';\n');
150+
151+
log.debug(materialsTypes);
107152

108153
generateFiles(tree, join(__dirname, 'files'), dirname(options.output), {
109154
tmpl: '',
@@ -123,11 +168,14 @@ export async function gltfGenerator(tree: Tree, options: GltfGeneratorSchema) {
123168
gltfAnimationTypeName,
124169
gltfAnimationApiTypeName,
125170
gltfResultTypeName,
126-
url: modelPath,
171+
gltfPath: modelPath,
127172
gltfOptions,
128-
meshes,
129-
bones,
130-
materials,
173+
meshesTypes,
174+
bonesTypes,
175+
materialsTypes,
176+
angularImports,
177+
header: options.header,
178+
size,
131179
});
132180

133181
await formatFiles(tree);

libs/plugin/src/generators/gltf/schema.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@
8181
"alias": "I",
8282
"default": false
8383
},
84+
"transform": {
85+
"type": "boolean",
86+
"description": "Transform meshes via @gltf-transform/extensions",
87+
"alias": "t",
88+
"default": false
89+
},
90+
"degrade": {
91+
"type": "string",
92+
"description": "Degrade meshes via @gltf-transform/extensions",
93+
"alias": "q",
94+
"default": ""
95+
},
96+
"degradeResolution": {
97+
"type": "number",
98+
"description": "Degrade meshes via @gltf-transform/extensions",
99+
"alias": "Q",
100+
"default": 512
101+
},
84102
"resolution": {
85103
"type": "number",
86104
"description": "Resolution for texture resizing (default: 1024)",
@@ -141,6 +159,12 @@
141159
"alias": "e",
142160
"default": 0.0001
143161
},
162+
"header": {
163+
"type": "string",
164+
"description": "Custom header to add to the generated file",
165+
"alias": "H",
166+
"default": ""
167+
},
144168
"verbose": {
145169
"type": "boolean",
146170
"description": "Verbose log",

0 commit comments

Comments
 (0)