boneAxesViewer.ts 2.2 KB

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