directionalLightFrustumViewer.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import { Camera } from "../Cameras/camera";
  2. import { DirectionalLight } from "../Lights/directionalLight";
  3. import { StandardMaterial } from "../Materials/standardMaterial";
  4. import { Color3 } from "../Maths/math.color";
  5. import { Matrix, TmpVectors, Vector3 } from "../Maths/math.vector";
  6. import { LinesBuilder } from "../Meshes/Builders/linesBuilder";
  7. import { LinesMesh } from "../Meshes/linesMesh";
  8. import { Mesh } from "../Meshes/mesh";
  9. import { VertexData } from "../Meshes/mesh.vertexData";
  10. import { TransformNode } from "../Meshes/transformNode";
  11. import { Scene } from "../scene";
  12. /**
  13. * Class used to render a debug view of the frustum for a directional light
  14. * @see https://playground.babylonjs.com/#7EFGSG#3
  15. */
  16. export class DirectionalLightFrustumViewer {
  17. private _scene: Scene;
  18. private _light: DirectionalLight;
  19. private _camera: Camera;
  20. private _inverseViewMatrix: Matrix;
  21. private _visible: boolean;
  22. private _rootNode: TransformNode;
  23. private _lightHelperFrustumMeshes: Mesh[];
  24. private _nearLinesPoints: Vector3[];
  25. private _farLinesPoints: Vector3[];
  26. private _trLinesPoints: Vector3[];
  27. private _brLinesPoints: Vector3[];
  28. private _tlLinesPoints: Vector3[];
  29. private _blLinesPoints: Vector3[];
  30. private _nearPlaneVertices: number[];
  31. private _farPlaneVertices: number[];
  32. private _rightPlaneVertices: number[];
  33. private _leftPlaneVertices: number[];
  34. private _topPlaneVertices: number[];
  35. private _bottomPlaneVertices: number[];
  36. private _oldPosition: Vector3 = new Vector3(Number.NaN, Number.NaN, Number.NaN);
  37. private _oldDirection: Vector3 = new Vector3(Number.NaN, Number.NaN, Number.NaN);
  38. private _oldAutoCalc: boolean;
  39. private _oldMinZ: number;
  40. private _oldMaxZ: number;
  41. private _transparency = 0.3;
  42. /**
  43. * Gets or sets the transparency of the frustum planes
  44. */
  45. public get transparency(): number {
  46. return this._transparency;
  47. }
  48. public set transparency(alpha: number) {
  49. this._transparency = alpha;
  50. for (let i = 6; i < 12; ++i) {
  51. this._lightHelperFrustumMeshes[i].material!.alpha = alpha;
  52. }
  53. }
  54. private _showLines = true;
  55. /**
  56. * true to display the edges of the frustum
  57. */
  58. public get showLines(): boolean {
  59. return this._showLines;
  60. }
  61. public set showLines(show: boolean) {
  62. if (this._showLines === show) {
  63. return;
  64. }
  65. this._showLines = show;
  66. for (let i = 0; i < 6; ++i) {
  67. this._lightHelperFrustumMeshes[i].setEnabled(show);
  68. }
  69. }
  70. private _showPlanes = true;
  71. /**
  72. * true to display the planes of the frustum
  73. */
  74. public get showPlanes(): boolean {
  75. return this._showPlanes;
  76. }
  77. public set showPlanes(show: boolean) {
  78. if (this._showPlanes === show) {
  79. return;
  80. }
  81. this._showPlanes = show;
  82. for (let i = 6; i < 12; ++i) {
  83. this._lightHelperFrustumMeshes[i].setEnabled(show);
  84. }
  85. }
  86. /**
  87. * Creates a new frustum viewer
  88. * @param light directional light to display the frustum for
  89. * @param camera camera used to retrieve the minZ / maxZ values if the shadowMinZ/shadowMaxZ values of the light are not setup
  90. */
  91. constructor(light: DirectionalLight, camera: Camera) {
  92. this._scene = light.getScene();
  93. this._light = light;
  94. this._camera = camera;
  95. this._inverseViewMatrix = Matrix.Identity();
  96. this._lightHelperFrustumMeshes = [];
  97. this._createGeometry();
  98. this.show();
  99. this.update();
  100. }
  101. /**
  102. * Shows the frustum
  103. */
  104. public show() {
  105. this._lightHelperFrustumMeshes.forEach((mesh, index) => {
  106. mesh.setEnabled(index < 6 && this._showLines || index >= 6 && this._showPlanes);
  107. });
  108. this._oldPosition.set(Number.NaN, Number.NaN, Number.NaN);
  109. this._visible = true;
  110. }
  111. /**
  112. * Hides the frustum
  113. */
  114. public hide() {
  115. this._lightHelperFrustumMeshes.forEach((mesh) => {
  116. mesh.setEnabled(false);
  117. });
  118. this._visible = false;
  119. }
  120. /**
  121. * Updates the frustum.
  122. * Call this method to update the frustum view if the light has changed position/direction
  123. */
  124. public update() {
  125. if (!this._visible) {
  126. return;
  127. }
  128. if (this._oldPosition.equals(this._light.position)
  129. && this._oldDirection.equals(this._light.direction)
  130. && this._oldAutoCalc === this._light.autoCalcShadowZBounds
  131. && this._oldMinZ === this._light.shadowMinZ
  132. && this._oldMaxZ === this._light.shadowMaxZ
  133. ) {
  134. return;
  135. }
  136. this._oldPosition.copyFrom(this._light.position);
  137. this._oldDirection.copyFrom(this._light.direction);
  138. this._oldAutoCalc = this._light.autoCalcShadowZBounds;
  139. this._oldMinZ = this._light.shadowMinZ;
  140. this._oldMaxZ = this._light.shadowMaxZ;
  141. TmpVectors.Vector3[0].set(this._light.orthoLeft, this._light.orthoBottom, this._light.shadowMinZ !== undefined ? this._light.shadowMinZ : this._camera.minZ); // min light extents
  142. TmpVectors.Vector3[1].set(this._light.orthoRight, this._light.orthoTop, this._light.shadowMaxZ !== undefined ? this._light.shadowMaxZ : this._camera.maxZ); // max light extents
  143. const invLightView = this._getInvertViewMatrix();
  144. TmpVectors.Vector3[2].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[0].z); // n1
  145. TmpVectors.Vector3[3].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[0].z); // n2
  146. TmpVectors.Vector3[4].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[0].z); // n3
  147. TmpVectors.Vector3[5].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[0].z); // n4
  148. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[2], invLightView, TmpVectors.Vector3[2]); // near1
  149. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[3], invLightView, TmpVectors.Vector3[3]); // near2
  150. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[4], invLightView, TmpVectors.Vector3[4]); // near3
  151. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[5], invLightView, TmpVectors.Vector3[5]); // near4
  152. TmpVectors.Vector3[6].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[1].z); // f1
  153. TmpVectors.Vector3[7].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[1].z); // f2
  154. TmpVectors.Vector3[8].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[1].z); // f3
  155. TmpVectors.Vector3[9].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[1].z); // f4
  156. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[6], invLightView, TmpVectors.Vector3[6]); // far1
  157. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[7], invLightView, TmpVectors.Vector3[7]); // far2
  158. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[8], invLightView, TmpVectors.Vector3[8]); // far3
  159. Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[9], invLightView, TmpVectors.Vector3[9]); // far4
  160. LinesBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints, instance: this._lightHelperFrustumMeshes[0] as LinesMesh }, this._scene);
  161. LinesBuilder.CreateLines("farlines", { updatable: true, points: this._farLinesPoints, instance: this._lightHelperFrustumMeshes[1] as LinesMesh }, this._scene);
  162. LinesBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints, instance: this._lightHelperFrustumMeshes[2] as LinesMesh }, this._scene);
  163. LinesBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints, instance: this._lightHelperFrustumMeshes[3] as LinesMesh }, this._scene);
  164. LinesBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints, instance: this._lightHelperFrustumMeshes[4] as LinesMesh }, this._scene);
  165. LinesBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints, instance: this._lightHelperFrustumMeshes[5] as LinesMesh }, this._scene);
  166. TmpVectors.Vector3[2].toArray(this._nearPlaneVertices, 0);
  167. TmpVectors.Vector3[3].toArray(this._nearPlaneVertices, 3);
  168. TmpVectors.Vector3[4].toArray(this._nearPlaneVertices, 6);
  169. TmpVectors.Vector3[5].toArray(this._nearPlaneVertices, 9);
  170. this._lightHelperFrustumMeshes[6].geometry?.updateVerticesDataDirectly("position", this._nearPlaneVertices, 0);
  171. TmpVectors.Vector3[6].toArray(this._farPlaneVertices, 0);
  172. TmpVectors.Vector3[7].toArray(this._farPlaneVertices, 3);
  173. TmpVectors.Vector3[8].toArray(this._farPlaneVertices, 6);
  174. TmpVectors.Vector3[9].toArray(this._farPlaneVertices, 9);
  175. this._lightHelperFrustumMeshes[7].geometry?.updateVerticesDataDirectly("position", this._farPlaneVertices, 0);
  176. TmpVectors.Vector3[2].toArray(this._rightPlaneVertices, 0);
  177. TmpVectors.Vector3[6].toArray(this._rightPlaneVertices, 3);
  178. TmpVectors.Vector3[7].toArray(this._rightPlaneVertices, 6);
  179. TmpVectors.Vector3[3].toArray(this._rightPlaneVertices, 9);
  180. this._lightHelperFrustumMeshes[8].geometry?.updateVerticesDataDirectly("position", this._rightPlaneVertices, 0);
  181. TmpVectors.Vector3[5].toArray(this._leftPlaneVertices, 0);
  182. TmpVectors.Vector3[9].toArray(this._leftPlaneVertices, 3);
  183. TmpVectors.Vector3[8].toArray(this._leftPlaneVertices, 6);
  184. TmpVectors.Vector3[4].toArray(this._leftPlaneVertices, 9);
  185. this._lightHelperFrustumMeshes[9].geometry?.updateVerticesDataDirectly("position", this._leftPlaneVertices, 0);
  186. TmpVectors.Vector3[2].toArray(this._topPlaneVertices, 0);
  187. TmpVectors.Vector3[6].toArray(this._topPlaneVertices, 3);
  188. TmpVectors.Vector3[9].toArray(this._topPlaneVertices, 6);
  189. TmpVectors.Vector3[5].toArray(this._topPlaneVertices, 9);
  190. this._lightHelperFrustumMeshes[10].geometry?.updateVerticesDataDirectly("position", this._topPlaneVertices, 0);
  191. TmpVectors.Vector3[3].toArray(this._bottomPlaneVertices, 0);
  192. TmpVectors.Vector3[7].toArray(this._bottomPlaneVertices, 3);
  193. TmpVectors.Vector3[8].toArray(this._bottomPlaneVertices, 6);
  194. TmpVectors.Vector3[4].toArray(this._bottomPlaneVertices, 9);
  195. this._lightHelperFrustumMeshes[11].geometry?.updateVerticesDataDirectly("position", this._bottomPlaneVertices, 0);
  196. }
  197. /**
  198. * Dispose of the class / remove the frustum view
  199. */
  200. public dispose() {
  201. this._lightHelperFrustumMeshes.forEach((mesh) => {
  202. mesh.material?.dispose();
  203. mesh.dispose();
  204. });
  205. this._rootNode.dispose();
  206. }
  207. protected _createGeometry() {
  208. this._rootNode = new TransformNode("directionalLightHelperRoot_" + this._light.name, this._scene);
  209. this._rootNode.parent = this._light.parent;
  210. this._nearLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  211. const nearLines = LinesBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints }, this._scene);
  212. nearLines.parent = this._rootNode;
  213. nearLines.alwaysSelectAsActiveMesh = true;
  214. this._farLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  215. const farLines = LinesBuilder.CreateLines("farlines", { updatable: true, points: this._farLinesPoints }, this._scene);
  216. farLines.parent = this._rootNode;
  217. farLines.alwaysSelectAsActiveMesh = true;
  218. this._trLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  219. const trLines = LinesBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints }, this._scene);
  220. trLines.parent = this._rootNode;
  221. trLines.alwaysSelectAsActiveMesh = true;
  222. this._brLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  223. const brLines = LinesBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints }, this._scene);
  224. brLines.parent = this._rootNode;
  225. brLines.alwaysSelectAsActiveMesh = true;
  226. this._tlLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  227. const tlLines = LinesBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints }, this._scene);
  228. tlLines.parent = this._rootNode;
  229. tlLines.alwaysSelectAsActiveMesh = true;
  230. this._blLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  231. const blLines = LinesBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints }, this._scene);
  232. blLines.parent = this._rootNode;
  233. blLines.alwaysSelectAsActiveMesh = true;
  234. this._lightHelperFrustumMeshes.push(nearLines, farLines, trLines, brLines, tlLines, blLines);
  235. const makePlane = (name: string, color: Color3, positions: number[]) => {
  236. const plane = new Mesh(name + "plane", this._scene);
  237. const mat = new StandardMaterial(name + "PlaneMat", this._scene);
  238. plane.material = mat;
  239. plane.parent = this._rootNode;
  240. plane.alwaysSelectAsActiveMesh = true;
  241. mat.emissiveColor = color;
  242. mat.alpha = this.transparency;
  243. mat.backFaceCulling = false;
  244. mat.disableLighting = true;
  245. const indices = [0, 1, 2, 0, 2, 3];
  246. const vertexData = new VertexData();
  247. vertexData.positions = positions;
  248. vertexData.indices = indices;
  249. vertexData.applyToMesh(plane, true);
  250. this._lightHelperFrustumMeshes.push(plane);
  251. };
  252. this._nearPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  253. this._farPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  254. this._rightPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  255. this._leftPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  256. this._topPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  257. this._bottomPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  258. makePlane("near", new Color3(1, 0, 0), this._nearPlaneVertices);
  259. makePlane("far", new Color3(0.3, 0, 0), this._farPlaneVertices);
  260. makePlane("right", new Color3(0, 1, 0), this._rightPlaneVertices);
  261. makePlane("left", new Color3(0, 0.3, 0), this._leftPlaneVertices);
  262. makePlane("top", new Color3(0, 0, 1), this._topPlaneVertices);
  263. makePlane("bottom", new Color3(0, 0, 0.3), this._bottomPlaneVertices);
  264. this._nearLinesPoints[0] = TmpVectors.Vector3[2];
  265. this._nearLinesPoints[1] = TmpVectors.Vector3[3];
  266. this._nearLinesPoints[2] = TmpVectors.Vector3[4];
  267. this._nearLinesPoints[3] = TmpVectors.Vector3[5];
  268. this._nearLinesPoints[4] = TmpVectors.Vector3[2];
  269. this._farLinesPoints[0] = TmpVectors.Vector3[6];
  270. this._farLinesPoints[1] = TmpVectors.Vector3[7];
  271. this._farLinesPoints[2] = TmpVectors.Vector3[8];
  272. this._farLinesPoints[3] = TmpVectors.Vector3[9];
  273. this._farLinesPoints[4] = TmpVectors.Vector3[6];
  274. this._trLinesPoints[0] = TmpVectors.Vector3[2];
  275. this._trLinesPoints[1] = TmpVectors.Vector3[6];
  276. this._brLinesPoints[0] = TmpVectors.Vector3[3];
  277. this._brLinesPoints[1] = TmpVectors.Vector3[7];
  278. this._tlLinesPoints[0] = TmpVectors.Vector3[4];
  279. this._tlLinesPoints[1] = TmpVectors.Vector3[8];
  280. this._blLinesPoints[0] = TmpVectors.Vector3[5];
  281. this._blLinesPoints [1] = TmpVectors.Vector3[9];
  282. }
  283. protected _getInvertViewMatrix(): Matrix {
  284. Matrix.LookAtLHToRef(this._light.position, this._light.position.add(this._light.direction), Vector3.UpReadOnly, this._inverseViewMatrix);
  285. this._inverseViewMatrix.invertToRef(this._inverseViewMatrix);
  286. return this._inverseViewMatrix;
  287. }
  288. }