boneAxesViewer.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Nullable } from "../types";
  2. import { AxesViewer } from "../Debug/axesViewer";
  3. import { Vector3, Axis } from "../Maths/math";
  4. import { Mesh } from "../Meshes/mesh";
  5. import { Bone } from "../Bones/bone";
  6. import { Scene } from "../scene";
  7. /**
  8. * The BoneAxesViewer will attach 3 axes to a specific bone of a specific mesh
  9. * @see demo here: https://www.babylonjs-playground.com/#0DE8F4#8
  10. */
  11. export class BoneAxesViewer extends AxesViewer {
  12. /**
  13. * Gets or sets the target mesh where to display the axes viewer
  14. */
  15. public mesh: Nullable<Mesh>;
  16. /**
  17. * Gets or sets the target bone where to display the axes viewer
  18. */
  19. public bone: Nullable<Bone>;
  20. /** Gets current position */
  21. public pos = Vector3.Zero();
  22. /** Gets direction of X axis */
  23. public xaxis = Vector3.Zero();
  24. /** Gets direction of Y axis */
  25. public yaxis = Vector3.Zero();
  26. /** Gets direction of Z axis */
  27. public zaxis = Vector3.Zero();
  28. /**
  29. * Creates a new BoneAxesViewer
  30. * @param scene defines the hosting scene
  31. * @param bone defines the target bone
  32. * @param mesh defines the target mesh
  33. * @param scaleLines defines a scaling factor for line length (1 by default)
  34. */
  35. constructor(scene: Scene, bone: Bone, mesh: Mesh, scaleLines = 1) {
  36. super(scene, scaleLines);
  37. this.mesh = mesh;
  38. this.bone = bone;
  39. }
  40. /**
  41. * Force the viewer to update
  42. */
  43. public update(): void {
  44. if (!this.mesh || !this.bone) {
  45. return;
  46. }
  47. var bone = this.bone;
  48. bone.getAbsolutePositionToRef(this.mesh, this.pos);
  49. bone.getDirectionToRef(Axis.X, this.mesh, this.xaxis);
  50. bone.getDirectionToRef(Axis.Y, this.mesh, this.yaxis);
  51. bone.getDirectionToRef(Axis.Z, this.mesh, this.zaxis);
  52. super.update(this.pos, this.xaxis, this.yaxis, this.zaxis);
  53. }
  54. /** Releases resources */
  55. public dispose() {
  56. if (this.mesh) {
  57. this.mesh = null;
  58. this.bone = null;
  59. super.dispose();
  60. }
  61. }
  62. }