/
root.ts
261 lines (227 loc) · 7.79 KB
/
root.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { Nullable, PropertyType, VERSION } from '../constants';
import type { Extension } from '../extension';
import type { Graph } from 'property-graph';
import { Accessor } from './accessor';
import { Animation } from './animation';
import { Buffer } from './buffer';
import { Camera } from './camera';
import { Material } from './material';
import { Mesh } from './mesh';
import { Node } from './node';
import { COPY_IDENTITY, Property } from './property';
import { Scene } from './scene';
import { Skin } from './skin';
import { Texture } from './texture';
import { ExtensibleProperty, IExtensibleProperty } from './extensible-property';
import type { ExtensionProperty } from './extension-property';
interface IAsset {
version: string;
minVersion?: string;
generator?: string;
copyright?: string;
[key: string]: unknown;
}
interface IRoot extends IExtensibleProperty {
asset: IAsset;
defaultScene: Scene;
accessors: Accessor[];
animations: Animation[];
buffers: Buffer[];
cameras: Camera[];
materials: Material[];
meshes: Mesh[];
nodes: Node[];
scenes: Scene[];
skins: Skin[];
textures: Texture[];
}
/**
* # Root
*
* *Root property of a glTF asset.*
*
* Any properties to be exported with a particular asset must be referenced (directly or
* indirectly) by the root. Metadata about the asset's license, generator, and glTF specification
* version are stored in the asset, accessible with {@link .getAsset}().
*
* Properties are added to the root with factory methods on its {@link Document}, and removed by
* calling {@link Property.dispose}() on the resource. Any properties that have been created but
* not disposed will be included when calling the various `root.list*()` methods.
*
* A document's root cannot be removed, and no other root may be created. Unlike other
* {@link Property} types, the `.dispose()`, `.detach()` methods have no useful function on a
* Root property.
*
* Usage:
*
* ```ts
* const root = document.getRoot();
* const scene = document.createScene('myScene');
* const node = document.createNode('myNode');
* scene.addChild(node);
*
* console.log(root.listScenes()); // → [scene x 1]
* ```
*
* Reference: [glTF → Concepts](https://github.com/KhronosGroup/gltf/blob/main/specification/2.0/README.md#concepts)
*
* @category Properties
*/
export class Root extends ExtensibleProperty<IRoot> {
public declare propertyType: PropertyType.ROOT;
private readonly _extensions: Set<Extension> = new Set();
protected init(): void {
this.propertyType = PropertyType.ROOT;
}
protected getDefaults(): Nullable<IRoot> {
return Object.assign(super.getDefaults() as IExtensibleProperty, {
asset: {
generator: `glTF-Transform ${VERSION}`,
version: '2.0',
},
defaultScene: null,
accessors: [],
animations: [],
buffers: [],
cameras: [],
materials: [],
meshes: [],
nodes: [],
scenes: [],
skins: [],
textures: [],
});
}
/** @internal */
constructor(graph: Graph<Property>) {
super(graph);
graph.addEventListener('node:create', (event) => {
this._addChildOfRoot(event.target as Property);
});
}
public clone(): this {
throw new Error('Root cannot be cloned.');
}
public copy(other: this, resolve = COPY_IDENTITY): this {
// Root cannot be cloned in isolation: only with its Document. Extensions are managed by
// the Document during cloning. The Root, and only the Root, should keep existing
// references while copying to avoid overwriting during a merge.
if (resolve === COPY_IDENTITY) throw new Error('Root cannot be copied.');
// IMPORTANT: Root cannot call super.copy(), which removes existing references.
this.set('asset', { ...other.get('asset') });
this.setName(other.getName());
this.setExtras({ ...other.getExtras() });
this.setDefaultScene(other.getDefaultScene() ? resolve(other.getDefaultScene()!) : null);
for (const extensionName of other.listRefMapKeys('extensions')) {
const otherExtension = other.getExtension(extensionName) as ExtensionProperty;
this.setExtension(extensionName, resolve(otherExtension));
}
return this;
}
private _addChildOfRoot(child: Property): this {
if (child instanceof Scene) {
this.addRef('scenes', child);
} else if (child instanceof Node) {
this.addRef('nodes', child);
} else if (child instanceof Camera) {
this.addRef('cameras', child);
} else if (child instanceof Skin) {
this.addRef('skins', child);
} else if (child instanceof Mesh) {
this.addRef('meshes', child);
} else if (child instanceof Material) {
this.addRef('materials', child);
} else if (child instanceof Texture) {
this.addRef('textures', child);
} else if (child instanceof Animation) {
this.addRef('animations', child);
} else if (child instanceof Accessor) {
this.addRef('accessors', child);
} else if (child instanceof Buffer) {
this.addRef('buffers', child);
}
// No error for untracked property types.
return this;
}
/**
* Returns the `asset` object, which specifies the target glTF version of the asset. Additional
* metadata can be stored in optional properties such as `generator` or `copyright`.
*
* Reference: [glTF → Asset](https://github.com/KhronosGroup/gltf/blob/main/specification/2.0/README.md#asset)
*/
public getAsset(): IAsset {
return this.get('asset');
}
/**********************************************************************************************
* Extensions.
*/
/** Lists all {@link Extension} properties enabled for this root. */
public listExtensionsUsed(): Extension[] {
return Array.from(this._extensions);
}
/** Lists all {@link Extension} properties enabled and required for this root. */
public listExtensionsRequired(): Extension[] {
return this.listExtensionsUsed().filter((extension) => extension.isRequired());
}
/** @internal */
public _enableExtension(extension: Extension): this {
this._extensions.add(extension);
return this;
}
/** @internal */
public _disableExtension(extension: Extension): this {
this._extensions.delete(extension);
return this;
}
/**********************************************************************************************
* Properties.
*/
/** Lists all {@link Scene} properties associated with this root. */
public listScenes(): Scene[] {
return this.listRefs('scenes');
}
/** Default {@link Scene} associated with this root. */
public setDefaultScene(defaultScene: Scene | null): this {
return this.setRef('defaultScene', defaultScene);
}
/** Default {@link Scene} associated with this root. */
public getDefaultScene(): Scene | null {
return this.getRef('defaultScene');
}
/** Lists all {@link Node} properties associated with this root. */
public listNodes(): Node[] {
return this.listRefs('nodes');
}
/** Lists all {@link Camera} properties associated with this root. */
public listCameras(): Camera[] {
return this.listRefs('cameras');
}
/** Lists all {@link Skin} properties associated with this root. */
public listSkins(): Skin[] {
return this.listRefs('skins');
}
/** Lists all {@link Mesh} properties associated with this root. */
public listMeshes(): Mesh[] {
return this.listRefs('meshes');
}
/** Lists all {@link Material} properties associated with this root. */
public listMaterials(): Material[] {
return this.listRefs('materials');
}
/** Lists all {@link Texture} properties associated with this root. */
public listTextures(): Texture[] {
return this.listRefs('textures');
}
/** Lists all {@link Animation} properties associated with this root. */
public listAnimations(): Animation[] {
return this.listRefs('animations');
}
/** Lists all {@link Accessor} properties associated with this root. */
public listAccessors(): Accessor[] {
return this.listRefs('accessors');
}
/** Lists all {@link Buffer} properties associated with this root. */
public listBuffers(): Buffer[] {
return this.listRefs('buffers');
}
}