From 92a32c15f717c2cfe879a1ea62bc180cf219c0d1 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Wed, 11 Sep 2019 23:48:26 +0100 Subject: [PATCH] dynamic sky atmosphere updates --- WickedEngine/skyHF.hlsli | 37 +++++++++++++----------------- WickedEngine/wiRenderer.cpp | 2 +- WickedEngine/wiSceneSystem.h | 2 +- models/lightmap_bake_test.wiscene | Bin 126585 -> 126930 bytes 4 files changed, 18 insertions(+), 23 deletions(-) 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 c294a4c3eb17d96cbf81e321009ad9c6f0ee8819..82f49039dcdfe83b3ae7237d580e2fcb98f4e446 100644 GIT binary patch delta 937 zcmex)hyBuhc1GEaj5iqTGcH`RW&i?z$NwQ9nseDpZxFpM!`===tL)DV1JQk7Kk9?% zq#qovAo`V7EE|aaegBshh(2}9TMi#Ac0G9f#s7^nS5+uE`iw$vk7K0x{1gJY<|ns!&py=2_UDig9soX z)11jaSXNFpVB?&=?=2(m^oF;L>XR+mxF@e+?ZptCfg!>@`3@Vpx=eO7btb~Qi} z&NY~!#R4*NGG{bGxY;mzyJ0k=MIQ^umg%`K86k2iu%rV^DzKyhOA4TA(~F`ytd delta 838 zcmca~pZ(_@c1FpKj5iqT?TlWUGXR0);v*3tI^N}yJBa?##bN}ai(-SKK=cjqQ_3LP zM9SC`MDIC~#s#9++8$H`(Q{M^bV2m|gQ6-RnvFStA4L0XzL^A~H~F=DfoKLPM;#EQ z@%dOikm6-1X|Zqw3AF6Vs-B$6?!9HVhEXNELSUAJN6&8-La07+M<}l6{4i=Eg$@c^mCd;vN zY~~ZvU}XX6nQSEv5y=vlkYWMpp4?`ruz8)~0xdz984y=9Ff>eF$SMrd-rN+ky(xy# zqK^e+{PcaV7!_1t5e$n^SOmf%3=~1rxn48Muz*xf*Luz9!U9q|-RljI1yT(W1F1)s zMW_Rt0@Q0`LQWt~_j=2?g)E(uH?qo1_j||ap#w@!pa5ZnhKglL&7l{f?uSI&V-Fqg zPIlrc?QtrLX>fqwVAato+mY-ZN^5&zd>Y4usF1IRnH#wjc~NWxDTs###<1 J!D3;MkpO1e;1d7<