babylon.skeleton.ts 2.7 KB

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