babylon.skeleton.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }
  14. // Members
  15. public getTransformMatrices() {
  16. return this._transformMatrices;
  17. }
  18. // Methods
  19. public _markAsDirty(): void {
  20. this._isDirty = true;
  21. }
  22. public prepare(): void {
  23. if (!this._isDirty) {
  24. return;
  25. }
  26. if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
  27. this._transformMatrices = new Float32Array(16 * (this.bones.length + 1));
  28. }
  29. for (var index = 0; index < this.bones.length; index++) {
  30. var bone = this.bones[index];
  31. var parentBone = bone.getParent();
  32. if (parentBone) {
  33. bone.getLocalMatrix().multiplyToRef(parentBone.getWorldMatrix(), bone.getWorldMatrix());
  34. } else {
  35. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  36. }
  37. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), this._transformMatrices, index * 16);
  38. }
  39. this._identity.copyToArray(this._transformMatrices, this.bones.length * 16);
  40. this._isDirty = false;
  41. }
  42. public getAnimatables(): IAnimatable[] {
  43. if (!this._animatables || this._animatables.length != this.bones.length) {
  44. this._animatables = [];
  45. for (var index = 0; index < this.bones.length; index++) {
  46. this._animatables.push(this.bones[index]);
  47. }
  48. }
  49. return this._animatables;
  50. }
  51. public clone(name: string, id: string): Skeleton {
  52. var result = new BABYLON.Skeleton(name, id || name, this._scene);
  53. for (var index = 0; index < this.bones.length; index++) {
  54. var source = this.bones[index];
  55. var parentBone = null;
  56. if (source.getParent()) {
  57. var parentIndex = this.bones.indexOf(source.getParent());
  58. parentBone = result.bones[parentIndex];
  59. }
  60. var bone = new BABYLON.Bone(source.name, result, parentBone, source.getBaseMatrix());
  61. BABYLON.Tools.DeepCopy(source.animations, bone.animations);
  62. }
  63. return result;
  64. }
  65. }
  66. }