Skip to content

Commit

Permalink
#28440 Fixed BufferGeometryUtils.mergeVertices to handle morphAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-enache committed May 20, 2024
1 parent 2de0ee4 commit 22f9cca
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
34 changes: 18 additions & 16 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,20 +623,22 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
const name = attributeNames[ i ];
const attr = geometry.attributes[ name ];

tmpAttributes[ name ] = new BufferAttribute(
tmpAttributes[ name ] = new attr.constructor(
new attr.array.constructor( attr.count * attr.itemSize ),
attr.itemSize,
attr.normalized
);

const morphAttr = geometry.morphAttributes[ name ];
if ( morphAttr ) {
const morphAttrCollection = geometry.morphAttributes[ name ];
if ( morphAttrCollection ) {

tmpMorphAttributes[ name ] = new BufferAttribute(
new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize ),
morphAttr.itemSize,
morphAttr.normalized
);
if ( ! tmpMorphAttributes[ name ] ) tmpMorphAttributes[ name ] = [];
morphAttrCollection.forEach( ( morphAttr, i ) => {

const array = new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize );
tmpMorphAttributes[ name ][ i ] = new morphAttr.constructor( array, morphAttr.itemSize, morphAttr.normalized );

} );

}

Expand Down Expand Up @@ -681,22 +683,22 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {

const name = attributeNames[ j ];
const attribute = geometry.getAttribute( name );
const morphAttr = geometry.morphAttributes[ name ];
const morphAttrCollection = geometry.morphAttributes[ name ];
const itemSize = attribute.itemSize;
const newarray = tmpAttributes[ name ];
const newArray = tmpAttributes[ name ];
const newMorphArrays = tmpMorphAttributes[ name ];

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

const getterFunc = getters[ k ];
const setterFunc = setters[ k ];
newarray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) );
newArray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) );

if ( morphAttr ) {
if ( morphAttrCollection ) {

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

newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttr[ m ][ getterFunc ]( index ) );
newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttrCollection[ m ][ getterFunc ]( index ) );

}

Expand All @@ -720,7 +722,7 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {

const tmpAttribute = tmpAttributes[ name ];

result.setAttribute( name, new BufferAttribute(
result.setAttribute( name, new tmpAttribute.constructor(
tmpAttribute.array.slice( 0, nextIndex * tmpAttribute.itemSize ),
tmpAttribute.itemSize,
tmpAttribute.normalized,
Expand All @@ -732,7 +734,7 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {

const tmpMorphAttribute = tmpMorphAttributes[ name ][ j ];

result.morphAttributes[ name ][ j ] = new BufferAttribute(
result.morphAttributes[ name ][ j ] = new tmpMorphAttribute.constructor(
tmpMorphAttribute.array.slice( 0, nextIndex * tmpMorphAttribute.itemSize ),
tmpMorphAttribute.itemSize,
tmpMorphAttribute.normalized,
Expand Down
60 changes: 60 additions & 0 deletions test/unit/addons/utils/BufferGeometryUtils.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { BufferGeometry } from '../../../../src/core/BufferGeometry.js';
import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
import * as BufferGeometryUtils from '../../../../examples/jsm/utils/BufferGeometryUtils.js';

const getGeometry = () => {

const geometry = new BufferGeometry();

// square
const vertices = new Float32Array( [
- 1.0, - 1.0, 0.0, // Bottom left
1.0, - 1.0, 0.0, // Bottom right
1.0, 1.0, 0.0, // Top right
- 1.0, 1.0, 0.0 // Top left
] );

const morphVertices = new Float32Array( [
0.0, - 1.0, 0.0, // Bottom
1.0, 0.0, 0.0, // Right
0.0, 1.0, 0.0, // Top
- 1.0, 0.0, 0.0 // Left
] );

geometry.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );

geometry.morphAttributes.position = [
new BufferAttribute( morphVertices, 3 )
];

return geometry;

};

export default QUnit.module( 'Addons', () => {

QUnit.module( 'Utils', () => {

QUnit.module( 'BufferGeometryUtils', () => {

QUnit.module( 'mergeVertices', () => {

QUnit.test( 'can handle morphAttributes without crashing', ( assert ) => {

const geometry = getGeometry();

const indexedGeometry = BufferGeometryUtils.mergeVertices( geometry );

assert.deepEqual( geometry.morphAttributes.position[ 0 ], indexedGeometry.morphAttributes.position[ 0 ], 'morphAttributes were handled' );
assert.ok( indexedGeometry.index, 'has index' );

} );

} );

} );

} );


} );
3 changes: 3 additions & 0 deletions test/unit/three.source.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,6 @@ import './src/textures/FramebufferTexture.tests.js';
import './src/textures/Source.tests.js';
import './src/textures/Texture.tests.js';
import './src/textures/VideoTexture.tests.js';

//addons
import './addons/utils/BufferGeometryUtils.tests.js';

0 comments on commit 22f9cca

Please sign in to comment.