babylon.bone.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Bone = function (name, skeleton, parentBone, matrix) {
  4. this.name = name;
  5. this._skeleton = skeleton;
  6. this._matrix = matrix;
  7. this._baseMatrix = matrix;
  8. this._worldTransform = new BABYLON.Matrix();
  9. this._absoluteTransform = new BABYLON.Matrix();
  10. this._invertedAbsoluteTransform = new BABYLON.Matrix();
  11. this.children = [];
  12. this.animations = [];
  13. skeleton.bones.push(this);
  14. if (parentBone) {
  15. this._parent = parentBone;
  16. parentBone.children.push(this);
  17. } else {
  18. this._parent = null;
  19. }
  20. this._updateDifferenceMatrix();
  21. };
  22. // Members
  23. BABYLON.Bone.prototype.getParent = function() {
  24. return this._parent;
  25. };
  26. BABYLON.Bone.prototype.getLocalMatrix = function () {
  27. return this._matrix;
  28. };
  29. BABYLON.Bone.prototype.getAbsoluteMatrix = function () {
  30. var matrix = this._matrix.clone();
  31. var parent = this._parent;
  32. while (parent) {
  33. matrix = matrix.multiply(parent.getLocalMatrix());
  34. parent = parent.getParent();
  35. }
  36. return matrix;
  37. };
  38. // Methods
  39. BABYLON.Bone.prototype.updateMatrix = function(matrix) {
  40. this._matrix = matrix;
  41. this._skeleton._markAsDirty();
  42. this._updateDifferenceMatrix();
  43. };
  44. BABYLON.Bone.prototype._updateDifferenceMatrix = function() {
  45. if (this._parent) {
  46. this._matrix.multiplyToRef(this._parent._absoluteTransform, this._absoluteTransform);
  47. } else {
  48. this._absoluteTransform.copyFrom(this._matrix);
  49. }
  50. this._absoluteTransform.invertToRef(this._invertedAbsoluteTransform);
  51. for (var index = 0; index < this.children.length; index++) {
  52. this.children[index]._updateDifferenceMatrix();
  53. }
  54. };
  55. BABYLON.Bone.prototype.markAsDirty = function() {
  56. this._skeleton._markAsDirty();
  57. };
  58. })();