Materials getting darker when disabling shadows

Currently i have the problem that my materials are getting darker when i disable the shadows for a light like this: light.shadowEnabled = false.
This only happens if the materials are frozen. I’m calling markDirty() already for each material. The question is how can i correctly update my materials after disabling the shadows (without unfreezing them)?

Here is a PG to demonstrate the problem: Babylon.js Playground

You need to let the system be able to recreate the effects with the new settings when you enable/disable shadows. When materials are frozen it can’t do it. So you should unfreeze the materials for the next frame then refreeze them:

https://playground.babylonjs.com/#5EPWDN#2

If you don’t want to do any unfreeze at all, the easiest way is to create another light that does not cast shadows and switch (meaning, disable/enable) between both lights.

1 Like

I was thinking that markDirty() will recreate the effects. If not it would be nice if there would be a way to force a full update of a frozen material without temporarily unfreezing them.

Pinging @sebavan, I don’t know if it’s something we should have in the engine.

@sebavan Did you had time to look into this?

I do not think it should be in engine either, something frozen means it should not change.

@Deltakosh any thoughts ?

agree you need to unfreeze / freeze it again

Ok i can unfreeze and freeze them again but i think this will get complicated.
If i unfreeze them lets say for one frame, then only the currently visible meshes/materials would be updated or? How can i be sure that i can freeze the material again? Isn’t there a way to update all at once?

I guess you could provide a quick repro in the playground as it becomes complicated to follow your needs :slight_smile:
That will help offering the best option

But to your question, you have the list of materials on scene.materials so I’m not sure to get the problem :frowning:

As expected it does not work :worried:
Here a PG to show the issue: https://playground.babylonjs.com/#5EPWDN#3
In this PG i unfreeze the materials and freeze them again after rendering.
If you load it and press directly on “Toggle Shadows” it works correktly because all the meshes are visible. But if you do the following it is not working and the materials are dark:

  • reload the PG
  • press “Toggle Camera”
  • press “Toggle Shadows”
  • press “Toggle Camera” again

As i was expecting if a mesh is currently not visible the material will not update. When is the correct time to freeze the materials again? How can i be sure that all subMeshes for this mterial were rendered?

I think it would be easier if there would be a way to reset the internal “wasReady” state for a material and the related effects.

Note that I could not make the PG fail even when following your instructions.

Anyway, the problem is that the “frozen” flag is on the material, not the submesh. If you have a submesh A visible and another B which isn’t, you would need to keep the “frozen” flag cleared until B is drawn, even if A has already been drawn, before setting frozen to true.

Having said that, it seems your very first PG does now work…

1 Like

I know that, my question now is: How can i know that A and B were drawn?

I tested my PG again. It seems the problem shows up only in Firefox not in Chrome.

You can use the material.onBindObservable to know when your mesh is displayed with this material: the mesh is the parameter of the event.

Even in Firefox your PG works for me. Maybe it’s a caching problem?

I switched to version 4.2.0 in the playground because this is the version we are using. If i use the latest version i also can’t reproduce this. So its fixed in 5.0.0 ?
I think for for version 4.2.0 i will have to unfreeze all materials permanently and live with the performance drop for now. :worried: In the end its very unlikely that the user is looking in every necessary direction to get all meshes rendered. The materials would stay unfrozen a very long time anyway.

It would seem so, but I don’t know what fixed it!