babylon.skeletonViewer.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = [];
  9. private _debugMesh: 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. for (var i = 0; i < len; i++) {
  54. var bone = bones[i];
  55. var points = this._debugLines[i];
  56. if (!points) {
  57. points = [Vector3.Zero(), Vector3.Zero()];
  58. this._debugLines[i] = points;
  59. }
  60. this._getBonePosition(points[0], bone, meshMat);
  61. this._getBonePosition(points[1], bone, meshMat, 0, bone.length, 0);
  62. }
  63. }
  64. private _getLinesForBonesNoLength(bones: Bone[], meshMat: Matrix): void {
  65. var len = bones.length;
  66. var boneNum = 0;
  67. for (var i = len - 1; i >= 0; i--) {
  68. var childBone = bones[i];
  69. var parentBone = childBone.getParent();
  70. if (!parentBone) {
  71. continue;
  72. }
  73. var points = this._debugLines[boneNum];
  74. if (!points) {
  75. points = [Vector3.Zero(), Vector3.Zero()];
  76. this._debugLines[boneNum] = points;
  77. }
  78. this._getBonePosition(points[0], childBone, meshMat);
  79. this._getBonePosition(points[1], parentBone, meshMat);
  80. boneNum++;
  81. }
  82. }
  83. public update() {
  84. if (this.autoUpdateBonesMatrices) {
  85. this._updateBoneMatrix(this.skeleton.bones[0]);
  86. }
  87. if (this.skeleton.bones[0].length === undefined) {
  88. this._getLinesForBonesNoLength(this.skeleton.bones, this.mesh.getWorldMatrix());
  89. } else {
  90. this._getLinesForBonesWithLength(this.skeleton.bones, this.mesh.getWorldMatrix());
  91. }
  92. if (!this._debugMesh) {
  93. this._debugMesh = BABYLON.MeshBuilder.CreateLineSystem(null, { lines: this._debugLines, updatable: true }, this._scene);
  94. this._debugMesh.renderingGroupId = this.renderingGroupId;
  95. } else {
  96. BABYLON.MeshBuilder.CreateLineSystem(null, { lines: this._debugLines, updatable: true, instance: this._debugMesh }, this._scene);
  97. }
  98. this._debugMesh.color = this.color;
  99. }
  100. private _updateBoneMatrix(bone: Bone) {
  101. if (bone.getParent()) {
  102. bone.getLocalMatrix().multiplyToRef(bone.getParent().getAbsoluteTransform(), bone.getAbsoluteTransform());
  103. }
  104. var children = bone.children;
  105. var len = children.length;
  106. for (var i = 0; i < len; i++) {
  107. this._updateBoneMatrix(children[i]);
  108. }
  109. }
  110. public dispose() {
  111. if (this._debugMesh) {
  112. this.isEnabled = false;
  113. this._debugMesh.dispose();
  114. this._debugMesh = null;
  115. }
  116. }
  117. }
  118. }