Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/objects/SkinnedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Ray } from '../math/Ray.js';
import { AttachedBindMode, DetachedBindMode } from '../constants.js';
import { warn } from '../utils.js';

const _basePosition = /*@__PURE__*/ new Vector3();
const _baseVector = /*@__PURE__*/ new Vector4();

const _skinIndex = /*@__PURE__*/ new Vector4();
const _skinWeight = /*@__PURE__*/ new Vector4();

const _vector3 = /*@__PURE__*/ new Vector3();
const _vector4 = /*@__PURE__*/ new Vector4();
const _matrix4 = /*@__PURE__*/ new Matrix4();
const _vertex = /*@__PURE__*/ new Vector3();

Expand Down Expand Up @@ -309,12 +309,12 @@ class SkinnedMesh extends Mesh {

/**
* Applies the bone transform associated with the given index to the given
* vertex position. Returns the updated vector.
* vector. Can be used to transform positions or direction vectors by providing
* a Vector4 with 1 or 0 in the w component respectively. Returns the updated vector.
*
* @param {number} index - The vertex index.
* @param {Vector3} target - The target object that is used to store the method's result.
* the skinned mesh's world matrix will be used instead.
* @return {Vector3} The updated vertex position.
* @param {Vector3|Vector4} target - The target object that is used to store the method's result.
* @return {Vector3|Vector4} The updated vertex attribute data.
*/
applyBoneTransform( index, target ) {

Expand All @@ -324,9 +324,19 @@ class SkinnedMesh extends Mesh {
_skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
_skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );

_basePosition.copy( target ).applyMatrix4( this.bindMatrix );
if ( target.isVector4 ) {

_baseVector.copy( target );
target.set( 0, 0, 0, 0 );

} else {

_baseVector.set( ...target, 1 );
target.set( 0, 0, 0 );

target.set( 0, 0, 0 );
}

_baseVector.applyMatrix4( this.bindMatrix );

for ( let i = 0; i < 4; i ++ ) {

Expand All @@ -338,12 +348,19 @@ class SkinnedMesh extends Mesh {

_matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );

target.addScaledVector( _vector3.copy( _basePosition ).applyMatrix4( _matrix4 ), weight );
target.addScaledVector( _vector4.copy( _baseVector ).applyMatrix4( _matrix4 ), weight );

}

}

if ( target.isVector4 ) {

// ensure the homogenous coordinate remains unchanged after vector operations
target.w = _baseVector.w;

}

return target.applyMatrix4( this.bindMatrixInverse );

}
Expand Down