babylon.skeletonViewer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Debug;
  4. (function (Debug) {
  5. /**
  6. * Demo available here: http://www.babylonjs-playground.com/#1BZJVJ#8
  7. */
  8. var SkeletonViewer = (function () {
  9. function SkeletonViewer(skeleton, mesh, scene, autoUpdateBonesMatrices, renderingGroupId) {
  10. if (autoUpdateBonesMatrices === void 0) { autoUpdateBonesMatrices = true; }
  11. if (renderingGroupId === void 0) { renderingGroupId = 1; }
  12. this.skeleton = skeleton;
  13. this.mesh = mesh;
  14. this.autoUpdateBonesMatrices = autoUpdateBonesMatrices;
  15. this.renderingGroupId = renderingGroupId;
  16. this.color = BABYLON.Color3.White();
  17. this._debugLines = [];
  18. this._isEnabled = false;
  19. this._scene = scene;
  20. this.update();
  21. this._renderFunction = this.update.bind(this);
  22. }
  23. Object.defineProperty(SkeletonViewer.prototype, "isEnabled", {
  24. get: function () {
  25. return this._isEnabled;
  26. },
  27. set: function (value) {
  28. if (this._isEnabled === value) {
  29. return;
  30. }
  31. this._isEnabled = value;
  32. if (value) {
  33. this._scene.registerBeforeRender(this._renderFunction);
  34. }
  35. else {
  36. this._scene.unregisterBeforeRender(this._renderFunction);
  37. }
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. SkeletonViewer.prototype._getBonePosition = function (position, bone, meshMat, x, y, z) {
  43. if (x === void 0) { x = 0; }
  44. if (y === void 0) { y = 0; }
  45. if (z === void 0) { z = 0; }
  46. var tmat = BABYLON.Tmp.Matrix[0];
  47. var parentBone = bone.getParent();
  48. tmat.copyFrom(bone.getLocalMatrix());
  49. if (x !== 0 || y !== 0 || z !== 0) {
  50. var tmat2 = BABYLON.Tmp.Matrix[1];
  51. BABYLON.Matrix.IdentityToRef(tmat2);
  52. tmat2.m[12] = x;
  53. tmat2.m[13] = y;
  54. tmat2.m[14] = z;
  55. tmat2.multiplyToRef(tmat, tmat);
  56. }
  57. if (parentBone) {
  58. tmat.multiplyToRef(parentBone.getAbsoluteTransform(), tmat);
  59. }
  60. tmat.multiplyToRef(meshMat, tmat);
  61. position.x = tmat.m[12];
  62. position.y = tmat.m[13];
  63. position.z = tmat.m[14];
  64. };
  65. SkeletonViewer.prototype._getLinesForBonesWithLength = function (bones, meshMat) {
  66. var len = bones.length;
  67. for (var i = 0; i < len; i++) {
  68. var bone = bones[i];
  69. var points = this._debugLines[i];
  70. if (!points) {
  71. points = [BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero()];
  72. this._debugLines[i] = points;
  73. }
  74. this._getBonePosition(points[0], bone, meshMat);
  75. this._getBonePosition(points[1], bone, meshMat, 0, bone.length, 0);
  76. }
  77. };
  78. SkeletonViewer.prototype._getLinesForBonesNoLength = function (bones, meshMat) {
  79. var len = bones.length;
  80. var boneNum = 0;
  81. for (var i = len - 1; i >= 0; i--) {
  82. var childBone = bones[i];
  83. var parentBone = childBone.getParent();
  84. if (!parentBone) {
  85. continue;
  86. }
  87. var points = this._debugLines[boneNum];
  88. if (!points) {
  89. points = [BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero()];
  90. this._debugLines[boneNum] = points;
  91. }
  92. childBone.getAbsolutePositionToRef(this.mesh, points[0]);
  93. parentBone.getAbsolutePositionToRef(this.mesh, points[1]);
  94. boneNum++;
  95. }
  96. };
  97. SkeletonViewer.prototype.update = function () {
  98. if (this.autoUpdateBonesMatrices) {
  99. this.skeleton.computeAbsoluteTransforms();
  100. }
  101. if (this.skeleton.bones[0].length === undefined) {
  102. this._getLinesForBonesNoLength(this.skeleton.bones, this.mesh.getWorldMatrix());
  103. }
  104. else {
  105. this._getLinesForBonesWithLength(this.skeleton.bones, this.mesh.getWorldMatrix());
  106. }
  107. if (!this._debugMesh) {
  108. this._debugMesh = BABYLON.MeshBuilder.CreateLineSystem(null, { lines: this._debugLines, updatable: true }, this._scene);
  109. this._debugMesh.renderingGroupId = this.renderingGroupId;
  110. }
  111. else {
  112. BABYLON.MeshBuilder.CreateLineSystem(null, { lines: this._debugLines, updatable: true, instance: this._debugMesh }, this._scene);
  113. }
  114. this._debugMesh.color = this.color;
  115. };
  116. SkeletonViewer.prototype.dispose = function () {
  117. if (this._debugMesh) {
  118. this.isEnabled = false;
  119. this._debugMesh.dispose();
  120. this._debugMesh = null;
  121. }
  122. };
  123. return SkeletonViewer;
  124. }());
  125. Debug.SkeletonViewer = SkeletonViewer;
  126. })(Debug = BABYLON.Debug || (BABYLON.Debug = {}));
  127. })(BABYLON || (BABYLON = {}));