it seems CascadedShadowMap does not work with a custom projection matrix for a camera.
The playground shows stripes, when I set a custom projection matrix.
The debug layers seem to work properly (this however is not the case in my own environment)

This is really tricky indeed as well as definitely an edge case. I hope it is an easy one for @Evgeni_Popov but if not, would you want to contribute smthg ?

Ah the multiplication with world matrix was on purpose, since otherwise projection matrix of camera and tempCamera should be equal and that is why your playground works, while mine does not.

If you look at the integration into maplibre, it does the following:

Maplibre configures Babylon in a special mode, to be able to interoperate correctly:

they create a basic Camera, not a FreeCamera or ArcRotateCamera

they call scene.render(false), meaning camera.update() is not called

thanks to this, they are able to trick the system regarding the view/projection matrices!

camera.viewMatrix is equal to the identity matrix and never changes

camera.projectionMatrix is the world*view*projection matrix calculated by worldMatrix.multiply(cameraMatrix);

Because the final transformation matrix of the camera is calculated by the scene by doing this.setTransformMatrix(activeCamera.getViewMatrix(), activeCamera.getProjectionMatrix(force));, and setTransformMatrix is simply multiplying both matrices, you get the right result.

But, in reality, the projection matrix they set in the camera is not the projection matrix, it is the world*view*projection matrix!

CSM needs that a proper view matrix exists to work correctly. I don’t know if Maplibre is able to provide a separate view and projection matrices, instead of giving a view*projection combined matrix…

Thanks for your reply!
I understand how maplibre is misusing the projection matrix as by applying transforms to it. I was surprised to see, that this works, since the geometries all stay close to 0,0,0 and there are no jittery floating point imprecisions.

So what is missing is the correct view matrix? Since maplibre is only setting the projection matrix and not the view matrix? Maybe there is a chance then to get it to work by setting _computedViewMatrix.
Or I just need to rotate and position the camera, but I think this won’t be as easy.

I realised GreasedLine has a similiar - maybe the same - issue.

CSM itself does work, the shadow maps are generated correctly (we only need the final transformation matrix of the camera, not view/projection independently).

What fails is the rendering of meshes with receiveShadows = true: that’s where the view matrix is used, because we must transform the current vertex position into the camera view space. So, I’m afraid that this view matrix is required for CSM to work…

Yes you are right it is most probably the rendering within the shader. A normal shadow generator does not need the view matrix?
But wouldn’t it be enough if I then set the computedViewMatrix of the camera to a different value? Then this view matrix should be used in the rendering process.
I tried setting _computedViewMatrix in my plaground. But this does not change anything. I am not sure if it is because it will be recomputed at some point.

You can try to override Camera._getViewMatrix, as the result of this function is assigned to _computedViewMatrix. However, for getTransformationMatrix to work correctly, you must make sure that projectionMatrix only contains a projection and nothing else:

public getTransformationMatrix(): Matrix {
this._computedViewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
return this._transformMatrix;
}

I just realized that ArcRotateCamera and TargetCamera implement different _getViewMatrix functions, probably need to have a closer look.
Hope I can come back with a solution soon.

calculating the projection matrix as in maplibre helped me at the end to get it done! now i have a proper babylon camera again without hiccups because of a weird projection matrix
here is the part where it is calculated - it is mainly copy and paste: