axesViewer.js 5.3 KB

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