-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
node.ts
278 lines (235 loc) · 8.56 KB
/
node.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { multiply } from 'gl-matrix/mat4';
import { PropertyType, mat4, vec3, vec4 } from '../constants';
import { GraphChild, GraphChildList } from '../graph/graph-decorators';
import { Link } from '../graph/graph-links';
import { MathUtils } from '../utils';
import { Camera } from './camera';
import { ExtensibleProperty } from './extensible-property';
import { Mesh } from './mesh';
import { COPY_IDENTITY } from './property';
import { Skin } from './skin';
/**
* # Node
*
* *Nodes are the objects that comprise a {@link Scene}.*
*
* Each node may have one or more children, and a transform (position, rotation, and scale) that
* applies to all of its descendants. A node may also reference (or "instantiate") other resources
* at its location, including {@link Mesh}, Camera, Light, and Skin properties. A node cannot be
* part of more than one {@link Scene}.
*
* A node's local transform is represented with array-like objects, intended to be compatible with
* [gl-matrix](https://github.com/toji/gl-matrix), or with the `toArray`/`fromArray` methods of
* libraries like three.js and babylon.js.
*
* Usage:
*
* ```ts
* const node = doc.createNode('myNode')
* .setMesh(mesh)
* .setTranslation([0, 0, 0])
* .addChild(otherNode);
* ```
*
* References:
* - [glTF → Nodes and Hierarchy](https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#nodes-and-hierarchy)
*
* @category Properties
*/
export class Node extends ExtensibleProperty {
public readonly propertyType = PropertyType.NODE;
private _translation: vec3 = [0, 0, 0];
private _rotation: vec4 = [0, 0, 0, 1];
private _scale: vec3 = [1, 1, 1];
private _weights: number[] = [];
/** @internal Internal reference to node's parent, omitted from {@link Graph}. */
public _parent: SceneNode | null = null;
@GraphChild private camera: Link<Node, Camera> | null = null;
@GraphChild private mesh: Link<Node, Mesh> | null = null;
@GraphChild private skin: Link<Node, Skin> | null = null;
@GraphChildList private children: Link<Node, Node>[] = [];
public copy(other: this, resolve = COPY_IDENTITY): this {
super.copy(other, resolve);
this._translation = [...other._translation] as vec3;
this._rotation = [...other._rotation] as vec4;
this._scale = [...other._scale] as vec3;
this._weights = [...other._weights];
this.setCamera(other.camera ? resolve(other.camera.getChild()) : null);
this.setMesh(other.mesh ? resolve(other.mesh.getChild()) : null);
this.setSkin(other.skin ? resolve(other.skin.getChild()) : null);
if (resolve !== COPY_IDENTITY) {
this.clearGraphChildList(this.children);
other.children.forEach((link) => this.addChild(resolve(link.getChild())));
}
return this;
}
/**********************************************************************************************
* Local transform.
*/
/** Returns the translation (position) of this node in local space. */
public getTranslation(): vec3 {
return this._translation;
}
/** Returns the rotation (quaternion) of this node in local space. */
public getRotation(): vec4 {
return this._rotation;
}
/** Returns the scale of this node in local space. */
public getScale(): vec3 {
return this._scale;
}
/** Sets the translation (position) of this node in local space. */
public setTranslation(translation: vec3): this {
this._translation = translation;
return this;
}
/** Sets the rotation (quaternion) of this node in local space. */
public setRotation(rotation: vec4): this {
this._rotation = rotation;
return this;
}
/** Sets the scale of this node in local space. */
public setScale(scale: vec3): this {
this._scale = scale;
return this;
}
/** Returns the local matrix of this node. */
public getMatrix(): mat4 {
return MathUtils.compose(this._translation, this._rotation, this._scale, [] as unknown as mat4);
}
/** Sets the local matrix of this node. Matrix will be decomposed to TRS properties. */
public setMatrix(matrix: mat4): this {
MathUtils.decompose(matrix, this._translation, this._rotation, this._scale);
return this;
}
/**********************************************************************************************
* World transform.
*/
/** Returns the translation (position) of this node in world space. */
public getWorldTranslation(): vec3 {
const t = [0, 0, 0] as vec3;
MathUtils.decompose(this.getWorldMatrix(), t, [0, 0, 0, 1], [1, 1, 1]);
return t;
}
/** Returns the rotation (quaternion) of this node in world space. */
public getWorldRotation(): vec4 {
const r = [0, 0, 0, 1] as vec4;
MathUtils.decompose(this.getWorldMatrix(), [0, 0, 0], r, [1, 1, 1]);
return r;
}
/** Returns the scale of this node in world space. */
public getWorldScale(): vec3 {
const s = [1, 1, 1] as vec3;
MathUtils.decompose(this.getWorldMatrix(), [0, 0, 0], [0, 0, 0, 1], s);
return s;
}
/** Returns the world matrix of this node. */
public getWorldMatrix(): mat4 {
// Build ancestor chain.
const ancestors: Node[] = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
for (let node: SceneNode | null = this; node instanceof Node; node = node._parent) {
ancestors.push(node);
}
// Compute world matrix.
let ancestor: Node | undefined;
const worldMatrix = ancestors.pop()!.getMatrix();
while ((ancestor = ancestors.pop())) {
multiply(worldMatrix, worldMatrix, ancestor.getMatrix());
}
return worldMatrix;
}
/**********************************************************************************************
* Scene hierarchy.
*/
/** Adds another node as a child of this one. Nodes cannot have multiple parents. */
public addChild(child: Node): this {
// Remove existing parent.
if (child._parent) child._parent.removeChild(child);
// Link in graph.
const link = this.graph.link('child', this, child);
this.addGraphChild(this.children, link);
// Set new parent.
child._parent = this;
link.onDispose(() => (child._parent = null));
return this;
}
/** Removes a node from this node's child node list. */
public removeChild(child: Node): this {
return this.removeGraphChild(this.children, child);
}
/** Lists all child nodes of this node. */
public listChildren(): Node[] {
return this.children.map((link) => link.getChild());
}
/**
* Returns the unique parent ({@link Scene}, {@link Node}, or null) of this node in the scene
* hierarchy. Unrelated to {@link Property.listParents}, which lists all resource references.
*/
public getParent(): SceneNode | null {
return this._parent;
}
/**********************************************************************************************
* Attachments.
*/
/** Returns the {@link Mesh}, if any, instantiated at this node. */
public getMesh(): Mesh | null {
return this.mesh ? this.mesh.getChild() : null;
}
/**
* Sets a {@link Mesh} to be instantiated at this node. A single mesh may be instatiated by
* multiple nodes; reuse of this sort is strongly encouraged.
*/
public setMesh(mesh: Mesh | null): this {
this.mesh = this.graph.link('mesh', this, mesh);
return this;
}
/** Returns the {@link Camera}, if any, instantiated at this node. */
public getCamera(): Camera | null {
return this.camera ? this.camera.getChild() : null;
}
/** Sets a {@link Camera} to be instantiated at this node. */
public setCamera(camera: Camera | null): this {
this.camera = this.graph.link('camera', this, camera);
return this;
}
/** Returns the {@link Skin}, if any, instantiated at this node. */
public getSkin(): Skin | null {
return this.skin ? this.skin.getChild() : null;
}
/** Sets a {@link Skin} to be instantiated at this node. */
public setSkin(skin: Skin | null): this {
this.skin = this.graph.link('skin', this, skin);
return this;
}
/**
* Initial weights of each {@link PrimitiveTarget} for the mesh instance at this node.
* Most engines only support 4-8 active morph targets at a time.
*/
public getWeights(): number[] {
return this._weights;
}
/**
* Initial weights of each {@link PrimitiveTarget} for the mesh instance at this node.
* Most engines only support 4-8 active morph targets at a time.
*/
public setWeights(weights: number[]): this {
this._weights = weights;
return this;
}
/**********************************************************************************************
* Helpers.
*/
/** Visits this {@link Node} and its descendants, top-down. */
public traverse(fn: (node: Node) => void): this {
fn(this);
for (const child of this.listChildren()) child.traverse(fn);
return this;
}
}
interface SceneNode {
propertyType: PropertyType;
_parent?: SceneNode | null;
addChild(node: Node): this;
removeChild(node: Node): this;
}