-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCSG.js
364 lines (318 loc) · 8.85 KB
/
CSG.js
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
import {
BufferAttribute,
BufferGeometry,
Mesh,
MeshNormalMaterial,
Vector2,
Vector3,
} from "three";
import { BSPNode } from "./components/BSPNode.js";
import { Polygon } from "./components/Polygon.js";
import { Vertex } from "./components/Vertex.js";
// Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean
// operations like union and intersection to combine 3D solids. This library
// implements CSG operations on meshes elegantly and concisely using BSP trees,
// and is meant to serve as an easily understandable implementation of the
// algorithm. All edge cases involving overlapping coplanar polygons in both
// solids are correctly handled.
//
// ## Implementation Details
//
// All CSG operations are implemented in terms of two functions, `clipTo()` and
// `invert()`, which remove parts of a BSP tree inside another BSP tree and swap
// solid and empty space, respectively. To find the union of `a` and `b`, we
// want to remove everything in `a` inside `b` and everything in `b` inside `a`,
// then combine polygons from `a` and `b` into one solid:
//
// a.clipTo(b);
// b.clipTo(a);
// a.build(b.allPolygons());
//
// The only tricky part is handling overlapping coplanar polygons in both trees.
// The code above keeps both copies, but we need to keep them in one tree and
// remove them in the other tree. To remove them from `b` we can clip the
// inverse of `b` against `a`. The code for union now looks like this:
//
// a.clipTo(b);
// b.clipTo(a);
// b.invert();
// b.clipTo(a);
// b.invert();
// a.build(b.allPolygons());
//
// Subtraction and intersection naturally follow from set operations. If
// union is `A | B`, subtraction is `A - B = ~(~A | B)` and intersection is
// `A & B = ~(~A | ~B)` where `~` is the complement operator.
//
// ## License
//
// Copyright (c) 2011 Evan Wallace (http://madebyevan.com/), under the MIT license.
// # class CSG
// Holds a binary space partition tree representing a 3D solid. Two solids can
// be combined using the `union()`, `subtract()`, and `intersect()` methods.
class CSG {
constructor() {
this.polygons = [];
this.material = [];
}
setFromGeometry(geometry) {
if (!(geometry instanceof BufferGeometry)) {
console.error("This library only works with three.js BufferGeometry");
return;
}
if (geometry.index !== null) {
geometry = geometry.toNonIndexed();
}
const positions = geometry.attributes.position;
const normals = geometry.attributes.normal;
const uvs = geometry.attributes.uv;
// TODO
// const colors = geometry.attributes.color;
function createVertex(index) {
const position = new Vector3(
positions.getX(index),
positions.getY(index),
positions.getZ(index)
);
const normal = normals
? new Vector3(
normals.getX(index),
normals.getY(index),
normals.getZ(index)
)
: null;
const uv = uvs ? new Vector2(uvs.getX(index), uvs.getY(index)) : null;
return new Vertex(position, normal, uv);
}
for (let i = 0; i < positions.count; i += 3) {
const v1 = createVertex(i);
const v2 = createVertex(i + 1);
const v3 = createVertex(i + 2);
this.polygons.push(new Polygon([v1, v2, v3]));
}
return this;
}
setFromMesh(mesh) {
mesh.updateWorldMatrix();
const transformedGeometry = mesh.geometry.clone();
transformedGeometry.applyMatrix4(mesh.matrix);
this.material.push(mesh.material);
this.setFromGeometry(transformedGeometry);
return this;
}
setPolygons(polygons) {
this.polygons = polygons;
return this;
}
toMesh() {
return new Mesh(this.toGeometry(), this.material[0]);
}
toGeometry() {
const geometry = new BufferGeometry();
const positions = [];
const normals = [];
const uvs = [];
const createFace = (a, b, c) => {
positions.push(
a.pos.x,
a.pos.y,
a.pos.z,
b.pos.x,
b.pos.y,
b.pos.z,
c.pos.x,
c.pos.y,
c.pos.z
);
// TODO: should not assume that all vertices have the same attributes
if (a.normal) {
normals.push(
a.normal.x,
a.normal.y,
a.normal.z,
b.normal.x,
b.normal.y,
b.normal.z,
c.normal.x,
c.normal.y,
c.normal.z
);
}
if (a.uv) {
uvs.push(a.uv.x, a.uv.y, b.uv.x, b.uv.y, c.uv.x, c.uv.y);
}
};
for (const polygon of this.polygons) {
// triangulate the polygon
for (let i = 0; i <= polygon.vertices.length - 3; i++) {
createFace(
polygon.vertices[0],
polygon.vertices[i + 1],
polygon.vertices[i + 2]
);
}
}
geometry.setAttribute(
"position",
new BufferAttribute(new Float32Array(positions), 3)
);
if (normals.length) {
geometry.setAttribute(
"normal",
new BufferAttribute(new Float32Array(normals), 3)
);
}
if (uvs.length) {
geometry.setAttribute(
"uv",
new BufferAttribute(new Float32Array(uvs), 2)
);
}
return geometry;
}
clone() {
const csg = new CSG();
csg.polygons = this.polygons.map(function (p) {
return p.clone();
});
return csg;
}
// Return a new CSG solid representing space in either this solid or in the
// solid `csg`
//
// A.union(B)
//
// +-------+ +-------+
// | | | |
// | A | | |
// | +--+----+ = | +----+
// +----+--+ | +----+ |
// | B | | |
// | | | |
// +-------+ +-------+
//
// A || B
union(operands) {
for (const operand of operands) {
// console.log('operand: ', operand);
if (!this.polygons.length) {
this.setFromMesh(operand);
} else {
// todo: support multimaterial
this.material.push(operand.material);
this.unionOperand(new CSG().setFromMesh(operand));
}
}
return this;
}
unionOperand(operand) {
const a = new BSPNode(this.polygons);
const b = new BSPNode(operand.polygons);
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.build(b.allPolygons());
this.polygons = a.allPolygons();
return this;
}
// Return a new CSG solid representing space in this solid but not in the
// solid `csg`
//
// A.subtract(B)
//
// +-------+ +-------+
// | | | |
// | A | | |
// | +--+----+ = | +--+
// +----+--+ | +----+
// | B |
// | |
// +-------+
//
// A && !B
subtract(operands) {
for (const operand of operands) {
if (!this.polygons.length) {
this.setFromMesh(operand);
} else {
this.material.push(operand.material);
this.subtractOperand(new CSG().setFromMesh(operand));
}
}
return this;
}
subtractOperand(operand) {
this.complement().unionOperand(operand).complement();
}
// subtractOperand(operand) {
// const a = new BSPNode(this.polygons);
// const b = new BSPNode(operand.polygons);
// a.invert();
// a.clipTo(b);
// b.clipTo(a);
// b.invert();
// b.clipTo(a);
// b.invert();
// a.build(b.allPolygons());
// a.invert();
// this.polygons = a.allPolygons();
// }
// Return a new CSG solid representing space both this solid and in the
// solid `csg`
//
// A.intersect(B)
//
// +-------+
// | |
// | A |
// | +--+----+ = +--+
// +----+--+ | +--+
// | B |
// | |
// +-------+
//
// A && B
intersect(operands) {
for (const operand of operands) {
if (!this.polygons.length) {
this.setFromMesh(operand);
} else {
this.material.push(operand.material);
this.intersectOperand(new CSG().setFromMesh(operand));
}
}
return this;
}
intersectOperand(operand) {
const a = new BSPNode(this.polygons);
const b = new BSPNode(operand.polygons);
const d = new BSPNode(this.clone().polygons);
const c = new BSPNode(operand.clone().polygons);
a.invert();
b.clipTo(a);
b.invert();
a.clipTo(b);
b.clipTo(a);
a.build(b.allPolygons());
a.invert();
c.invert();
d.clipTo(c);
d.invert();
c.clipTo(d);
d.clipTo(c);
c.build(d.allPolygons());
c.invert();
this.polygons = c.allPolygons().concat(a.allPolygons());
}
// Switch solid and empty space
// !A
complement() {
this.polygons.map((p) => {
p.negate();
});
return this;
}
}
export { CSG };