axesViewer.ts 5.7 KB

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