babylon.skeleton.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module BABYLON {
  2. export class Skeleton {
  3. public bones = new Array<Bone>();
  4. private _scene: Scene;
  5. private _isDirty = true;
  6. private _transformMatrices: Float32Array;
  7. private _animatables: IAnimatable[];
  8. private _identity = Matrix.Identity();
  9. constructor(public name: string, public id: string, scene: Scene) {
  10. this.bones = [];
  11. this._scene = scene;
  12. scene.skeletons.push(this);
  13. this.prepare();
  14. //make sure it will recalculate the matrix next time prepare is called.
  15. this._isDirty = true;
  16. }
  17. // Members
  18. public getTransformMatrices() {
  19. return this._transformMatrices;
  20. }
  21. // Methods
  22. public _markAsDirty(): void {
  23. this._isDirty = true;
  24. }
  25. public prepare(): void {
  26. if (!this._isDirty) {
  27. return;
  28. }
  29. if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
  30. this._transformMatrices = new Float32Array(16 * (this.bones.length + 1));
  31. }
  32. for (var index = 0; index < this.bones.length; index++) {
  33. var bone = this.bones[index];
  34. var parentBone = bone.getParent();
  35. if (parentBone) {
  36. bone.getLocalMatrix().multiplyToRef(parentBone.getWorldMatrix(), bone.getWorldMatrix());
  37. } else {
  38. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  39. }
  40. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), this._transformMatrices, index * 16);
  41. }
  42. this._identity.copyToArray(this._transformMatrices, this.bones.length * 16);
  43. this._isDirty = false;
  44. this._scene._activeBones += this.bones.length;
  45. }
  46. public getAnimatables(): IAnimatable[] {
  47. if (!this._animatables || this._animatables.length !== this.bones.length) {
  48. this._animatables = [];
  49. for (var index = 0; index < this.bones.length; index++) {
  50. this._animatables.push(this.bones[index]);
  51. }
  52. }
  53. return this._animatables;
  54. }
  55. public clone(name: string, id: string): Skeleton {
  56. var result = new Skeleton(name, id || name, this._scene);
  57. for (var index = 0; index < this.bones.length; index++) {
  58. var source = this.bones[index];
  59. var parentBone = null;
  60. if (source.getParent()) {
  61. var parentIndex = this.bones.indexOf(source.getParent());
  62. parentBone = result.bones[parentIndex];
  63. }
  64. var bone = new Bone(source.name, result, parentBone, source.getBaseMatrix());
  65. Tools.DeepCopy(source.animations, bone.animations);
  66. }
  67. return result;
  68. }
  69. }
  70. }