diff --git a/WickedEngine/skyHF.hlsli b/WickedEngine/skyHF.hlsli index 850f07c58..3927751c4 100644 --- a/WickedEngine/skyHF.hlsli +++ b/WickedEngine/skyHF.hlsli @@ -25,38 +25,35 @@ float noise(in float2 p) return dot(n, float3(70.0, 70.0, 70.0)); } -//#define SIMPLE_SKY -float3 GetDynamicSkyColor(in float3 normal, bool sun_enabled = true, bool clouds_enabled = true, bool dark_enabled = false) +// Returns sky color modulated by the sun and clouds +// V : view direction +float3 GetDynamicSkyColor(in float3 V, bool sun_enabled = true, bool clouds_enabled = true, bool dark_enabled = false) { -#ifdef SIMPLE_SKY - const float aboveHorizon = saturate(pow(saturate(normal.y), 0.3f + g_xFrame_Fog.z) / (g_xFrame_Fog.z + 1)); - const float3 sky = lerp(GetHorizonColor(), GetZenithColor(), smoothstep(0, 1, aboveHorizon)); -#else const float3 skyColor = GetZenithColor(); const float3 sunDirection = GetSunDirection(); const float3 sunColor = GetSunColor(); + sun_enabled = sun_enabled && any(sunColor); - const float zenith = normal.y; // how much is above (0: horizon, 1: directly above) + const float zenith = V.y; // how much is above (0: horizon, 1: directly above) const float sunScatter = saturate(sunDirection.y + 0.1f); // how much the sun is directly above. Even if sunis at horizon, we add a constant scattering amount so that light still scatters at horizon - const float atmosphereDensity = 0.7f; // constant of air density (bigger is more obstruction of sun) + const float atmosphereDensity = 0.5 + g_xFrame_Fog.z; // constant of air density, or "fog height" as interpreted here (bigger is more obstruction of sun) const float zenithDensity = atmosphereDensity / pow(max(0.000001f, zenith), 0.75f); const float sunScatterDensity = atmosphereDensity / pow(max(0.000001f, sunScatter), 0.75f); - const float3 skyAbsorption = exp2(skyColor * -zenithDensity) * 2.0f; // gradient on horizon - const float3 sunAbsorption = sunColor * exp2(skyColor * -sunScatterDensity) * 2.0f; // gradient of sun when it's getting below horizon + const float3 aberration = float3(0.39, 0.57, 1.0); // the chromatic aberration effect on the horizon-zenith fade line + const float3 skyAbsorption = saturate(exp2(aberration * -zenithDensity) * 2.0f); // gradient on horizon + const float3 sunAbsorption = sun_enabled ? saturate(sunColor * exp2(aberration * -sunScatterDensity) * 2.0f) : 1; // gradient of sun when it's getting below horizon - const float sunAmount = distance(normal, sunDirection); // sun falloff descreasing from mid point - const float rayleigh = 1.0 + pow(1.0 - saturate(sunAmount), 2.0) * PI * 0.5; + const float sunAmount = distance(V, sunDirection); // sun falloff descreasing from mid point + const float rayleigh = sun_enabled ? 1.0 + pow(1.0 - saturate(sunAmount), 2.0) * PI * 0.5 : 1; const float mie_disk = saturate(1.0 - pow(sunAmount, 0.1)); const float3 mie = mie_disk * mie_disk*(3.0 - 2.0 * mie_disk) * 2.0 * PI * sunAbsorption; - const float3 sun = smoothstep(0.03, 0.026, sunAmount) * 50.0 * skyAbsorption; // sun disc - const float3 horizon = GetHorizonColor() * saturate(1 - skyAbsorption); // horizon color will affect when zenith density decreases + const float3 sun = smoothstep(0.03, 0.026, sunAmount) * sunColor * 50.0 * skyAbsorption; // sun disc - float3 sky = skyColor * zenithDensity * rayleigh; - sky = lerp(sky * skyAbsorption, sky / (sky + 0.5), sunScatter); // when sun goes below horizon, absorb sky color - sky += horizon; // tint horizon color + float3 sky = lerp(GetHorizonColor(), GetZenithColor() * zenithDensity * rayleigh, skyAbsorption); + sky = lerp(sky * skyAbsorption, sky, sunScatter); // when sun goes below horizon, absorb sky color if (sun_enabled) { sky += sun; @@ -67,7 +64,7 @@ float3 GetDynamicSkyColor(in float3 normal, bool sun_enabled = true, bool clouds if (dark_enabled) { - sky = max(pow(saturate(dot(GetSunDirection().xyz, normal)), 64)*GetSunColor().rgb, 0) * skyAbsorption; + sky = max(pow(saturate(dot(GetSunDirection().xyz, V)), 64) * sunColor, 0) * skyAbsorption; } if (clouds_enabled) @@ -79,7 +76,7 @@ float3 GetDynamicSkyColor(in float3 normal, bool sun_enabled = true, bool clouds // Trace a cloud layer plane: const float3 o = g_xCamera_CamPos; - const float3 d = normal; + const float3 d = V; const float3 planeOrigin = float3(0, 1000, 0); const float3 planeNormal = float3(0, -1, 0); const float t = Trace_plane(o, d, planeOrigin, planeNormal); @@ -157,8 +154,6 @@ float3 GetDynamicSkyColor(in float3 normal, bool sun_enabled = true, bool clouds } } -#endif // SIMPLE_SKY - return sky; } diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 11fc09a82..21cd6a565 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -3550,7 +3550,7 @@ void UpdatePerFrameData(float dt, uint32_t layerMask) { renderTime_Prev = renderTime; deltaTime = dt * GetGameSpeed(); - renderTime += dt; + renderTime += deltaTime; GraphicsDevice* device = GetDevice(); Scene& scene = GetScene(); diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index 78f3ad5d9..19e9bedb9 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -906,7 +906,7 @@ namespace wiSceneSystem }; uint32_t _flags = EMPTY; - XMFLOAT3 sunColor = XMFLOAT3(1, 1, 1); + XMFLOAT3 sunColor = XMFLOAT3(0, 0, 0); XMFLOAT3 sunDirection = XMFLOAT3(0, 1, 0); XMFLOAT3 horizon = XMFLOAT3(0.0f, 0.0f, 0.0f); XMFLOAT3 zenith = XMFLOAT3(0.0f, 0.0f, 0.0f); diff --git a/models/lightmap_bake_test.wiscene b/models/lightmap_bake_test.wiscene index c294a4c3e..82f49039d 100644 Binary files a/models/lightmap_bake_test.wiscene and b/models/lightmap_bake_test.wiscene differ