Prechádzať zdrojové kódy

Move the helper class to debug/

Popov72 4 rokov pred
rodič
commit
96ce4903aa

+ 7 - 7
inspector/src/components/actionTabs/tabs/propertyGrids/lights/directionalLightPropertyGridComponent.tsx

@@ -8,11 +8,11 @@ import { Color3LineComponent } from "../../../../../sharedUiComponents/lines/col
 import { Vector3LineComponent } from "../../../../../sharedUiComponents/lines/vector3LineComponent";
 import { CommonShadowLightPropertyGridComponent } from "./commonShadowLightPropertyGridComponent";
 import { LockObject } from "../../../../../sharedUiComponents/tabs/propertyGrids/lockObject";
-import { GlobalState } from '../../../../globalState';
+import { GlobalState } from "../../../../globalState";
 import { CheckBoxLineComponent } from "../../../../../sharedUiComponents/lines/checkBoxLineComponent"
-import { ShadowGenerator } from 'babylonjs/Lights/Shadows/shadowGenerator';
-import { CascadedShadowGenerator } from 'babylonjs/Lights/Shadows/cascadedShadowGenerator';
-import { DirectionalLightHelper } from "../../tools/directionalLightHelper";
+import { ShadowGenerator } from "babylonjs/Lights/Shadows/shadowGenerator";
+import { CascadedShadowGenerator } from "babylonjs/Lights/Shadows/cascadedShadowGenerator";
+import { DirectionalLightFrustumViewer } from "babylonjs/Debug/directionalLightFrustumViewer";
 
 interface IDirectionalLightPropertyGridComponentProps {
     globalState: GlobalState,
@@ -33,14 +33,14 @@ export class DirectionalLightPropertyGridComponent extends React.Component<IDire
         let displayFrustum = (light as any)._displayFrustum = !(!!(light as any)._displayFrustum);
 
         if ((light as any)._displayFrustumObservable) {
-            (light as any)._displayFrustumDLH.hide();
             light.getScene().onAfterRenderObservable.remove((light as any)._displayFrustumObservable);
+            (light as any)._displayFrustumDLH.dispose();
         }
 
         if (displayFrustum && camera) {
-            const dlh = (light as any)._displayFrustumDLH = new DirectionalLightHelper(light, camera);
+            const dlh = (light as any)._displayFrustumDLH = new DirectionalLightFrustumViewer(light, camera);
             (light as any)._displayFrustumObservable = light.getScene().onAfterRenderObservable.add(() => {
-                dlh.show();
+                dlh.update();
             });
         }
     }

+ 0 - 165
inspector/src/components/actionTabs/tabs/tools/directionalLightHelper.ts

@@ -1,165 +0,0 @@
-import { TransformNode } from "babylonjs";
-import { Camera } from "babylonjs/Cameras/camera";
-import { DirectionalLight } from "babylonjs/Lights/directionalLight";
-import { StandardMaterial } from "babylonjs/Materials/standardMaterial";
-import { Color3, Matrix, Vector3 } from "babylonjs/Maths/math";
-import { Mesh } from "babylonjs/Meshes/mesh";
-import { VertexData } from "babylonjs/Meshes/mesh.vertexData";
-import { MeshBuilder } from "babylonjs/Meshes/meshBuilder";
-import { Scene } from "babylonjs/scene";
-
-export class DirectionalLightHelper {
-
-    private _scene: Scene;
-    private _light: DirectionalLight;
-    private _camera: Camera;
-    private _viewMatrix: Matrix;
-    private _lightHelperFrustumLines: TransformNode[];
-    private _oldPosition: Vector3;
-    private _oldDirection: Vector3;
-    private _oldAutoCalc: boolean;
-    private _oldMinZ: number;
-    private _oldMaxZ: number;
-
-    constructor(light: DirectionalLight, camera: Camera) {
-        this._scene = light.getScene();
-        this._light = light;
-        this._camera = camera;
-        this._viewMatrix = Matrix.Identity();
-        this._lightHelperFrustumLines = [];
-    }
-
-    public hide() {
-        this._hide();
-        this._oldPosition = null as any;
-    }
-
-    public show() {
-        if (this._oldPosition 
-            && this._oldPosition.equals(this._light.position) 
-            && this._oldDirection.equals(this._light.direction) 
-            && this._oldAutoCalc === this._light.autoCalcShadowZBounds
-            && this._oldMinZ === this._light.shadowMinZ
-            && this._oldMaxZ === this._light.shadowMaxZ
-        ) {
-            return;
-        }
-
-        this._oldPosition = this._light.position.clone();
-        this._oldDirection = this._light.direction.clone();
-        this._oldAutoCalc = this._light.autoCalcShadowZBounds;
-        this._oldMinZ = this._light.shadowMinZ;
-        this._oldMaxZ = this._light.shadowMaxZ;
-
-        this._hide();
-
-        const lightExtents = this._getLightExtents();
-        const lightView = this._getViewMatrix();
-
-        if (!lightExtents || !lightView) {
-            return;
-        }
-
-        const invLightView = Matrix.Invert(lightView);
-
-        const n1 = new Vector3(lightExtents.max.x, lightExtents.max.y, lightExtents.min.z);
-        const n2 = new Vector3(lightExtents.max.x, lightExtents.min.y, lightExtents.min.z);
-        const n3 = new Vector3(lightExtents.min.x, lightExtents.min.y, lightExtents.min.z);
-        const n4 = new Vector3(lightExtents.min.x, lightExtents.max.y, lightExtents.min.z);
-
-        const near1 = Vector3.TransformCoordinates(n1, invLightView);
-        const near2 = Vector3.TransformCoordinates(n2, invLightView);
-        const near3 = Vector3.TransformCoordinates(n3, invLightView);
-        const near4 = Vector3.TransformCoordinates(n4, invLightView);
-
-        const f1 = new Vector3(lightExtents.max.x, lightExtents.max.y, lightExtents.max.z);
-        const f2 = new Vector3(lightExtents.max.x, lightExtents.min.y, lightExtents.max.z);
-        const f3 = new Vector3(lightExtents.min.x, lightExtents.min.y, lightExtents.max.z);
-        const f4 = new Vector3(lightExtents.min.x, lightExtents.max.y, lightExtents.max.z);
-
-        const far1 = Vector3.TransformCoordinates(f1, invLightView);
-        const far2 = Vector3.TransformCoordinates(f2, invLightView);
-        const far3 = Vector3.TransformCoordinates(f3, invLightView);
-        const far4 = Vector3.TransformCoordinates(f4, invLightView);
-
-        const rootNode = new TransformNode("directionalLightHelperRoot_" + this._light.name, this._scene);
-        rootNode.parent = this._light;
-
-        const nearLines = MeshBuilder.CreateLines("nearlines", { points: [near1, near2, near3, near4, near1] }, this._scene);
-        nearLines.parent = rootNode;
-
-        const farLines = MeshBuilder.CreateLines("farlines",  { points: [far1, far2, far3, far4, far1] }, this._scene);
-        farLines.parent = rootNode;
-
-        const trLines = MeshBuilder.CreateLines("trlines", { points: [ near1, far1 ] }, this._scene);
-        trLines.parent = rootNode;
-
-        const brLines = MeshBuilder.CreateLines("brlines", { points: [ near2, far2 ] }, this._scene);
-        brLines.parent = rootNode;
-
-        const tlLines = MeshBuilder.CreateLines("tllines", { points: [ near3, far3 ] }, this._scene);
-        tlLines.parent = rootNode;
-
-        const blLines = MeshBuilder.CreateLines("bllines", { points: [ near4, far4 ] }, this._scene);
-        blLines.parent = rootNode;
-
-        this._lightHelperFrustumLines.push(rootNode, nearLines, farLines, trLines, brLines, tlLines, blLines);
-
-        const makePlane = (name: string, color: Color3, positions: number[]) => {
-            const plane = new Mesh(name + "plane", this._scene);
-            const mat = new StandardMaterial(name + "PlaneMat", this._scene);
-
-            plane.material = mat;
-            plane.parent = rootNode;
-
-            mat.emissiveColor = color;
-            mat.alpha = 0.3;
-            mat.backFaceCulling = false;
-            mat.disableLighting = true;
-
-            const indices = [0, 1, 2, 0, 2, 3];
-
-            const vertexData = new VertexData();
-
-            vertexData.positions = positions;
-            vertexData.indices = indices;
-
-            vertexData.applyToMesh(plane);
-
-            this._lightHelperFrustumLines.push(plane);
-        };
-
-        makePlane("near",   new Color3(1, 0, 0),    [near1.x, near1.y, near1.z, near2.x, near2.y, near2.z, near3.x, near3.y, near3.z, near4.x, near4.y, near4.z ]);
-        makePlane("far",    new Color3(0.3, 0, 0),  [far1.x, far1.y, far1.z, far2.x, far2.y, far2.z, far3.x, far3.y, far3.z, far4.x, far4.y, far4.z ]);
-        makePlane("right",  new Color3(0, 1, 0),    [near1.x, near1.y, near1.z, far1.x, far1.y, far1.z, far2.x, far2.y, far2.z, near2.x, near2.y, near2.z ]);
-        makePlane("left",   new Color3(0, 0.3, 0),  [near4.x, near4.y, near4.z, far4.x, far4.y, far4.z, far3.x, far3.y, far3.z, near3.x, near3.y, near3.z ]);
-        makePlane("top",    new Color3(0, 0, 1),    [near1.x, near1.y, near1.z, far1.x, far1.y, far1.z, far4.x, far4.y, far4.z, near4.x, near4.y, near4.z ]);
-        makePlane("bottom", new Color3(0, 0, 0.3),  [near2.x, near2.y, near2.z, far2.x, far2.y, far2.z, far3.x, far3.y, far3.z, near3.x, near3.y, near3.z ]);
-    }
-
-    protected _hide() {
-        this._lightHelperFrustumLines.forEach((t) => {
-            if (t instanceof Mesh) {
-                t.material?.dispose();
-            }
-            t.dispose();
-        });
-
-        this._lightHelperFrustumLines.length = 0;
-    }
-
-    protected _getLightExtents() {
-        const light = this._light;
-
-        return {
-            "min": new Vector3(light.orthoLeft, light.orthoBottom, light.shadowMinZ !== undefined ? light.shadowMinZ : this._camera.minZ),
-            "max": new Vector3(light.orthoRight, light.orthoTop, light.shadowMaxZ !== undefined ? light.shadowMaxZ : this._camera.maxZ)
-        };
-    }
-
-    protected _getViewMatrix() {
-        // same computation here than in the shadow generator
-        Matrix.LookAtLHToRef(this._light.position, this._light.position.add(this._light.direction), Vector3.Up(), this._viewMatrix);
-        return this._viewMatrix;
-    }
-}

+ 344 - 0
src/Debug/directionalLightFrustumViewer.ts

@@ -0,0 +1,344 @@
+import { Camera } from "../Cameras/camera";
+import { DirectionalLight } from "../Lights/directionalLight";
+import { StandardMaterial } from "../Materials/standardMaterial";
+import { Color3 } from "../Maths/math.color";
+import { Matrix, TmpVectors, Vector3 } from "../Maths/math.vector";
+import { LinesMesh } from "../Meshes/linesMesh";
+import { Mesh } from "../Meshes/mesh";
+import { VertexData } from "../Meshes/mesh.vertexData";
+import { MeshBuilder } from "../Meshes/meshBuilder";
+import { TransformNode } from "../Meshes/transformNode";
+import { Scene } from "../scene";
+
+/**
+ * Class used to render a debug view of the frustum for a directional light
+ * @see https://playground.babylonjs.com/#7EFGSG#3
+ */
+export class DirectionalLightFrustumViewer {
+    private _scene: Scene;
+    private _light: DirectionalLight;
+    private _camera: Camera;
+    private _viewMatrix: Matrix;
+    private _visible: boolean;
+
+    private _rootNode: TransformNode;
+    private _lightHelperFrustumMeshes: Mesh[];
+
+    private _nearLinesPoints: Vector3[];
+    private _farLinesPoints: Vector3[];
+    private _trLinesPoints: Vector3[];
+    private _brLinesPoints: Vector3[];
+    private _tlLinesPoints: Vector3[];
+    private _blLinesPoints: Vector3[];
+
+    private _nearPlaneVertices: number[];
+    private _farPlaneVertices: number[];
+    private _rightPlaneVertices: number[];
+    private _leftPlaneVertices: number[];
+    private _topPlaneVertices: number[];
+    private _bottomPlaneVertices: number[];
+
+    private _oldPosition: Vector3;
+    private _oldDirection: Vector3;
+    private _oldAutoCalc: boolean;
+    private _oldMinZ: number;
+    private _oldMaxZ: number;
+
+    private _transparency = 0.3;
+    /**
+     * Gets or sets the transparency of the frustum planes
+     */
+    public get transparency(): number {
+        return this._transparency;
+    }
+
+    public set transparency(alpha: number) {
+        this._transparency = alpha;
+        for (let i = 6; i < 12; ++i) {
+            this._lightHelperFrustumMeshes[i].material!.alpha = alpha;
+        }
+    }
+
+    private _showLines = true;
+    /**
+     * true to display the edges of the frustum
+     */
+    public get showLines(): boolean {
+        return this._showLines;
+    }
+
+    public set showLines(show: boolean) {
+        if (this._showLines === show) {
+            return;
+        }
+        this._showLines = show;
+        for (let i = 0; i < 6; ++i) {
+            this._lightHelperFrustumMeshes[i].setEnabled(show);
+        }
+    }
+
+    private _showPlanes = true;
+    /**
+     * true to display the planes of the frustum
+     */
+    public get showPlanes(): boolean {
+        return this._showPlanes;
+    }
+
+    public set showPlanes(show: boolean) {
+        if (this._showPlanes === show) {
+            return;
+        }
+        this._showPlanes = show;
+        for (let i = 6; i < 12; ++i) {
+            this._lightHelperFrustumMeshes[i].setEnabled(show);
+        }
+    }
+
+    /**
+     * Creates a new frustum viewer
+     * @param light directional light to display the frustum for
+     * @param camera camera used to retrieve the minZ / maxZ values if the shadowMinZ/shadowMaxZ values of the light are not setup
+     */
+    constructor(light: DirectionalLight, camera: Camera) {
+        this._scene = light.getScene();
+        this._light = light;
+        this._camera = camera;
+        this._viewMatrix = Matrix.Identity();
+        this._lightHelperFrustumMeshes = [];
+        this._createGeometry();
+        this.show();
+        this.update();
+    }
+
+    /**
+     * Shows the frustum
+     */
+    public show() {
+        this._lightHelperFrustumMeshes.forEach((mesh, index) => {
+            mesh.setEnabled(index < 6 && this._showLines || index >= 6 && this._showPlanes);
+        });
+        this._oldPosition = null as any;
+        this._visible = true;
+    }
+
+    /**
+     * Hides the frustum
+     */
+    public hide() {
+        this._lightHelperFrustumMeshes.forEach((mesh) => {
+            mesh.setEnabled(false);
+        });
+        this._visible = false;
+    }
+
+    /**
+     * Updates the frustum.
+     * Call this method to update the frustum view if the light has changed position/direction
+     */
+    public update() {
+        if (!this._visible) {
+            return;
+        }
+
+        if (this._oldPosition 
+            && this._oldPosition.equals(this._light.position) 
+            && this._oldDirection.equals(this._light.direction) 
+            && this._oldAutoCalc === this._light.autoCalcShadowZBounds
+            && this._oldMinZ === this._light.shadowMinZ
+            && this._oldMaxZ === this._light.shadowMaxZ
+        ) {
+            return;
+        }
+
+        this._oldPosition = this._light.position.clone();
+        this._oldDirection = this._light.direction.clone();
+        this._oldAutoCalc = this._light.autoCalcShadowZBounds;
+        this._oldMinZ = this._light.shadowMinZ;
+        this._oldMaxZ = this._light.shadowMaxZ;
+
+        TmpVectors.Vector3[0].set(this._light.orthoLeft, this._light.orthoBottom, this._light.shadowMinZ !== undefined ? this._light.shadowMinZ : this._camera.minZ); // min light extents
+        TmpVectors.Vector3[1].set(this._light.orthoRight, this._light.orthoTop, this._light.shadowMaxZ !== undefined ? this._light.shadowMaxZ : this._camera.maxZ); // max light extents
+
+        const lightView = this._getViewMatrix();
+        const invLightView = Matrix.Invert(lightView);
+
+        TmpVectors.Vector3[2].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[0].z); // n1
+        TmpVectors.Vector3[3].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[0].z); // n2
+        TmpVectors.Vector3[4].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[0].z); // n3
+        TmpVectors.Vector3[5].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[0].z); // n4
+
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[2], invLightView, TmpVectors.Vector3[2]); // near1
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[3], invLightView, TmpVectors.Vector3[3]); // near2
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[4], invLightView, TmpVectors.Vector3[4]); // near3
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[5], invLightView, TmpVectors.Vector3[5]); // near4
+
+        TmpVectors.Vector3[6].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[1].z); // f1
+        TmpVectors.Vector3[7].copyFromFloats(TmpVectors.Vector3[1].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[1].z); // f2
+        TmpVectors.Vector3[8].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[0].y, TmpVectors.Vector3[1].z); // f3
+        TmpVectors.Vector3[9].copyFromFloats(TmpVectors.Vector3[0].x, TmpVectors.Vector3[1].y, TmpVectors.Vector3[1].z); // f4
+
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[6], invLightView, TmpVectors.Vector3[6]); // far1
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[7], invLightView, TmpVectors.Vector3[7]); // far2
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[8], invLightView, TmpVectors.Vector3[8]); // far3
+        Vector3.TransformCoordinatesToRef(TmpVectors.Vector3[9], invLightView, TmpVectors.Vector3[9]); // far4
+
+        this._nearLinesPoints[0] = TmpVectors.Vector3[2];
+        this._nearLinesPoints[1] = TmpVectors.Vector3[3];
+        this._nearLinesPoints[2] = TmpVectors.Vector3[4];
+        this._nearLinesPoints[3] = TmpVectors.Vector3[5];
+        this._nearLinesPoints[4] = TmpVectors.Vector3[2];
+        MeshBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints, instance: this._lightHelperFrustumMeshes[0] as LinesMesh }, this._scene);
+
+        this._farLinesPoints[0] = TmpVectors.Vector3[6];
+        this._farLinesPoints[1] = TmpVectors.Vector3[7];
+        this._farLinesPoints[2] = TmpVectors.Vector3[8];
+        this._farLinesPoints[3] = TmpVectors.Vector3[9];
+        this._farLinesPoints[4] = TmpVectors.Vector3[6];
+        MeshBuilder.CreateLines("farlines",  { updatable: true, points: this._farLinesPoints, instance: this._lightHelperFrustumMeshes[1] as LinesMesh }, this._scene);
+
+        this._trLinesPoints[0] = TmpVectors.Vector3[2];
+        this._trLinesPoints[1] = TmpVectors.Vector3[6];
+        MeshBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints, instance: this._lightHelperFrustumMeshes[2] as LinesMesh }, this._scene);
+
+        this._brLinesPoints[0] = TmpVectors.Vector3[3];
+        this._brLinesPoints[1] = TmpVectors.Vector3[7];
+        MeshBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints, instance: this._lightHelperFrustumMeshes[3] as LinesMesh }, this._scene);
+
+        this._tlLinesPoints[0] = TmpVectors.Vector3[4];
+        this._tlLinesPoints[1] = TmpVectors.Vector3[8];
+        MeshBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints, instance: this._lightHelperFrustumMeshes[4] as LinesMesh }, this._scene);
+
+        this._blLinesPoints[0] = TmpVectors.Vector3[5];
+        this._blLinesPoints [1] = TmpVectors.Vector3[9];
+        MeshBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints, instance: this._lightHelperFrustumMeshes[5] as LinesMesh }, this._scene);
+
+        TmpVectors.Vector3[2].toArray(this._nearPlaneVertices, 0);
+        TmpVectors.Vector3[3].toArray(this._nearPlaneVertices, 3);
+        TmpVectors.Vector3[4].toArray(this._nearPlaneVertices, 6);
+        TmpVectors.Vector3[5].toArray(this._nearPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[6].geometry?.updateVerticesDataDirectly("position", this._nearPlaneVertices, 0);
+
+        TmpVectors.Vector3[6].toArray(this._farPlaneVertices, 0);
+        TmpVectors.Vector3[7].toArray(this._farPlaneVertices, 3);
+        TmpVectors.Vector3[8].toArray(this._farPlaneVertices, 6);
+        TmpVectors.Vector3[9].toArray(this._farPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[7].geometry?.updateVerticesDataDirectly("position", this._farPlaneVertices, 0);
+
+        TmpVectors.Vector3[2].toArray(this._rightPlaneVertices, 0);
+        TmpVectors.Vector3[6].toArray(this._rightPlaneVertices, 3);
+        TmpVectors.Vector3[7].toArray(this._rightPlaneVertices, 6);
+        TmpVectors.Vector3[3].toArray(this._rightPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[8].geometry?.updateVerticesDataDirectly("position", this._rightPlaneVertices, 0);
+
+        TmpVectors.Vector3[5].toArray(this._leftPlaneVertices, 0);
+        TmpVectors.Vector3[9].toArray(this._leftPlaneVertices, 3);
+        TmpVectors.Vector3[8].toArray(this._leftPlaneVertices, 6);
+        TmpVectors.Vector3[4].toArray(this._leftPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[9].geometry?.updateVerticesDataDirectly("position", this._leftPlaneVertices, 0);
+
+        TmpVectors.Vector3[2].toArray(this._topPlaneVertices, 0);
+        TmpVectors.Vector3[6].toArray(this._topPlaneVertices, 3);
+        TmpVectors.Vector3[9].toArray(this._topPlaneVertices, 6);
+        TmpVectors.Vector3[5].toArray(this._topPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[10].geometry?.updateVerticesDataDirectly("position", this._topPlaneVertices, 0);
+
+        TmpVectors.Vector3[3].toArray(this._bottomPlaneVertices, 0);
+        TmpVectors.Vector3[7].toArray(this._bottomPlaneVertices, 3);
+        TmpVectors.Vector3[8].toArray(this._bottomPlaneVertices, 6);
+        TmpVectors.Vector3[4].toArray(this._bottomPlaneVertices, 9);
+        this._lightHelperFrustumMeshes[11].geometry?.updateVerticesDataDirectly("position", this._bottomPlaneVertices, 0);
+    }
+
+    /**
+     * Dispose of the class / remove the frustum view
+     */
+    public dispose() {
+        this._lightHelperFrustumMeshes.forEach((mesh) => {
+            mesh.material?.dispose();
+            mesh.dispose();
+        });
+        this._rootNode.dispose();
+    }
+
+    protected _createGeometry() {
+        this._rootNode = new TransformNode("directionalLightHelperRoot_" + this._light.name, this._scene);
+        this._rootNode.parent = this._light.parent;
+
+        this._nearLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const nearLines = MeshBuilder.CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints }, this._scene);
+        nearLines.parent = this._rootNode;
+        nearLines.alwaysSelectAsActiveMesh = true;
+
+        this._farLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const farLines = MeshBuilder.CreateLines("farlines",  { updatable: true, points: this._farLinesPoints }, this._scene);
+        farLines.parent = this._rootNode;
+        farLines.alwaysSelectAsActiveMesh = true;
+
+        this._trLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const trLines = MeshBuilder.CreateLines("trlines", { updatable: true, points: this._trLinesPoints }, this._scene);
+        trLines.parent = this._rootNode;
+        trLines.alwaysSelectAsActiveMesh = true;
+
+        this._brLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const brLines = MeshBuilder.CreateLines("brlines", { updatable: true, points: this._brLinesPoints }, this._scene);
+        brLines.parent = this._rootNode;
+        brLines.alwaysSelectAsActiveMesh = true;
+
+        this._tlLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const tlLines = MeshBuilder.CreateLines("tllines", { updatable: true, points: this._tlLinesPoints }, this._scene);
+        tlLines.parent = this._rootNode;
+        tlLines.alwaysSelectAsActiveMesh = true;
+
+        this._blLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
+        const blLines = MeshBuilder.CreateLines("bllines", { updatable: true, points: this._blLinesPoints }, this._scene);
+        blLines.parent = this._rootNode;
+        blLines.alwaysSelectAsActiveMesh = true;
+
+        this._lightHelperFrustumMeshes.push(nearLines, farLines, trLines, brLines, tlLines, blLines);
+
+        const makePlane = (name: string, color: Color3, positions: number[]) => {
+            const plane = new Mesh(name + "plane", this._scene);
+            const mat = new StandardMaterial(name + "PlaneMat", this._scene);
+
+            plane.material = mat;
+            plane.parent = this._rootNode;
+            plane.alwaysSelectAsActiveMesh = true;
+
+            mat.emissiveColor = color;
+            mat.alpha = this.transparency;
+            mat.backFaceCulling = false;
+            mat.disableLighting = true;
+
+            const indices = [0, 1, 2, 0, 2, 3];
+
+            const vertexData = new VertexData();
+
+            vertexData.positions = positions;
+            vertexData.indices = indices;
+
+            vertexData.applyToMesh(plane, true);
+
+            this._lightHelperFrustumMeshes.push(plane);
+        };
+
+        this._nearPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+        this._farPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+        this._rightPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+        this._leftPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+        this._topPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+        this._bottomPlaneVertices = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+
+        makePlane("near",   new Color3(1, 0, 0),    this._nearPlaneVertices);
+        makePlane("far",    new Color3(0.3, 0, 0),  this._farPlaneVertices);
+        makePlane("right",  new Color3(0, 1, 0),    this._rightPlaneVertices);
+        makePlane("left",   new Color3(0, 0.3, 0),  this._leftPlaneVertices);
+        makePlane("top",    new Color3(0, 0, 1),    this._topPlaneVertices);
+        makePlane("bottom", new Color3(0, 0, 0.3),  this._bottomPlaneVertices);
+    }
+
+    protected _getViewMatrix(): Matrix {
+        Matrix.LookAtLHToRef(this._light.position, this._light.position.add(this._light.direction), Vector3.Up(), this._viewMatrix);
+        return this._viewMatrix;
+    }
+}

+ 2 - 1
src/Debug/index.ts

@@ -4,4 +4,5 @@ export * from "./debugLayer";
 export * from "./physicsViewer";
 export * from "./rayHelper";
 export * from "./skeletonViewer";
-export * from "./ISkeletonViewer";
+export * from "./ISkeletonViewer";
+export * from "./directionalLightFrustumViewer";