babylon.axesViewer.ts 6.3 KB

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