Skip to content

Commit 21e158d

Browse files
support gltf-transform
1 parent 8a55c02 commit 21e158d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Note: If you're using an older version of Blender or prefer manual installation,
3131
2. Import the extension in your Three.js project
3232
3. Add the extension to your GLTFLoader:
3333

34+
### Support with GLTF-Transform
35+
`gltf-transform optimize cube.glb cube.glb --config /path/to/gltf-transform-curve.config.mjs`
36+
3437
```javascript
3538
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
3639
import { GLTFCurveExtension } from './path/to/GLTFCurveExtension.js';

gltf-transform-curve.config.mjs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
2+
import {
3+
Extension,
4+
PropertyType,
5+
ExtensionProperty,
6+
} from '@gltf-transform/core';
7+
8+
const NAME = 'UTSUBO_curve_extension';
9+
10+
class CurveData extends ExtensionProperty {
11+
init() {
12+
this.extensionName = NAME;
13+
this.propertyType = 'CurveData';
14+
this.parentTypes = [PropertyType.NODE];
15+
this.splines = [];
16+
this.dimensions = '3D';
17+
}
18+
19+
getDefaults() {
20+
return {
21+
splines: [],
22+
dimensions: '3D',
23+
};
24+
}
25+
26+
getSplines() {
27+
return this.get('splines');
28+
}
29+
30+
setSplines(splines) {
31+
return this.set('splines', splines);
32+
}
33+
34+
getDimensions() {
35+
return this.get('dimensions');
36+
}
37+
38+
setDimensions(dimensions) {
39+
return this.set('dimensions', dimensions);
40+
}
41+
}
42+
43+
class CurveExtension extends Extension {
44+
constructor(doc) {
45+
super(doc);
46+
this.extensionName = NAME;
47+
this.propertyType = CurveData;
48+
}
49+
50+
static get EXTENSION_NAME() {
51+
return NAME;
52+
}
53+
54+
createCurveData() {
55+
return new CurveData(this.document.getGraph());
56+
}
57+
58+
read(context) {
59+
const jsonDoc = context.jsonDoc;
60+
61+
(jsonDoc.json.nodes || []).forEach((nodeDef, nodeIndex) => {
62+
if (nodeDef.extensions && nodeDef.extensions[NAME]) {
63+
const curveDataDef = nodeDef.extensions[NAME];
64+
const curveData = this.createCurveData()
65+
.setSplines(curveDataDef.splines)
66+
.setDimensions(curveDataDef.dimensions);
67+
68+
console.log(curveData);
69+
const node = context.nodes[nodeIndex];
70+
node.setExtension(NAME, curveData);
71+
}
72+
});
73+
74+
return this;
75+
}
76+
77+
write(context) {
78+
const jsonDoc = context.jsonDoc;
79+
80+
this.document
81+
.getRoot()
82+
.listNodes()
83+
.forEach((node) => {
84+
const curveData = node.getExtension(NAME);
85+
if (curveData) {
86+
const nodeIndex = context.nodeIndexMap.get(node);
87+
const nodeDef = jsonDoc.json.nodes[nodeIndex];
88+
nodeDef.extensions = nodeDef.extensions || {};
89+
nodeDef.extensions[NAME] = {
90+
splines: curveData.getSplines(),
91+
dimensions: curveData.getDimensions(),
92+
};
93+
}
94+
});
95+
96+
// Ensure the extension is listed in extensionsUsed
97+
if (!jsonDoc.json.extensionsUsed) {
98+
jsonDoc.json.extensionsUsed = [];
99+
}
100+
if (!jsonDoc.json.extensionsUsed.includes(NAME)) {
101+
jsonDoc.json.extensionsUsed.push(NAME);
102+
}
103+
104+
return this;
105+
}
106+
}
107+
108+
export default { extensions: [...ALL_EXTENSIONS, CurveExtension] };

0 commit comments

Comments
 (0)