-
Notifications
You must be signed in to change notification settings - Fork 5
/
NodeMesh.ts
357 lines (278 loc) · 10.7 KB
/
NodeMesh.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import * as THREE from 'three';
import * as Nodes from 'three/examples/jsm/nodes/Nodes';
import {
Data, DataDict
} from './Data';
import {
BasicNodeMaterial
} from './utils/BasicNodeMaterial';
export
enum NodeOperation {
ASSIGN,
SUB,
ADD,
DIV,
MUL,
}
export
type MeshConstructor = new (geometry: THREE.BufferGeometry, material: Nodes.NodeMaterial) => THREE.Object3D;
export
type MaterialConstructor = new () => Nodes.StandardNodeMaterial | BasicNodeMaterial;
export
type NodeOperationResult<T extends Nodes.Node> = T | Nodes.OperatorNode;
export
class NodeOperator<T extends Nodes.Node> {
constructor (operation: NodeOperation, rhs: NodeOperationResult<T>) {
this.operation = operation;
this.rhs = rhs;
}
operate (lhs: NodeOperationResult<T>) : NodeOperationResult<T> {
switch (this.operation) {
case NodeOperation.ASSIGN:
return this.rhs;
break;
case NodeOperation.SUB:
return new Nodes.OperatorNode(lhs, this.rhs, Nodes.OperatorNode.SUB);
break;
case NodeOperation.ADD:
return new Nodes.OperatorNode(lhs, this.rhs, Nodes.OperatorNode.ADD);
break;
case NodeOperation.DIV:
return new Nodes.OperatorNode(lhs, this.rhs, Nodes.OperatorNode.DIV);
break;
case NodeOperation.MUL:
return new Nodes.OperatorNode(lhs, this.rhs, Nodes.OperatorNode.MUL);
break;
}
}
operation: NodeOperation;
rhs: NodeOperationResult<T>;
}
/**
* NodeMesh class
* This class contains a THREE.Mesh that has a Nodes.StandardNodeMaterial
*/
export
class NodeMesh {
constructor (MeshT: MeshConstructor, MaterialT: MaterialConstructor, geometry: THREE.BufferGeometry, data: Data[]) {
this.meshCtor = MeshT;
this.materialCtor = MaterialT;
this.geometry = geometry;
this.geometry.computeVertexNormals();
this.material = new MaterialT();
this.material.extensions.derivatives = true;
this.data = data;
// TODO: Add those components lazily by looking over which nodes are used?
for (const data of this.data) {
for (const component of data.components) {
this.geometry.setAttribute(component.shaderName, component.bufferAttribute);
}
}
this.hasIndex = this.geometry.index != null;
this.mesh = new MeshT(geometry, this.material);
// We need to set this to false because we directly play with the position matrix
this.mesh.matrixAutoUpdate = false;
this._defaultColor = new THREE.Color('#6395b0');
this.defaultColorNode = new Nodes.ColorNode(this._defaultColor);
}
set vertices (vertices: Float32Array) {
const vertexBuffer = (this.geometry.getAttribute('position') as THREE.Float32BufferAttribute);
if (vertexBuffer.array.length == vertices.length) {
vertexBuffer.set(vertices);
vertexBuffer.needsUpdate = true;
} else {
this.geometry.deleteAttribute('position');
const newVertexBuffer = new THREE.BufferAttribute(vertices, 3);
this.geometry.setAttribute('position', newVertexBuffer);
}
this.geometry.computeVertexNormals();
}
updateData (dict: DataDict) {
for (const data of this.data) {
for (const component of data.components) {
const oldBuffer = component.bufferAttribute;
component.array = new Float32Array(dict[data.name][component.name].array)
if (component.bufferAttribute !== oldBuffer) {
this.geometry.deleteAttribute(component.shaderName);
this.geometry.setAttribute(component.shaderName, component.bufferAttribute);
}
}
}
}
buildMaterial () {
let position: NodeOperationResult<Nodes.Node> = new Nodes.PositionNode();
let alpha: NodeOperationResult<Nodes.Node> = new Nodes.FloatNode(1.);
let color: NodeOperationResult<Nodes.Node> = this.defaultColorNode;
let mask = new Nodes.BoolNode(true);
for (const transformOperator of this.transformOperators) {
position = transformOperator.operate(position);
}
for (const colorOperator of this.colorOperators) {
color = colorOperator.operate(color);
}
for (const alphaOperator of this.alphaOperators) {
alpha = alphaOperator.operate(alpha);
}
for (const maskNode of this.maskNodes) {
// TODO Use a logical node? See https://github.com/mrdoob/three.js/issues/20212
// @ts-ignore: See https://github.com/mrdoob/three.js/pull/20213
mask = new Nodes.ExpressionNode('a && b', 'bool', { a: mask, b: maskNode });
}
for (const expressionNode of this.expressionNodes) {
position = new Nodes.BypassNode(expressionNode, position);
}
this.material.flatShading = true;
this.material.side = THREE.DoubleSide;
this.material.position = position;
this.material.alpha = alpha;
this.material.color = color;
this.material.mask = mask;
// Workaround for https://github.com/mrdoob/three.js/issues/18152
if (this.mesh.type == 'Points' && this.material instanceof Nodes.StandardNodeMaterial) {
this.material.normal = new Nodes.Vector3Node(1, 1, 1);
}
this.material.alphaTest = 0.1;
this.material.build();
}
copy (materialCtor?: MaterialConstructor) {
const materialConstructor = materialCtor ? materialCtor : this.materialCtor;
const copy = new NodeMesh(this.meshCtor, materialConstructor, this.geometry, this.data);
copy.hasIndex = this.hasIndex;
copy.transformOperators = this.transformOperators.slice();
copy.alphaOperators = this.alphaOperators.slice();
copy.colorOperators = this.colorOperators.slice();
copy.maskNodes = this.maskNodes.slice();
copy.expressionNodes = this.expressionNodes.slice();
copy.defaultColor = this.defaultColor;
return copy;
}
copyMaterial (other: NodeMesh) {
this.transformOperators = other.transformOperators.slice();
this.alphaOperators = other.alphaOperators.slice();
this.colorOperators = other.colorOperators.slice();
this.maskNodes = other.maskNodes.slice();
this.expressionNodes = other.expressionNodes.slice();
this.defaultColor = other.defaultColor;
}
set matrix (matrix: THREE.Matrix4) {
this.mesh.matrix.copy(matrix);
this.mesh.updateMatrixWorld(true);
}
get boundingSphere () : THREE.Sphere {
this.geometry.computeBoundingSphere();
return this.geometry.boundingSphere as THREE.Sphere;
}
set defaultColor (defaultColor: THREE.Color) {
this._defaultColor = defaultColor;
this.defaultColorNode.value = this._defaultColor;
}
get defaultColor () {
return this._defaultColor;
}
/**
* Add a Transform node to this mesh material
*/
addTransformNode (operation: NodeOperation, transformNode: Nodes.Node) {
this.transformOperators.push(new NodeOperator<Nodes.Node>(operation, transformNode));
}
/**
* Add a Color node to this mesh material
*/
addColorNode (operation: NodeOperation, colorNode: Nodes.Node) {
this.colorOperators.push(new NodeOperator<Nodes.Node>(operation, colorNode));
}
/**
* Add an Alpha node to this mesh material
*/
addAlphaNode (operation: NodeOperation, alphaNode: Nodes.Node) {
this.alphaOperators.push(new NodeOperator<Nodes.Node>(operation, alphaNode));
}
/**
* Add an Mask node to this mesh material
*/
addMaskNode (maskNode: Nodes.Node) {
this.maskNodes.push(maskNode);
}
/**
* Add an expression node to this mesh material
*/
addExpressionNode (expressionNode: Nodes.Node) {
this.expressionNodes.push(expressionNode);
}
/**
* Sort triangle indices by distance Camera/triangle.
*/
sortTriangleIndices (cameraPosition: THREE.Vector3) {
// Project camera position in the mesh coordinate system
const matrixInverse = new THREE.Matrix4().getInverse(this.mesh.matrix);
const projectedCameraPosition = new THREE.Vector3()
.copy(cameraPosition)
.applyMatrix4(matrixInverse);
if (this.mesh.type == 'Mesh') {
const vertex = this.geometry.getAttribute('position').array;
let indices: ArrayLike<number>;
if (this.hasIndex && this.geometry.index !== null) {
indices = this.geometry.index.array;
} else {
indices = Array.from(Array(vertex.length / 3).keys());
}
// Triangle indices to sort
const triangles = Array.from(Array(indices.length / 3).keys());
// Compute distances camera/triangle
const distances = triangles.map((i: number) => {
const triangleIndex = 3 * i;
const triangle = [indices[triangleIndex], indices[triangleIndex + 1], indices[triangleIndex + 2]];
const v1Index = 3 * triangle[0];
const v2Index = 3 * triangle[1];
const v3Index = 3 * triangle[2];
// Get the three vertices
const v1 = [vertex[v1Index], vertex[v1Index + 1], vertex[v1Index + 2]];
const v2 = [vertex[v2Index], vertex[v2Index + 1], vertex[v2Index + 2]];
const v3 = [vertex[v3Index], vertex[v3Index + 1], vertex[v3Index + 2]];
// The triangle position is the mean of it's three points positions
const x = (v1[0] + v2[0] + v3[0]) / 3;
const y = (v1[1] + v2[1] + v3[1]) / 3;
const z = (v1[2] + v2[2] + v3[2]) / 3;
const trianglePosition = new THREE.Vector3(x, y, z);
return projectedCameraPosition.distanceToSquared(trianglePosition);
});
// Sort triangle indices
triangles.sort((t1: number, t2: number) : number => {
return distances[t2] - distances[t1];
});
// And then compute new vertex indices
const newIndices = new Uint32Array(indices.length);
for (let i = 0; i < triangles.length; i++) {
const triangleIndex = triangles[i];
newIndices[3 * i] = indices[3 * triangleIndex]
newIndices[3 * i + 1] = indices[3 * triangleIndex + 1]
newIndices[3 * i + 2] = indices[3 * triangleIndex + 2]
}
if (this.hasIndex && this.geometry.index !== null) {
this.geometry.index.set(newIndices);
this.geometry.index.needsUpdate = true;
} else {
const indexBuffer = new THREE.BufferAttribute(newIndices, 1);
this.geometry.setIndex(indexBuffer);
}
}
}
dispose () {
this.geometry.dispose();
this.material.dispose();
}
geometry: THREE.BufferGeometry;
material: Nodes.StandardNodeMaterial | BasicNodeMaterial;
mesh: THREE.Object3D;
readonly data: Data[];
private meshCtor: MeshConstructor;
private materialCtor: MaterialConstructor;
private transformOperators: NodeOperator<Nodes.Node>[] = [];
private alphaOperators: NodeOperator<Nodes.Node>[] = [];
private colorOperators: NodeOperator<Nodes.Node>[] = [];
private maskNodes: Nodes.Node[] = [];
private expressionNodes: Nodes.Node[] = [];
private _defaultColor: THREE.Color;
private defaultColorNode: Nodes.ColorNode;
private hasIndex: boolean;
}