babylon.bone.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var Bone = (function (_super) {
  9. __extends(Bone, _super);
  10. function Bone(name, skeleton, parentBone, matrix, restPose) {
  11. _super.call(this, name, skeleton.getScene());
  12. this.name = name;
  13. this.children = new Array();
  14. this.animations = new Array();
  15. this._worldTransform = new BABYLON.Matrix();
  16. this._absoluteTransform = new BABYLON.Matrix();
  17. this._invertedAbsoluteTransform = new BABYLON.Matrix();
  18. this._skeleton = skeleton;
  19. this._matrix = matrix;
  20. this._baseMatrix = matrix;
  21. this._restPose = restPose ? restPose : matrix.clone();
  22. skeleton.bones.push(this);
  23. if (parentBone) {
  24. this._parent = parentBone;
  25. parentBone.children.push(this);
  26. }
  27. else {
  28. this._parent = null;
  29. }
  30. this._updateDifferenceMatrix();
  31. }
  32. // Members
  33. Bone.prototype.getParent = function () {
  34. return this._parent;
  35. };
  36. Bone.prototype.getLocalMatrix = function () {
  37. return this._matrix;
  38. };
  39. Bone.prototype.getBaseMatrix = function () {
  40. return this._baseMatrix;
  41. };
  42. Bone.prototype.getRestPose = function () {
  43. return this._restPose;
  44. };
  45. Bone.prototype.returnToRest = function () {
  46. this.updateMatrix(this._restPose.clone());
  47. };
  48. Bone.prototype.getWorldMatrix = function () {
  49. return this._worldTransform;
  50. };
  51. Bone.prototype.getInvertedAbsoluteTransform = function () {
  52. return this._invertedAbsoluteTransform;
  53. };
  54. Bone.prototype.getAbsoluteTransform = function () {
  55. return this._absoluteTransform;
  56. };
  57. // Methods
  58. Bone.prototype.updateMatrix = function (matrix) {
  59. this._baseMatrix = matrix.clone();
  60. this._matrix = matrix.clone();
  61. this._skeleton._markAsDirty();
  62. this._updateDifferenceMatrix();
  63. };
  64. Bone.prototype._updateDifferenceMatrix = function (rootMatrix) {
  65. if (!rootMatrix) {
  66. rootMatrix = this._baseMatrix;
  67. }
  68. if (this._parent) {
  69. rootMatrix.multiplyToRef(this._parent._absoluteTransform, this._absoluteTransform);
  70. }
  71. else {
  72. this._absoluteTransform.copyFrom(rootMatrix);
  73. }
  74. this._absoluteTransform.invertToRef(this._invertedAbsoluteTransform);
  75. for (var index = 0; index < this.children.length; index++) {
  76. this.children[index]._updateDifferenceMatrix();
  77. }
  78. };
  79. Bone.prototype.markAsDirty = function () {
  80. this._currentRenderId++;
  81. this._skeleton._markAsDirty();
  82. };
  83. Bone.prototype.copyAnimationRange = function (source, rangeName, frameOffset, rescaleAsRequired) {
  84. if (rescaleAsRequired === void 0) { rescaleAsRequired = false; }
  85. // all animation may be coming from a library skeleton, so may need to create animation
  86. if (this.animations.length === 0) {
  87. this.animations.push(new BABYLON.Animation(this.name, "_matrix", source.animations[0].framePerSecond, BABYLON.Animation.ANIMATIONTYPE_MATRIX, 0));
  88. this.animations[0].setKeys([]);
  89. }
  90. // get animation info / verify there is such a range from the source bone
  91. var sourceRange = source.animations[0].getRange(rangeName);
  92. if (!sourceRange) {
  93. return false;
  94. }
  95. var from = sourceRange.from;
  96. var to = sourceRange.to;
  97. var sourceKeys = source.animations[0].getKeys();
  98. // rescaling prep
  99. var sourceBoneLength = source.length;
  100. var scalingReqd = rescaleAsRequired && sourceBoneLength && this.length && sourceBoneLength !== this.length;
  101. var ratio = scalingReqd ? this.length / sourceBoneLength : null;
  102. var destKeys = this.animations[0].getKeys();
  103. // loop vars declaration / initialization
  104. var orig;
  105. var origScale = scalingReqd ? BABYLON.Vector3.Zero() : null;
  106. var origRotation = scalingReqd ? new BABYLON.Quaternion() : null;
  107. var origTranslation = scalingReqd ? BABYLON.Vector3.Zero() : null;
  108. var mat;
  109. for (var key = 0, nKeys = sourceKeys.length; key < nKeys; key++) {
  110. orig = sourceKeys[key];
  111. if (orig.frame >= from && orig.frame <= to) {
  112. if (scalingReqd) {
  113. orig.value.decompose(origScale, origRotation, origTranslation);
  114. origTranslation.scaleInPlace(ratio);
  115. mat = BABYLON.Matrix.Compose(origScale, origRotation, origTranslation);
  116. }
  117. else {
  118. mat = orig.value;
  119. }
  120. destKeys.push({ frame: orig.frame + frameOffset, value: mat });
  121. }
  122. }
  123. this.animations[0].createRange(rangeName, from + frameOffset, to + frameOffset);
  124. return true;
  125. };
  126. return Bone;
  127. }(BABYLON.Node));
  128. BABYLON.Bone = Bone;
  129. })(BABYLON || (BABYLON = {}));