123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import { Bone } from './Bone.js';
- import { Matrix4 } from '../math/Matrix4.js';
- import { MathUtils } from '../math/MathUtils.js';
- const _offsetMatrix = new Matrix4();
- const _identityMatrix = new Matrix4();
- function Skeleton( bones = [], boneInverses = [] ) {
- this.uuid = MathUtils.generateUUID();
- this.bones = bones.slice( 0 );
- this.boneInverses = boneInverses;
- this.boneMatrices = null;
- this.boneTexture = null;
- this.boneTextureSize = 0;
- this.frame = - 1;
- this.init();
- }
- Object.assign( Skeleton.prototype, {
- init: function () {
- const bones = this.bones;
- const boneInverses = this.boneInverses;
- this.boneMatrices = new Float32Array( bones.length * 16 );
- // calculate inverse bone matrices if necessary
- if ( boneInverses.length === 0 ) {
- this.calculateInverses();
- } else {
- // handle special case
- if ( bones.length !== boneInverses.length ) {
- console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' );
- this.boneInverses = [];
- for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
- this.boneInverses.push( new Matrix4() );
- }
- }
- }
- },
- calculateInverses: function () {
- this.boneInverses.length = 0;
- for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
- const inverse = new Matrix4();
- if ( this.bones[ i ] ) {
- inverse.copy( this.bones[ i ].matrixWorld ).invert();
- }
- this.boneInverses.push( inverse );
- }
- },
- pose: function () {
- // recover the bind-time world matrices
- for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
- const bone = this.bones[ i ];
- if ( bone ) {
- bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
- }
- }
- // compute the local matrices, positions, rotations and scales
- for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
- const bone = this.bones[ i ];
- if ( bone ) {
- if ( bone.parent && bone.parent.isBone ) {
- bone.matrix.copy( bone.parent.matrixWorld ).invert();
- bone.matrix.multiply( bone.matrixWorld );
- } else {
- bone.matrix.copy( bone.matrixWorld );
- }
- bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
- }
- }
- },
- update: function () {
- const bones = this.bones;
- const boneInverses = this.boneInverses;
- const boneMatrices = this.boneMatrices;
- const boneTexture = this.boneTexture;
- // flatten bone matrices to array
- for ( let i = 0, il = bones.length; i < il; i ++ ) {
- // compute the offset between the current and the original transform
- const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
- _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
- _offsetMatrix.toArray( boneMatrices, i * 16 );
- }
- if ( boneTexture !== null ) {
- boneTexture.needsUpdate = true;
- }
- },
- clone: function () {
- return new Skeleton( this.bones, this.boneInverses );
- },
- getBoneByName: function ( name ) {
- for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
- const bone = this.bones[ i ];
- if ( bone.name === name ) {
- return bone;
- }
- }
- return undefined;
- },
- dispose: function ( ) {
- if ( this.boneTexture !== null ) {
- this.boneTexture.dispose();
- this.boneTexture = null;
- }
- },
- fromJSON: function ( json, bones ) {
- this.uuid = json.uuid;
- for ( let i = 0, l = json.bones.length; i < l; i ++ ) {
- const uuid = json.bones[ i ];
- let bone = bones[ uuid ];
- if ( bone === undefined ) {
- console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid );
- bone = new Bone();
- }
- this.bones.push( bone );
- this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) );
- }
- this.init();
- return this;
- },
- toJSON: function () {
- const data = {
- metadata: {
- version: 4.5,
- type: 'Skeleton',
- generator: 'Skeleton.toJSON'
- },
- bones: [],
- boneInverses: []
- };
- data.uuid = this.uuid;
- const bones = this.bones;
- const boneInverses = this.boneInverses;
- for ( let i = 0, l = bones.length; i < l; i ++ ) {
- const bone = bones[ i ];
- data.bones.push( bone.uuid );
- const boneInverse = boneInverses[ i ];
- data.boneInverses.push( boneInverse.toArray() );
- }
- return data;
- }
- } );
- export { Skeleton };
|