Skip to content

Commit

Permalink
InterleavedBufferAttribute: Implement .clone().
Browse files Browse the repository at this point in the history
  • Loading branch information
Don McCurdy committed Dec 6, 2017
1 parent 8998cfc commit 8f78c13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
33 changes: 4 additions & 29 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ THREE.GLTFLoader = ( function () {
// So morphTarget value will depend on mesh's position, then cloning attribute
// for the case if attribute is shared among two or more meshes.

positionAttribute = cloneBufferAttribute( accessors[ target.POSITION ] );
positionAttribute = accessors[ target.POSITION ].clone();
var position = geometry.attributes.position;

for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) {
Expand All @@ -1139,7 +1139,7 @@ THREE.GLTFLoader = ( function () {

// Copying the original position not to affect the final position.
// See the formula above.
positionAttribute = cloneBufferAttribute( geometry.attributes.position );
positionAttribute = geometry.attributes.position.clone();

}

Expand All @@ -1156,7 +1156,7 @@ THREE.GLTFLoader = ( function () {

// see target.POSITION's comment

normalAttribute = cloneBufferAttribute( accessors[ target.NORMAL ] );
normalAttribute = accessors[ target.NORMAL ].clone();
var normal = geometry.attributes.normal;

for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) {
Expand All @@ -1172,7 +1172,7 @@ THREE.GLTFLoader = ( function () {

} else if ( geometry.attributes.normal !== undefined ) {

normalAttribute = cloneBufferAttribute( geometry.attributes.normal );
normalAttribute = geometry.attributes.normal.clone();

}

Expand All @@ -1199,31 +1199,6 @@ THREE.GLTFLoader = ( function () {

}

function cloneBufferAttribute( attribute ) {

if ( attribute.isInterleavedBufferAttribute ) {

var count = attribute.count;
var itemSize = attribute.itemSize;
var array = attribute.array.slice( 0, count * itemSize );

for ( var i = 0; i < count; ++ i ) {

array[ i ] = attribute.getX( i );
if ( itemSize >= 2 ) array[ i + 1 ] = attribute.getY( i );
if ( itemSize >= 3 ) array[ i + 2 ] = attribute.getZ( i );
if ( itemSize >= 4 ) array[ i + 3 ] = attribute.getW( i );

}

return new THREE.BufferAttribute( array, itemSize, attribute.normalized );

}

return attribute.clone();

}

/* GLTF PARSER */

function GLTFParser( json, extensions, options ) {
Expand Down
20 changes: 20 additions & 0 deletions src/core/InterleavedBufferAttribute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BufferAttribute } from './BufferAttribute.js';
import { _Math } from '../math/Math.js';

/**
Expand Down Expand Up @@ -134,6 +135,25 @@ Object.assign( InterleavedBufferAttribute.prototype, {

return this;

},

clone: function () {

var count = this.count;
var itemSize = this.itemSize;
var array = this.data.array.slice( 0, count * itemSize );

for ( var i = 0; i < count; ++ i ) {

array[ i ] = this.getX( i );
if ( itemSize >= 2 ) array[ i + 1 ] = this.getY( i );
if ( itemSize >= 3 ) array[ i + 2 ] = this.getZ( i );
if ( itemSize >= 4 ) array[ i + 3 ] = this.getW( i );

}

return new BufferAttribute( array, itemSize, this.normalized );

}

} );
Expand Down

0 comments on commit 8f78c13

Please sign in to comment.