-
Notifications
You must be signed in to change notification settings - Fork 152
/
threeCSG.es6
583 lines (469 loc) · 17.2 KB
/
threeCSG.es6
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*jshint esversion: 6 */
const EPSILON = 1e-5,
COPLANAR = 0,
FRONT = 1,
BACK = 2,
SPANNING = 3;
export default class ThreeBSP {
constructor(geometry) {
// Convert THREE.Geometry to ThreeBSP
var i, _length_i,
face, vertex, faceVertexUvs, uvs,
polygon,
polygons = [],
tree;
this.Polygon = Polygon;
this.Vertex = Vertex;
this.Node = Node;
if (geometry instanceof THREE.Geometry) {
this.matrix = new THREE.Matrix4();
} else if (geometry instanceof THREE.Mesh) {
// #todo: add hierarchy support
geometry.updateMatrix();
this.matrix = geometry.matrix.clone();
geometry = geometry.geometry;
} else if (geometry instanceof Node) {
this.tree = geometry;
this.matrix = new THREE.Matrix4();
return this;
} else {
throw 'ThreeBSP: Given geometry is unsupported';
}
for (i = 0, _length_i = geometry.faces.length; i < _length_i; i++) {
face = geometry.faces[i];
faceVertexUvs = geometry.faceVertexUvs[0][i];
polygon = new Polygon();
if (face instanceof THREE.Face3) {
vertex = geometry.vertices[face.a];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[0].x, faceVertexUvs[0].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[0], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
vertex = geometry.vertices[face.b];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[1].x, faceVertexUvs[1].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[1], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
vertex = geometry.vertices[face.c];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[2].x, faceVertexUvs[2].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[2], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
} else if (typeof THREE.Face4) {
vertex = geometry.vertices[face.a];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[0].x, faceVertexUvs[0].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[0], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
vertex = geometry.vertices[face.b];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[1].x, faceVertexUvs[1].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[1], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
vertex = geometry.vertices[face.c];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[2].x, faceVertexUvs[2].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[2], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
vertex = geometry.vertices[face.d];
uvs = faceVertexUvs ? new THREE.Vector2(faceVertexUvs[3].x, faceVertexUvs[3].y) : null;
vertex = new Vertex(vertex.x, vertex.y, vertex.z, face.vertexNormals[3], uvs);
vertex.applyMatrix4(this.matrix);
polygon.vertices.push(vertex);
} else {
throw 'Invalid face type at index ' + i;
}
polygon.calculateProperties();
polygons.push(polygon);
}
this.tree = new Node(polygons);
}
subtract(other_tree) {
var a = this.tree.clone(),
b = other_tree.tree.clone();
a.invert();
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.build(b.allPolygons());
a.invert();
a = new ThreeBSP(a);
a.matrix = this.matrix;
return a;
}
union(other_tree) {
var a = this.tree.clone(),
b = other_tree.tree.clone();
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.build(b.allPolygons());
a = new ThreeBSP(a);
a.matrix = this.matrix;
return a;
}
intersect(other_tree) {
var a = this.tree.clone(),
b = other_tree.tree.clone();
a.invert();
b.clipTo(a);
b.invert();
a.clipTo(b);
b.clipTo(a);
a.build(b.allPolygons());
a.invert();
a = new ThreeBSP(a);
a.matrix = this.matrix;
return a;
}
toGeometry() {
var i, j,
matrix = new THREE.Matrix4().getInverse(this.matrix),
geometry = new THREE.Geometry(),
polygons = this.tree.allPolygons(),
polygon_count = polygons.length,
polygon, polygon_vertice_count,
vertice_dict = {},
vertex_idx_a, vertex_idx_b, vertex_idx_c,
vertex, face,
verticeUvs;
for (i = 0; i < polygon_count; i++) {
polygon = polygons[i];
polygon_vertice_count = polygon.vertices.length;
for (j = 2; j < polygon_vertice_count; j++) {
verticeUvs = [];
vertex = polygon.vertices[0];
verticeUvs.push(new THREE.Vector2(vertex.uv.x, vertex.uv.y));
vertex = new THREE.Vector3(vertex.x, vertex.y, vertex.z);
vertex.applyMatrix4(matrix);
if (typeof vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] !== 'undefined') {
vertex_idx_a = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z];
} else {
geometry.vertices.push(vertex);
vertex_idx_a = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] = geometry.vertices.length - 1;
}
vertex = polygon.vertices[j - 1];
verticeUvs.push(new THREE.Vector2(vertex.uv.x, vertex.uv.y));
vertex = new THREE.Vector3(vertex.x, vertex.y, vertex.z);
vertex.applyMatrix4(matrix);
if (typeof vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] !== 'undefined') {
vertex_idx_b = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z];
} else {
geometry.vertices.push(vertex);
vertex_idx_b = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] = geometry.vertices.length - 1;
}
vertex = polygon.vertices[j];
verticeUvs.push(new THREE.Vector2(vertex.uv.x, vertex.uv.y));
vertex = new THREE.Vector3(vertex.x, vertex.y, vertex.z);
vertex.applyMatrix4(matrix);
if (typeof vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] !== 'undefined') {
vertex_idx_c = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z];
} else {
geometry.vertices.push(vertex);
vertex_idx_c = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] = geometry.vertices.length - 1;
}
face = new THREE.Face3(
vertex_idx_a,
vertex_idx_b,
vertex_idx_c,
new THREE.Vector3(polygon.normal.x, polygon.normal.y, polygon.normal.z)
);
geometry.faces.push(face);
geometry.faceVertexUvs[0].push(verticeUvs);
}
}
return geometry;
}
toMesh (material) {
var geometry = this.toGeometry(),
mesh = new THREE.Mesh(geometry, material);
mesh.position.setFromMatrixPosition(this.matrix);
mesh.rotation.setFromRotationMatrix(this.matrix);
return mesh;
}
}
class Polygon {
constructor(vertices, normal, w) {
if (!( vertices instanceof Array )) {
vertices = [];
}
this.vertices = vertices;
if (vertices.length > 0) {
this.calculateProperties();
} else {
this.normal = this.w = undefined;
}
}
calculateProperties() {
var a = this.vertices[0],
b = this.vertices[1],
c = this.vertices[2];
this.normal = b.clone().subtract(a).cross(
c.clone().subtract(a)
).normalize();
this.w = this.normal.clone().dot(a);
return this;
}
clone() {
var i, vertice_count,
polygon = new Polygon();
for (i = 0, vertice_count = this.vertices.length; i < vertice_count; i++) {
polygon.vertices.push(this.vertices[i].clone());
}
polygon.calculateProperties();
return polygon;
}
flip() {
var i, vertices = [];
this.normal.multiplyScalar(-1);
this.w *= -1;
for (i = this.vertices.length - 1; i >= 0; i--) {
vertices.push(this.vertices[i]);
}
this.vertices = vertices;
return this;
}
classifyVertex(vertex) {
var side_value = this.normal.dot(vertex) - this.w;
if (side_value < -EPSILON) {
return BACK;
} else if (side_value > EPSILON) {
return FRONT;
} else {
return COPLANAR;
}
}
classifySide(polygon) {
var i, vertex, classification,
num_positive = 0,
num_negative = 0,
vertice_count = polygon.vertices.length;
for (i = 0; i < vertice_count; i++) {
vertex = polygon.vertices[i];
classification = this.classifyVertex(vertex);
if (classification === FRONT) {
num_positive++;
} else if (classification === BACK) {
num_negative++;
}
}
if (num_positive === vertice_count && num_negative === 0) {
return FRONT;
} else if (num_positive === 0 && num_negative === vertice_count) {
return BACK;
} else if (num_positive > 0 && num_negative > 0) {
return SPANNING;
} else {
return COPLANAR;
}
}
splitPolygon(polygon, coplanar_front, coplanar_back, front, back) {
var classification = this.classifySide(polygon);
if (classification === COPLANAR) {
( this.normal.dot(polygon.normal) > 0 ? coplanar_front : coplanar_back ).push(polygon);
} else if (classification === FRONT) {
front.push(polygon);
} else if (classification === BACK) {
back.push(polygon);
} else {
var vertice_count,
i, j, ti, tj, vi, vj,
t, v,
f = [],
b = [];
for (i = 0, vertice_count = polygon.vertices.length; i < vertice_count; i++) {
j = (i + 1) % vertice_count;
vi = polygon.vertices[i];
vj = polygon.vertices[j];
ti = this.classifyVertex(vi);
tj = this.classifyVertex(vj);
if (ti != BACK) f.push(vi);
if (ti != FRONT) b.push(vi);
if ((ti | tj) === SPANNING) {
t = ( this.w - this.normal.dot(vi) ) / this.normal.dot(vj.clone().subtract(vi));
v = vi.interpolate(vj, t);
f.push(v);
b.push(v);
}
}
if (f.length >= 3) front.push(new Polygon(f).calculateProperties());
if (b.length >= 3) back.push(new Polygon(b).calculateProperties());
}
}
}
class Vertex {
constructor(x, y, z, normal, uv) {
this.x = x;
this.y = y;
this.z = z;
this.normal = normal || new THREE.Vector3();
this.uv = uv || new THREE.Vector2();
}
clone() {
return new Vertex(this.x, this.y, this.z, this.normal.clone(), this.uv.clone());
}
add(vertex) {
this.x += vertex.x;
this.y += vertex.y;
this.z += vertex.z;
return this;
}
subtract(vertex) {
this.x -= vertex.x;
this.y -= vertex.y;
this.z -= vertex.z;
return this;
}
multiplyScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
cross(vertex) {
var x = this.x,
y = this.y,
z = this.z;
this.x = y * vertex.z - z * vertex.y;
this.y = z * vertex.x - x * vertex.z;
this.z = x * vertex.y - y * vertex.x;
return this;
}
normalize() {
var length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
this.x /= length;
this.y /= length;
this.z /= length;
return this;
}
dot(vertex) {
return this.x * vertex.x + this.y * vertex.y + this.z * vertex.z;
}
lerp(a, t) {
this.add(
a.clone().subtract(this).multiplyScalar(t)
);
this.normal.add(
a.normal.clone().sub(this.normal).multiplyScalar(t)
);
this.uv.add(
a.uv.clone().sub(this.uv).multiplyScalar(t)
);
return this;
}
interpolate(other, t) {
return this.clone().lerp(other, t);
}
applyMatrix4(m) {
// input: THREE.Matrix4 affine matrix
var x = this.x, y = this.y, z = this.z;
var e = m.elements;
this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
return this;
}
}
class Node {
constructor(polygons) {
var i, polygon_count,
front = [],
back = [];
this.polygons = [];
this.front = this.back = undefined;
if (!(polygons instanceof Array) || polygons.length === 0) return;
this.divider = polygons[0].clone();
for (i = 0, polygon_count = polygons.length; i < polygon_count; i++) {
this.divider.splitPolygon(polygons[i], this.polygons, this.polygons, front, back);
}
if (front.length > 0) {
this.front = new Node(front);
}
if (back.length > 0) {
this.back = new Node(back);
}
}
isConvex(polygons) {
var i, j;
for (i = 0; i < polygons.length; i++) {
for (j = 0; j < polygons.length; j++) {
if (i !== j && polygons[i].classifySide(polygons[j]) !== BACK) {
return false;
}
}
}
return true;
}
build(polygons) {
var i, polygon_count,
front = [],
back = [];
if (!this.divider) {
this.divider = polygons[0].clone();
}
for (i = 0, polygon_count = polygons.length; i < polygon_count; i++) {
this.divider.splitPolygon(polygons[i], this.polygons, this.polygons, front, back);
}
if (front.length > 0) {
if (!this.front) this.front = new Node();
this.front.build(front);
}
if (back.length > 0) {
if (!this.back) this.back = new Node();
this.back.build(back);
}
}
allPolygons() {
var polygons = this.polygons.slice();
if (this.front) polygons = polygons.concat(this.front.allPolygons());
if (this.back) polygons = polygons.concat(this.back.allPolygons());
return polygons;
}
clone() {
var node = new Node();
node.divider = this.divider.clone();
node.polygons = this.polygons.map(function (polygon) {
return polygon.clone();
});
node.front = this.front && this.front.clone();
node.back = this.back && this.back.clone();
return node;
}
invert() {
var i, polygon_count, temp;
for (i = 0, polygon_count = this.polygons.length; i < polygon_count; i++) {
this.polygons[i].flip();
}
this.divider.flip();
if (this.front) this.front.invert();
if (this.back) this.back.invert();
temp = this.front;
this.front = this.back;
this.back = temp;
return this;
}
clipPolygons(polygons) {
var i, polygon_count,
front, back;
if (!this.divider) return polygons.slice();
front = [];
back = [];
for (i = 0, polygon_count = polygons.length; i < polygon_count; i++) {
this.divider.splitPolygon(polygons[i], front, back, front, back);
}
if (this.front) front = this.front.clipPolygons(front);
if (this.back) back = this.back.clipPolygons(back);
else back = [];
return front.concat(back);
}
clipTo(node) {
this.polygons = node.clipPolygons(this.polygons);
if (this.front) this.front.clipTo(node);
if (this.back) this.back.clipTo(node);
}
}
window.ThreeBSP = ThreeBSP;