Can't toggle lights when performancePriority is set

scene.lights[0].setEnabled(false/true) doesn’t do anything when scene.performancePriority is != 0. Is this an expected tradeoff when using the performance optimization?

It works for me:

I don’t want to interfere. Also not being a specialist for that.
However, I think you have to add the autoClear (else, when moving the cam you’ll get artefacts).

It works the first time only. I updated the PG to toggle the light on a timer. When performancePriority is set, the light doesn’t toggle.

No interference at all, all input welcome.

I tried autoClear=true, but it didn’t change anything. AFAIK, autoClear will cause the scene to be wiped to the background color first, before rendering. That does remove artifacts in some cases, but it has no effect on lighting.

This is some performance optimization to avoid excessive lighting calculations, but it should still refresh when it detects that lights change.

Fairly sure the performance mode does so it doesn’t even check if the light has changed, for performance reasons :smile:

Edit;
It’s because the materials are frozen so they are not updated with new lighting.

you can manually unfreeze & freeze the material(s) to update them when lights are changed.

If your lights change very often, skipping the re-freezing step will probably have better performance.

1 Like

Thanks, @aWeirdo!

FWIW, frozen materials respond to changes in a light’s intensity or position, but not to it’s enabled-ness.

1 Like

@alekop Oh yeah!
so you could just negate the light’s intensity to “turn it off”, if that’s good enough for your use case.

I thought about that, but the light’s shadow generator would still spend time computing shadows. Better to just disable it.

Yep. So I tried unlit but it’s the same. Understandable even if disappointing :grin:
So what about you unfreeze the mat just for the time of the change and then freeze it again.
I believe if it’s set to unlit the shadowgen should be computing this mesh.

Yes, that’s essentially what I ended up doing:

scene.unfreezeMaterials()
light.setEnabled(true/false)
scene.onAfterRenderObservable.addOnce(() => {
    scene.freezeMaterials()
})

This works, except that occasionally the whole scene flickers, and some materials stop responding to the light. But I can’t replicate that in a PG.

I wonder if it would not be better to freeze the material before render. Did you try to do it at another time? May be onBeforeCameraRenderObservable or onBeforeDrawPhaseObservable. Not sure it changes anything though? :thinking:

Unfreezing the materials before render didn’t make a difference. I’ll try to setup a PG that shows the issue.

Once you have this pg, @Evgeni_Popov can eventually have a quick look. I’m sure he can tell you (and me) about the order of processing for best results.

Rather than unfreezing the material, try material.markDirty(true) instead.

1 Like