Skip to content

Implement moon in terrain/weather#1549

Draft
Anthony-Gaudino wants to merge 12 commits intoturanszkij:masterfrom
Anthony-Gaudino:implement-moon
Draft

Implement moon in terrain/weather#1549
Anthony-Gaudino wants to merge 12 commits intoturanszkij:masterfrom
Anthony-Gaudino:implement-moon

Conversation

@Anthony-Gaudino
Copy link
Copy Markdown

@Anthony-Gaudino Anthony-Gaudino commented Feb 11, 2026

Adds a moon when a terrain is added.

The moon is a second Directional light, so it has it's own light and casts shadows.
Users can load a texture for the moon.
Moon properties are accessible in the Weather component.

Moon, sun and earth lighting interactions simulations are implemented, for example, the earth shadow is cast on the moon.

Tested on Linux with Vulkan.


Current issues:

  • The moon glow is always a circle, which is not accurate, the glow should come from lighted areas in the moon.
  • Stars are visible trough the moon
  • Solar eclipse is not accurate, the moon is not visible in front of the sun and the eclipse happens too abruptly.
  • Lunar eclipse needs work too
  • Default settings, like moon size are not accurate, the moon should have the same size as the Sun in the sky, also automatic eclipses should be the default.
  • Moon color should change depending on some factors
  • Code cleanup and improvement

Help would be appreciated.

Add moon support to the scene and editor:
- Add moon parameters to WeatherComponent
  (direction, color, intensity, angular radius,
  phase, texture, mip bias).
- Create EnsureMoonLight to auto-create/sync a
  directional moon light entity (created
  automatically when terrain/sun is added).
- Render lunar disk in the sky shader and expose a
  moon texture picker in the Weather window.
- Expose moon controls to Lua and serialize moon
  settings with the scene.
- Wire editor UI (Weather panel) to control moon
  orientation and appearance.
Enable the moon to illuminate volumetric clouds by
streaming moon lighting parameters to the GPU and
sampling them during the volumetric cloud
scattering pass:

- Export moon direction/color/intensity/phase to
  GPU via ShaderInterop_Weather.
- Add moon light helpers to globals.hlsli for
  shareable math/phase evaluation.
- Update volumetricCloud_renderCS.hlsl to include
  moon scattering, rim-lighting, and simple
  self-shadowing from the lunar direction.
- Ensure wiScene/Weather updates push moon
  parameters to shader constant buffers; add minor
  serialization/UI sync where needed.

Note: this change affects cloud appearance at
night; tune moon intensity and phase controls and
rebuild shader cache after merging.
Extend moon lighting with shadow map support:
track its directional light index alongside the
sun, feed it through the renderer's light buffer,
and sample the cascade array in sky/cloud shaders
so moonlit scenes receive proper shadowing.
Implement dynamic moon phases so the visible disk,
halo, and moonlight intensity depend on sun/moon
alignment. The renderer now derives a phase
visibility factor per frame, scales moon
directional light output, and the sky shader uses
that same value to shade crescents and gibbous
shapes procedurally, providing consistent night
lighting as sun–moon directions change.
Extend the dynamic sky moon disk shading to
animate its lit hemisphere. The
shader now warps the local normal toward the sun,
layers time-driven crater detail, and adds a
subtle rim glow so crescents evolve smoothly as
phase visibility changes.
Add an eclipse strength scalar to
WeatherComponent, serialize it, and feed it to the
sky shader so designers can dim the moon disk,
halo, and moonlight on demand.
Add a weather flag and UI toggle that detects when
the Moon moves into Earth's shadow, dims the
disk/light automatically, and serializes the state
with archive version 99. The editor slider can
still override the effect when automation is off,
and Lua bindings expose the new control for
scripts.
Add solar eclipse pipeline including weather
controls, shaders, renderer, and serialization

- extend WeatherComponent, serialization, and Lua
  bindings with sun eclipse automation
- thread eclipse factor through shader interop,
  realistic sky shading, and directional light
  scaling
- expose solar eclipse sliders/toggles in the
  editor and reset defaults when weather is
  missing
- bump archive/component versions and update
  resolver logic to keep moon lighting consistent
Copy link
Copy Markdown
Owner

@turanszkij turanszkij left a comment

Choose a reason for hiding this comment

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

Haven't checked everything, just a quick try and some observations.

  • When I place a terrain, now there are two directional lights, sun and moon. Moon cannot be rotated like the sun
  • Moon can be rotated only inside weather settings, but I think it should be working like the sun. It should be active only if someone puts a directional light, and for example tags it as moon
  • It looks like moon is lit on the other side of where it should be relative to the sun-moon directions
  • I think the moon halo effect is not necessary, since it is not the same shape as the moon it looks out of place. Maybe we can just rely on the bloom effect picking up a bright moon, or it could be also handled by the lensflare system
  • Moon should be optional, just like the sun. Especially moon shadows, we don't want to render them usually, only when there is no sun. Moon parameters on a directional light could work better, because that can be then placed like the sun.

Good work so far!

@@ -1,5 +1,12 @@
This file contains changelog of wi::Archive versions

100: serialized WeatherComponent sun eclipse controls
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

These changes must go into the component versioning now, the versions are declared in wiScene.h in the ComponentManager declarations. Also, make just one version for the pull request, no need to make multiple versions, all new changes can go into the next version.

{
// this should always be only INCREMENTED and only if a new serialization is implemeted somewhere!
static constexpr uint64_t __archiveVersion = 93;
static constexpr uint64_t __archiveVersion = 100;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Same as above, this must go into the WeatherComponent's own versioning instead.

XMVECTOR moon = XMLoadFloat3(&moon_dir);
const float sun_len_sq = XMVectorGetX(XMVector3LengthSq(sun));
const float moon_len_sq = XMVectorGetX(XMVector3LengthSq(moon));
if (!std::isfinite(sun_len_sq) || !std::isfinite(moon_len_sq) || sun_len_sq < 1e-6f || moon_len_sq < 1e-6f)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We should avoid all these std::isfinite calls, instead make it to never be infinite (check that source vector is not zero if you must). Then we also don't need to include cmath.

@socialtwisty
Copy link
Copy Markdown
Contributor

I love that you are adding the moon!
Is there any way to make it more visible? its very faint (texture or no texture), and I don't know how to control the phase:

image

@Anthony-Gaudino
Copy link
Copy Markdown
Author

Hi @socialtwisty

Answering your questions:

You can make the moon more visible by changing it's color:
Screenshot From 2026-02-22 13-38-03
If you change it to white then it will be very visible.


The moon phase is calculated automatically based on the sun position. If you rotate the sun you can have different moon phases. Currently it's not accurate.

@socialtwisty
Copy link
Copy Markdown
Contributor

I found a problem where playing with the rain amount can cause a crash

@Anthony-Gaudino
Copy link
Copy Markdown
Author

I found a problem where playing with the rain amount can cause a crash

@socialtwisty could you please try to replicate the crash in the main branch to check if this is crash is related to this branch or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants