axesViewer.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Vector3, Color3 } from "Maths/math";
  2. import { Nullable } from "types";
  3. import { Scene } from "scene";
  4. import { TransformNode } from "Meshes/transformNode";
  5. import { StandardMaterial } from "Materials/standardMaterial";
  6. import { AxisDragGizmo } from "Gizmos/axisDragGizmo";
  7. /**
  8. * The Axes viewer will show 3 axes in a specific point in space
  9. */
  10. export class AxesViewer {
  11. private _xAxis: TransformNode;
  12. private _yAxis: TransformNode;
  13. private _zAxis: TransformNode;
  14. private _scaleLinesFactor = 4;
  15. private _instanced = false;
  16. /**
  17. * Gets the hosting scene
  18. */
  19. public scene: Scene;
  20. /**
  21. * Gets or sets a number used to scale line length
  22. */
  23. public scaleLines = 1;
  24. /** Gets the node hierarchy used to render x-axis */
  25. public get xAxis(): TransformNode {
  26. return this._xAxis;
  27. }
  28. /** Gets the node hierarchy used to render y-axis */
  29. public get yAxis(): TransformNode {
  30. return this._yAxis;
  31. }
  32. /** Gets the node hierarchy used to render z-axis */
  33. public get zAxis(): TransformNode {
  34. return this._zAxis;
  35. }
  36. /**
  37. * Creates a new AxesViewer
  38. * @param scene defines the hosting scene
  39. * @param scaleLines defines a number used to scale line length (1 by default)
  40. * @param renderingGroupId defines a number used to set the renderingGroupId of the meshes (2 by default)
  41. * @param xAxis defines the node hierarchy used to render the x-axis
  42. * @param yAxis defines the node hierarchy used to render the y-axis
  43. * @param zAxis defines the node hierarchy used to render the z-axis
  44. */
  45. constructor(scene: Scene, scaleLines = 1, renderingGroupId: Nullable<number> = 2, xAxis?: TransformNode, yAxis?: TransformNode, zAxis?: TransformNode) {
  46. this.scaleLines = scaleLines;
  47. if (!xAxis) {
  48. var redColoredMaterial = new StandardMaterial("", scene);
  49. redColoredMaterial.disableLighting = true;
  50. redColoredMaterial.emissiveColor = Color3.Red().scale(0.5);
  51. xAxis = AxisDragGizmo._CreateArrow(scene, redColoredMaterial);
  52. }
  53. if (!yAxis) {
  54. var greenColoredMaterial = new StandardMaterial("", scene);
  55. greenColoredMaterial.disableLighting = true;
  56. greenColoredMaterial.emissiveColor = Color3.Green().scale(0.5);
  57. yAxis = AxisDragGizmo._CreateArrow(scene, greenColoredMaterial);
  58. }
  59. if (!zAxis) {
  60. var blueColoredMaterial = new StandardMaterial("", scene);
  61. blueColoredMaterial.disableLighting = true;
  62. blueColoredMaterial.emissiveColor = Color3.Blue().scale(0.5);
  63. zAxis = AxisDragGizmo._CreateArrow(scene, blueColoredMaterial);
  64. }
  65. this._xAxis = xAxis;
  66. this._xAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  67. this._yAxis = yAxis;
  68. this._yAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  69. this._zAxis = zAxis;
  70. this._zAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  71. if (renderingGroupId != null) {
  72. AxesViewer._SetRenderingGroupId(this._xAxis, renderingGroupId);
  73. AxesViewer._SetRenderingGroupId(this._yAxis, renderingGroupId);
  74. AxesViewer._SetRenderingGroupId(this._zAxis, renderingGroupId);
  75. }
  76. this.scene = scene;
  77. this.update(new Vector3(), Vector3.Right(), Vector3.Up(), Vector3.Forward());
  78. }
  79. /**
  80. * Force the viewer to update
  81. * @param position defines the position of the viewer
  82. * @param xaxis defines the x axis of the viewer
  83. * @param yaxis defines the y axis of the viewer
  84. * @param zaxis defines the z axis of the viewer
  85. */
  86. public update(position: Vector3, xaxis: Vector3, yaxis: Vector3, zaxis: Vector3): void {
  87. this._xAxis.position.copyFrom(position);
  88. this._xAxis.setDirection(xaxis);
  89. this._xAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  90. this._yAxis.position.copyFrom(position);
  91. this._yAxis.setDirection(yaxis);
  92. this._yAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  93. this._zAxis.position.copyFrom(position);
  94. this._zAxis.setDirection(zaxis);
  95. this._zAxis.scaling.setAll(this.scaleLines * this._scaleLinesFactor);
  96. }
  97. /**
  98. * Creates an instance of this axes viewer.
  99. * @returns a new axes viewer with instanced meshes
  100. */
  101. public createInstance(): AxesViewer {
  102. const xAxis = AxisDragGizmo._CreateArrowInstance(this.scene, this._xAxis);
  103. const yAxis = AxisDragGizmo._CreateArrowInstance(this.scene, this._yAxis);
  104. const zAxis = AxisDragGizmo._CreateArrowInstance(this.scene, this._zAxis);
  105. const axesViewer = new AxesViewer(this.scene, this.scaleLines, null, xAxis, yAxis, zAxis);
  106. axesViewer._instanced = true;
  107. return axesViewer;
  108. }
  109. /** Releases resources */
  110. public dispose() {
  111. if (this._xAxis) {
  112. this._xAxis.dispose(false, !this._instanced);
  113. delete this._xAxis;
  114. }
  115. if (this._yAxis) {
  116. this._yAxis.dispose(false, !this._instanced);
  117. delete this._yAxis;
  118. }
  119. if (this._zAxis) {
  120. this._zAxis.dispose(false, !this._instanced);
  121. delete this._zAxis;
  122. }
  123. delete this.scene;
  124. }
  125. private static _SetRenderingGroupId(node: TransformNode, id: number) {
  126. node.getChildMeshes().forEach((mesh) => {
  127. mesh.renderingGroupId = id;
  128. });
  129. }
  130. }