Skip to content

Commit f70ead9

Browse files
authored
BufferGeometryUtils: Fix mergeVertices() with normalized vertex attributes (#24577)
* BufferGeometryUtils: Fix mergeVertices() with normalized vertex attributes * Clean up
1 parent 118eb9e commit f70ead9

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

examples/jsm/utils/BufferGeometryUtils.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
InstancedBufferAttribute,
66
InterleavedBuffer,
77
InterleavedBufferAttribute,
8-
MathUtils,
98
TriangleFanDrawMode,
109
TriangleStripDrawMode,
1110
TrianglesDrawMode,
@@ -36,17 +35,16 @@ function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) {
3635

3736
if ( attribute.normalized || attribute.isInterleavedBufferAttribute ) {
3837

39-
const srcArray = attribute.isInterleavedBufferAttribute ? attribute.data.array : attribute.array;
4038
const dstArray = new Float32Array( attribute.getCount() * attribute.itemSize );
4139

4240
for ( let i = 0, j = 0; i < attribute.getCount(); i ++ ) {
4341

44-
dstArray[ j ++ ] = MathUtils.denormalize( attribute.getX( i ), srcArray );
45-
dstArray[ j ++ ] = MathUtils.denormalize( attribute.getY( i ), srcArray );
42+
dstArray[ j ++ ] = attribute.getX( i );
43+
dstArray[ j ++ ] = attribute.getY( i );
4644

4745
if ( attribute.itemSize > 2 ) {
4846

49-
dstArray[ j ++ ] = MathUtils.denormalize( attribute.getZ( i ), srcArray );
47+
dstArray[ j ++ ] = attribute.getZ( i );
5048

5149
}
5250

@@ -573,22 +571,33 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
573571

574572
// attributes and new attribute arrays
575573
const attributeNames = Object.keys( geometry.attributes );
576-
const attrArrays = {};
577-
const morphAttrsArrays = {};
574+
const tmpAttributes = {};
575+
const tmpMorphAttributes = {};
578576
const newIndices = [];
579577
const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
578+
const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
580579

581-
// initialize the arrays
580+
// Initialize the arrays, allocating space conservatively. Extra
581+
// space will be trimmed in the last step.
582582
for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
583583

584584
const name = attributeNames[ i ];
585+
const attr = geometry.attributes[ name ];
585586

586-
attrArrays[ name ] = [];
587+
tmpAttributes[ name ] = new BufferAttribute(
588+
new attr.array.constructor( attr.count * attr.itemSize ),
589+
attr.itemSize,
590+
attr.normalized
591+
);
587592

588593
const morphAttr = geometry.morphAttributes[ name ];
589594
if ( morphAttr ) {
590595

591-
morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] );
596+
tmpMorphAttributes[ name ] = new BufferAttribute(
597+
new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize ),
598+
morphAttr.itemSize,
599+
morphAttr.normalized
600+
);
592601

593602
}
594603

@@ -626,26 +635,27 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
626635

627636
} else {
628637

629-
// copy data to the new index in the attribute arrays
638+
// copy data to the new index in the temporary attributes
630639
for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
631640

632641
const name = attributeNames[ j ];
633642
const attribute = geometry.getAttribute( name );
634643
const morphAttr = geometry.morphAttributes[ name ];
635644
const itemSize = attribute.itemSize;
636-
const newarray = attrArrays[ name ];
637-
const newMorphArrays = morphAttrsArrays[ name ];
645+
const newarray = tmpAttributes[ name ];
646+
const newMorphArrays = tmpMorphAttributes[ name ];
638647

639648
for ( let k = 0; k < itemSize; k ++ ) {
640649

641650
const getterFunc = getters[ k ];
642-
newarray.push( attribute[ getterFunc ]( index ) );
651+
const setterFunc = setters[ k ];
652+
newarray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) );
643653

644654
if ( morphAttr ) {
645655

646656
for ( let m = 0, ml = morphAttr.length; m < ml; m ++ ) {
647657

648-
newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) );
658+
newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttr[ m ][ getterFunc ]( index ) );
649659

650660
}
651661

@@ -663,31 +673,29 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
663673

664674
}
665675

666-
// Generate typed arrays from new attribute arrays and update
667-
// the attributeBuffers
676+
// generate result BufferGeometry
668677
const result = geometry.clone();
669-
for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
670-
671-
const name = attributeNames[ i ];
672-
const oldAttribute = geometry.getAttribute( name );
673-
674-
const buffer = new oldAttribute.array.constructor( attrArrays[ name ] );
675-
const attribute = new BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.normalized );
678+
for ( const name in geometry.attributes ) {
676679

677-
result.setAttribute( name, attribute );
680+
const tmpAttribute = tmpAttributes[ name ];
678681

679-
// Update the attribute arrays
680-
if ( name in morphAttrsArrays ) {
682+
result.setAttribute( name, new BufferAttribute(
683+
tmpAttribute.array.slice( 0, nextIndex * tmpAttribute.itemSize ),
684+
tmpAttribute.itemSize,
685+
tmpAttribute.normalized,
686+
) );
681687

682-
for ( let j = 0; j < morphAttrsArrays[ name ].length; j ++ ) {
688+
if ( ! ( name in tmpMorphAttributes ) ) continue;
683689

684-
const oldMorphAttribute = geometry.morphAttributes[ name ][ j ];
690+
for ( let j = 0; j < tmpMorphAttributes[ name ].length; j ++ ) {
685691

686-
const buffer = new oldMorphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] );
687-
const morphAttribute = new BufferAttribute( buffer, oldMorphAttribute.itemSize, oldMorphAttribute.normalized );
688-
result.morphAttributes[ name ][ j ] = morphAttribute;
692+
const tmpMorphAttribute = tmpMorphAttributes[ name ][ j ];
689693

690-
}
694+
result.morphAttributes[ name ][ j ] = new BufferAttribute(
695+
tmpMorphAttribute.array.slice( 0, nextIndex * tmpMorphAttribute.itemSize ),
696+
tmpMorphAttribute.itemSize,
697+
tmpMorphAttribute.normalized,
698+
);
691699

692700
}
693701

0 commit comments

Comments
 (0)