Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove shadow direction quantization, increase shadow frames instead #15665

Merged
merged 5 commits into from
Jan 12, 2025

Conversation

lhofhansl
Copy link
Contributor

@lhofhansl lhofhansl commented Jan 10, 2025

Ever wondered why shadows are laggy even when you set shadow_update_frames to 1?
This most visible at dusk and dawn when shadows change quickly due to the angle of the sun.

This has bothered me for quite a while, so I finally tracked it down.
This was due to the direction quantization we do, which was added to reduce the jittering in the shadows (if it is updated less frequently you do not notice the jittering so much).

I had an earlier version where I quantize less during dusk and dawn, but in the end I think we should just remove it. If we really wanted to I can bring quantization back around midday and midnight whrere shadows move the least at the jittering would be most obvious)

The old logic shows a different shadow every PI/2880 change, at a day length of 20 minutes that comes to about 0.2s (20*60/2/pi/2880 = 0.2083...), which matches my observation.

So we go through all the work calculating shadows, etc, just to not show them more than 5 times/s.

Instead we roughly get the same effect if shadow_update_frames is set to 16 (assuming 60 FPS).
For nice machines and larger shadow texture, folks now reduce this to get smooth shadow movement.

  • Goal of the PR
    Allow for smoothly changing shadows.

  • How does the PR work?
    Removing sun/moon direction quantization, replace it by increasing shadow update frames.
    And that allows getting smooth shadow movements on better machine by reducing shadow update frames.

  • Does it resolve any reported issue?
    Nope

  • Does this relate to a goal in the roadmap?
    Nope

  • If not a bug fix, why is this PR needed? What usecases does it solve?
    Shadows were laggy.

To do

This PR is Ready for Review.

How to test

Enable shadows. Pick a good view distance, set time to dusk/dawn and observe shadows moving in a choppy way.
Assuming 60FS this should be roughly the same before and after.

Now reduce shadow_update_frames to 1 and enjoy smoothly moving shadows.

@tigercoding56
Copy link

i think we should make it a option, especially now that sfan5 removed some options with the latest commit, we need something to fill the settings screen
i propose 3 options:
less directional quantisiation in certain situations (shadows that don't change quickly) (partial)
full directional quantisation (full)
and no directional quantisation (disabled)

Copy link
Collaborator

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense.

Mizokuiam

This comment was marked as spam.

@lhofhansl
Copy link
Contributor Author

lhofhansl commented Jan 11, 2025

quantize less during dusk and dawn, but in the end I think we should just remove it

Changed my mind. Now shadows are quantized around noon and midnight, and in a way that does not waste resources.
Removed the "One Approval" since the code changed.

@sfan5 Would you have another look?

@lhofhansl lhofhansl requested a review from sfan5 January 11, 2025 19:36
@lhofhansl lhofhansl changed the title Remove shadow direction quantization, increase shadow frames instead Quantize shadow direction only around noon and midnight Jan 11, 2025
* At those times we update shadows less often.
*/
v3f q_direction = quantizeDirection(direction, M_PI / 2880 /*15 seconds*/);
if (q_direction == last_quant_directin)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float comparison is OK here, since quantizeDirection uses ceil for integer math.

@@ -102,6 +102,7 @@ class DirectionalLight

v3f last_cam_pos_world{0,0,0};
v3f last_look{0,1,0};
v3f last_quant_directin{0,0,0};
Copy link
Member

@SmallJoker SmallJoker Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
v3f last_quant_directin{0,0,0};
v3f last_quant_direction{0,0,0};

EDIT: or

Suggested change
v3f last_quant_directin{0,0,0};
v3f last_quant_direction;

(use default constructor)

{
if (dirty && !force)
return;

if (timeoftheday < 0.1 || timeoftheday > 0.9 || (timeoftheday > 0.4 && timeoftheday < 0.6)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if set_sky is used, where the sun is tilted? The shadows would still change noticeably during these time limits.

@sfan5
Copy link
Collaborator

sfan5 commented Jan 11, 2025

I don't see why we should quantize the direction at all?
Instead increasing shadow_update_frames makes it equivalent, more performant and user-configuable.

@lhofhansl
Copy link
Contributor Author

@sfan5 ok... I'll revert the last two commits and then merge (the changes you had already reviewed)

@lhofhansl lhofhansl changed the title Quantize shadow direction only around noon and midnight Remove shadow direction quantization, increase shadow frames instead Jan 12, 2025
@lhofhansl
Copy link
Contributor Author

OK. Reverted the last two commits and re-added @sfan5's original approval of the first commit.

@lhofhansl lhofhansl merged commit d15214a into luanti-org:master Jan 12, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants