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