directionalLightFrustumViewer.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 { LinesMesh } from "../Meshes/linesMesh";
  7. import { Mesh } from "../Meshes/mesh";
  8. import { VertexData } from "../Meshes/mesh.vertexData";
  9. import { MeshBuilder } from "../Meshes/meshBuilder";
  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. this._nearLinesPoints[0] = TmpVectors.Vector3[2];
  161. this._nearLinesPoints[1] = TmpVectors.Vector3[3];
  162. this._nearLinesPoints[2] = TmpVectors.Vector3[4];
  163. this._nearLinesPoints[3] = TmpVectors.Vector3[5];
  164. this._nearLinesPoints[4] = TmpVectors.Vector3[2];
  165. MeshBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints, instance: this._lightHelperFrustumMeshes[0] as LinesMesh }, this._scene);
  166. this._farLinesPoints[0] = TmpVectors.Vector3[6];
  167. this._farLinesPoints[1] = TmpVectors.Vector3[7];
  168. this._farLinesPoints[2] = TmpVectors.Vector3[8];
  169. this._farLinesPoints[3] = TmpVectors.Vector3[9];
  170. this._farLinesPoints[4] = TmpVectors.Vector3[6];
  171. MeshBuilder.CreateLines("farlines", { updatable: true, points: this._farLinesPoints, instance: this._lightHelperFrustumMeshes[1] as LinesMesh }, this._scene);
  172. this._trLinesPoints[0] = TmpVectors.Vector3[2];
  173. this._trLinesPoints[1] = TmpVectors.Vector3[6];
  174. MeshBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints, instance: this._lightHelperFrustumMeshes[2] as LinesMesh }, this._scene);
  175. this._brLinesPoints[0] = TmpVectors.Vector3[3];
  176. this._brLinesPoints[1] = TmpVectors.Vector3[7];
  177. MeshBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints, instance: this._lightHelperFrustumMeshes[3] as LinesMesh }, this._scene);
  178. this._tlLinesPoints[0] = TmpVectors.Vector3[4];
  179. this._tlLinesPoints[1] = TmpVectors.Vector3[8];
  180. MeshBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints, instance: this._lightHelperFrustumMeshes[4] as LinesMesh }, this._scene);
  181. this._blLinesPoints[0] = TmpVectors.Vector3[5];
  182. this._blLinesPoints [1] = TmpVectors.Vector3[9];
  183. MeshBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints, instance: this._lightHelperFrustumMeshes[5] as LinesMesh }, this._scene);
  184. TmpVectors.Vector3[2].toArray(this._nearPlaneVertices, 0);
  185. TmpVectors.Vector3[3].toArray(this._nearPlaneVertices, 3);
  186. TmpVectors.Vector3[4].toArray(this._nearPlaneVertices, 6);
  187. TmpVectors.Vector3[5].toArray(this._nearPlaneVertices, 9);
  188. this._lightHelperFrustumMeshes[6].geometry?.updateVerticesDataDirectly("position", this._nearPlaneVertices, 0);
  189. TmpVectors.Vector3[6].toArray(this._farPlaneVertices, 0);
  190. TmpVectors.Vector3[7].toArray(this._farPlaneVertices, 3);
  191. TmpVectors.Vector3[8].toArray(this._farPlaneVertices, 6);
  192. TmpVectors.Vector3[9].toArray(this._farPlaneVertices, 9);
  193. this._lightHelperFrustumMeshes[7].geometry?.updateVerticesDataDirectly("position", this._farPlaneVertices, 0);
  194. TmpVectors.Vector3[2].toArray(this._rightPlaneVertices, 0);
  195. TmpVectors.Vector3[6].toArray(this._rightPlaneVertices, 3);
  196. TmpVectors.Vector3[7].toArray(this._rightPlaneVertices, 6);
  197. TmpVectors.Vector3[3].toArray(this._rightPlaneVertices, 9);
  198. this._lightHelperFrustumMeshes[8].geometry?.updateVerticesDataDirectly("position", this._rightPlaneVertices, 0);
  199. TmpVectors.Vector3[5].toArray(this._leftPlaneVertices, 0);
  200. TmpVectors.Vector3[9].toArray(this._leftPlaneVertices, 3);
  201. TmpVectors.Vector3[8].toArray(this._leftPlaneVertices, 6);
  202. TmpVectors.Vector3[4].toArray(this._leftPlaneVertices, 9);
  203. this._lightHelperFrustumMeshes[9].geometry?.updateVerticesDataDirectly("position", this._leftPlaneVertices, 0);
  204. TmpVectors.Vector3[2].toArray(this._topPlaneVertices, 0);
  205. TmpVectors.Vector3[6].toArray(this._topPlaneVertices, 3);
  206. TmpVectors.Vector3[9].toArray(this._topPlaneVertices, 6);
  207. TmpVectors.Vector3[5].toArray(this._topPlaneVertices, 9);
  208. this._lightHelperFrustumMeshes[10].geometry?.updateVerticesDataDirectly("position", this._topPlaneVertices, 0);
  209. TmpVectors.Vector3[3].toArray(this._bottomPlaneVertices, 0);
  210. TmpVectors.Vector3[7].toArray(this._bottomPlaneVertices, 3);
  211. TmpVectors.Vector3[8].toArray(this._bottomPlaneVertices, 6);
  212. TmpVectors.Vector3[4].toArray(this._bottomPlaneVertices, 9);
  213. this._lightHelperFrustumMeshes[11].geometry?.updateVerticesDataDirectly("position", this._bottomPlaneVertices, 0);
  214. }
  215. /**
  216. * Dispose of the class / remove the frustum view
  217. */
  218. public dispose() {
  219. this._lightHelperFrustumMeshes.forEach((mesh) => {
  220. mesh.material?.dispose();
  221. mesh.dispose();
  222. });
  223. this._rootNode.dispose();
  224. }
  225. protected _createGeometry() {
  226. this._rootNode = new TransformNode("directionalLightHelperRoot_" + this._light.name, this._scene);
  227. this._rootNode.parent = this._light.parent;
  228. this._nearLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  229. const nearLines = MeshBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints }, this._scene);
  230. nearLines.parent = this._rootNode;
  231. nearLines.alwaysSelectAsActiveMesh = true;
  232. this._farLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  233. const farLines = MeshBuilder.CreateLines("farlines", { updatable: true, points: this._farLinesPoints }, this._scene);
  234. farLines.parent = this._rootNode;
  235. farLines.alwaysSelectAsActiveMesh = true;
  236. this._trLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  237. const trLines = MeshBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints }, this._scene);
  238. trLines.parent = this._rootNode;
  239. trLines.alwaysSelectAsActiveMesh = true;
  240. this._brLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  241. const brLines = MeshBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints }, this._scene);
  242. brLines.parent = this._rootNode;
  243. brLines.alwaysSelectAsActiveMesh = true;
  244. this._tlLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  245. const tlLines = MeshBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints }, this._scene);
  246. tlLines.parent = this._rootNode;
  247. tlLines.alwaysSelectAsActiveMesh = true;
  248. this._blLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
  249. const blLines = MeshBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints }, this._scene);
  250. blLines.parent = this._rootNode;
  251. blLines.alwaysSelectAsActiveMesh = true;
  252. this._lightHelperFrustumMeshes.push(nearLines, farLines, trLines, brLines, tlLines, blLines);
  253. const makePlane = (name: string, color: Color3, positions: number[]) => {
  254. const plane = new Mesh(name + "plane", this._scene);
  255. const mat = new StandardMaterial(name + "PlaneMat", this._scene);
  256. plane.material = mat;
  257. plane.parent = this._rootNode;
  258. plane.alwaysSelectAsActiveMesh = true;
  259. mat.emissiveColor = color;
  260. mat.alpha = this.transparency;
  261. mat.backFaceCulling = false;
  262. mat.disableLighting = true;
  263. const indices = [0, 1, 2, 0, 2, 3];
  264. const vertexData = new VertexData();
  265. vertexData.positions = positions;
  266. vertexData.indices = indices;
  267. vertexData.applyToMesh(plane, true);
  268. this._lightHelperFrustumMeshes.push(plane);
  269. };
  270. this._nearPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  271. this._farPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  272. this._rightPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  273. this._leftPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  274. this._topPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  275. this._bottomPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  276. makePlane("near", new Color3(1, 0, 0), this._nearPlaneVertices);
  277. makePlane("far", new Color3(0.3, 0, 0), this._farPlaneVertices);
  278. makePlane("right", new Color3(0, 1, 0), this._rightPlaneVertices);
  279. makePlane("left", new Color3(0, 0.3, 0), this._leftPlaneVertices);
  280. makePlane("top", new Color3(0, 0, 1), this._topPlaneVertices);
  281. makePlane("bottom", new Color3(0, 0, 0.3), this._bottomPlaneVertices);
  282. }
  283. protected _getInvertViewMatrix(): Matrix {
  284. Matrix.LookAtLHToRef(this._light.position, this._light.position.add(this._light.direction), Vector3.Up(), this._inverseViewMatrix);
  285. this._inverseViewMatrix.invertToRef(this._inverseViewMatrix);
  286. return this._inverseViewMatrix;
  287. }
  288. }