Change resolution of shadows dinamically

i try to change resolution of shadows dinamically but when change shadows disappear. The code that i execute to change resolution after scene is loaded is:

this.directionalLight.shadow.mapSize.width = maxTextureSize;
this.directionalLight.shadow.mapSize.height = maxTextureSize;

this.directionalLight.shadow.camera.updateProjectionMatrix();
this.directionalLight.shadow.needsUpdate = true;
this.directionalLight.shadow.updateMatrices(this.directionalLight)

where maxTextureSize is the value i want to change dinamically.

Thanks for your support.

Try logging maxTextureSize and see if it’s not NaN. Considering that to create a shadow map initially you have to change it’s size on runtime - there should be absolutely no problem with changing it later on when the app is already running.

(Also take a look at three-csm - maybe it’s already doing what you’re trying to do?)

Thanks, i will try to follow that repo code to find the solution.

Ummm…nothing. I tried for a few hours, reading code of three-csm repo also, and i didnt found anything else to change resolution once the scene is loaded and shadows are activated at least one time with a default resolution. I thought that problem was because i didnt mark materials as “needUpdates” = true after change resolution and these are not recompiled again (refresh shaders or something else), but it didnt work either.

Here is a demo of dynamical change of shadow resolution of DirectionalLight – it is automatically changed every second. See lines 100-101. Both of them are necessary.

https://codepen.io/boytchev/full/QWZqGBq

image

2 Likes

WTF, you are right¡¡¡

Solution is to use these method to change resolution, and not how i am done:
light.shadow.mapSize.set( k, k );
light.shadow.map.setSize( k, k );

Thank you, so much

2 Likes