babylon.skeleton.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Skeleton = function (name, id, scene) {
  5. this.id = id;
  6. this.name = name;
  7. this.bones = [];
  8. this._scene = scene;
  9. scene.skeletons.push(this);
  10. this._isDirty = true;
  11. };
  12. // Members
  13. BABYLON.Skeleton.prototype.getTransformMatrices = function () {
  14. return this._transformMatrices;
  15. };
  16. // Methods
  17. BABYLON.Skeleton.prototype._markAsDirty = function() {
  18. this._isDirty = true;
  19. };
  20. BABYLON.Skeleton.prototype.prepare = function() {
  21. if (!this._isDirty) {
  22. return;
  23. }
  24. if (!this._transformMatrices || this._transformMatrices.length !== 16 * this.bones.length) {
  25. this._transformMatrices = new BABYLON.MatrixType(16 * this.bones.length);
  26. }
  27. for (var index = 0; index < this.bones.length; index++) {
  28. var bone = this.bones[index];
  29. var parentBone = bone.getParent();
  30. if (parentBone) {
  31. bone._matrix.multiplyToRef(parentBone._worldTransform, bone._worldTransform);
  32. } else {
  33. bone._worldTransform.copyFrom(bone._matrix);
  34. }
  35. bone._invertedAbsoluteTransform.multiplyToArray(bone._worldTransform, this._transformMatrices, index * 16);
  36. }
  37. this._isDirty = false;
  38. };
  39. BABYLON.Skeleton.prototype.getAnimatables = function () {
  40. if (!this._animatables || this._animatables.length != this.bones.length) {
  41. this._animatables = [];
  42. for (var index = 0; index < this.bones.length; index++) {
  43. this._animatables.push(this.bones[index]);
  44. }
  45. }
  46. return this._animatables;
  47. };
  48. BABYLON.Skeleton.prototype.clone = function(name, id) {
  49. var result = new BABYLON.Skeleton(name, id || name, this._scene);
  50. for (var index = 0; index < this.bones.length; index++) {
  51. var source = this.bones[index];
  52. var parentBone = null;
  53. if (source.getParent()) {
  54. var parentIndex = this.bones.indexOf(source.getParent());
  55. parentBone = result.bones[parentIndex];
  56. }
  57. var bone = new BABYLON.Bone(source.name, result, parentBone, source._baseMatrix);
  58. BABYLON.Tools.DeepCopy(source.animations, bone.animations);
  59. }
  60. return result;
  61. };
  62. })();