From 46dbfce7fc59cd93566251b6cc7a2d8521c991bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Mon, 25 Jul 2022 19:11:29 +0200 Subject: [PATCH] Cycles: Nishita Sky: Fix sun disk imprecision for large elevation The issue was introduced by rBad5e3d30a2d2 which made possible to use unbounded elevation angle. In order to not touch the shading code, we just remap the value to the expected range the shading code expects. This means that elevation angles above +/-PI/2 effectively flip the sun rotation angle. --- intern/cycles/blender/shader.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/intern/cycles/blender/shader.cpp b/intern/cycles/blender/shader.cpp index 81a64457c88..4218a9a8a68 100644 --- a/intern/cycles/blender/shader.cpp +++ b/intern/cycles/blender/shader.cpp @@ -928,8 +928,22 @@ static ShaderNode *add_node(Scene *scene, sky->set_sun_disc(b_sky_node.sun_disc()); sky->set_sun_size(b_sky_node.sun_size()); sky->set_sun_intensity(b_sky_node.sun_intensity()); - sky->set_sun_elevation(b_sky_node.sun_elevation()); - sky->set_sun_rotation(b_sky_node.sun_rotation()); + /* Patch sun position to be able to animate daylight cycle while keeping the shading code + * simple. */ + float sun_rotation = b_sky_node.sun_rotation(); + /* Wrap into [-2PI..2PI] range. */ + float sun_elevation = fmodf(b_sky_node.sun_elevation(), M_2PI_F); + /* Wrap into [-PI..PI] range. */ + if (fabsf(sun_elevation) >= M_PI_F) { + sun_elevation -= copysignf(2.0f, sun_elevation) * M_PI_F; + } + /* Wrap into [-PI/2..PI/2] range while keeping the same absolute position. */ + if (sun_elevation >= M_PI_2_F || sun_elevation <= -M_PI_2_F) { + sun_elevation = copysignf(M_PI_F, sun_elevation) - sun_elevation; + sun_rotation += M_PI_F; + } + sky->set_sun_elevation(sun_elevation); + sky->set_sun_rotation(sun_rotation); sky->set_altitude(b_sky_node.altitude()); sky->set_air_density(b_sky_node.air_density()); sky->set_dust_density(b_sky_node.dust_density());