Trouble limiting shadow map size with customProjectionMatrixBuilder

I’ve built a little physics simulation drawing test here:

(Click and drag the canvas to see shapes appear).

In the code sample above, you’ll notice on line 140 I’ve tried to use customProjectionMatrix as shown here to limit the shadowmap bounds. Without a limit the shadow map seems to grow as more shapes are added to the scene, very quickly blurring into nothingness.

But I can’t get it to work. The shadows just disappear when I use that customProjectMatrix code.
Has anyone worked with it? I can’t find any other references or examples on how to use, and I’m unsure if I’m even going about it correclty…

Thank you,

Welcome aboard!

The example is wrong, it should read:

light.customProjectionMatrixBuilder = function(viewMatrix: Matrix, renderList: Array<AbstractMesh>, result: Matrix) {
    BABYLON.Matrix.PerspectiveFovLHToRef(angle, 1.0, activeCamera.minZ, this.shadowMaxZ, result);

I have created a PR to fix the doc:

Note that in your case you should use OrthoLHToRef or OrthoOffCenterLHToRef because the light is a directional light.

You have a simpler way to use a fixed frustum: set the shadowFrustumSize to something greater than 0 (15 for eg).


Awesome. Thank you so much!
So I tested both methods and got them working, and here’s my final code (with the alternative method commented out):

// So Babylon knows where to project hte shadowmap from...
dirLight.position = new Vector3(0, 100, 0);
// Then, to define the size of the shadowmap area
dirLight.shadowFrustumSize = 200;
// OR...
// dirLight.customProjectionMatrixBuilder = function (viewMatrix: Matrix, renderList: Array<AbstractMesh>, result: Matrix) {
//   // Ortho because it's a directional light
//   Matrix.OrthoLHToRef(200, 200, camera.minZ, camera.maxZ, result);
// }