| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- module BABYLON {
- export class Bone extends Node {
- public children = new Array<Bone>();
- public animations = new Array<Animation>();
- public length: number;
- private _skeleton: Skeleton;
- public _matrix: Matrix;
- private _restPose: Matrix;
- private _baseMatrix: Matrix;
- private _worldTransform = new Matrix();
- private _absoluteTransform = new Matrix();
- private _invertedAbsoluteTransform = new Matrix();
- private _parent: Bone;
- constructor(public name: string, skeleton: Skeleton, parentBone: Bone, matrix: Matrix, restPose?: Matrix) {
- super(name, skeleton.getScene());
- this._skeleton = skeleton;
- this._matrix = matrix;
- this._baseMatrix = matrix;
- this._restPose = restPose ? restPose : matrix.clone();
- skeleton.bones.push(this);
- if (parentBone) {
- this._parent = parentBone;
- parentBone.children.push(this);
- } else {
- this._parent = null;
- }
- this._updateDifferenceMatrix();
- }
- // Members
- public getParent(): Bone {
- return this._parent;
- }
- public getLocalMatrix(): Matrix {
- return this._matrix;
- }
- public getBaseMatrix(): Matrix {
- return this._baseMatrix;
- }
- public getRestPose(): Matrix {
- return this._restPose;
- }
- public returnToRest(): void {
- this.updateMatrix(this._restPose.clone());
- }
- public getWorldMatrix(): Matrix {
- return this._worldTransform;
- }
- public getInvertedAbsoluteTransform(): Matrix {
- return this._invertedAbsoluteTransform;
- }
- public getAbsoluteTransform(): Matrix {
- return this._absoluteTransform;
- }
- // Methods
- public updateMatrix(matrix: Matrix): void {
- this._baseMatrix = matrix.clone();
- this._matrix = matrix.clone();
- this._skeleton._markAsDirty();
- this._updateDifferenceMatrix();
- }
- public _updateDifferenceMatrix(rootMatrix?: Matrix): void {
- if (!rootMatrix) {
- rootMatrix = this._baseMatrix;
- }
- if (this._parent) {
- rootMatrix.multiplyToRef(this._parent._absoluteTransform, this._absoluteTransform);
- } else {
- this._absoluteTransform.copyFrom(rootMatrix);
- }
- this._absoluteTransform.invertToRef(this._invertedAbsoluteTransform);
- for (var index = 0; index < this.children.length; index++) {
- this.children[index]._updateDifferenceMatrix();
- }
- }
- public markAsDirty(): void {
- this._currentRenderId++;
- this._skeleton._markAsDirty();
- }
- public copyAnimationRange(source: Bone, rangeName: string, frameOffset: number, rescaleAsRequired = false): boolean {
- // all animation may be coming from a library skeleton, so may need to create animation
- if (this.animations.length === 0) {
- this.animations.push(new Animation(this.name, "_matrix", source.animations[0].framePerSecond, Animation.ANIMATIONTYPE_MATRIX, 0));
- this.animations[0].setKeys([]);
- }
- // get animation info / verify there is such a range from the source bone
- var sourceRange = source.animations[0].getRange(rangeName);
- if (!sourceRange) {
- return false;
- }
- var from = sourceRange.from;
- var to = sourceRange.to;
- var sourceKeys = source.animations[0].getKeys();
-
- // rescaling prep
- var sourceBoneLength = source.length;
- var scalingReqd = rescaleAsRequired && sourceBoneLength && this.length && sourceBoneLength !== this.length;
- var ratio = scalingReqd ? this.length / sourceBoneLength : null;
- var destKeys = this.animations[0].getKeys();
-
- // loop vars declaration / initialization
- var orig: { frame: number, value: Matrix };
- var origScale = scalingReqd ? Vector3.Zero() : null;
- var origRotation = scalingReqd ? new Quaternion() : null;
- var origTranslation = scalingReqd ? Vector3.Zero() : null;
- var mat: Matrix;
- for (var key = 0, nKeys = sourceKeys.length; key < nKeys; key++) {
- orig = sourceKeys[key];
- if (orig.frame >= from && orig.frame <= to) {
- if (scalingReqd) {
- orig.value.decompose(origScale, origRotation, origTranslation);
- origTranslation.scaleInPlace(ratio);
- mat = Matrix.Compose(origScale, origRotation, origTranslation);
- } else {
- mat = orig.value;
- }
- destKeys.push({ frame: orig.frame + frameOffset, value: mat });
- }
- }
- this.animations[0].createRange(rangeName, from + frameOffset, to + frameOffset);
- return true;
- }
- }
- }
|