axesViewer.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Vector3, Color3 } from "Maths/math";
  2. import { Nullable } from "types";
  3. import { Scene } from "scene";
  4. import { Mesh } from "Meshes/mesh";
  5. import { LinesMesh } from "Meshes/linesMesh";
  6. /**
  7. * The Axes viewer will show 3 axes in a specific point in space
  8. */
  9. export class AxesViewer {
  10. private _xline = [Vector3.Zero(), Vector3.Zero()];
  11. private _yline = [Vector3.Zero(), Vector3.Zero()];
  12. private _zline = [Vector3.Zero(), Vector3.Zero()];
  13. private _xmesh: Nullable<LinesMesh>;
  14. private _ymesh: Nullable<LinesMesh>;
  15. private _zmesh: Nullable<LinesMesh>;
  16. /**
  17. * Gets the hosting scene
  18. */
  19. public scene: Nullable<Scene>;
  20. /**
  21. * Gets or sets a number used to scale line length
  22. */
  23. public scaleLines = 1;
  24. /**
  25. * Creates a new AxesViewer
  26. * @param scene defines the hosting scene
  27. * @param scaleLines defines a number used to scale line length (1 by default)
  28. */
  29. constructor(scene: Scene, scaleLines = 1) {
  30. this.scaleLines = scaleLines;
  31. this._xmesh = Mesh.CreateLines("xline", this._xline, scene, true);
  32. this._ymesh = Mesh.CreateLines("yline", this._yline, scene, true);
  33. this._zmesh = Mesh.CreateLines("zline", this._zline, scene, true);
  34. this._xmesh.renderingGroupId = 2;
  35. this._ymesh.renderingGroupId = 2;
  36. this._zmesh.renderingGroupId = 2;
  37. this._xmesh.material.checkReadyOnlyOnce = true;
  38. this._xmesh.color = new Color3(1, 0, 0);
  39. this._ymesh.material.checkReadyOnlyOnce = true;
  40. this._ymesh.color = new Color3(0, 1, 0);
  41. this._zmesh.material.checkReadyOnlyOnce = true;
  42. this._zmesh.color = new Color3(0, 0, 1);
  43. this.scene = scene;
  44. }
  45. /**
  46. * Force the viewer to update
  47. * @param position defines the position of the viewer
  48. * @param xaxis defines the x axis of the viewer
  49. * @param yaxis defines the y axis of the viewer
  50. * @param zaxis defines the z axis of the viewer
  51. */
  52. public update(position: Vector3, xaxis: Vector3, yaxis: Vector3, zaxis: Vector3): void {
  53. var scaleLines = this.scaleLines;
  54. if (this._xmesh) {
  55. this._xmesh.position.copyFrom(position);
  56. }
  57. if (this._ymesh) {
  58. this._ymesh.position.copyFrom(position);
  59. }
  60. if (this._zmesh) {
  61. this._zmesh.position.copyFrom(position);
  62. }
  63. var point2 = this._xline[1];
  64. point2.x = xaxis.x * scaleLines;
  65. point2.y = xaxis.y * scaleLines;
  66. point2.z = xaxis.z * scaleLines;
  67. Mesh.CreateLines("", this._xline, null, false, this._xmesh);
  68. point2 = this._yline[1];
  69. point2.x = yaxis.x * scaleLines;
  70. point2.y = yaxis.y * scaleLines;
  71. point2.z = yaxis.z * scaleLines;
  72. Mesh.CreateLines("", this._yline, null, false, this._ymesh);
  73. point2 = this._zline[1];
  74. point2.x = zaxis.x * scaleLines;
  75. point2.y = zaxis.y * scaleLines;
  76. point2.z = zaxis.z * scaleLines;
  77. Mesh.CreateLines("", this._zline, null, false, this._zmesh);
  78. }
  79. /** Releases resources */
  80. public dispose() {
  81. if (this._xmesh) {
  82. this._xmesh.dispose();
  83. }
  84. if (this._ymesh) {
  85. this._ymesh.dispose();
  86. }
  87. if (this._zmesh) {
  88. this._zmesh.dispose();
  89. }
  90. this._xmesh = null;
  91. this._ymesh = null;
  92. this._zmesh = null;
  93. this.scene = null;
  94. }
  95. }