babylon.skeleton.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Methods
  22. Skeleton.prototype._markAsDirty = function () {
  23. this._isDirty = true;
  24. };
  25. Skeleton.prototype.prepare = function () {
  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. }
  38. else {
  39. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  40. }
  41. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), this._transformMatrices, index * 16);
  42. }
  43. this._identity.copyToArray(this._transformMatrices, this.bones.length * 16);
  44. this._isDirty = false;
  45. this._scene._activeBones += this.bones.length;
  46. };
  47. Skeleton.prototype.getAnimatables = function () {
  48. if (!this._animatables || this._animatables.length !== this.bones.length) {
  49. this._animatables = [];
  50. for (var index = 0; index < this.bones.length; index++) {
  51. this._animatables.push(this.bones[index]);
  52. }
  53. }
  54. return this._animatables;
  55. };
  56. Skeleton.prototype.clone = function (name, id) {
  57. var result = new Skeleton(name, id || name, this._scene);
  58. for (var index = 0; index < this.bones.length; index++) {
  59. var source = this.bones[index];
  60. var parentBone = null;
  61. if (source.getParent()) {
  62. var parentIndex = this.bones.indexOf(source.getParent());
  63. parentBone = result.bones[parentIndex];
  64. }
  65. var bone = new BABYLON.Bone(source.name, result, parentBone, source.getBaseMatrix());
  66. BABYLON.Tools.DeepCopy(source.animations, bone.animations);
  67. }
  68. return result;
  69. };
  70. return Skeleton;
  71. })();
  72. BABYLON.Skeleton = Skeleton;
  73. })(BABYLON || (BABYLON = {}));
  74. //# sourceMappingURL=babylon.skeleton.js.map