babylon.bone.js 2.0 KB

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