Skip to content

Add per-player shadow direction override via set_lighting#17014

Open
sofar wants to merge 1 commit intoluanti-org:masterfrom
sofar:sofar/shadow_direction
Open

Add per-player shadow direction override via set_lighting#17014
sofar wants to merge 1 commit intoluanti-org:masterfrom
sofar:sofar/shadow_direction

Conversation

@sofar
Copy link
Contributor

@sofar sofar commented Mar 11, 2026

Add an optional shadow.direction field to the lighting struct, allowing server mods to override the shadow light direction on a per-player basis. This is needed for games that use custom skyboxes where the sun/moon position in the skybox texture does not correspond to the engine's internal time-of-day cycle.

When shadow_direction is set, Game::updateShadows() uses it directly instead of computing direction from getSunDirection()/getMoonDirection(), and applies shadow_intensity unconditionally (bypassing the sun/moon visibility check that would otherwise zero out shadows).

Lua API:
player:set_lighting({shadows = {direction = {x=0.3, y=0.9, z=0.3}}})
player:set_lighting({shadows = {direction = falsevector.zero()}}) -- clear override

The TOCLIENT_SET_LIGHTING packet documentation in networkprotocol.h was outdated, listing only the original fields (shadow_intensity, saturation, exposure parameters) while the actual packet has grown over several releases to include volumetric_light_strength, shadow_tint, and bloom parameters. The documentation is now updated to reflect all fields actually sent and parsed, plus the new shadow direction.

Network compatibility: the new field is appended to the end of the packet. Old clients ignore the extra bytes. New clients handle missing bytes via the existing hasRemainingBytes() pattern, falling back to sun/moon-based shadows when the server does not send a direction.

@sfan5 sfan5 added Feature ✨ PRs that add or enhance a feature @ Client / Audiovisuals labels Mar 11, 2026
@sfan5
Copy link
Member

sfan5 commented Mar 11, 2026

This is needed for games that use custom skyboxes where the sun/moon position in the skybox texture does not correspond to the engine's internal time-of-day cycle.

If you also want to have a different time-of-day that doesn't match the sun direction, yes.

@sofar sofar force-pushed the sofar/shadow_direction branch from dcf421f to d751eec Compare March 12, 2026 02:15
@sofar
Copy link
Contributor Author

sofar commented Mar 12, 2026

Thanks @sfan5 - those were spot on comments. I've fixed all of them up.

While testing I ran into the "what about old clients" problem and there's no clean way to handle them gracefully without bumping the protocol version, so that's in this new updated version. Maybe that's too aggressive, but, without that servers that want to use this feature have no way to tell clients apart that support it and clients that will use the time based shadow direction.

(Tested with a modified version of the skybox mod, of course)

@sofar sofar requested a review from sfan5 March 12, 2026 02:22
@sofar
Copy link
Contributor Author

sofar commented Mar 12, 2026

Ah so this isn't entirely right yet with the proto version bump. Want me to drop it from this patch, or, figure out how to fix it up?

@sofar sofar force-pushed the sofar/shadow_direction branch from d751eec to 2e4d55a Compare March 13, 2026 19:32
@sfan5
Copy link
Member

sfan5 commented Mar 13, 2026

Ah so this isn't entirely right yet with the proto version bump. Want me to drop it from this patch, or, figure out how to fix it up?

Hm? Looks good to me.

@sofar
Copy link
Contributor Author

sofar commented Mar 13, 2026

Ah so this isn't entirely right yet with the proto version bump. Want me to drop it from this patch, or, figure out how to fix it up?

Hm? Looks good to me.

Nah It needs an entry in the unittest lua file for 5.16. I'll add it in a sec.

@sofar sofar force-pushed the sofar/shadow_direction branch from 2e4d55a to 287b72d Compare March 13, 2026 20:21
@sofar sofar requested a review from sfan5 March 13, 2026 22:15
Copy link
Member

@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.

works

@sofar sofar force-pushed the sofar/shadow_direction branch from 287b72d to dc5d3a7 Compare March 14, 2026 21:03
@sofar
Copy link
Contributor Author

sofar commented Mar 14, 2026

Applied the three suggestions.

@sofar
Copy link
Contributor Author

sofar commented Mar 15, 2026

Win build failure is a dependency download issue.

Copy link
Member

@SmallJoker SmallJoker left a comment

Choose a reason for hiding this comment

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

Looks good overall. A few remarks.

@sofar sofar force-pushed the sofar/shadow_direction branch from dc5d3a7 to 0b2151e Compare March 15, 2026 19:03
Copy link
Member

@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.

lookin good

Add an optional shadow.direction field to the lighting struct, allowing
server mods to override the shadow light direction on a per-player basis.
This is needed for games that use custom skyboxes where the sun/moon
position in the skybox texture does not correspond to the engine's
internal time-of-day cycle.

When shadow_direction is set, Game::updateShadows() uses it directly
instead of computing direction from getSunDirection()/getMoonDirection(),
and applies shadow_intensity unconditionally (bypassing the sun/moon
visibility check that would otherwise zero out shadows).

Lua API:
  player:set_lighting({shadows = {direction = vector.new(0.3, 0.9, 0.3)}})
  player:set_lighting({shadows = {direction = vector.zero()}})  -- clear

A zero vector is treated as "unset" throughout: Lua API, network
protocol, and client all use the same convention. The shadow direction
is always sent as a v3f on the wire (no flag byte), with {0,0,0}
meaning "use default sun/moon-based direction".

Removed dead code: the timeoftheday fmod computation in updateShadows()
was unused even before this change, and is now removed. The remaining
timeoftheday variable is scoped to the else branch where it is needed.

Updated TOCLIENT_SET_LIGHTING packet documentation in networkprotocol.h
to reflect all fields actually sent and parsed, plus the new shadow
direction.

Network compatibility: the new field is appended to the end of the
packet. Old clients ignore the extra bytes. New clients handle missing
bytes via the existing hasRemainingBytes() pattern, falling back to
sun/moon-based shadows when the server does not send a direction.

To ensure that servers can differentiate between clients that can, and
can not use this feature, the LATEST_PROTOCOL_VERSION must be bumped before
or at the next release.
@sofar sofar force-pushed the sofar/shadow_direction branch from 0b2151e to 4908adc Compare March 17, 2026 18:01
@sofar
Copy link
Contributor Author

sofar commented Mar 17, 2026

Dropped protocol version bump because @SmallJoker told me to ;)

Copy link
Member

@SmallJoker SmallJoker left a comment

Choose a reason for hiding this comment

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

Thanks. Looks good.

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.

3 participants