Skip to content

Commit 5e43514

Browse files
authored
BatchedMesh: Make data copying more robust, handle interleaved attributes (mrdoob#27066)
* BatchedMesh: Remove unused fields * BatchedMesh: Add thrown errors in unimplemented functions * Add PURE and glsl identifiers * Add more error handling * More errors * Fix batched index overrun * BatchedMesh: Check for normalized, as well * Fix screenshot * Handle interleaved data * Add comments, check if constructors are the same * Move update setting * Remove pure annotations
1 parent b1ff959 commit 5e43514

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

examples/jsm/objects/BatchedMesh.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ const batchingVertex = /* glsl */`
6464
// @TODO: SkinnedMesh support?
6565
// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
6666

67+
// copies data from attribute "src" into "target" starting at "targetOffset"
68+
function copyAttributeData( src, target, targetOffset = 0 ) {
69+
70+
const itemSize = target.itemSize;
71+
if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
72+
73+
// use the component getters and setters if the array data cannot
74+
// be copied directly
75+
const vertexCount = src.count;
76+
for ( let i = 0; i < vertexCount; i ++ ) {
77+
78+
for ( let c = 0; c < itemSize; c ++ ) {
79+
80+
target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
81+
82+
}
83+
84+
}
85+
86+
} else {
87+
88+
// faster copy approach using typed array set function
89+
target.array.set( src.array, targetOffset * itemSize );
90+
91+
}
92+
93+
target.needsUpdate = true;
94+
95+
}
96+
6797
class BatchedMesh extends Mesh {
6898

6999
constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
@@ -332,9 +362,7 @@ class BatchedMesh extends Mesh {
332362

333363
const srcAttribute = geometry.getAttribute( attributeName );
334364
const dstAttribute = batchGeometry.getAttribute( attributeName );
335-
336-
dstAttribute.array.set( srcAttribute.array, vertexCount * dstAttribute.itemSize );
337-
dstAttribute.needsUpdate = true;
365+
copyAttributeData( srcAttribute, dstAttribute, vertexCount );
338366

339367
}
340368

0 commit comments

Comments
 (0)