Skip to content

Commit

Permalink
Account for existing groups in BufferGeometryUtils.mergeBufferGeometries
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-capalbo authored Aug 22, 2022
1 parent 9bb4782 commit 006995a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,14 @@ function mergeBufferGeometries( geometries, useGroups = false ) {
const mergedGeometry = new BufferGeometry();

let offset = 0;

let nextGroupIndex = 0;

for ( let i = 0; i < geometries.length; ++ i ) {

const geometry = geometries[ i ];
let attributesCount = 0;
let groupIndex = nextGroupIndex;

// ensure that all geometries are indexed, or none

Expand Down Expand Up @@ -199,7 +202,22 @@ function mergeBufferGeometries( geometries, useGroups = false ) {
mergedGeometry.userData.mergedUserData = mergedGeometry.userData.mergedUserData || [];
mergedGeometry.userData.mergedUserData.push( geometry.userData );

if ( useGroups ) {
if ( useGroups && geometry.groups.length > 0 ) {

// Keep existing groups

for (let group of geometry.groups ) {

let offsetMaterialIndex = groupIndex + group.materialIndex
mergedGeometry.addGroup( offset, group.count, offsetMaterialIndex);
offset += group.count;
nextGroupIndex = Math.max( nextGroupIndex, offsetMaterialIndex );

}

++ nextGroupIndex;

} else if ( useGroups ) {

let count;

Expand All @@ -221,7 +239,8 @@ function mergeBufferGeometries( geometries, useGroups = false ) {
mergedGeometry.addGroup( offset, count, i );

offset += count;


++ nextGroupIndex;
}

}
Expand Down

0 comments on commit 006995a

Please sign in to comment.