@damienmortini/three 0.1.192 → 0.1.195

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.
@@ -1,1346 +1,1087 @@
1
- import {
2
- BufferAttribute,
3
- BufferGeometry,
4
- Float32BufferAttribute,
5
- InstancedBufferAttribute,
6
- InterleavedBuffer,
7
- InterleavedBufferAttribute,
8
- TriangleFanDrawMode,
9
- TriangleStripDrawMode,
10
- TrianglesDrawMode,
11
- Vector3,
12
- } from '../../../../three/src/Three.js';
13
-
1
+ import {
2
+ BufferAttribute,
3
+ BufferGeometry,
4
+ Float32BufferAttribute,
5
+ InstancedBufferAttribute,
6
+ InterleavedBuffer,
7
+ InterleavedBufferAttribute,
8
+ TriangleFanDrawMode,
9
+ TrianglesDrawMode,
10
+ TriangleStripDrawMode,
11
+ Vector3,
12
+ } from '../../../../three/src/Three.js';
13
+
14
14
  function computeTangents() {
15
-
16
- throw new Error( 'BufferGeometryUtils: computeTangents renamed to computeMikkTSpaceTangents.' );
17
-
18
- }
19
-
20
- function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) {
21
-
22
- if ( ! MikkTSpace || ! MikkTSpace.isReady ) {
23
-
24
- throw new Error( 'BufferGeometryUtils: Initialized MikkTSpace library required.' );
25
-
26
- }
27
-
28
- if ( ! geometry.hasAttribute( 'position' ) || ! geometry.hasAttribute( 'normal' ) || ! geometry.hasAttribute( 'uv' ) ) {
29
-
30
- throw new Error( 'BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.' );
31
-
32
- }
33
-
34
- function getAttributeArray( attribute ) {
35
-
36
- if ( attribute.normalized || attribute.isInterleavedBufferAttribute ) {
37
-
38
- const dstArray = new Float32Array( attribute.getCount() * attribute.itemSize );
39
-
40
- for ( let i = 0, j = 0; i < attribute.getCount(); i ++ ) {
41
-
42
- dstArray[ j ++ ] = attribute.getX( i );
43
- dstArray[ j ++ ] = attribute.getY( i );
44
-
45
- if ( attribute.itemSize > 2 ) {
46
-
47
- dstArray[ j ++ ] = attribute.getZ( i );
48
-
49
- }
50
-
51
- }
52
-
53
- return dstArray;
54
-
55
- }
56
-
57
- if ( attribute.array instanceof Float32Array ) {
58
-
59
- return attribute.array;
60
-
61
- }
62
-
63
- return new Float32Array( attribute.array );
64
-
65
- }
66
-
67
- // MikkTSpace algorithm requires non-indexed input.
68
-
69
- const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;
70
-
71
- // Compute vertex tangents.
72
-
73
- const tangents = MikkTSpace.generateTangents(
74
-
75
- getAttributeArray( _geometry.attributes.position ),
76
- getAttributeArray( _geometry.attributes.normal ),
77
- getAttributeArray( _geometry.attributes.uv )
78
-
79
- );
80
-
81
- // Texture coordinate convention of glTF differs from the apparent
82
- // default of the MikkTSpace library; .w component must be flipped.
83
-
84
- if ( negateSign ) {
85
-
86
- for ( let i = 3; i < tangents.length; i += 4 ) {
87
-
88
- tangents[ i ] *= - 1;
89
-
90
- }
91
-
92
- }
93
-
94
- //
95
-
96
- _geometry.setAttribute( 'tangent', new BufferAttribute( tangents, 4 ) );
97
-
98
- if ( geometry !== _geometry ) {
99
-
100
- geometry.copy( _geometry );
101
-
102
- }
103
-
104
- return geometry;
105
-
106
- }
107
-
108
- /**
109
- * @param {Array<BufferGeometry>} geometries
110
- * @param {Boolean} useGroups
111
- * @return {BufferGeometry}
112
- */
113
- function mergeBufferGeometries( geometries, useGroups = false ) {
114
-
115
- const isIndexed = geometries[ 0 ].index !== null;
116
-
117
- const attributesUsed = new Set( Object.keys( geometries[ 0 ].attributes ) );
118
- const morphAttributesUsed = new Set( Object.keys( geometries[ 0 ].morphAttributes ) );
119
-
120
- const attributes = {};
121
- const morphAttributes = {};
122
-
123
- const morphTargetsRelative = geometries[ 0 ].morphTargetsRelative;
124
-
125
- const mergedGeometry = new BufferGeometry();
126
-
127
- let offset = 0;
128
-
129
- for ( let i = 0; i < geometries.length; ++ i ) {
130
-
131
- const geometry = geometries[ i ];
132
- let attributesCount = 0;
133
-
134
- // ensure that all geometries are indexed, or none
135
-
136
- if ( isIndexed !== ( geometry.index !== null ) ) {
137
-
138
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.' );
139
- return null;
140
-
141
- }
142
-
143
- // gather attributes, exit early if they're different
144
-
145
- for ( const name in geometry.attributes ) {
146
-
147
- if ( ! attributesUsed.has( name ) ) {
148
-
149
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure "' + name + '" attribute exists among all geometries, or in none of them.' );
150
- return null;
151
-
152
- }
153
-
154
- if ( attributes[ name ] === undefined ) attributes[ name ] = [];
155
-
156
- attributes[ name ].push( geometry.attributes[ name ] );
157
-
158
- attributesCount ++;
159
-
160
- }
161
-
162
- // ensure geometries have the same number of attributes
163
-
164
- if ( attributesCount !== attributesUsed.size ) {
165
-
166
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.' );
167
- return null;
168
-
169
- }
170
-
171
- // gather morph attributes, exit early if they're different
172
-
173
- if ( morphTargetsRelative !== geometry.morphTargetsRelative ) {
174
-
175
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.' );
176
- return null;
177
-
178
- }
179
-
180
- for ( const name in geometry.morphAttributes ) {
181
-
182
- if ( ! morphAttributesUsed.has( name ) ) {
183
-
184
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.' );
185
- return null;
186
-
187
- }
188
-
189
- if ( morphAttributes[ name ] === undefined ) morphAttributes[ name ] = [];
190
-
191
- morphAttributes[ name ].push( geometry.morphAttributes[ name ] );
192
-
193
- }
194
-
195
- if ( useGroups ) {
196
-
197
- let count;
198
-
199
- if ( isIndexed ) {
200
-
201
- count = geometry.index.count;
202
-
203
- } else if ( geometry.attributes.position !== undefined ) {
204
-
205
- count = geometry.attributes.position.count;
206
-
207
- } else {
208
-
209
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute' );
210
- return null;
211
-
212
- }
213
-
214
- mergedGeometry.addGroup( offset, count, i );
215
-
216
- offset += count;
217
-
218
- }
219
-
220
- }
221
-
222
- // merge indices
223
-
224
- if ( isIndexed ) {
225
-
226
- let indexOffset = 0;
227
- const mergedIndex = [];
228
-
229
- for ( let i = 0; i < geometries.length; ++ i ) {
230
-
231
- const index = geometries[ i ].index;
232
-
233
- for ( let j = 0; j < index.count; ++ j ) {
234
-
235
- mergedIndex.push( index.getX( j ) + indexOffset );
236
-
237
- }
238
-
239
- indexOffset += geometries[ i ].attributes.position.count;
240
-
241
- }
242
-
243
- mergedGeometry.setIndex( mergedIndex );
244
-
245
- }
246
-
247
- // merge attributes
248
-
249
- for ( const name in attributes ) {
250
-
251
- const mergedAttribute = mergeBufferAttributes( attributes[ name ] );
252
-
253
- if ( ! mergedAttribute ) {
254
-
255
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' attribute.' );
256
- return null;
257
-
258
- }
259
-
260
- mergedGeometry.setAttribute( name, mergedAttribute );
261
-
262
- }
263
-
264
- // merge morph attributes
265
-
266
- for ( const name in morphAttributes ) {
267
-
268
- const numMorphTargets = morphAttributes[ name ][ 0 ].length;
269
-
270
- if ( numMorphTargets === 0 ) break;
271
-
272
- mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
273
- mergedGeometry.morphAttributes[ name ] = [];
274
-
275
- for ( let i = 0; i < numMorphTargets; ++ i ) {
276
-
277
- const morphAttributesToMerge = [];
278
-
279
- for ( let j = 0; j < morphAttributes[ name ].length; ++ j ) {
280
-
281
- morphAttributesToMerge.push( morphAttributes[ name ][ j ][ i ] );
282
-
283
- }
284
-
285
- const mergedMorphAttribute = mergeBufferAttributes( morphAttributesToMerge );
286
-
287
- if ( ! mergedMorphAttribute ) {
288
-
289
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' morphAttribute.' );
290
- return null;
291
-
292
- }
293
-
294
- mergedGeometry.morphAttributes[ name ].push( mergedMorphAttribute );
295
-
296
- }
297
-
298
- }
299
-
300
- return mergedGeometry;
301
-
302
- }
303
-
304
- /**
305
- * @param {Array<BufferAttribute>} attributes
306
- * @return {BufferAttribute}
307
- */
308
- function mergeBufferAttributes( attributes ) {
309
-
310
- let TypedArray;
311
- let itemSize;
312
- let normalized;
313
- let arrayLength = 0;
314
-
315
- for ( let i = 0; i < attributes.length; ++ i ) {
316
-
317
- const attribute = attributes[ i ];
318
-
319
- if ( attribute.isInterleavedBufferAttribute ) {
320
-
321
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. InterleavedBufferAttributes are not supported.' );
322
- return null;
323
-
324
- }
325
-
326
- if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
327
- if ( TypedArray !== attribute.array.constructor ) {
328
-
329
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.' );
330
- return null;
331
-
332
- }
333
-
334
- if ( itemSize === undefined ) itemSize = attribute.itemSize;
335
- if ( itemSize !== attribute.itemSize ) {
336
-
337
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.' );
338
- return null;
339
-
340
- }
341
-
342
- if ( normalized === undefined ) normalized = attribute.normalized;
343
- if ( normalized !== attribute.normalized ) {
344
-
345
- console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.' );
346
- return null;
347
-
348
- }
349
-
350
- arrayLength += attribute.array.length;
351
-
352
- }
353
-
354
- const array = new TypedArray( arrayLength );
355
- let offset = 0;
356
-
357
- for ( let i = 0; i < attributes.length; ++ i ) {
358
-
359
- array.set( attributes[ i ].array, offset );
360
-
361
- offset += attributes[ i ].array.length;
362
-
363
- }
364
-
365
- return new BufferAttribute( array, itemSize, normalized );
366
-
367
- }
368
-
369
- /**
370
- * @param {BufferAttribute}
371
- * @return {BufferAttribute}
372
- */
373
- export function deepCloneAttribute( attribute ) {
374
-
375
- if ( attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute ) {
376
-
377
- return deinterleaveAttribute( attribute );
378
-
379
- }
380
-
381
- if ( attribute.isInstancedBufferAttribute ) {
382
-
383
- return new InstancedBufferAttribute().copy( attribute );
384
-
385
- }
386
-
387
- return new BufferAttribute().copy( attribute );
388
-
389
- }
390
-
391
- /**
392
- * @param {Array<BufferAttribute>} attributes
393
- * @return {Array<InterleavedBufferAttribute>}
394
- */
395
- function interleaveAttributes( attributes ) {
396
-
397
- // Interleaves the provided attributes into an InterleavedBuffer and returns
398
- // a set of InterleavedBufferAttributes for each attribute
399
- let TypedArray;
400
- let arrayLength = 0;
401
- let stride = 0;
402
-
403
- // calculate the length and type of the interleavedBuffer
404
- for ( let i = 0, l = attributes.length; i < l; ++ i ) {
405
-
406
- const attribute = attributes[ i ];
407
-
408
- if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
409
- if ( TypedArray !== attribute.array.constructor ) {
410
-
411
- console.error( 'AttributeBuffers of different types cannot be interleaved' );
412
- return null;
413
-
414
- }
415
-
416
- arrayLength += attribute.array.length;
417
- stride += attribute.itemSize;
418
-
419
- }
420
-
421
- // Create the set of buffer attributes
422
- const interleavedBuffer = new InterleavedBuffer( new TypedArray( arrayLength ), stride );
423
- let offset = 0;
424
- const res = [];
425
- const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
426
- const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
427
-
428
- for ( let j = 0, l = attributes.length; j < l; j ++ ) {
429
-
430
- const attribute = attributes[ j ];
431
- const itemSize = attribute.itemSize;
432
- const count = attribute.count;
433
- const iba = new InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, attribute.normalized );
434
- res.push( iba );
435
-
436
- offset += itemSize;
437
-
438
- // Move the data for each attribute into the new interleavedBuffer
439
- // at the appropriate offset
440
- for ( let c = 0; c < count; c ++ ) {
441
-
442
- for ( let k = 0; k < itemSize; k ++ ) {
443
-
444
- iba[ setters[ k ] ]( c, attribute[ getters[ k ] ]( c ) );
445
-
446
- }
447
-
448
- }
449
-
450
- }
451
-
452
- return res;
453
-
454
- }
455
-
456
- // returns a new, non-interleaved version of the provided attribute
457
- export function deinterleaveAttribute( attribute ) {
458
-
459
- const cons = attribute.data.array.constructor;
460
- const count = attribute.count;
461
- const itemSize = attribute.itemSize;
462
- const normalized = attribute.normalized;
463
-
464
- const array = new cons( count * itemSize );
465
- let newAttribute;
466
- if ( attribute.isInstancedInterleavedBufferAttribute ) {
467
-
468
- newAttribute = new InstancedBufferAttribute( array, itemSize, normalized, attribute.meshPerAttribute );
469
-
470
- } else {
471
-
472
- newAttribute = new BufferAttribute( array, itemSize, normalized );
473
-
474
- }
475
-
476
- for ( let i = 0; i < count; i ++ ) {
477
-
478
- newAttribute.setX( i, attribute.getX( i ) );
479
-
480
- if ( itemSize >= 2 ) {
481
-
482
- newAttribute.setY( i, attribute.getY( i ) );
483
-
484
- }
485
-
486
- if ( itemSize >= 3 ) {
487
-
488
- newAttribute.setZ( i, attribute.getZ( i ) );
489
-
490
- }
491
-
492
- if ( itemSize >= 4 ) {
493
-
494
- newAttribute.setW( i, attribute.getW( i ) );
495
-
496
- }
497
-
498
- }
499
-
500
- return newAttribute;
501
-
502
- }
503
-
504
- // deinterleaves all attributes on the geometry
505
- export function deinterleaveGeometry( geometry ) {
506
-
507
- const attributes = geometry.attributes;
508
- const morphTargets = geometry.morphTargets;
509
- const attrMap = new Map();
510
-
511
- for ( const key in attributes ) {
512
-
513
- const attr = attributes[ key ];
514
- if ( attr.isInterleavedBufferAttribute ) {
515
-
516
- if ( ! attrMap.has( attr ) ) {
517
-
518
- attrMap.set( attr, deinterleaveAttribute( attr ) );
519
-
520
- }
521
-
522
- attributes[ key ] = attrMap.get( attr );
523
-
524
- }
525
-
526
- }
527
-
528
- for ( const key in morphTargets ) {
529
-
530
- const attr = morphTargets[ key ];
531
- if ( attr.isInterleavedBufferAttribute ) {
532
-
533
- if ( ! attrMap.has( attr ) ) {
534
-
535
- attrMap.set( attr, deinterleaveAttribute( attr ) );
536
-
537
- }
538
-
539
- morphTargets[ key ] = attrMap.get( attr );
540
-
541
- }
542
-
543
- }
544
-
545
- }
546
-
547
- /**
548
- * @param {Array<BufferGeometry>} geometry
549
- * @return {number}
550
- */
551
- function estimateBytesUsed( geometry ) {
552
-
553
- // Return the estimated memory used by this geometry in bytes
554
- // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
555
- // for InterleavedBufferAttributes.
556
- let mem = 0;
557
- for ( const name in geometry.attributes ) {
558
-
559
- const attr = geometry.getAttribute( name );
560
- mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
561
-
562
- }
563
-
564
- const indices = geometry.getIndex();
565
- mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
566
- return mem;
567
-
568
- }
569
-
570
- /**
571
- * @param {BufferGeometry} geometry
572
- * @param {number} tolerance
573
- * @return {BufferGeometry}
574
- */
575
- function mergeVertices( geometry, tolerance = 1e-4 ) {
576
-
577
- tolerance = Math.max( tolerance, Number.EPSILON );
578
-
579
- // Generate an index buffer if the geometry doesn't have one, or optimize it
580
- // if it's already available.
581
- const hashToIndex = {};
582
- const indices = geometry.getIndex();
583
- const positions = geometry.getAttribute( 'position' );
584
- const vertexCount = indices ? indices.count : positions.count;
585
-
586
- // next value for triangle indices
587
- let nextIndex = 0;
588
-
589
- // attributes and new attribute arrays
590
- const attributeNames = Object.keys( geometry.attributes );
591
- const tmpAttributes = {};
592
- const tmpMorphAttributes = {};
593
- const newIndices = [];
594
- const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
595
- const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
596
-
597
- // Initialize the arrays, allocating space conservatively. Extra
598
- // space will be trimmed in the last step.
599
- for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
600
-
601
- const name = attributeNames[ i ];
602
- const attr = geometry.attributes[ name ];
603
-
604
- tmpAttributes[ name ] = new BufferAttribute(
605
- new attr.array.constructor( attr.count * attr.itemSize ),
606
- attr.itemSize,
607
- attr.normalized
608
- );
609
-
610
- const morphAttr = geometry.morphAttributes[ name ];
611
- if ( morphAttr ) {
612
-
613
- tmpMorphAttributes[ name ] = new BufferAttribute(
614
- new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize ),
615
- morphAttr.itemSize,
616
- morphAttr.normalized
617
- );
618
-
619
- }
620
-
621
- }
622
-
623
- // convert the error tolerance to an amount of decimal places to truncate to
624
- const decimalShift = Math.log10( 1 / tolerance );
625
- const shiftMultiplier = Math.pow( 10, decimalShift );
626
- for ( let i = 0; i < vertexCount; i ++ ) {
627
-
628
- const index = indices ? indices.getX( i ) : i;
629
-
630
- // Generate a hash for the vertex attributes at the current index 'i'
631
- let hash = '';
632
- for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
633
-
634
- const name = attributeNames[ j ];
635
- const attribute = geometry.getAttribute( name );
636
- const itemSize = attribute.itemSize;
637
-
638
- for ( let k = 0; k < itemSize; k ++ ) {
639
-
640
- // double tilde truncates the decimal value
641
- hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier ) },`;
642
-
643
- }
644
-
645
- }
646
-
647
- // Add another reference to the vertex if it's already
648
- // used by another index
649
- if ( hash in hashToIndex ) {
650
-
651
- newIndices.push( hashToIndex[ hash ] );
652
-
653
- } else {
654
-
655
- // copy data to the new index in the temporary attributes
656
- for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
657
-
658
- const name = attributeNames[ j ];
659
- const attribute = geometry.getAttribute( name );
660
- const morphAttr = geometry.morphAttributes[ name ];
661
- const itemSize = attribute.itemSize;
662
- const newarray = tmpAttributes[ name ];
663
- const newMorphArrays = tmpMorphAttributes[ name ];
664
-
665
- for ( let k = 0; k < itemSize; k ++ ) {
666
-
667
- const getterFunc = getters[ k ];
668
- const setterFunc = setters[ k ];
669
- newarray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) );
670
-
671
- if ( morphAttr ) {
672
-
673
- for ( let m = 0, ml = morphAttr.length; m < ml; m ++ ) {
674
-
675
- newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttr[ m ][ getterFunc ]( index ) );
676
-
677
- }
678
-
679
- }
680
-
681
- }
682
-
683
- }
684
-
685
- hashToIndex[ hash ] = nextIndex;
686
- newIndices.push( nextIndex );
687
- nextIndex ++;
688
-
689
- }
690
-
691
- }
692
-
693
- // generate result BufferGeometry
694
- const result = geometry.clone();
695
- for ( const name in geometry.attributes ) {
696
-
697
- const tmpAttribute = tmpAttributes[ name ];
698
-
699
- result.setAttribute( name, new BufferAttribute(
700
- tmpAttribute.array.slice( 0, nextIndex * tmpAttribute.itemSize ),
701
- tmpAttribute.itemSize,
702
- tmpAttribute.normalized,
703
- ) );
704
-
705
- if ( ! ( name in tmpMorphAttributes ) ) continue;
706
-
707
- for ( let j = 0; j < tmpMorphAttributes[ name ].length; j ++ ) {
708
-
709
- const tmpMorphAttribute = tmpMorphAttributes[ name ][ j ];
710
-
711
- result.morphAttributes[ name ][ j ] = new BufferAttribute(
712
- tmpMorphAttribute.array.slice( 0, nextIndex * tmpMorphAttribute.itemSize ),
713
- tmpMorphAttribute.itemSize,
714
- tmpMorphAttribute.normalized,
715
- );
716
-
717
- }
718
-
719
- }
720
-
721
- // indices
722
-
723
- result.setIndex( newIndices );
724
-
725
- return result;
726
-
727
- }
728
-
729
- /**
730
- * @param {BufferGeometry} geometry
731
- * @param {number} drawMode
732
- * @return {BufferGeometry}
733
- */
734
- function toTrianglesDrawMode( geometry, drawMode ) {
735
-
736
- if ( drawMode === TrianglesDrawMode ) {
737
-
738
- console.warn( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.' );
739
- return geometry;
740
-
741
- }
742
-
743
- if ( drawMode === TriangleFanDrawMode || drawMode === TriangleStripDrawMode ) {
744
-
745
- let index = geometry.getIndex();
746
-
747
- // generate index if not present
748
-
749
- if ( index === null ) {
750
-
751
- const indices = [];
752
-
753
- const position = geometry.getAttribute( 'position' );
754
-
755
- if ( position !== undefined ) {
756
-
757
- for ( let i = 0; i < position.count; i ++ ) {
758
-
759
- indices.push( i );
760
-
761
- }
762
-
763
- geometry.setIndex( indices );
764
- index = geometry.getIndex();
765
-
766
- } else {
767
-
768
- console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' );
769
- return geometry;
770
-
771
- }
772
-
773
- }
774
-
775
- //
776
-
777
- const numberOfTriangles = index.count - 2;
778
- const newIndices = [];
779
-
780
- if ( drawMode === TriangleFanDrawMode ) {
781
-
782
- // gl.TRIANGLE_FAN
783
-
784
- for ( let i = 1; i <= numberOfTriangles; i ++ ) {
785
-
786
- newIndices.push( index.getX( 0 ) );
787
- newIndices.push( index.getX( i ) );
788
- newIndices.push( index.getX( i + 1 ) );
789
-
790
- }
791
-
792
- } else {
793
-
794
- // gl.TRIANGLE_STRIP
795
-
796
- for ( let i = 0; i < numberOfTriangles; i ++ ) {
797
-
798
- if ( i % 2 === 0 ) {
799
-
800
- newIndices.push( index.getX( i ) );
801
- newIndices.push( index.getX( i + 1 ) );
802
- newIndices.push( index.getX( i + 2 ) );
803
-
804
- } else {
805
-
806
- newIndices.push( index.getX( i + 2 ) );
807
- newIndices.push( index.getX( i + 1 ) );
808
- newIndices.push( index.getX( i ) );
809
-
810
- }
811
-
812
- }
813
-
814
- }
815
-
816
- if ( ( newIndices.length / 3 ) !== numberOfTriangles ) {
817
-
818
- console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' );
819
-
820
- }
821
-
822
- // build final geometry
823
-
824
- const newGeometry = geometry.clone();
825
- newGeometry.setIndex( newIndices );
826
- newGeometry.clearGroups();
827
-
828
- return newGeometry;
829
-
830
- } else {
831
-
832
- console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode );
833
- return geometry;
834
-
835
- }
836
-
837
- }
838
-
839
- /**
840
- * Calculates the morphed attributes of a morphed/skinned BufferGeometry.
841
- * Helpful for Raytracing or Decals.
842
- * @param {Mesh | Line | Points} object An instance of Mesh, Line or Points.
843
- * @return {Object} An Object with original position/normal attributes and morphed ones.
844
- */
845
- function computeMorphedAttributes( object ) {
846
-
847
- if ( object.geometry.isBufferGeometry !== true ) {
848
-
849
- console.error( 'THREE.BufferGeometryUtils: Geometry is not of type BufferGeometry.' );
850
- return null;
851
-
852
- }
853
-
854
- const _vA = new Vector3();
855
- const _vB = new Vector3();
856
- const _vC = new Vector3();
857
-
858
- const _tempA = new Vector3();
859
- const _tempB = new Vector3();
860
- const _tempC = new Vector3();
861
-
862
- const _morphA = new Vector3();
863
- const _morphB = new Vector3();
864
- const _morphC = new Vector3();
865
-
866
- function _calculateMorphedAttributeData(
867
- object,
868
- attribute,
869
- morphAttribute,
870
- morphTargetsRelative,
871
- a,
872
- b,
873
- c,
874
- modifiedAttributeArray
875
- ) {
876
-
877
- _vA.fromBufferAttribute( attribute, a );
878
- _vB.fromBufferAttribute( attribute, b );
879
- _vC.fromBufferAttribute( attribute, c );
880
-
881
- const morphInfluences = object.morphTargetInfluences;
882
-
883
- if ( morphAttribute && morphInfluences ) {
884
-
885
- _morphA.set( 0, 0, 0 );
886
- _morphB.set( 0, 0, 0 );
887
- _morphC.set( 0, 0, 0 );
888
-
889
- for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
890
-
891
- const influence = morphInfluences[ i ];
892
- const morph = morphAttribute[ i ];
893
-
894
- if ( influence === 0 ) continue;
895
-
896
- _tempA.fromBufferAttribute( morph, a );
897
- _tempB.fromBufferAttribute( morph, b );
898
- _tempC.fromBufferAttribute( morph, c );
899
-
900
- if ( morphTargetsRelative ) {
901
-
902
- _morphA.addScaledVector( _tempA, influence );
903
- _morphB.addScaledVector( _tempB, influence );
904
- _morphC.addScaledVector( _tempC, influence );
905
-
906
- } else {
907
-
908
- _morphA.addScaledVector( _tempA.sub( _vA ), influence );
909
- _morphB.addScaledVector( _tempB.sub( _vB ), influence );
910
- _morphC.addScaledVector( _tempC.sub( _vC ), influence );
911
-
912
- }
913
-
914
- }
915
-
916
- _vA.add( _morphA );
917
- _vB.add( _morphB );
918
- _vC.add( _morphC );
919
-
920
- }
921
-
922
- if ( object.isSkinnedMesh ) {
923
-
924
- object.boneTransform( a, _vA );
925
- object.boneTransform( b, _vB );
926
- object.boneTransform( c, _vC );
927
-
928
- }
929
-
930
- modifiedAttributeArray[ a * 3 + 0 ] = _vA.x;
931
- modifiedAttributeArray[ a * 3 + 1 ] = _vA.y;
932
- modifiedAttributeArray[ a * 3 + 2 ] = _vA.z;
933
- modifiedAttributeArray[ b * 3 + 0 ] = _vB.x;
934
- modifiedAttributeArray[ b * 3 + 1 ] = _vB.y;
935
- modifiedAttributeArray[ b * 3 + 2 ] = _vB.z;
936
- modifiedAttributeArray[ c * 3 + 0 ] = _vC.x;
937
- modifiedAttributeArray[ c * 3 + 1 ] = _vC.y;
938
- modifiedAttributeArray[ c * 3 + 2 ] = _vC.z;
939
-
940
- }
941
-
942
- const geometry = object.geometry;
943
- const material = object.material;
944
-
945
- let a, b, c;
946
- const index = geometry.index;
947
- const positionAttribute = geometry.attributes.position;
948
- const morphPosition = geometry.morphAttributes.position;
949
- const morphTargetsRelative = geometry.morphTargetsRelative;
950
- const normalAttribute = geometry.attributes.normal;
951
- const morphNormal = geometry.morphAttributes.position;
952
-
953
- const groups = geometry.groups;
954
- const drawRange = geometry.drawRange;
955
- let i, j, il, jl;
956
- let group;
957
- let start, end;
958
-
959
- const modifiedPosition = new Float32Array( positionAttribute.count * positionAttribute.itemSize );
960
- const modifiedNormal = new Float32Array( normalAttribute.count * normalAttribute.itemSize );
961
-
962
- if ( index !== null ) {
963
-
964
- // indexed buffer geometry
965
-
966
- if ( Array.isArray( material ) ) {
967
-
968
- for ( i = 0, il = groups.length; i < il; i ++ ) {
969
-
970
- group = groups[ i ];
971
-
972
- start = Math.max( group.start, drawRange.start );
973
- end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
974
-
975
- for ( j = start, jl = end; j < jl; j += 3 ) {
976
-
977
- a = index.getX( j );
978
- b = index.getX( j + 1 );
979
- c = index.getX( j + 2 );
980
-
981
- _calculateMorphedAttributeData(
982
- object,
983
- positionAttribute,
984
- morphPosition,
985
- morphTargetsRelative,
986
- a, b, c,
987
- modifiedPosition
988
- );
989
-
990
- _calculateMorphedAttributeData(
991
- object,
992
- normalAttribute,
993
- morphNormal,
994
- morphTargetsRelative,
995
- a, b, c,
996
- modifiedNormal
997
- );
998
-
999
- }
1000
-
1001
- }
1002
-
1003
- } else {
1004
-
1005
- start = Math.max( 0, drawRange.start );
1006
- end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
1007
-
1008
- for ( i = start, il = end; i < il; i += 3 ) {
1009
-
1010
- a = index.getX( i );
1011
- b = index.getX( i + 1 );
1012
- c = index.getX( i + 2 );
1013
-
1014
- _calculateMorphedAttributeData(
1015
- object,
1016
- positionAttribute,
1017
- morphPosition,
1018
- morphTargetsRelative,
1019
- a, b, c,
1020
- modifiedPosition
1021
- );
1022
-
1023
- _calculateMorphedAttributeData(
1024
- object,
1025
- normalAttribute,
1026
- morphNormal,
1027
- morphTargetsRelative,
1028
- a, b, c,
1029
- modifiedNormal
1030
- );
1031
-
1032
- }
1033
-
1034
- }
1035
-
1036
- } else {
1037
-
1038
- // non-indexed buffer geometry
1039
-
1040
- if ( Array.isArray( material ) ) {
1041
-
1042
- for ( i = 0, il = groups.length; i < il; i ++ ) {
1043
-
1044
- group = groups[ i ];
1045
-
1046
- start = Math.max( group.start, drawRange.start );
1047
- end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
1048
-
1049
- for ( j = start, jl = end; j < jl; j += 3 ) {
1050
-
1051
- a = j;
1052
- b = j + 1;
1053
- c = j + 2;
1054
-
1055
- _calculateMorphedAttributeData(
1056
- object,
1057
- positionAttribute,
1058
- morphPosition,
1059
- morphTargetsRelative,
1060
- a, b, c,
1061
- modifiedPosition
1062
- );
1063
-
1064
- _calculateMorphedAttributeData(
1065
- object,
1066
- normalAttribute,
1067
- morphNormal,
1068
- morphTargetsRelative,
1069
- a, b, c,
1070
- modifiedNormal
1071
- );
1072
-
1073
- }
1074
-
1075
- }
1076
-
1077
- } else {
1078
-
1079
- start = Math.max( 0, drawRange.start );
1080
- end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
1081
-
1082
- for ( i = start, il = end; i < il; i += 3 ) {
1083
-
1084
- a = i;
1085
- b = i + 1;
1086
- c = i + 2;
1087
-
1088
- _calculateMorphedAttributeData(
1089
- object,
1090
- positionAttribute,
1091
- morphPosition,
1092
- morphTargetsRelative,
1093
- a, b, c,
1094
- modifiedPosition
1095
- );
1096
-
1097
- _calculateMorphedAttributeData(
1098
- object,
1099
- normalAttribute,
1100
- morphNormal,
1101
- morphTargetsRelative,
1102
- a, b, c,
1103
- modifiedNormal
1104
- );
1105
-
1106
- }
1107
-
1108
- }
1109
-
1110
- }
1111
-
1112
- const morphedPositionAttribute = new Float32BufferAttribute( modifiedPosition, 3 );
1113
- const morphedNormalAttribute = new Float32BufferAttribute( modifiedNormal, 3 );
1114
-
1115
- return {
1116
-
1117
- positionAttribute: positionAttribute,
1118
- normalAttribute: normalAttribute,
1119
- morphedPositionAttribute: morphedPositionAttribute,
1120
- morphedNormalAttribute: morphedNormalAttribute
1121
-
1122
- };
1123
-
1124
- }
1125
-
1126
- function mergeGroups( geometry ) {
1127
-
1128
- if ( geometry.groups.length === 0 ) {
1129
-
1130
- console.warn( 'THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.' );
1131
- return geometry;
1132
-
1133
- }
1134
-
1135
- let groups = geometry.groups;
1136
-
1137
- // sort groups by material index
1138
-
1139
- groups = groups.sort( ( a, b ) => {
1140
-
1141
- if ( a.materialIndex !== b.materialIndex ) return a.materialIndex - b.materialIndex;
1142
-
1143
- return a.start - b.start;
1144
-
1145
- } );
1146
-
1147
- // create index for non-indexed geometries
1148
-
1149
- if ( geometry.getIndex() === null ) {
1150
-
1151
- const positionAttribute = geometry.getAttribute( 'position' );
1152
- const indices = [];
1153
-
1154
- for ( let i = 0; i < positionAttribute.count; i += 3 ) {
1155
-
1156
- indices.push( i, i + 1, i + 2 );
1157
-
1158
- }
1159
-
1160
- geometry.setIndex( indices );
1161
-
1162
- }
1163
-
1164
- // sort index
1165
-
1166
- const index = geometry.getIndex();
1167
-
1168
- const newIndices = [];
1169
-
1170
- for ( let i = 0; i < groups.length; i ++ ) {
1171
-
1172
- const group = groups[ i ];
1173
-
1174
- const groupStart = group.start;
1175
- const groupLength = groupStart + group.count;
1176
-
1177
- for ( let j = groupStart; j < groupLength; j ++ ) {
1178
-
1179
- newIndices.push( index.getX( j ) );
1180
-
1181
- }
1182
-
1183
- }
1184
-
1185
- geometry.dispose(); // Required to force buffer recreation
1186
- geometry.setIndex( newIndices );
1187
-
1188
- // update groups indices
1189
-
1190
- let start = 0;
1191
-
1192
- for ( let i = 0; i < groups.length; i ++ ) {
1193
-
1194
- const group = groups[ i ];
1195
-
1196
- group.start = start;
1197
- start += group.count;
1198
-
1199
- }
1200
-
1201
- // merge groups
1202
-
1203
- let currentGroup = groups[ 0 ];
1204
-
1205
- geometry.groups = [ currentGroup ];
1206
-
1207
- for ( let i = 1; i < groups.length; i ++ ) {
1208
-
1209
- const group = groups[ i ];
1210
-
1211
- if ( currentGroup.materialIndex === group.materialIndex ) {
1212
-
1213
- currentGroup.count += group.count;
1214
-
1215
- } else {
1216
-
1217
- currentGroup = group;
1218
- geometry.groups.push( currentGroup );
1219
-
1220
- }
1221
-
1222
- }
1223
-
1224
- return geometry;
1225
-
1226
- }
1227
-
1228
-
1229
- // Creates a new, non-indexed geometry with smooth normals everywhere except faces that meet at
1230
- // an angle greater than the crease angle.
1231
- function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) {
1232
-
1233
- const creaseDot = Math.cos( creaseAngle );
1234
- const hashMultiplier = ( 1 + 1e-10 ) * 1e2;
1235
-
1236
- // reusable vertors
1237
- const verts = [ new Vector3(), new Vector3(), new Vector3() ];
1238
- const tempVec1 = new Vector3();
1239
- const tempVec2 = new Vector3();
1240
- const tempNorm = new Vector3();
1241
- const tempNorm2 = new Vector3();
1242
-
1243
- // hashes a vector
1244
- function hashVertex( v ) {
1245
-
1246
- const x = ~ ~ ( v.x * hashMultiplier );
1247
- const y = ~ ~ ( v.y * hashMultiplier );
1248
- const z = ~ ~ ( v.z * hashMultiplier );
1249
- return `${x},${y},${z}`;
1250
-
1251
- }
1252
-
1253
- const resultGeometry = geometry.toNonIndexed();
1254
- const posAttr = resultGeometry.attributes.position;
1255
- const vertexMap = {};
1256
-
1257
- // find all the normals shared by commonly located vertices
1258
- for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
1259
-
1260
- const i3 = 3 * i;
1261
- const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
1262
- const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
1263
- const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
1264
-
1265
- tempVec1.subVectors( c, b );
1266
- tempVec2.subVectors( a, b );
1267
-
1268
- // add the normal to the map for all vertices
1269
- const normal = new Vector3().crossVectors( tempVec1, tempVec2 ).normalize();
1270
- for ( let n = 0; n < 3; n ++ ) {
1271
-
1272
- const vert = verts[ n ];
1273
- const hash = hashVertex( vert );
1274
- if ( ! ( hash in vertexMap ) ) {
1275
-
1276
- vertexMap[ hash ] = [];
1277
-
1278
- }
1279
-
1280
- vertexMap[ hash ].push( normal );
1281
-
1282
- }
1283
-
1284
- }
1285
-
1286
- // average normals from all vertices that share a common location if they are within the
1287
- // provided crease threshold
1288
- const normalArray = new Float32Array( posAttr.count * 3 );
1289
- const normAttr = new BufferAttribute( normalArray, 3, false );
1290
- for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
1291
-
1292
- // get the face normal for this vertex
1293
- const i3 = 3 * i;
1294
- const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
1295
- const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
1296
- const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
1297
-
1298
- tempVec1.subVectors( c, b );
1299
- tempVec2.subVectors( a, b );
1300
-
1301
- tempNorm.crossVectors( tempVec1, tempVec2 ).normalize();
1302
-
1303
- // average all normals that meet the threshold and set the normal value
1304
- for ( let n = 0; n < 3; n ++ ) {
1305
-
1306
- const vert = verts[ n ];
1307
- const hash = hashVertex( vert );
1308
- const otherNormals = vertexMap[ hash ];
1309
- tempNorm2.set( 0, 0, 0 );
1310
-
1311
- for ( let k = 0, lk = otherNormals.length; k < lk; k ++ ) {
1312
-
1313
- const otherNorm = otherNormals[ k ];
1314
- if ( tempNorm.dot( otherNorm ) > creaseDot ) {
1315
-
1316
- tempNorm2.add( otherNorm );
1317
-
1318
- }
1319
-
1320
- }
1321
-
1322
- tempNorm2.normalize();
1323
- normAttr.setXYZ( i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z );
1324
-
1325
- }
1326
-
1327
- }
1328
-
1329
- resultGeometry.setAttribute( 'normal', normAttr );
1330
- return resultGeometry;
1331
-
1332
- }
1333
-
1334
- export {
1335
- computeTangents,
1336
- computeMikkTSpaceTangents,
1337
- mergeBufferGeometries,
1338
- mergeBufferAttributes,
1339
- interleaveAttributes,
1340
- estimateBytesUsed,
1341
- mergeVertices,
1342
- toTrianglesDrawMode,
1343
- computeMorphedAttributes,
1344
- mergeGroups,
1345
- toCreasedNormals
1346
- };
15
+ throw new Error('BufferGeometryUtils: computeTangents renamed to computeMikkTSpaceTangents.');
16
+ }
17
+
18
+ function computeMikkTSpaceTangents(geometry, MikkTSpace, negateSign = true) {
19
+ if (!MikkTSpace || !MikkTSpace.isReady) {
20
+ throw new Error('BufferGeometryUtils: Initialized MikkTSpace library required.');
21
+ }
22
+
23
+ if (!geometry.hasAttribute('position') || !geometry.hasAttribute('normal') || !geometry.hasAttribute('uv')) {
24
+ throw new Error('BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.');
25
+ }
26
+
27
+ function getAttributeArray(attribute) {
28
+ if (attribute.normalized || attribute.isInterleavedBufferAttribute) {
29
+ const dstArray = new Float32Array(attribute.getCount() * attribute.itemSize);
30
+
31
+ for (let i = 0, j = 0; i < attribute.getCount(); i++) {
32
+ dstArray[j++] = attribute.getX(i);
33
+ dstArray[j++] = attribute.getY(i);
34
+
35
+ if (attribute.itemSize > 2) {
36
+ dstArray[j++] = attribute.getZ(i);
37
+ }
38
+ }
39
+
40
+ return dstArray;
41
+ }
42
+
43
+ if (attribute.array instanceof Float32Array) {
44
+ return attribute.array;
45
+ }
46
+
47
+ return new Float32Array(attribute.array);
48
+ }
49
+
50
+ // MikkTSpace algorithm requires non-indexed input.
51
+
52
+ const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;
53
+
54
+ // Compute vertex tangents.
55
+
56
+ const tangents = MikkTSpace.generateTangents(
57
+
58
+ getAttributeArray(_geometry.attributes.position),
59
+ getAttributeArray(_geometry.attributes.normal),
60
+ getAttributeArray(_geometry.attributes.uv),
61
+
62
+ );
63
+
64
+ // Texture coordinate convention of glTF differs from the apparent
65
+ // default of the MikkTSpace library; .w component must be flipped.
66
+
67
+ if (negateSign) {
68
+ for (let i = 3; i < tangents.length; i += 4) {
69
+ tangents[i] *= -1;
70
+ }
71
+ }
72
+
73
+ //
74
+
75
+ _geometry.setAttribute('tangent', new BufferAttribute(tangents, 4));
76
+
77
+ if (geometry !== _geometry) {
78
+ geometry.copy(_geometry);
79
+ }
80
+
81
+ return geometry;
82
+ }
83
+
84
+ /**
85
+ * @param {Array<BufferGeometry>} geometries
86
+ * @param {Boolean} useGroups
87
+ * @return {BufferGeometry}
88
+ */
89
+ function mergeBufferGeometries(geometries, useGroups = false) {
90
+ const isIndexed = geometries[0].index !== null;
91
+
92
+ const attributesUsed = new Set(Object.keys(geometries[0].attributes));
93
+ const morphAttributesUsed = new Set(Object.keys(geometries[0].morphAttributes));
94
+
95
+ const attributes = {};
96
+ const morphAttributes = {};
97
+
98
+ const morphTargetsRelative = geometries[0].morphTargetsRelative;
99
+
100
+ const mergedGeometry = new BufferGeometry();
101
+
102
+ let offset = 0;
103
+
104
+ for (let i = 0; i < geometries.length; ++i) {
105
+ const geometry = geometries[i];
106
+ let attributesCount = 0;
107
+
108
+ // ensure that all geometries are indexed, or none
109
+
110
+ if (isIndexed !== (geometry.index !== null)) {
111
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.');
112
+ return null;
113
+ }
114
+
115
+ // gather attributes, exit early if they're different
116
+
117
+ for (const name in geometry.attributes) {
118
+ if (!attributesUsed.has(name)) {
119
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure "' + name + '" attribute exists among all geometries, or in none of them.');
120
+ return null;
121
+ }
122
+
123
+ if (attributes[name] === undefined) attributes[name] = [];
124
+
125
+ attributes[name].push(geometry.attributes[name]);
126
+
127
+ attributesCount++;
128
+ }
129
+
130
+ // ensure geometries have the same number of attributes
131
+
132
+ if (attributesCount !== attributesUsed.size) {
133
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.');
134
+ return null;
135
+ }
136
+
137
+ // gather morph attributes, exit early if they're different
138
+
139
+ if (morphTargetsRelative !== geometry.morphTargetsRelative) {
140
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.');
141
+ return null;
142
+ }
143
+
144
+ for (const name in geometry.morphAttributes) {
145
+ if (!morphAttributesUsed.has(name)) {
146
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.');
147
+ return null;
148
+ }
149
+
150
+ if (morphAttributes[name] === undefined) morphAttributes[name] = [];
151
+
152
+ morphAttributes[name].push(geometry.morphAttributes[name]);
153
+ }
154
+
155
+ if (useGroups) {
156
+ let count;
157
+
158
+ if (isIndexed) {
159
+ count = geometry.index.count;
160
+ }
161
+ else if (geometry.attributes.position !== undefined) {
162
+ count = geometry.attributes.position.count;
163
+ }
164
+ else {
165
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute');
166
+ return null;
167
+ }
168
+
169
+ mergedGeometry.addGroup(offset, count, i);
170
+
171
+ offset += count;
172
+ }
173
+ }
174
+
175
+ // merge indices
176
+
177
+ if (isIndexed) {
178
+ let indexOffset = 0;
179
+ const mergedIndex = [];
180
+
181
+ for (let i = 0; i < geometries.length; ++i) {
182
+ const index = geometries[i].index;
183
+
184
+ for (let j = 0; j < index.count; ++j) {
185
+ mergedIndex.push(index.getX(j) + indexOffset);
186
+ }
187
+
188
+ indexOffset += geometries[i].attributes.position.count;
189
+ }
190
+
191
+ mergedGeometry.setIndex(mergedIndex);
192
+ }
193
+
194
+ // merge attributes
195
+
196
+ for (const name in attributes) {
197
+ const mergedAttribute = mergeBufferAttributes(attributes[name]);
198
+
199
+ if (!mergedAttribute) {
200
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' attribute.');
201
+ return null;
202
+ }
203
+
204
+ mergedGeometry.setAttribute(name, mergedAttribute);
205
+ }
206
+
207
+ // merge morph attributes
208
+
209
+ for (const name in morphAttributes) {
210
+ const numMorphTargets = morphAttributes[name][0].length;
211
+
212
+ if (numMorphTargets === 0) break;
213
+
214
+ mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
215
+ mergedGeometry.morphAttributes[name] = [];
216
+
217
+ for (let i = 0; i < numMorphTargets; ++i) {
218
+ const morphAttributesToMerge = [];
219
+
220
+ for (let j = 0; j < morphAttributes[name].length; ++j) {
221
+ morphAttributesToMerge.push(morphAttributes[name][j][i]);
222
+ }
223
+
224
+ const mergedMorphAttribute = mergeBufferAttributes(morphAttributesToMerge);
225
+
226
+ if (!mergedMorphAttribute) {
227
+ console.error('THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' morphAttribute.');
228
+ return null;
229
+ }
230
+
231
+ mergedGeometry.morphAttributes[name].push(mergedMorphAttribute);
232
+ }
233
+ }
234
+
235
+ return mergedGeometry;
236
+ }
237
+
238
+ /**
239
+ * @param {Array<BufferAttribute>} attributes
240
+ * @return {BufferAttribute}
241
+ */
242
+ function mergeBufferAttributes(attributes) {
243
+ let TypedArray;
244
+ let itemSize;
245
+ let normalized;
246
+ let arrayLength = 0;
247
+
248
+ for (let i = 0; i < attributes.length; ++i) {
249
+ const attribute = attributes[i];
250
+
251
+ if (attribute.isInterleavedBufferAttribute) {
252
+ console.error('THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. InterleavedBufferAttributes are not supported.');
253
+ return null;
254
+ }
255
+
256
+ if (TypedArray === undefined) TypedArray = attribute.array.constructor;
257
+ if (TypedArray !== attribute.array.constructor) {
258
+ console.error('THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.');
259
+ return null;
260
+ }
261
+
262
+ if (itemSize === undefined) itemSize = attribute.itemSize;
263
+ if (itemSize !== attribute.itemSize) {
264
+ console.error('THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.');
265
+ return null;
266
+ }
267
+
268
+ if (normalized === undefined) normalized = attribute.normalized;
269
+ if (normalized !== attribute.normalized) {
270
+ console.error('THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.');
271
+ return null;
272
+ }
273
+
274
+ arrayLength += attribute.array.length;
275
+ }
276
+
277
+ const array = new TypedArray(arrayLength);
278
+ let offset = 0;
279
+
280
+ for (let i = 0; i < attributes.length; ++i) {
281
+ array.set(attributes[i].array, offset);
282
+
283
+ offset += attributes[i].array.length;
284
+ }
285
+
286
+ return new BufferAttribute(array, itemSize, normalized);
287
+ }
288
+
289
+ /**
290
+ * @param {BufferAttribute}
291
+ * @return {BufferAttribute}
292
+ */
293
+ export function deepCloneAttribute(attribute) {
294
+ if (attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute) {
295
+ return deinterleaveAttribute(attribute);
296
+ }
297
+
298
+ if (attribute.isInstancedBufferAttribute) {
299
+ return new InstancedBufferAttribute().copy(attribute);
300
+ }
301
+
302
+ return new BufferAttribute().copy(attribute);
303
+ }
304
+
305
+ /**
306
+ * @param {Array<BufferAttribute>} attributes
307
+ * @return {Array<InterleavedBufferAttribute>}
308
+ */
309
+ function interleaveAttributes(attributes) {
310
+ // Interleaves the provided attributes into an InterleavedBuffer and returns
311
+ // a set of InterleavedBufferAttributes for each attribute
312
+ let TypedArray;
313
+ let arrayLength = 0;
314
+ let stride = 0;
315
+
316
+ // calculate the length and type of the interleavedBuffer
317
+ for (let i = 0, l = attributes.length; i < l; ++i) {
318
+ const attribute = attributes[i];
319
+
320
+ if (TypedArray === undefined) TypedArray = attribute.array.constructor;
321
+ if (TypedArray !== attribute.array.constructor) {
322
+ console.error('AttributeBuffers of different types cannot be interleaved');
323
+ return null;
324
+ }
325
+
326
+ arrayLength += attribute.array.length;
327
+ stride += attribute.itemSize;
328
+ }
329
+
330
+ // Create the set of buffer attributes
331
+ const interleavedBuffer = new InterleavedBuffer(new TypedArray(arrayLength), stride);
332
+ let offset = 0;
333
+ const res = [];
334
+ const getters = ['getX', 'getY', 'getZ', 'getW'];
335
+ const setters = ['setX', 'setY', 'setZ', 'setW'];
336
+
337
+ for (let j = 0, l = attributes.length; j < l; j++) {
338
+ const attribute = attributes[j];
339
+ const itemSize = attribute.itemSize;
340
+ const count = attribute.count;
341
+ const iba = new InterleavedBufferAttribute(interleavedBuffer, itemSize, offset, attribute.normalized);
342
+ res.push(iba);
343
+
344
+ offset += itemSize;
345
+
346
+ // Move the data for each attribute into the new interleavedBuffer
347
+ // at the appropriate offset
348
+ for (let c = 0; c < count; c++) {
349
+ for (let k = 0; k < itemSize; k++) {
350
+ iba[setters[k]](c, attribute[getters[k]](c));
351
+ }
352
+ }
353
+ }
354
+
355
+ return res;
356
+ }
357
+
358
+ // returns a new, non-interleaved version of the provided attribute
359
+ export function deinterleaveAttribute(attribute) {
360
+ const cons = attribute.data.array.constructor;
361
+ const count = attribute.count;
362
+ const itemSize = attribute.itemSize;
363
+ const normalized = attribute.normalized;
364
+
365
+ const array = new cons(count * itemSize);
366
+ let newAttribute;
367
+ if (attribute.isInstancedInterleavedBufferAttribute) {
368
+ newAttribute = new InstancedBufferAttribute(array, itemSize, normalized, attribute.meshPerAttribute);
369
+ }
370
+ else {
371
+ newAttribute = new BufferAttribute(array, itemSize, normalized);
372
+ }
373
+
374
+ for (let i = 0; i < count; i++) {
375
+ newAttribute.setX(i, attribute.getX(i));
376
+
377
+ if (itemSize >= 2) {
378
+ newAttribute.setY(i, attribute.getY(i));
379
+ }
380
+
381
+ if (itemSize >= 3) {
382
+ newAttribute.setZ(i, attribute.getZ(i));
383
+ }
384
+
385
+ if (itemSize >= 4) {
386
+ newAttribute.setW(i, attribute.getW(i));
387
+ }
388
+ }
389
+
390
+ return newAttribute;
391
+ }
392
+
393
+ // deinterleaves all attributes on the geometry
394
+ export function deinterleaveGeometry(geometry) {
395
+ const attributes = geometry.attributes;
396
+ const morphTargets = geometry.morphTargets;
397
+ const attrMap = new Map();
398
+
399
+ for (const key in attributes) {
400
+ const attr = attributes[key];
401
+ if (attr.isInterleavedBufferAttribute) {
402
+ if (!attrMap.has(attr)) {
403
+ attrMap.set(attr, deinterleaveAttribute(attr));
404
+ }
405
+
406
+ attributes[key] = attrMap.get(attr);
407
+ }
408
+ }
409
+
410
+ for (const key in morphTargets) {
411
+ const attr = morphTargets[key];
412
+ if (attr.isInterleavedBufferAttribute) {
413
+ if (!attrMap.has(attr)) {
414
+ attrMap.set(attr, deinterleaveAttribute(attr));
415
+ }
416
+
417
+ morphTargets[key] = attrMap.get(attr);
418
+ }
419
+ }
420
+ }
421
+
422
+ /**
423
+ * @param {Array<BufferGeometry>} geometry
424
+ * @return {number}
425
+ */
426
+ function estimateBytesUsed(geometry) {
427
+ // Return the estimated memory used by this geometry in bytes
428
+ // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
429
+ // for InterleavedBufferAttributes.
430
+ let mem = 0;
431
+ for (const name in geometry.attributes) {
432
+ const attr = geometry.getAttribute(name);
433
+ mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
434
+ }
435
+
436
+ const indices = geometry.getIndex();
437
+ mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
438
+ return mem;
439
+ }
440
+
441
+ /**
442
+ * @param {BufferGeometry} geometry
443
+ * @param {number} tolerance
444
+ * @return {BufferGeometry}
445
+ */
446
+ function mergeVertices(geometry, tolerance = 1e-4) {
447
+ tolerance = Math.max(tolerance, Number.EPSILON);
448
+
449
+ // Generate an index buffer if the geometry doesn't have one, or optimize it
450
+ // if it's already available.
451
+ const hashToIndex = {};
452
+ const indices = geometry.getIndex();
453
+ const positions = geometry.getAttribute('position');
454
+ const vertexCount = indices ? indices.count : positions.count;
455
+
456
+ // next value for triangle indices
457
+ let nextIndex = 0;
458
+
459
+ // attributes and new attribute arrays
460
+ const attributeNames = Object.keys(geometry.attributes);
461
+ const tmpAttributes = {};
462
+ const tmpMorphAttributes = {};
463
+ const newIndices = [];
464
+ const getters = ['getX', 'getY', 'getZ', 'getW'];
465
+ const setters = ['setX', 'setY', 'setZ', 'setW'];
466
+
467
+ // Initialize the arrays, allocating space conservatively. Extra
468
+ // space will be trimmed in the last step.
469
+ for (let i = 0, l = attributeNames.length; i < l; i++) {
470
+ const name = attributeNames[i];
471
+ const attr = geometry.attributes[name];
472
+
473
+ tmpAttributes[name] = new BufferAttribute(
474
+ new attr.array.constructor(attr.count * attr.itemSize),
475
+ attr.itemSize,
476
+ attr.normalized,
477
+ );
478
+
479
+ const morphAttr = geometry.morphAttributes[name];
480
+ if (morphAttr) {
481
+ tmpMorphAttributes[name] = new BufferAttribute(
482
+ new morphAttr.array.constructor(morphAttr.count * morphAttr.itemSize),
483
+ morphAttr.itemSize,
484
+ morphAttr.normalized,
485
+ );
486
+ }
487
+ }
488
+
489
+ // convert the error tolerance to an amount of decimal places to truncate to
490
+ const decimalShift = Math.log10(1 / tolerance);
491
+ const shiftMultiplier = Math.pow(10, decimalShift);
492
+ for (let i = 0; i < vertexCount; i++) {
493
+ const index = indices ? indices.getX(i) : i;
494
+
495
+ // Generate a hash for the vertex attributes at the current index 'i'
496
+ let hash = '';
497
+ for (let j = 0, l = attributeNames.length; j < l; j++) {
498
+ const name = attributeNames[j];
499
+ const attribute = geometry.getAttribute(name);
500
+ const itemSize = attribute.itemSize;
501
+
502
+ for (let k = 0; k < itemSize; k++) {
503
+ // double tilde truncates the decimal value
504
+ hash += `${~~(attribute[getters[k]](index) * shiftMultiplier)},`;
505
+ }
506
+ }
507
+
508
+ // Add another reference to the vertex if it's already
509
+ // used by another index
510
+ if (hash in hashToIndex) {
511
+ newIndices.push(hashToIndex[hash]);
512
+ }
513
+ else {
514
+ // copy data to the new index in the temporary attributes
515
+ for (let j = 0, l = attributeNames.length; j < l; j++) {
516
+ const name = attributeNames[j];
517
+ const attribute = geometry.getAttribute(name);
518
+ const morphAttr = geometry.morphAttributes[name];
519
+ const itemSize = attribute.itemSize;
520
+ const newarray = tmpAttributes[name];
521
+ const newMorphArrays = tmpMorphAttributes[name];
522
+
523
+ for (let k = 0; k < itemSize; k++) {
524
+ const getterFunc = getters[k];
525
+ const setterFunc = setters[k];
526
+ newarray[setterFunc](nextIndex, attribute[getterFunc](index));
527
+
528
+ if (morphAttr) {
529
+ for (let m = 0, ml = morphAttr.length; m < ml; m++) {
530
+ newMorphArrays[m][setterFunc](nextIndex, morphAttr[m][getterFunc](index));
531
+ }
532
+ }
533
+ }
534
+ }
535
+
536
+ hashToIndex[hash] = nextIndex;
537
+ newIndices.push(nextIndex);
538
+ nextIndex++;
539
+ }
540
+ }
541
+
542
+ // generate result BufferGeometry
543
+ const result = geometry.clone();
544
+ for (const name in geometry.attributes) {
545
+ const tmpAttribute = tmpAttributes[name];
546
+
547
+ result.setAttribute(name, new BufferAttribute(
548
+ tmpAttribute.array.slice(0, nextIndex * tmpAttribute.itemSize),
549
+ tmpAttribute.itemSize,
550
+ tmpAttribute.normalized,
551
+ ));
552
+
553
+ if (!(name in tmpMorphAttributes)) continue;
554
+
555
+ for (let j = 0; j < tmpMorphAttributes[name].length; j++) {
556
+ const tmpMorphAttribute = tmpMorphAttributes[name][j];
557
+
558
+ result.morphAttributes[name][j] = new BufferAttribute(
559
+ tmpMorphAttribute.array.slice(0, nextIndex * tmpMorphAttribute.itemSize),
560
+ tmpMorphAttribute.itemSize,
561
+ tmpMorphAttribute.normalized,
562
+ );
563
+ }
564
+ }
565
+
566
+ // indices
567
+
568
+ result.setIndex(newIndices);
569
+
570
+ return result;
571
+ }
572
+
573
+ /**
574
+ * @param {BufferGeometry} geometry
575
+ * @param {number} drawMode
576
+ * @return {BufferGeometry}
577
+ */
578
+ function toTrianglesDrawMode(geometry, drawMode) {
579
+ if (drawMode === TrianglesDrawMode) {
580
+ console.warn('THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.');
581
+ return geometry;
582
+ }
583
+
584
+ if (drawMode === TriangleFanDrawMode || drawMode === TriangleStripDrawMode) {
585
+ let index = geometry.getIndex();
586
+
587
+ // generate index if not present
588
+
589
+ if (index === null) {
590
+ const indices = [];
591
+
592
+ const position = geometry.getAttribute('position');
593
+
594
+ if (position !== undefined) {
595
+ for (let i = 0; i < position.count; i++) {
596
+ indices.push(i);
597
+ }
598
+
599
+ geometry.setIndex(indices);
600
+ index = geometry.getIndex();
601
+ }
602
+ else {
603
+ console.error('THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.');
604
+ return geometry;
605
+ }
606
+ }
607
+
608
+ //
609
+
610
+ const numberOfTriangles = index.count - 2;
611
+ const newIndices = [];
612
+
613
+ if (drawMode === TriangleFanDrawMode) {
614
+ // gl.TRIANGLE_FAN
615
+
616
+ for (let i = 1; i <= numberOfTriangles; i++) {
617
+ newIndices.push(index.getX(0));
618
+ newIndices.push(index.getX(i));
619
+ newIndices.push(index.getX(i + 1));
620
+ }
621
+ }
622
+ else {
623
+ // gl.TRIANGLE_STRIP
624
+
625
+ for (let i = 0; i < numberOfTriangles; i++) {
626
+ if (i % 2 === 0) {
627
+ newIndices.push(index.getX(i));
628
+ newIndices.push(index.getX(i + 1));
629
+ newIndices.push(index.getX(i + 2));
630
+ }
631
+ else {
632
+ newIndices.push(index.getX(i + 2));
633
+ newIndices.push(index.getX(i + 1));
634
+ newIndices.push(index.getX(i));
635
+ }
636
+ }
637
+ }
638
+
639
+ if ((newIndices.length / 3) !== numberOfTriangles) {
640
+ console.error('THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.');
641
+ }
642
+
643
+ // build final geometry
644
+
645
+ const newGeometry = geometry.clone();
646
+ newGeometry.setIndex(newIndices);
647
+ newGeometry.clearGroups();
648
+
649
+ return newGeometry;
650
+ }
651
+ else {
652
+ console.error('THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode);
653
+ return geometry;
654
+ }
655
+ }
656
+
657
+ /**
658
+ * Calculates the morphed attributes of a morphed/skinned BufferGeometry.
659
+ * Helpful for Raytracing or Decals.
660
+ * @param {Mesh | Line | Points} object An instance of Mesh, Line or Points.
661
+ * @return {Object} An Object with original position/normal attributes and morphed ones.
662
+ */
663
+ function computeMorphedAttributes(object) {
664
+ if (object.geometry.isBufferGeometry !== true) {
665
+ console.error('THREE.BufferGeometryUtils: Geometry is not of type BufferGeometry.');
666
+ return null;
667
+ }
668
+
669
+ const _vA = new Vector3();
670
+ const _vB = new Vector3();
671
+ const _vC = new Vector3();
672
+
673
+ const _tempA = new Vector3();
674
+ const _tempB = new Vector3();
675
+ const _tempC = new Vector3();
676
+
677
+ const _morphA = new Vector3();
678
+ const _morphB = new Vector3();
679
+ const _morphC = new Vector3();
680
+
681
+ function _calculateMorphedAttributeData(
682
+ object,
683
+ attribute,
684
+ morphAttribute,
685
+ morphTargetsRelative,
686
+ a,
687
+ b,
688
+ c,
689
+ modifiedAttributeArray,
690
+ ) {
691
+ _vA.fromBufferAttribute(attribute, a);
692
+ _vB.fromBufferAttribute(attribute, b);
693
+ _vC.fromBufferAttribute(attribute, c);
694
+
695
+ const morphInfluences = object.morphTargetInfluences;
696
+
697
+ if (morphAttribute && morphInfluences) {
698
+ _morphA.set(0, 0, 0);
699
+ _morphB.set(0, 0, 0);
700
+ _morphC.set(0, 0, 0);
701
+
702
+ for (let i = 0, il = morphAttribute.length; i < il; i++) {
703
+ const influence = morphInfluences[i];
704
+ const morph = morphAttribute[i];
705
+
706
+ if (influence === 0) continue;
707
+
708
+ _tempA.fromBufferAttribute(morph, a);
709
+ _tempB.fromBufferAttribute(morph, b);
710
+ _tempC.fromBufferAttribute(morph, c);
711
+
712
+ if (morphTargetsRelative) {
713
+ _morphA.addScaledVector(_tempA, influence);
714
+ _morphB.addScaledVector(_tempB, influence);
715
+ _morphC.addScaledVector(_tempC, influence);
716
+ }
717
+ else {
718
+ _morphA.addScaledVector(_tempA.sub(_vA), influence);
719
+ _morphB.addScaledVector(_tempB.sub(_vB), influence);
720
+ _morphC.addScaledVector(_tempC.sub(_vC), influence);
721
+ }
722
+ }
723
+
724
+ _vA.add(_morphA);
725
+ _vB.add(_morphB);
726
+ _vC.add(_morphC);
727
+ }
728
+
729
+ if (object.isSkinnedMesh) {
730
+ object.boneTransform(a, _vA);
731
+ object.boneTransform(b, _vB);
732
+ object.boneTransform(c, _vC);
733
+ }
734
+
735
+ modifiedAttributeArray[a * 3 + 0] = _vA.x;
736
+ modifiedAttributeArray[a * 3 + 1] = _vA.y;
737
+ modifiedAttributeArray[a * 3 + 2] = _vA.z;
738
+ modifiedAttributeArray[b * 3 + 0] = _vB.x;
739
+ modifiedAttributeArray[b * 3 + 1] = _vB.y;
740
+ modifiedAttributeArray[b * 3 + 2] = _vB.z;
741
+ modifiedAttributeArray[c * 3 + 0] = _vC.x;
742
+ modifiedAttributeArray[c * 3 + 1] = _vC.y;
743
+ modifiedAttributeArray[c * 3 + 2] = _vC.z;
744
+ }
745
+
746
+ const geometry = object.geometry;
747
+ const material = object.material;
748
+
749
+ let a, b, c;
750
+ const index = geometry.index;
751
+ const positionAttribute = geometry.attributes.position;
752
+ const morphPosition = geometry.morphAttributes.position;
753
+ const morphTargetsRelative = geometry.morphTargetsRelative;
754
+ const normalAttribute = geometry.attributes.normal;
755
+ const morphNormal = geometry.morphAttributes.position;
756
+
757
+ const groups = geometry.groups;
758
+ const drawRange = geometry.drawRange;
759
+ let i, j, il, jl;
760
+ let group;
761
+ let start, end;
762
+
763
+ const modifiedPosition = new Float32Array(positionAttribute.count * positionAttribute.itemSize);
764
+ const modifiedNormal = new Float32Array(normalAttribute.count * normalAttribute.itemSize);
765
+
766
+ if (index !== null) {
767
+ // indexed buffer geometry
768
+
769
+ if (Array.isArray(material)) {
770
+ for (i = 0, il = groups.length; i < il; i++) {
771
+ group = groups[i];
772
+
773
+ start = Math.max(group.start, drawRange.start);
774
+ end = Math.min((group.start + group.count), (drawRange.start + drawRange.count));
775
+
776
+ for (j = start, jl = end; j < jl; j += 3) {
777
+ a = index.getX(j);
778
+ b = index.getX(j + 1);
779
+ c = index.getX(j + 2);
780
+
781
+ _calculateMorphedAttributeData(
782
+ object,
783
+ positionAttribute,
784
+ morphPosition,
785
+ morphTargetsRelative,
786
+ a, b, c,
787
+ modifiedPosition,
788
+ );
789
+
790
+ _calculateMorphedAttributeData(
791
+ object,
792
+ normalAttribute,
793
+ morphNormal,
794
+ morphTargetsRelative,
795
+ a, b, c,
796
+ modifiedNormal,
797
+ );
798
+ }
799
+ }
800
+ }
801
+ else {
802
+ start = Math.max(0, drawRange.start);
803
+ end = Math.min(index.count, (drawRange.start + drawRange.count));
804
+
805
+ for (i = start, il = end; i < il; i += 3) {
806
+ a = index.getX(i);
807
+ b = index.getX(i + 1);
808
+ c = index.getX(i + 2);
809
+
810
+ _calculateMorphedAttributeData(
811
+ object,
812
+ positionAttribute,
813
+ morphPosition,
814
+ morphTargetsRelative,
815
+ a, b, c,
816
+ modifiedPosition,
817
+ );
818
+
819
+ _calculateMorphedAttributeData(
820
+ object,
821
+ normalAttribute,
822
+ morphNormal,
823
+ morphTargetsRelative,
824
+ a, b, c,
825
+ modifiedNormal,
826
+ );
827
+ }
828
+ }
829
+ }
830
+ else {
831
+ // non-indexed buffer geometry
832
+
833
+ if (Array.isArray(material)) {
834
+ for (i = 0, il = groups.length; i < il; i++) {
835
+ group = groups[i];
836
+
837
+ start = Math.max(group.start, drawRange.start);
838
+ end = Math.min((group.start + group.count), (drawRange.start + drawRange.count));
839
+
840
+ for (j = start, jl = end; j < jl; j += 3) {
841
+ a = j;
842
+ b = j + 1;
843
+ c = j + 2;
844
+
845
+ _calculateMorphedAttributeData(
846
+ object,
847
+ positionAttribute,
848
+ morphPosition,
849
+ morphTargetsRelative,
850
+ a, b, c,
851
+ modifiedPosition,
852
+ );
853
+
854
+ _calculateMorphedAttributeData(
855
+ object,
856
+ normalAttribute,
857
+ morphNormal,
858
+ morphTargetsRelative,
859
+ a, b, c,
860
+ modifiedNormal,
861
+ );
862
+ }
863
+ }
864
+ }
865
+ else {
866
+ start = Math.max(0, drawRange.start);
867
+ end = Math.min(positionAttribute.count, (drawRange.start + drawRange.count));
868
+
869
+ for (i = start, il = end; i < il; i += 3) {
870
+ a = i;
871
+ b = i + 1;
872
+ c = i + 2;
873
+
874
+ _calculateMorphedAttributeData(
875
+ object,
876
+ positionAttribute,
877
+ morphPosition,
878
+ morphTargetsRelative,
879
+ a, b, c,
880
+ modifiedPosition,
881
+ );
882
+
883
+ _calculateMorphedAttributeData(
884
+ object,
885
+ normalAttribute,
886
+ morphNormal,
887
+ morphTargetsRelative,
888
+ a, b, c,
889
+ modifiedNormal,
890
+ );
891
+ }
892
+ }
893
+ }
894
+
895
+ const morphedPositionAttribute = new Float32BufferAttribute(modifiedPosition, 3);
896
+ const morphedNormalAttribute = new Float32BufferAttribute(modifiedNormal, 3);
897
+
898
+ return {
899
+
900
+ positionAttribute: positionAttribute,
901
+ normalAttribute: normalAttribute,
902
+ morphedPositionAttribute: morphedPositionAttribute,
903
+ morphedNormalAttribute: morphedNormalAttribute,
904
+
905
+ };
906
+ }
907
+
908
+ function mergeGroups(geometry) {
909
+ if (geometry.groups.length === 0) {
910
+ console.warn('THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.');
911
+ return geometry;
912
+ }
913
+
914
+ let groups = geometry.groups;
915
+
916
+ // sort groups by material index
917
+
918
+ groups = groups.sort((a, b) => {
919
+ if (a.materialIndex !== b.materialIndex) return a.materialIndex - b.materialIndex;
920
+
921
+ return a.start - b.start;
922
+ });
923
+
924
+ // create index for non-indexed geometries
925
+
926
+ if (geometry.getIndex() === null) {
927
+ const positionAttribute = geometry.getAttribute('position');
928
+ const indices = [];
929
+
930
+ for (let i = 0; i < positionAttribute.count; i += 3) {
931
+ indices.push(i, i + 1, i + 2);
932
+ }
933
+
934
+ geometry.setIndex(indices);
935
+ }
936
+
937
+ // sort index
938
+
939
+ const index = geometry.getIndex();
940
+
941
+ const newIndices = [];
942
+
943
+ for (let i = 0; i < groups.length; i++) {
944
+ const group = groups[i];
945
+
946
+ const groupStart = group.start;
947
+ const groupLength = groupStart + group.count;
948
+
949
+ for (let j = groupStart; j < groupLength; j++) {
950
+ newIndices.push(index.getX(j));
951
+ }
952
+ }
953
+
954
+ geometry.dispose(); // Required to force buffer recreation
955
+ geometry.setIndex(newIndices);
956
+
957
+ // update groups indices
958
+
959
+ let start = 0;
960
+
961
+ for (let i = 0; i < groups.length; i++) {
962
+ const group = groups[i];
963
+
964
+ group.start = start;
965
+ start += group.count;
966
+ }
967
+
968
+ // merge groups
969
+
970
+ let currentGroup = groups[0];
971
+
972
+ geometry.groups = [currentGroup];
973
+
974
+ for (let i = 1; i < groups.length; i++) {
975
+ const group = groups[i];
976
+
977
+ if (currentGroup.materialIndex === group.materialIndex) {
978
+ currentGroup.count += group.count;
979
+ }
980
+ else {
981
+ currentGroup = group;
982
+ geometry.groups.push(currentGroup);
983
+ }
984
+ }
985
+
986
+ return geometry;
987
+ }
988
+
989
+ // Creates a new, non-indexed geometry with smooth normals everywhere except faces that meet at
990
+ // an angle greater than the crease angle.
991
+ function toCreasedNormals(geometry, creaseAngle = Math.PI / 3 /* 60 degrees */) {
992
+ const creaseDot = Math.cos(creaseAngle);
993
+ const hashMultiplier = (1 + 1e-10) * 1e2;
994
+
995
+ // reusable vertors
996
+ const verts = [new Vector3(), new Vector3(), new Vector3()];
997
+ const tempVec1 = new Vector3();
998
+ const tempVec2 = new Vector3();
999
+ const tempNorm = new Vector3();
1000
+ const tempNorm2 = new Vector3();
1001
+
1002
+ // hashes a vector
1003
+ function hashVertex(v) {
1004
+ const x = ~~(v.x * hashMultiplier);
1005
+ const y = ~~(v.y * hashMultiplier);
1006
+ const z = ~~(v.z * hashMultiplier);
1007
+ return `${x},${y},${z}`;
1008
+ }
1009
+
1010
+ const resultGeometry = geometry.toNonIndexed();
1011
+ const posAttr = resultGeometry.attributes.position;
1012
+ const vertexMap = {};
1013
+
1014
+ // find all the normals shared by commonly located vertices
1015
+ for (let i = 0, l = posAttr.count / 3; i < l; i++) {
1016
+ const i3 = 3 * i;
1017
+ const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);
1018
+ const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);
1019
+ const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);
1020
+
1021
+ tempVec1.subVectors(c, b);
1022
+ tempVec2.subVectors(a, b);
1023
+
1024
+ // add the normal to the map for all vertices
1025
+ const normal = new Vector3().crossVectors(tempVec1, tempVec2).normalize();
1026
+ for (let n = 0; n < 3; n++) {
1027
+ const vert = verts[n];
1028
+ const hash = hashVertex(vert);
1029
+ if (!(hash in vertexMap)) {
1030
+ vertexMap[hash] = [];
1031
+ }
1032
+
1033
+ vertexMap[hash].push(normal);
1034
+ }
1035
+ }
1036
+
1037
+ // average normals from all vertices that share a common location if they are within the
1038
+ // provided crease threshold
1039
+ const normalArray = new Float32Array(posAttr.count * 3);
1040
+ const normAttr = new BufferAttribute(normalArray, 3, false);
1041
+ for (let i = 0, l = posAttr.count / 3; i < l; i++) {
1042
+ // get the face normal for this vertex
1043
+ const i3 = 3 * i;
1044
+ const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);
1045
+ const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);
1046
+ const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);
1047
+
1048
+ tempVec1.subVectors(c, b);
1049
+ tempVec2.subVectors(a, b);
1050
+
1051
+ tempNorm.crossVectors(tempVec1, tempVec2).normalize();
1052
+
1053
+ // average all normals that meet the threshold and set the normal value
1054
+ for (let n = 0; n < 3; n++) {
1055
+ const vert = verts[n];
1056
+ const hash = hashVertex(vert);
1057
+ const otherNormals = vertexMap[hash];
1058
+ tempNorm2.set(0, 0, 0);
1059
+
1060
+ for (let k = 0, lk = otherNormals.length; k < lk; k++) {
1061
+ const otherNorm = otherNormals[k];
1062
+ if (tempNorm.dot(otherNorm) > creaseDot) {
1063
+ tempNorm2.add(otherNorm);
1064
+ }
1065
+ }
1066
+
1067
+ tempNorm2.normalize();
1068
+ normAttr.setXYZ(i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z);
1069
+ }
1070
+ }
1071
+
1072
+ resultGeometry.setAttribute('normal', normAttr);
1073
+ return resultGeometry;
1074
+ }
1075
+
1076
+ export {
1077
+ computeMikkTSpaceTangents,
1078
+ computeMorphedAttributes,
1079
+ computeTangents,
1080
+ estimateBytesUsed,
1081
+ interleaveAttributes,
1082
+ mergeBufferAttributes,
1083
+ mergeBufferGeometries,
1084
+ mergeGroups,
1085
+ mergeVertices,
1086
+ toCreasedNormals,
1087
+ toTrianglesDrawMode };