babylon.skeleton.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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._ranges = new Array();
  11. this.bones = [];
  12. this._scene = scene;
  13. scene.skeletons.push(this);
  14. this.prepare();
  15. //make sure it will recalculate the matrix next time prepare is called.
  16. this._isDirty = true;
  17. }
  18. // Members
  19. Skeleton.prototype.getTransformMatrices = function () {
  20. return this._transformMatrices;
  21. };
  22. Skeleton.prototype.getScene = function () {
  23. return this._scene;
  24. };
  25. // Methods
  26. Skeleton.prototype.createAnimationRange = function (name, from, to) {
  27. this._ranges.push(new BABYLON.AnimationRange(name, from, to));
  28. };
  29. Skeleton.prototype.deleteAnimationRange = function (name) {
  30. for (var index = 0; index < this._ranges.length; index++) {
  31. if (this._ranges[index].name === name) {
  32. this._ranges.splice(index, 1);
  33. return;
  34. }
  35. }
  36. };
  37. Skeleton.prototype.getAnimationRange = function (name) {
  38. for (var index = 0; index < this._ranges.length; index++) {
  39. if (this._ranges[index].name === name) {
  40. return this._ranges[index];
  41. }
  42. }
  43. return null;
  44. };
  45. Skeleton.prototype.beginAnimation = function (name, loop, speedRatio, onAnimationEnd) {
  46. var range = this.getAnimationRange(name);
  47. if (!range) {
  48. return null;
  49. }
  50. this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  51. };
  52. Skeleton.prototype._markAsDirty = function () {
  53. this._isDirty = true;
  54. };
  55. Skeleton.prototype.prepare = function () {
  56. if (!this._isDirty) {
  57. return;
  58. }
  59. if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
  60. this._transformMatrices = new Float32Array(16 * (this.bones.length + 1));
  61. }
  62. for (var index = 0; index < this.bones.length; index++) {
  63. var bone = this.bones[index];
  64. var parentBone = bone.getParent();
  65. if (parentBone) {
  66. bone.getLocalMatrix().multiplyToRef(parentBone.getWorldMatrix(), bone.getWorldMatrix());
  67. }
  68. else {
  69. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  70. }
  71. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), this._transformMatrices, index * 16);
  72. }
  73. this._identity.copyToArray(this._transformMatrices, this.bones.length * 16);
  74. this._isDirty = false;
  75. this._scene._activeBones += this.bones.length;
  76. };
  77. Skeleton.prototype.getAnimatables = function () {
  78. if (!this._animatables || this._animatables.length !== this.bones.length) {
  79. this._animatables = [];
  80. for (var index = 0; index < this.bones.length; index++) {
  81. this._animatables.push(this.bones[index]);
  82. }
  83. }
  84. return this._animatables;
  85. };
  86. Skeleton.prototype.clone = function (name, id) {
  87. var result = new Skeleton(name, id || name, this._scene);
  88. for (var index = 0; index < this.bones.length; index++) {
  89. var source = this.bones[index];
  90. var parentBone = null;
  91. if (source.getParent()) {
  92. var parentIndex = this.bones.indexOf(source.getParent());
  93. parentBone = result.bones[parentIndex];
  94. }
  95. var bone = new BABYLON.Bone(source.name, result, parentBone, source.getBaseMatrix());
  96. BABYLON.Tools.DeepCopy(source.animations, bone.animations);
  97. }
  98. return result;
  99. };
  100. Skeleton.prototype.dispose = function () {
  101. // Animations
  102. this.getScene().stopAnimation(this);
  103. // Remove from scene
  104. this.getScene().removeSkeleton(this);
  105. };
  106. return Skeleton;
  107. })();
  108. BABYLON.Skeleton = Skeleton;
  109. })(BABYLON || (BABYLON = {}));