zincjs 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/zinc.frontend.js +1 -1
- package/build/zinc.js +43 -35
- package/build/zinc.js.map +1 -1
- package/package.json +3 -3
- package/src/assets/disc.png +0 -0
- package/src/assets/mapMarker.svg +11 -0
- package/src/controls.js +1594 -0
- package/src/geometryCSG.js +148 -0
- package/src/glyphsetCSG.js +84 -0
- package/src/loaders/GLTFToZincJSLoader.js +85 -0
- package/src/loaders/JSONLoader.js +697 -0
- package/src/loaders/OBJLoader.js +911 -0
- package/src/loaders/STLLoader.js +399 -0
- package/src/loaders/primitivesLoader.js +46 -0
- package/src/minimap.js +82 -0
- package/src/primitives/augmentShader.js +22 -0
- package/src/primitives/geometry.js +109 -0
- package/src/primitives/glyph.js +150 -0
- package/src/primitives/glyphset.js +657 -0
- package/src/primitives/label.js +51 -0
- package/src/primitives/lines.js +35 -0
- package/src/primitives/marker.js +88 -0
- package/src/primitives/pointset.js +53 -0
- package/src/primitives/texturePrimitive.js +16 -0
- package/src/primitives/textureSlides.js +118 -0
- package/src/primitives/zincObject.js +573 -0
- package/src/region.js +554 -0
- package/src/renderer.js +612 -0
- package/src/scene.js +963 -0
- package/src/sceneExporter.js +32 -0
- package/src/sceneLoader.js +842 -0
- package/src/texture/texture.js +57 -0
- package/src/texture/textureArray.js +85 -0
- package/src/three/GLTFExporter.js +2448 -0
- package/src/three/Geometry.js +2084 -0
- package/src/three/Loader.js +344 -0
- package/src/three/Points.js +223 -0
- package/src/three/line/Line.js +293 -0
- package/src/three/line/LineSegments.js +65 -0
- package/src/three-js-csg.js +564 -0
- package/src/utilities.js +321 -0
- package/src/videoHandler.js +92 -0
- package/src/workers/geometryCSG.worker.js +73 -0
- package/src/workers/geometryCSGInternal.js +58 -0
- package/src/zinc.js +38 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ThreeBSP,
|
|
4
|
+
EPSILON = 1e-5,
|
|
5
|
+
COPLANAR = 0,
|
|
6
|
+
FRONT = 1,
|
|
7
|
+
BACK = 2,
|
|
8
|
+
SPANNING = 3;
|
|
9
|
+
|
|
10
|
+
module.exports = function( THREE ) {
|
|
11
|
+
var ThreeBSP = function( geometry ) {
|
|
12
|
+
// Convert THREE.Geometry to ThreeBSP
|
|
13
|
+
var i, _length_i,
|
|
14
|
+
face, vertex, faceVertexUvs, uvs,
|
|
15
|
+
polygon,
|
|
16
|
+
polygons = [],
|
|
17
|
+
tree;
|
|
18
|
+
|
|
19
|
+
if (geometry.isBufferGeometry)
|
|
20
|
+
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
|
|
21
|
+
if ( geometry instanceof THREE.Geometry ) {
|
|
22
|
+
this.matrix = new THREE.Matrix4;
|
|
23
|
+
} else if ( geometry.isMesh ) {
|
|
24
|
+
// #todo: add hierarchy support
|
|
25
|
+
geometry.updateMatrix();
|
|
26
|
+
this.matrix = geometry.matrix.clone();
|
|
27
|
+
geometry = geometry.geometry;
|
|
28
|
+
if (geometry.isBufferGeometry)
|
|
29
|
+
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
|
|
30
|
+
geometry.mergeVertices();
|
|
31
|
+
geometry.computeVertexNormals(false);
|
|
32
|
+
} else if ( geometry instanceof ThreeBSP.Node ) {
|
|
33
|
+
this.tree = geometry;
|
|
34
|
+
this.matrix = new THREE.Matrix4;
|
|
35
|
+
return this;
|
|
36
|
+
} else {
|
|
37
|
+
throw 'ThreeBSP: Given geometry is unsupported';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for ( i = 0, _length_i = geometry.faces.length; i < _length_i; i++ ) {
|
|
41
|
+
face = geometry.faces[i];
|
|
42
|
+
faceVertexUvs = geometry.faceVertexUvs[0][i];
|
|
43
|
+
polygon = new ThreeBSP.Polygon;
|
|
44
|
+
|
|
45
|
+
if ( face instanceof THREE.Face3 ) {
|
|
46
|
+
vertex = geometry.vertices[ face.a ];
|
|
47
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) : null;
|
|
48
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], uvs );
|
|
49
|
+
vertex.applyMatrix4(this.matrix);
|
|
50
|
+
polygon.vertices.push( vertex );
|
|
51
|
+
|
|
52
|
+
vertex = geometry.vertices[ face.b ];
|
|
53
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) : null;
|
|
54
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], uvs );
|
|
55
|
+
vertex.applyMatrix4(this.matrix);
|
|
56
|
+
polygon.vertices.push( vertex );
|
|
57
|
+
|
|
58
|
+
vertex = geometry.vertices[ face.c ];
|
|
59
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) : null;
|
|
60
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], uvs );
|
|
61
|
+
vertex.applyMatrix4(this.matrix);
|
|
62
|
+
polygon.vertices.push( vertex );
|
|
63
|
+
} else if ( typeof THREE.Face4 ) {
|
|
64
|
+
vertex = geometry.vertices[ face.a ];
|
|
65
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) : null;
|
|
66
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], uvs );
|
|
67
|
+
vertex.applyMatrix4(this.matrix);
|
|
68
|
+
polygon.vertices.push( vertex );
|
|
69
|
+
|
|
70
|
+
vertex = geometry.vertices[ face.b ];
|
|
71
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) : null;
|
|
72
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], uvs );
|
|
73
|
+
vertex.applyMatrix4(this.matrix);
|
|
74
|
+
polygon.vertices.push( vertex );
|
|
75
|
+
|
|
76
|
+
vertex = geometry.vertices[ face.c ];
|
|
77
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) : null;
|
|
78
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], uvs );
|
|
79
|
+
vertex.applyMatrix4(this.matrix);
|
|
80
|
+
polygon.vertices.push( vertex );
|
|
81
|
+
|
|
82
|
+
vertex = geometry.vertices[ face.d ];
|
|
83
|
+
uvs = faceVertexUvs ? new THREE.Vector2( faceVertexUvs[3].x, faceVertexUvs[3].y ) : null;
|
|
84
|
+
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[3], uvs );
|
|
85
|
+
vertex.applyMatrix4(this.matrix);
|
|
86
|
+
polygon.vertices.push( vertex );
|
|
87
|
+
} else {
|
|
88
|
+
throw 'Invalid face type at index ' + i;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
polygon.calculateProperties();
|
|
92
|
+
polygons.push( polygon );
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
this.tree = new ThreeBSP.Node( polygons );
|
|
96
|
+
};
|
|
97
|
+
ThreeBSP.prototype.subtract = function( other_tree ) {
|
|
98
|
+
var a = this.tree.clone(),
|
|
99
|
+
b = other_tree.tree.clone();
|
|
100
|
+
|
|
101
|
+
a.invert();
|
|
102
|
+
a.clipTo( b );
|
|
103
|
+
b.clipTo( a );
|
|
104
|
+
b.invert();
|
|
105
|
+
b.clipTo( a );
|
|
106
|
+
b.invert();
|
|
107
|
+
a.build( b.allPolygons() );
|
|
108
|
+
a.invert();
|
|
109
|
+
a = new ThreeBSP( a );
|
|
110
|
+
a.matrix = this.matrix;
|
|
111
|
+
return a;
|
|
112
|
+
};
|
|
113
|
+
ThreeBSP.prototype.union = function( other_tree ) {
|
|
114
|
+
var a = this.tree.clone(),
|
|
115
|
+
b = other_tree.tree.clone();
|
|
116
|
+
|
|
117
|
+
a.clipTo( b );
|
|
118
|
+
b.clipTo( a );
|
|
119
|
+
b.invert();
|
|
120
|
+
b.clipTo( a );
|
|
121
|
+
b.invert();
|
|
122
|
+
a.build( b.allPolygons() );
|
|
123
|
+
a = new ThreeBSP( a );
|
|
124
|
+
a.matrix = this.matrix;
|
|
125
|
+
return a;
|
|
126
|
+
};
|
|
127
|
+
ThreeBSP.prototype.intersect = function( other_tree ) {
|
|
128
|
+
var a = this.tree.clone(),
|
|
129
|
+
b = other_tree.tree.clone();
|
|
130
|
+
|
|
131
|
+
a.invert();
|
|
132
|
+
b.clipTo( a );
|
|
133
|
+
b.invert();
|
|
134
|
+
a.clipTo( b );
|
|
135
|
+
b.clipTo( a );
|
|
136
|
+
a.build( b.allPolygons() );
|
|
137
|
+
a.invert();
|
|
138
|
+
a = new ThreeBSP( a );
|
|
139
|
+
a.matrix = this.matrix;
|
|
140
|
+
return a;
|
|
141
|
+
};
|
|
142
|
+
ThreeBSP.prototype.toGeometry = function() {
|
|
143
|
+
var i, j,
|
|
144
|
+
matrix = new THREE.Matrix4().getInverse( this.matrix ),
|
|
145
|
+
geometry = new THREE.Geometry(),
|
|
146
|
+
polygons = this.tree.allPolygons(),
|
|
147
|
+
polygon_count = polygons.length,
|
|
148
|
+
polygon, polygon_vertice_count,
|
|
149
|
+
vertice_dict = {},
|
|
150
|
+
vertex_idx_a, vertex_idx_b, vertex_idx_c,
|
|
151
|
+
vertex, face,
|
|
152
|
+
verticeUvs;
|
|
153
|
+
|
|
154
|
+
for ( i = 0; i < polygon_count; i++ ) {
|
|
155
|
+
polygon = polygons[i];
|
|
156
|
+
polygon_vertice_count = polygon.vertices.length;
|
|
157
|
+
|
|
158
|
+
for ( j = 2; j < polygon_vertice_count; j++ ) {
|
|
159
|
+
verticeUvs = [];
|
|
160
|
+
|
|
161
|
+
vertex = polygon.vertices[0];
|
|
162
|
+
verticeUvs.push( new THREE.Vector2( vertex.uv.x, vertex.uv.y ) );
|
|
163
|
+
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
|
|
164
|
+
vertex.applyMatrix4(matrix);
|
|
165
|
+
|
|
166
|
+
if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
|
|
167
|
+
vertex_idx_a = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
|
|
168
|
+
} else {
|
|
169
|
+
geometry.vertices.push( vertex );
|
|
170
|
+
vertex_idx_a = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
vertex = polygon.vertices[j-1];
|
|
174
|
+
verticeUvs.push( new THREE.Vector2( vertex.uv.x, vertex.uv.y ) );
|
|
175
|
+
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
|
|
176
|
+
vertex.applyMatrix4(matrix);
|
|
177
|
+
if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
|
|
178
|
+
vertex_idx_b = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
|
|
179
|
+
} else {
|
|
180
|
+
geometry.vertices.push( vertex );
|
|
181
|
+
vertex_idx_b = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
vertex = polygon.vertices[j];
|
|
185
|
+
verticeUvs.push( new THREE.Vector2( vertex.uv.x, vertex.uv.y ) );
|
|
186
|
+
vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
|
|
187
|
+
vertex.applyMatrix4(matrix);
|
|
188
|
+
if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
|
|
189
|
+
vertex_idx_c = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
|
|
190
|
+
} else {
|
|
191
|
+
geometry.vertices.push( vertex );
|
|
192
|
+
vertex_idx_c = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
face = new THREE.Face3(
|
|
196
|
+
vertex_idx_a,
|
|
197
|
+
vertex_idx_b,
|
|
198
|
+
vertex_idx_c,
|
|
199
|
+
new THREE.Vector3( polygon.normal.x, polygon.normal.y, polygon.normal.z )
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
geometry.faces.push( face );
|
|
203
|
+
geometry.faceVertexUvs[0].push( verticeUvs );
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
}
|
|
207
|
+
return geometry;
|
|
208
|
+
};
|
|
209
|
+
ThreeBSP.prototype.toBufferGeometry = function( ) {
|
|
210
|
+
var geometry = this.toGeometry();
|
|
211
|
+
var bufferGeometry = new THREE.BufferGeometry().fromGeometry(geometry);
|
|
212
|
+
|
|
213
|
+
return bufferGeometry;
|
|
214
|
+
};
|
|
215
|
+
ThreeBSP.prototype.toMesh = function( material ) {
|
|
216
|
+
var geometry = this.toBufferGeometry(),
|
|
217
|
+
mesh = new THREE.Mesh( geometry, material );
|
|
218
|
+
|
|
219
|
+
mesh.position.setFromMatrixPosition( this.matrix );
|
|
220
|
+
mesh.rotation.setFromRotationMatrix( this.matrix );
|
|
221
|
+
|
|
222
|
+
return mesh;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
ThreeBSP.Polygon = function( vertices, normal, w ) {
|
|
227
|
+
if ( !( vertices instanceof Array ) ) {
|
|
228
|
+
vertices = [];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
this.vertices = vertices;
|
|
232
|
+
if ( vertices.length > 0 ) {
|
|
233
|
+
this.calculateProperties();
|
|
234
|
+
} else {
|
|
235
|
+
this.normal = this.w = undefined;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
ThreeBSP.Polygon.prototype.calculateProperties = function() {
|
|
239
|
+
var a = this.vertices[0],
|
|
240
|
+
b = this.vertices[1],
|
|
241
|
+
c = this.vertices[2];
|
|
242
|
+
|
|
243
|
+
this.normal = b.clone().subtract( a ).cross(
|
|
244
|
+
c.clone().subtract( a )
|
|
245
|
+
).normalize();
|
|
246
|
+
|
|
247
|
+
this.w = this.normal.clone().dot( a );
|
|
248
|
+
|
|
249
|
+
return this;
|
|
250
|
+
};
|
|
251
|
+
ThreeBSP.Polygon.prototype.clone = function() {
|
|
252
|
+
var i, vertice_count,
|
|
253
|
+
polygon = new ThreeBSP.Polygon;
|
|
254
|
+
|
|
255
|
+
for ( i = 0, vertice_count = this.vertices.length; i < vertice_count; i++ ) {
|
|
256
|
+
polygon.vertices.push( this.vertices[i].clone() );
|
|
257
|
+
};
|
|
258
|
+
polygon.calculateProperties();
|
|
259
|
+
|
|
260
|
+
return polygon;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
ThreeBSP.Polygon.prototype.flip = function() {
|
|
264
|
+
var i, vertices = [];
|
|
265
|
+
|
|
266
|
+
this.normal.multiplyScalar( -1 );
|
|
267
|
+
this.w *= -1;
|
|
268
|
+
|
|
269
|
+
for ( i = this.vertices.length - 1; i >= 0; i-- ) {
|
|
270
|
+
vertices.push( this.vertices[i] );
|
|
271
|
+
};
|
|
272
|
+
this.vertices = vertices;
|
|
273
|
+
|
|
274
|
+
return this;
|
|
275
|
+
};
|
|
276
|
+
ThreeBSP.Polygon.prototype.classifyVertex = function( vertex ) {
|
|
277
|
+
var side_value = this.normal.dot( vertex ) - this.w;
|
|
278
|
+
|
|
279
|
+
if ( side_value < -EPSILON ) {
|
|
280
|
+
return BACK;
|
|
281
|
+
} else if ( side_value > EPSILON ) {
|
|
282
|
+
return FRONT;
|
|
283
|
+
} else {
|
|
284
|
+
return COPLANAR;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
ThreeBSP.Polygon.prototype.classifySide = function( polygon ) {
|
|
288
|
+
var i, vertex, classification,
|
|
289
|
+
num_positive = 0,
|
|
290
|
+
num_negative = 0,
|
|
291
|
+
vertice_count = polygon.vertices.length;
|
|
292
|
+
|
|
293
|
+
for ( i = 0; i < vertice_count; i++ ) {
|
|
294
|
+
vertex = polygon.vertices[i];
|
|
295
|
+
classification = this.classifyVertex( vertex );
|
|
296
|
+
if ( classification === FRONT ) {
|
|
297
|
+
num_positive++;
|
|
298
|
+
} else if ( classification === BACK ) {
|
|
299
|
+
num_negative++;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if ( num_positive > 0 && num_negative === 0 ) {
|
|
304
|
+
return FRONT;
|
|
305
|
+
} else if ( num_positive === 0 && num_negative > 0 ) {
|
|
306
|
+
return BACK;
|
|
307
|
+
} else if ( num_positive === 0 && num_negative === 0 ) {
|
|
308
|
+
return COPLANAR;
|
|
309
|
+
} else {
|
|
310
|
+
return SPANNING;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
ThreeBSP.Polygon.prototype.splitPolygon = function( polygon, coplanar_front, coplanar_back, front, back ) {
|
|
314
|
+
var classification = this.classifySide( polygon );
|
|
315
|
+
|
|
316
|
+
if ( classification === COPLANAR ) {
|
|
317
|
+
|
|
318
|
+
( this.normal.dot( polygon.normal ) > 0 ? coplanar_front : coplanar_back ).push( polygon );
|
|
319
|
+
|
|
320
|
+
} else if ( classification === FRONT ) {
|
|
321
|
+
|
|
322
|
+
front.push( polygon );
|
|
323
|
+
|
|
324
|
+
} else if ( classification === BACK ) {
|
|
325
|
+
|
|
326
|
+
back.push( polygon );
|
|
327
|
+
|
|
328
|
+
} else {
|
|
329
|
+
|
|
330
|
+
var vertice_count,
|
|
331
|
+
i, j, ti, tj, vi, vj,
|
|
332
|
+
t, v,
|
|
333
|
+
f = [],
|
|
334
|
+
b = [];
|
|
335
|
+
|
|
336
|
+
for ( i = 0, vertice_count = polygon.vertices.length; i < vertice_count; i++ ) {
|
|
337
|
+
|
|
338
|
+
j = (i + 1) % vertice_count;
|
|
339
|
+
vi = polygon.vertices[i];
|
|
340
|
+
vj = polygon.vertices[j];
|
|
341
|
+
ti = this.classifyVertex( vi );
|
|
342
|
+
tj = this.classifyVertex( vj );
|
|
343
|
+
|
|
344
|
+
if ( ti != BACK ) f.push( vi );
|
|
345
|
+
if ( ti != FRONT ) b.push( vi );
|
|
346
|
+
if ( (ti | tj) === SPANNING ) {
|
|
347
|
+
t = ( this.w - this.normal.dot( vi ) ) / this.normal.dot( vj.clone().subtract( vi ) );
|
|
348
|
+
v = vi.interpolate( vj, t );
|
|
349
|
+
f.push( v );
|
|
350
|
+
b.push( v );
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
if ( f.length >= 3 ) front.push( new ThreeBSP.Polygon( f ).calculateProperties() );
|
|
356
|
+
if ( b.length >= 3 ) back.push( new ThreeBSP.Polygon( b ).calculateProperties() );
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
ThreeBSP.Vertex = function( x, y, z, normal, uv ) {
|
|
361
|
+
this.x = x;
|
|
362
|
+
this.y = y;
|
|
363
|
+
this.z = z;
|
|
364
|
+
this.normal = normal || new THREE.Vector3;
|
|
365
|
+
this.uv = uv || new THREE.Vector2;
|
|
366
|
+
};
|
|
367
|
+
ThreeBSP.Vertex.prototype.clone = function() {
|
|
368
|
+
return new ThreeBSP.Vertex( this.x, this.y, this.z, this.normal.clone(), this.uv.clone() );
|
|
369
|
+
};
|
|
370
|
+
ThreeBSP.Vertex.prototype.add = function( vertex ) {
|
|
371
|
+
this.x += vertex.x;
|
|
372
|
+
this.y += vertex.y;
|
|
373
|
+
this.z += vertex.z;
|
|
374
|
+
return this;
|
|
375
|
+
};
|
|
376
|
+
ThreeBSP.Vertex.prototype.subtract = function( vertex ) {
|
|
377
|
+
this.x -= vertex.x;
|
|
378
|
+
this.y -= vertex.y;
|
|
379
|
+
this.z -= vertex.z;
|
|
380
|
+
return this;
|
|
381
|
+
};
|
|
382
|
+
ThreeBSP.Vertex.prototype.multiplyScalar = function( scalar ) {
|
|
383
|
+
this.x *= scalar;
|
|
384
|
+
this.y *= scalar;
|
|
385
|
+
this.z *= scalar;
|
|
386
|
+
return this;
|
|
387
|
+
};
|
|
388
|
+
ThreeBSP.Vertex.prototype.cross = function( vertex ) {
|
|
389
|
+
var x = this.x,
|
|
390
|
+
y = this.y,
|
|
391
|
+
z = this.z;
|
|
392
|
+
|
|
393
|
+
this.x = y * vertex.z - z * vertex.y;
|
|
394
|
+
this.y = z * vertex.x - x * vertex.z;
|
|
395
|
+
this.z = x * vertex.y - y * vertex.x;
|
|
396
|
+
|
|
397
|
+
return this;
|
|
398
|
+
};
|
|
399
|
+
ThreeBSP.Vertex.prototype.normalize = function() {
|
|
400
|
+
var length = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
|
|
401
|
+
|
|
402
|
+
this.x /= length;
|
|
403
|
+
this.y /= length;
|
|
404
|
+
this.z /= length;
|
|
405
|
+
|
|
406
|
+
return this;
|
|
407
|
+
};
|
|
408
|
+
ThreeBSP.Vertex.prototype.dot = function( vertex ) {
|
|
409
|
+
return this.x * vertex.x + this.y * vertex.y + this.z * vertex.z;
|
|
410
|
+
};
|
|
411
|
+
ThreeBSP.Vertex.prototype.lerp = function( a, t ) {
|
|
412
|
+
this.add(
|
|
413
|
+
a.clone().subtract( this ).multiplyScalar( t )
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
this.normal.add(
|
|
417
|
+
a.normal.clone().sub( this.normal ).multiplyScalar( t )
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
this.uv.add(
|
|
421
|
+
a.uv.clone().sub( this.uv ).multiplyScalar( t )
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
return this;
|
|
425
|
+
};
|
|
426
|
+
ThreeBSP.Vertex.prototype.interpolate = function( other, t ) {
|
|
427
|
+
return this.clone().lerp( other, t );
|
|
428
|
+
};
|
|
429
|
+
ThreeBSP.Vertex.prototype.applyMatrix4 = function ( m ) {
|
|
430
|
+
|
|
431
|
+
// input: THREE.Matrix4 affine matrix
|
|
432
|
+
|
|
433
|
+
var x = this.x, y = this.y, z = this.z;
|
|
434
|
+
|
|
435
|
+
var e = m.elements;
|
|
436
|
+
|
|
437
|
+
this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
|
|
438
|
+
this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
|
|
439
|
+
this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
|
|
440
|
+
|
|
441
|
+
return this;
|
|
442
|
+
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
ThreeBSP.Node = function( polygons ) {
|
|
447
|
+
var i, polygon_count,
|
|
448
|
+
front = [],
|
|
449
|
+
back = [];
|
|
450
|
+
|
|
451
|
+
this.polygons = [];
|
|
452
|
+
this.front = this.back = undefined;
|
|
453
|
+
|
|
454
|
+
if ( !(polygons instanceof Array) || polygons.length === 0 ) return;
|
|
455
|
+
|
|
456
|
+
this.divider = polygons[0].clone();
|
|
457
|
+
|
|
458
|
+
for ( i = 0, polygon_count = polygons.length; i < polygon_count; i++ ) {
|
|
459
|
+
this.divider.splitPolygon( polygons[i], this.polygons, this.polygons, front, back );
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if ( front.length > 0 ) {
|
|
463
|
+
this.front = new ThreeBSP.Node( front );
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if ( back.length > 0 ) {
|
|
467
|
+
this.back = new ThreeBSP.Node( back );
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
ThreeBSP.Node.isConvex = function( polygons ) {
|
|
471
|
+
var i, j;
|
|
472
|
+
for ( i = 0; i < polygons.length; i++ ) {
|
|
473
|
+
for ( j = 0; j < polygons.length; j++ ) {
|
|
474
|
+
if ( i !== j && polygons[i].classifySide( polygons[j] ) !== BACK ) {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return true;
|
|
480
|
+
};
|
|
481
|
+
ThreeBSP.Node.prototype.build = function( polygons ) {
|
|
482
|
+
var i, polygon_count,
|
|
483
|
+
front = [],
|
|
484
|
+
back = [];
|
|
485
|
+
|
|
486
|
+
if ( !this.divider ) {
|
|
487
|
+
this.divider = polygons[0].clone();
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
for ( i = 0, polygon_count = polygons.length; i < polygon_count; i++ ) {
|
|
491
|
+
this.divider.splitPolygon( polygons[i], this.polygons, this.polygons, front, back );
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if ( front.length > 0 ) {
|
|
495
|
+
if ( !this.front ) this.front = new ThreeBSP.Node();
|
|
496
|
+
this.front.build( front );
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if ( back.length > 0 ) {
|
|
500
|
+
if ( !this.back ) this.back = new ThreeBSP.Node();
|
|
501
|
+
this.back.build( back );
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
ThreeBSP.Node.prototype.allPolygons = function() {
|
|
505
|
+
var polygons = this.polygons.slice();
|
|
506
|
+
if ( this.front ) polygons = polygons.concat( this.front.allPolygons() );
|
|
507
|
+
if ( this.back ) polygons = polygons.concat( this.back.allPolygons() );
|
|
508
|
+
return polygons;
|
|
509
|
+
};
|
|
510
|
+
ThreeBSP.Node.prototype.clone = function() {
|
|
511
|
+
var node = new ThreeBSP.Node();
|
|
512
|
+
|
|
513
|
+
node.divider = this.divider.clone();
|
|
514
|
+
node.polygons = this.polygons.map( function( polygon ) { return polygon.clone(); } );
|
|
515
|
+
node.front = this.front && this.front.clone();
|
|
516
|
+
node.back = this.back && this.back.clone();
|
|
517
|
+
|
|
518
|
+
return node;
|
|
519
|
+
};
|
|
520
|
+
ThreeBSP.Node.prototype.invert = function() {
|
|
521
|
+
var i, polygon_count, temp;
|
|
522
|
+
|
|
523
|
+
for ( i = 0, polygon_count = this.polygons.length; i < polygon_count; i++ ) {
|
|
524
|
+
this.polygons[i].flip();
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
this.divider.flip();
|
|
528
|
+
if ( this.front ) this.front.invert();
|
|
529
|
+
if ( this.back ) this.back.invert();
|
|
530
|
+
|
|
531
|
+
temp = this.front;
|
|
532
|
+
this.front = this.back;
|
|
533
|
+
this.back = temp;
|
|
534
|
+
|
|
535
|
+
return this;
|
|
536
|
+
};
|
|
537
|
+
ThreeBSP.Node.prototype.clipPolygons = function( polygons ) {
|
|
538
|
+
var i, polygon_count,
|
|
539
|
+
front, back;
|
|
540
|
+
|
|
541
|
+
if ( !this.divider ) return polygons.slice();
|
|
542
|
+
|
|
543
|
+
front = [], back = [];
|
|
544
|
+
|
|
545
|
+
for ( i = 0, polygon_count = polygons.length; i < polygon_count; i++ ) {
|
|
546
|
+
this.divider.splitPolygon( polygons[i], front, back, front, back );
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if ( this.front ) front = this.front.clipPolygons( front );
|
|
550
|
+
if ( this.back ) back = this.back.clipPolygons( back );
|
|
551
|
+
else back = [];
|
|
552
|
+
|
|
553
|
+
return front.concat( back );
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
ThreeBSP.Node.prototype.clipTo = function( node ) {
|
|
557
|
+
this.polygons = node.clipPolygons( this.polygons );
|
|
558
|
+
if ( this.front ) this.front.clipTo( node );
|
|
559
|
+
if ( this.back ) this.back.clipTo( node );
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
return ThreeBSP;
|
|
564
|
+
}
|