babylon.skeletonViewer.ts 5.0 KB

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