zincjs 1.3.1 → 1.3.3

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/src/utilities.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const THREE = require('three');
2
+ const THREEGeometry = require('./three/Geometry').Geometry;
2
3
 
3
4
  function resolveURL(url) {
4
5
  let actualURL = url;
@@ -16,6 +17,7 @@ function resolveURL(url) {
16
17
  return actualURL;
17
18
  }
18
19
 
20
+
19
21
  //Convenient function
20
22
  function loadExternalFile(url, data, callback, errorCallback) {
21
23
  // Set up an asynchronous request
@@ -81,218 +83,264 @@ exports.getColorsRGB = (colors, index) => {
81
83
  return [mycolor.r, mycolor.g, mycolor.b];
82
84
  }
83
85
 
86
+ exports.updateMorphColorAttribute = function(targetGeometry, morph) {
87
+ if (morph && targetGeometry && targetGeometry.morphAttributes &&
88
+ targetGeometry.morphAttributes[ "color" ]) {
89
+ const morphColors = targetGeometry.morphAttributes[ "color" ];
90
+ const influences = morph.morphTargetInfluences;
91
+ const length = influences.length;
92
+ targetGeometry.deleteAttribute( 'morphColor0' );
93
+ targetGeometry.deleteAttribute( 'morphColor1' );
94
+ let bound = 0;
95
+ let morphArray = [];
96
+ for (let i = 0; (1 > bound) || (i < length); i++) {
97
+ if (influences[i] > 0) {
98
+ bound++;
99
+ morphArray.push([i, influences[i]]);
100
+ }
101
+ }
102
+ if (morphArray.length == 2) {
103
+ targetGeometry.setAttribute('morphColor0', morphColors[ morphArray[0][0] ] );
104
+ targetGeometry.setAttribute('morphColor1', morphColors[ morphArray[1][0] ] );
105
+ } else if (morphArray.length == 1) {
106
+ targetGeometry.setAttribute('morphColor0', morphColors[ morphArray[0][0] ] );
107
+ targetGeometry.setAttribute('morphColor1', morphColors[ morphArray[0][0] ] );
108
+ }
109
+ }
110
+ }
111
+
112
+
113
+ exports.toBufferGeometry = (geometryIn, options) => {
114
+ let geometry = undefined;
115
+ if (geometryIn instanceof THREEGeometry) {
116
+ if (options.localTimeEnabled && !geometryIn.morphNormalsReady &&
117
+ (geometryIn.morphNormals == undefined || geometryIn.morphNormals.length == 0))
118
+ geometryIn.computeMorphNormals();
119
+ geometry = geometryIn.toIndexedBufferGeometry();
120
+ if (options.localMorphColour) {
121
+ copyMorphColorsToIndexedBufferGeometry(geometryIn, geometry);
122
+ }
123
+ } else if (geometryIn instanceof THREE.BufferGeometry) {
124
+ geometry = geometryIn.clone();
125
+ }
126
+ geometry.colorsNeedUpdate = true;
127
+ geometry.computeBoundingBox();
128
+ geometry.computeBoundingSphere();
129
+ if (geometryIn._video)
130
+ geometry._video = geometryIn._video;
131
+ return geometry;
132
+ }
84
133
 
85
134
  exports.copyMorphColorsToBufferGeometry = (geometry, bufferGeometry) => {
86
- if (geometry && geometry.morphColors && geometry.morphColors.length > 0 ) {
87
- let array = [];
88
- let morphColors = geometry.morphColors;
89
- const getColorsRGB = require("./utilities").getColorsRGB;
90
- for ( var i = 0, l = morphColors.length; i < l; i ++ ) {
91
- let morphColor = morphColors[ i ];
92
- let colorArray = [];
93
- for ( var j = 0; j < geometry.faces.length; j ++ ) {
94
- let face = geometry.faces[j];
95
- let color = getColorsRGB(morphColor.colors, face.a);
96
- colorArray.push(color[0], color[1], color[2]);
97
- color = getColorsRGB(morphColor.colors, face.b);
98
- colorArray.push(color[0], color[1], color[2]);
99
- color = getColorsRGB(morphColor.colors, face.c);
100
- colorArray.push(color[0], color[1], color[2]);
101
- }
102
- var attribute = new THREE.Float32BufferAttribute( geometry.faces.length * 3 * 3, 3 );
103
- attribute.name = morphColor.name;
104
- array.push( attribute.copyArray( colorArray ) );
135
+ if (geometry && geometry.morphColors && geometry.morphColors.length > 0 ) {
136
+ let array = [];
137
+ let morphColors = geometry.morphColors;
138
+ const getColorsRGB = require("./utilities").getColorsRGB;
139
+ for ( var i = 0, l = morphColors.length; i < l; i ++ ) {
140
+ let morphColor = morphColors[ i ];
141
+ let colorArray = [];
142
+ for ( var j = 0; j < geometry.faces.length; j ++ ) {
143
+ let face = geometry.faces[j];
144
+ let color = getColorsRGB(morphColor.colors, face.a);
145
+ colorArray.push(color[0], color[1], color[2]);
146
+ color = getColorsRGB(morphColor.colors, face.b);
147
+ colorArray.push(color[0], color[1], color[2]);
148
+ color = getColorsRGB(morphColor.colors, face.c);
149
+ colorArray.push(color[0], color[1], color[2]);
105
150
  }
106
- bufferGeometry.morphAttributes[ "color" ] = array;
151
+ var attribute = new THREE.Float32BufferAttribute( geometry.faces.length * 3 * 3, 3 );
152
+ attribute.name = morphColor.name;
153
+ array.push( attribute.copyArray( colorArray ) );
107
154
  }
155
+ bufferGeometry.morphAttributes[ "color" ] = array;
108
156
  }
157
+ }
109
158
 
110
159
 
111
- exports.copyMorphColorsToIndexedBufferGeometry = (geometry, bufferGeometry) => {
112
- if (geometry && geometry.morphColors && geometry.morphColors.length > 0 ) {
113
- let array = [];
114
- let morphColors = geometry.morphColors;
115
- const getColorsRGB = require("./utilities").getColorsRGB;
116
- for ( let i = 0, l = morphColors.length; i < l; i ++ ) {
117
- const morphColor = morphColors[ i ];
118
- const colorArray = [];
119
- for ( let j = 0; j < morphColor.colors.length * 3; j ++ ) {
120
- let color = getColorsRGB(morphColor.colors, j);
121
- colorArray.push(color[0], color[1], color[2]);
122
- }
123
- const attribute = new THREE.Float32BufferAttribute( colorArray, 3 );
124
- attribute.name = morphColor.name;
125
- array.push( attribute );
160
+ const copyMorphColorsToIndexedBufferGeometry = (geometry, bufferGeometry) => {
161
+ if (geometry && geometry.morphColors && geometry.morphColors.length > 0 ) {
162
+ let array = [];
163
+ let morphColors = geometry.morphColors;
164
+ const getColorsRGB = require("./utilities").getColorsRGB;
165
+ for ( let i = 0, l = morphColors.length; i < l; i ++ ) {
166
+ const morphColor = morphColors[ i ];
167
+ const colorArray = [];
168
+ for ( let j = 0; j < morphColor.colors.length * 3; j ++ ) {
169
+ let color = getColorsRGB(morphColor.colors, j);
170
+ colorArray.push(color[0], color[1], color[2]);
126
171
  }
127
- bufferGeometry.morphAttributes[ "color" ] = array;
172
+ const attribute = new THREE.Float32BufferAttribute( colorArray, 3 );
173
+ attribute.name = morphColor.name;
174
+ array.push( attribute );
128
175
  }
176
+ bufferGeometry.morphAttributes[ "color" ] = array;
129
177
  }
178
+ }
130
179
 
131
- exports.mergeVertices = ( geometry, tolerance = 1e-4 ) => {
132
-
133
- tolerance = Math.max( tolerance, Number.EPSILON );
180
+ exports.mergeVertices = ( geometry, tolerance = 1e-4 ) => {
134
181
 
135
- // Generate an index buffer if the geometry doesn't have one, or optimize it
136
- // if it's already available.
137
- var hashToIndex = {};
138
- var indices = geometry.getIndex();
139
- var positions = geometry.getAttribute( 'position' );
140
- var vertexCount = indices ? indices.count : positions.count;
182
+ tolerance = Math.max( tolerance, Number.EPSILON );
141
183
 
142
- // next value for triangle indices
143
- var nextIndex = 0;
184
+ // Generate an index buffer if the geometry doesn't have one, or optimize it
185
+ // if it's already available.
186
+ var hashToIndex = {};
187
+ var indices = geometry.getIndex();
188
+ var positions = geometry.getAttribute( 'position' );
189
+ var vertexCount = indices ? indices.count : positions.count;
144
190
 
145
- // attributes and new attribute arrays
146
- var attributeNames = Object.keys( geometry.attributes );
147
- var attrArrays = {};
148
- var morphAttrsArrays = {};
149
- var newIndices = [];
150
- var getters = [ 'getX', 'getY', 'getZ', 'getW' ];
191
+ // next value for triangle indices
192
+ var nextIndex = 0;
151
193
 
152
- // initialize the arrays
153
- for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
154
- var name = attributeNames[ i ];
194
+ // attributes and new attribute arrays
195
+ var attributeNames = Object.keys( geometry.attributes );
196
+ var attrArrays = {};
197
+ var morphAttrsArrays = {};
198
+ var newIndices = [];
199
+ var getters = [ 'getX', 'getY', 'getZ', 'getW' ];
155
200
 
156
- attrArrays[ name ] = [];
201
+ // initialize the arrays
202
+ for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
203
+ var name = attributeNames[ i ];
157
204
 
158
- var morphAttr = geometry.morphAttributes[ name ];
159
- if ( morphAttr ) {
205
+ attrArrays[ name ] = [];
160
206
 
161
- morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] );
207
+ var morphAttr = geometry.morphAttributes[ name ];
208
+ if ( morphAttr ) {
162
209
 
163
- }
210
+ morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] );
164
211
 
165
- }
212
+ }
166
213
 
167
- // convert the error tolerance to an amount of decimal places to truncate to
168
- var decimalShift = Math.log10( 1 / tolerance );
169
- var shiftMultiplier = Math.pow( 10, decimalShift );
170
- for ( var i = 0; i < vertexCount; i ++ ) {
214
+ }
171
215
 
172
- var index = indices ? indices.getX( i ) : i;
216
+ // convert the error tolerance to an amount of decimal places to truncate to
217
+ var decimalShift = Math.log10( 1 / tolerance );
218
+ var shiftMultiplier = Math.pow( 10, decimalShift );
219
+ for ( var i = 0; i < vertexCount; i ++ ) {
173
220
 
174
- // Generate a hash for the vertex attributes at the current index 'i'
175
- var hash = '';
176
- for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
221
+ var index = indices ? indices.getX( i ) : i;
177
222
 
178
- var name = attributeNames[ j ];
179
- var attribute = geometry.getAttribute( name );
180
- var itemSize = attribute.itemSize;
223
+ // Generate a hash for the vertex attributes at the current index 'i'
224
+ var hash = '';
225
+ for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
181
226
 
182
- for ( var k = 0; k < itemSize; k ++ ) {
227
+ var name = attributeNames[ j ];
228
+ var attribute = geometry.getAttribute( name );
229
+ var itemSize = attribute.itemSize;
183
230
 
184
- // double tilde truncates the decimal value
185
- hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier ) },`;
231
+ for ( var k = 0; k < itemSize; k ++ ) {
186
232
 
187
- }
233
+ // double tilde truncates the decimal value
234
+ hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier ) },`;
188
235
 
189
- }
236
+ }
190
237
 
191
- // Add another reference to the vertex if it's already
192
- // used by another index
193
- if ( hash in hashToIndex ) {
238
+ }
194
239
 
195
- newIndices.push( hashToIndex[ hash ] );
240
+ // Add another reference to the vertex if it's already
241
+ // used by another index
242
+ if ( hash in hashToIndex ) {
196
243
 
197
- } else {
244
+ newIndices.push( hashToIndex[ hash ] );
198
245
 
199
- // copy data to the new index in the attribute arrays
200
- for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
246
+ } else {
201
247
 
202
- var name = attributeNames[ j ];
203
- var attribute = geometry.getAttribute( name );
204
- var morphAttr = geometry.morphAttributes[ name ];
205
- var itemSize = attribute.itemSize;
206
- var newarray = attrArrays[ name ];
207
- var newMorphArrays = morphAttrsArrays[ name ];
248
+ // copy data to the new index in the attribute arrays
249
+ for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
208
250
 
209
- for ( var k = 0; k < itemSize; k ++ ) {
251
+ var name = attributeNames[ j ];
252
+ var attribute = geometry.getAttribute( name );
253
+ var morphAttr = geometry.morphAttributes[ name ];
254
+ var itemSize = attribute.itemSize;
255
+ var newarray = attrArrays[ name ];
256
+ var newMorphArrays = morphAttrsArrays[ name ];
210
257
 
211
- var getterFunc = getters[ k ];
212
- newarray.push( attribute[ getterFunc ]( index ) );
258
+ for ( var k = 0; k < itemSize; k ++ ) {
213
259
 
214
- if ( morphAttr ) {
260
+ var getterFunc = getters[ k ];
261
+ newarray.push( attribute[ getterFunc ]( index ) );
215
262
 
216
- for ( var m = 0, ml = morphAttr.length; m < ml; m ++ ) {
263
+ if ( morphAttr ) {
217
264
 
218
- newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) );
265
+ for ( var m = 0, ml = morphAttr.length; m < ml; m ++ ) {
219
266
 
220
- }
267
+ newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) );
221
268
 
222
- }
269
+ }
223
270
 
224
- }
271
+ }
225
272
 
226
- }
273
+ }
227
274
 
228
- hashToIndex[ hash ] = nextIndex;
229
- newIndices.push( nextIndex );
230
- nextIndex ++;
275
+ }
231
276
 
232
- }
277
+ hashToIndex[ hash ] = nextIndex;
278
+ newIndices.push( nextIndex );
279
+ nextIndex ++;
233
280
 
234
- }
281
+ }
235
282
 
236
- // Generate typed arrays from new attribute arrays and update
237
- // the attributeBuffers
238
- const result = geometry.clone();
239
- for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
283
+ }
240
284
 
241
- var name = attributeNames[ i ];
242
- var oldAttribute = geometry.getAttribute( name );
243
- var attribute;
285
+ // Generate typed arrays from new attribute arrays and update
286
+ // the attributeBuffers
287
+ const result = geometry.clone();
288
+ for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
244
289
 
245
- var buffer = new oldAttribute.array.constructor( attrArrays[ name ] );
246
- if ( oldAttribute.isInterleavedBufferAttribute ) {
290
+ var name = attributeNames[ i ];
291
+ var oldAttribute = geometry.getAttribute( name );
292
+ var attribute;
247
293
 
248
- attribute = new THREE.BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.itemSize );
294
+ var buffer = new oldAttribute.array.constructor( attrArrays[ name ] );
295
+ if ( oldAttribute.isInterleavedBufferAttribute ) {
249
296
 
250
- } else {
297
+ attribute = new THREE.BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.itemSize );
251
298
 
252
- attribute = geometry.getAttribute( name ).clone();
253
- attribute.setArray( buffer );
299
+ } else {
254
300
 
255
- }
301
+ attribute = geometry.getAttribute( name ).clone();
302
+ attribute.setArray( buffer );
256
303
 
257
- result.setAttribute( name, attribute );
304
+ }
258
305
 
259
- // Update the attribute arrays
260
- if ( name in morphAttrsArrays ) {
306
+ result.setAttribute( name, attribute );
261
307
 
262
- for ( var j = 0; j < morphAttrsArrays[ name ].length; j ++ ) {
308
+ // Update the attribute arrays
309
+ if ( name in morphAttrsArrays ) {
263
310
 
264
- var morphAttribute = geometry.morphAttributes[ name ][ j ].clone();
265
- morphAttribute.setArray( new morphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] ) );
266
- result.morphAttributes[ name ][ j ] = morphAttribute;
311
+ for ( var j = 0; j < morphAttrsArrays[ name ].length; j ++ ) {
267
312
 
268
- }
313
+ var morphAttribute = geometry.morphAttributes[ name ][ j ].clone();
314
+ morphAttribute.setArray( new morphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] ) );
315
+ result.morphAttributes[ name ][ j ] = morphAttribute;
269
316
 
270
- }
317
+ }
271
318
 
272
- }
319
+ }
273
320
 
274
- // Generate an index buffer typed array
275
- var cons = Uint8Array;
276
- if ( newIndices.length >= Math.pow( 2, 8 ) ) cons = Uint16Array;
277
- if ( newIndices.length >= Math.pow( 2, 16 ) ) cons = Uint32Array;
321
+ }
278
322
 
279
- var newIndexBuffer = new cons( newIndices );
280
- var newIndices = null;
281
- if ( indices === null ) {
323
+ // Generate an index buffer typed array
324
+ var cons = Uint8Array;
325
+ if ( newIndices.length >= Math.pow( 2, 8 ) ) cons = Uint16Array;
326
+ if ( newIndices.length >= Math.pow( 2, 16 ) ) cons = Uint32Array;
282
327
 
283
- newIndices = new THREE.BufferAttribute( newIndexBuffer, 1 );
328
+ var newIndexBuffer = new cons( newIndices );
329
+ var newIndices = null;
330
+ if ( indices === null ) {
284
331
 
285
- } else {
332
+ newIndices = new THREE.BufferAttribute( newIndexBuffer, 1 );
286
333
 
287
- newIndices = geometry.getIndex().clone();
288
- newIndices.setArray( newIndexBuffer );
334
+ } else {
289
335
 
290
- }
336
+ newIndices = geometry.getIndex().clone();
337
+ newIndices.setArray( newIndexBuffer );
291
338
 
292
- result.setIndex( newIndices );
339
+ }
293
340
 
294
- return result;
341
+ result.setIndex( newIndices );
295
342
 
343
+ return result;
296
344
  }
297
345
 
298
346
  function PhongToToon(materialIn) {