Align sun placement and shadow direction to active lighting

- drive shadow light direction from live LightingManager directionalDir
- normalize shadow light to downward-facing convention with grazing-angle guard
- make celestial/sky sun placement robust to directionalDir convention mismatches
- keep visible sun above horizon while preserving shadow alignment
This commit is contained in:
Kelsi 2026-02-21 03:21:08 -08:00
parent 566e7138f3
commit 8156942b66
3 changed files with 43 additions and 4 deletions

View file

@ -211,7 +211,13 @@ void Celestial::renderSun(const Camera& camera, float timeOfDay,
celestialShader->use();
glm::vec3 dir = sunDir ? glm::normalize(*sunDir) : glm::vec3(0.0f, 0.0f, 1.0f);
// Prefer opposite of light-ray direction (sun->world), but guard against
// profile/convention mismatches that can place the sun below the horizon.
glm::vec3 lightDir = sunDir ? glm::normalize(*sunDir) : glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 dir = -lightDir;
if (dir.z < 0.0f) {
dir = lightDir;
}
// Place sun on sky sphere at fixed distance
const float sunDistance = 800.0f;

View file

@ -3599,8 +3599,24 @@ glm::mat4 Renderer::computeLightSpaceMatrix() {
constexpr float kShadowNearPlane = 1.0f;
constexpr float kShadowFarPlane = 600.0f;
// Fixed sun direction matching current world lighting setup.
// Use active lighting direction so shadow projection matches sun/celestial.
glm::vec3 sunDir = glm::normalize(glm::vec3(-0.3f, -0.7f, -0.6f));
if (lightingManager) {
const auto& lighting = lightingManager->getLightingParams();
if (glm::length(lighting.directionalDir) > 0.001f) {
sunDir = glm::normalize(lighting.directionalDir);
}
}
// Shadow camera expects light rays pointing downward in render space (Z up).
// Some profiles/opcode paths provide the opposite convention; normalize here.
if (sunDir.z > 0.0f) {
sunDir = -sunDir;
}
// Keep a minimum downward component so the frustum doesn't collapse at grazing angles.
if (sunDir.z > -0.08f) {
sunDir.z = -0.08f;
sunDir = glm::normalize(sunDir);
}
// Keep a stable shadow focus center and move it smoothly toward the player
// to avoid visible shadow "state jumps" during movement.
@ -3738,6 +3754,19 @@ void Renderer::renderShadowPass() {
// directly by calling renderShadow with the light view/proj split.
// For simplicity, compute the split:
glm::vec3 sunDir = glm::normalize(glm::vec3(-0.3f, -0.7f, -0.6f));
if (lightingManager) {
const auto& lighting = lightingManager->getLightingParams();
if (glm::length(lighting.directionalDir) > 0.001f) {
sunDir = glm::normalize(lighting.directionalDir);
}
}
if (sunDir.z > 0.0f) {
sunDir = -sunDir;
}
if (sunDir.z > -0.08f) {
sunDir.z = -0.08f;
sunDir = glm::normalize(sunDir);
}
glm::vec3 center = shadowCenterInitialized ? shadowCenter : characterPosition;
float halfExtent = kShadowHalfExtent;
glm::vec3 up(0.0f, 0.0f, 1.0f);

View file

@ -148,9 +148,13 @@ void SkySystem::render(const Camera& camera, const SkyParams& params) {
glm::vec3 SkySystem::getSunPosition(const SkyParams& params) const {
glm::vec3 dir = glm::normalize(params.directionalDir);
if (glm::length(dir) < 0.0001f) {
dir = glm::vec3(0.0f, 0.0f, 1.0f);
dir = glm::vec3(0.0f, 0.0f, -1.0f);
}
glm::vec3 pos = dir * 800.0f;
glm::vec3 sunDir = -dir;
if (sunDir.z < 0.0f) {
sunDir = dir;
}
glm::vec3 pos = sunDir * 800.0f;
return pos;
}