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

@ -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);