babylon.skeleton.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Skeleton = (function () {
  4. function Skeleton(name, id, scene) {
  5. this.name = name;
  6. this.id = id;
  7. this.bones = new Array();
  8. this._isDirty = true;
  9. this._identity = BABYLON.Matrix.Identity();
  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. Skeleton.prototype.getTransformMatrices = function () {
  19. return this._transformMatrices;
  20. };
  21. Skeleton.prototype.getScene = function () {
  22. return this._scene;
  23. };
  24. // Methods
  25. Skeleton.prototype._markAsDirty = function () {
  26. this._isDirty = true;
  27. };
  28. Skeleton.prototype.prepare = function () {
  29. if (!this._isDirty) {
  30. return;
  31. }
  32. if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
  33. this._transformMatrices = new Float32Array(16 * (this.bones.length + 1));
  34. }
  35. for (var index = 0; index < this.bones.length; index++) {
  36. var bone = this.bones[index];
  37. var parentBone = bone.getParent();
  38. if (parentBone) {
  39. bone.getLocalMatrix().multiplyToRef(parentBone.getWorldMatrix(), bone.getWorldMatrix());
  40. }
  41. else {
  42. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  43. }
  44. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), this._transformMatrices, index * 16);
  45. }
  46. this._identity.copyToArray(this._transformMatrices, this.bones.length * 16);
  47. this._isDirty = false;
  48. this._scene._activeBones += this.bones.length;
  49. };
  50. Skeleton.prototype.getAnimatables = function () {
  51. if (!this._animatables || this._animatables.length !== this.bones.length) {
  52. this._animatables = [];
  53. for (var index = 0; index < this.bones.length; index++) {
  54. this._animatables.push(this.bones[index]);
  55. }
  56. }
  57. return this._animatables;
  58. };
  59. Skeleton.prototype.clone = function (name, id) {
  60. var result = new Skeleton(name, id || name, this._scene);
  61. for (var index = 0; index < this.bones.length; index++) {
  62. var source = this.bones[index];
  63. var parentBone = null;
  64. if (source.getParent()) {
  65. var parentIndex = this.bones.indexOf(source.getParent());
  66. parentBone = result.bones[parentIndex];
  67. }
  68. var bone = new BABYLON.Bone(source.name, result, parentBone, source.getBaseMatrix());
  69. BABYLON.Tools.DeepCopy(source.animations, bone.animations);
  70. }
  71. return result;
  72. };
  73. return Skeleton;
  74. })();
  75. BABYLON.Skeleton = Skeleton;
  76. })(BABYLON || (BABYLON = {}));
  77. //# sourceMappingURL=babylon.skeleton.js.map