How do I trigger Update Shadows programatically?

I have a scene with a directional light on which I want to use shadowUpdateMode = pc.SHADOWUPDATE_THISFRAME.
But I cant find a way to trigger an update programatically. In the editor a button for updating shadows appear, see image.

image

What code does that button run?

Okay I found something in the engine source code which did the drick!

updateShadow() {
        this.light.updateShadow();
}

If anyone stumbles on the same problem in the future :slight_smile:

2 Likes

the function Light.updateShadow() is private and might not work in the future.

Perhaps try this instead to render it just one time.

this.light.shadowUpdateMode = pc.SHADOWUPDATE_THISFRAME;

I did that first, but it doesnt work. The shadows dont re-render after I make that assignment.

Odd, all updateShadow does is

    updateShadow() {
        if (this.shadowUpdateMode !== SHADOWUPDATE_REALTIME) {
            this.shadowUpdateMode = SHADOWUPDATE_THISFRAME;
        }
    }
1 Like

Yes I saw that, I’m really confused as to why it only works when I’m using updateShadow(). Only thing I can imagine is if its some kind of race condition issue?

I ran into this problem and the solution I found is simply deactivate and reactivate the shadows, and the shadows regenerate

            if(scope.useUpdateShadow){

                for(var i = 0; i < scope.lightToUpdateShadow.length; ++i)
                    scope.lightToUpdateShadow[i].light.castShadows = false;


                for(var i = 0; i < scope.lightToUpdateShadow.length; ++i)
                    scope.lightToUpdateShadow[i].light.castShadows = true;