zincjs 1.20.1 → 2.0.0

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