babylon.bone.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. module BABYLON {
  2. export class Bone {
  3. public children = new Array<Bone>();
  4. public animations = new Array<Animation>();
  5. private _skeleton: Skeleton;
  6. private _matrix: Matrix;
  7. private _baseMatrix: Matrix;
  8. private _worldTransform = new Matrix();
  9. private _absoluteTransform = new Matrix();
  10. private _invertedAbsoluteTransform = new Matrix();
  11. private _parent: Bone;
  12. constructor(public name: string, skeleton: Skeleton, parentBone: Bone, matrix: Matrix) {
  13. this._skeleton = skeleton;
  14. this._matrix = matrix;
  15. this._baseMatrix = matrix;
  16. skeleton.bones.push(this);
  17. if (parentBone) {
  18. this._parent = parentBone;
  19. parentBone.children.push(this);
  20. } else {
  21. this._parent = null;
  22. }
  23. this._updateDifferenceMatrix();
  24. }
  25. // Members
  26. public getParent():Bone {
  27. return this._parent;
  28. }
  29. public getLocalMatrix():Matrix {
  30. return this._matrix;
  31. }
  32. public getBaseMatrix(): Matrix {
  33. return this._baseMatrix;
  34. }
  35. public getWorldMatrix(): Matrix {
  36. return this._worldTransform;
  37. }
  38. public getInvertedAbsoluteTransform(): Matrix {
  39. return this._invertedAbsoluteTransform;
  40. }
  41. public getAbsoluteMatrix(): Matrix {
  42. var matrix = this._matrix.clone();
  43. var parent = this._parent;
  44. while (parent) {
  45. matrix = matrix.multiply(parent.getLocalMatrix());
  46. parent = parent.getParent();
  47. }
  48. return matrix;
  49. }
  50. // Methods
  51. public updateMatrix(matrix: Matrix): void {
  52. this._matrix = matrix;
  53. this._skeleton._markAsDirty();
  54. this._updateDifferenceMatrix();
  55. }
  56. private _updateDifferenceMatrix(): void {
  57. if (this._parent) {
  58. this._matrix.multiplyToRef(this._parent._absoluteTransform, this._absoluteTransform);
  59. } else {
  60. this._absoluteTransform.copyFrom(this._matrix);
  61. }
  62. this._absoluteTransform.invertToRef(this._invertedAbsoluteTransform);
  63. for (var index = 0; index < this.children.length; index++) {
  64. this.children[index]._updateDifferenceMatrix();
  65. }
  66. }
  67. public markAsDirty(): void {
  68. this._skeleton._markAsDirty();
  69. }
  70. }
  71. }