-
Notifications
You must be signed in to change notification settings - Fork 137
feat: exponential height fog #1708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
391afb3
add exponential height fog
jiayev 544918b
directional light inscattering
jiayev 5f06d6e
more scattering stuff
jiayev 0d57ea8
dynamic cubemaps for inscattering
jiayev 87a3334
Merge branch 'dev' into height-fog
jiayev 72efc16
adapt to weather
jiayev 83c187c
Merge branch 'dev' into height-fog
jiayev 9fd8f06
Merge branch 'dev' into height-fog
jiayev 5fe4a69
Merge branch 'dev' into height-fog
jiayev 69eb503
adapt to weather
jiayev 4b40c6f
add sunlight fadeout in fog
jiayev 8073566
style: 🎨 apply pre-commit.ci formatting
pre-commit-ci[bot] 3102cb5
Merge branch 'dev' into height-fog
jiayev 6777eb1
try fix water, idk
jiayev be7632a
fix compile error
jiayev 798496c
fix some blending
jiayev d1ee6af
add alpha back
jiayev 68d74a1
Merge branch 'dev' into height-fog
jiayev e27754d
Merge branch 'dev' into height-fog
jiayev 7b72d4d
Merge branch 'dev' into height-fog
jiayev d56ee70
fix(water): remove unnecessary else statement in fog calculation
jiayev 5a6b74a
fix fog blending with new water
jiayev e535d17
Merge branch 'dev' into height-fog
jiayev d0832db
Merge branch 'dev' into height-fog
jiayev 1e858fb
Merge branch 'dev' into height-fog
jiayev 7a91928
Merge branch 'dev' into height-fog
jiayev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
features/Exponential Height Fog/Shaders/ExponentialHeightFog/ExponentialHeightFog.hlsli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #ifndef __EXPONENTIAL_HEIGHT_FOG_HLSLI__ | ||
| #define __EXPONENTIAL_HEIGHT_FOG_HLSLI__ | ||
|
|
||
| #include "Common/SharedData.hlsli" | ||
|
|
||
| # if defined(DYNAMIC_CUBEMAPS) | ||
| # include "DynamicCubemaps/DynamicCubemaps.hlsli" | ||
| # endif | ||
|
|
||
| namespace ExponentialHeightFog | ||
| { | ||
| float4 GetExponentialHeightFog(float3 positionWS, float3 cameraWS, float3 fogColor) | ||
| { | ||
| float fogHeightFalloff = SharedData::exponentialHeightFogSettings.fogHeightFalloff * 0.001f; | ||
| float fogDensity = SharedData::exponentialHeightFogSettings.fogDensity * 0.001f; | ||
| if (fogDensity <= 0.0f) | ||
| { | ||
| return 0.0f; | ||
| } | ||
| float3 viewToPos = positionWS; | ||
| float viewToPosLength = length(viewToPos); | ||
| float viewToPosLengthInv = rcp(viewToPosLength); | ||
|
|
||
| float rayOriginTerms = fogDensity * exp2(-fogHeightFalloff * max(cameraWS.z - SharedData::exponentialHeightFogSettings.fogHeight, 0)); | ||
| float rayLength = viewToPosLength; | ||
| float rayDirectionZ = viewToPos.z; | ||
|
|
||
| if (SharedData::exponentialHeightFogSettings.startDistance > 0) | ||
| { | ||
| float excludeIntersectionTime = SharedData::exponentialHeightFogSettings.startDistance * viewToPosLengthInv; | ||
| float cameraToExclusionIntersectionZ = excludeIntersectionTime * viewToPos.z; | ||
| float exclusionIntersectionZ = cameraWS.z + cameraToExclusionIntersectionZ; | ||
| rayLength = (1.0f - excludeIntersectionTime) * viewToPosLength; | ||
| rayDirectionZ = viewToPos.z - cameraToExclusionIntersectionZ; | ||
| float exponent = fogHeightFalloff * max(exclusionIntersectionZ - SharedData::exponentialHeightFogSettings.fogHeight, 0); | ||
| rayOriginTerms = fogDensity * exp2(-exponent); | ||
| } | ||
|
|
||
| float falloff = fogHeightFalloff * rayDirectionZ; | ||
| float lineIntegral = (1.0f - exp2(-falloff)) / falloff; | ||
| float lineIntegralTaylor = 0.69314718056f - 0.24022650695f * falloff; // log(2) - (0.5 * (log(2)^2)) * falloff | ||
| float exponentialHeightLineIntegralCalc = rayOriginTerms * (abs(falloff) > 0.01f ? lineIntegral : lineIntegralTaylor); | ||
| float exponentialHeightLineIntegral = exponentialHeightLineIntegralCalc * rayLength; | ||
|
|
||
| float expFogFactor = saturate(exp2(-exponentialHeightLineIntegral)); | ||
|
|
||
| # if defined(DYNAMIC_CUBEMAPS) | ||
| if (SharedData::exponentialHeightFogSettings.useDynamicCubemaps > 0) | ||
| { | ||
| float3 tintColor = lerp(fogColor, SharedData::exponentialHeightFogSettings.inscatteringTint.xyz, SharedData::exponentialHeightFogSettings.inscatteringTint.w); | ||
| float3 cubemapColor = DynamicCubemaps::EnvReflectionsTexture.SampleLevel(SampColorSampler, normalize(lerp(positionWS, float3(0,0,1), saturate((SharedData::exponentialHeightFogSettings.cubemapMipLevel + 1) / 8))), SharedData::exponentialHeightFogSettings.cubemapMipLevel).xyz; | ||
| fogColor = tintColor * cubemapColor * (1.0f - expFogFactor); | ||
| } | ||
| # endif | ||
|
|
||
| float3 directionalInscattering = 0; | ||
|
|
||
| // Calculate directional light inscattering | ||
| if (SharedData::exponentialHeightFogSettings.directionalInscatteringMultiplier > 0) | ||
| { | ||
| float3 directionalLightInscattering = SharedData::DirLightColor.xyz * pow(saturate(dot(normalize(positionWS), SharedData::DirLightDirection.xyz)), SharedData::exponentialHeightFogSettings.directionalInscatteringExponent) / (2 * Math::TAU); | ||
| float dirExponentialHeightLineIntegral = exponentialHeightLineIntegralCalc * max(rayLength - SharedData::exponentialHeightFogSettings.startDistance, 0); | ||
| float dirExpFogFactor = saturate(exp2(-dirExponentialHeightLineIntegral)); | ||
| directionalInscattering = directionalLightInscattering * (1 - dirExpFogFactor) * SharedData::exponentialHeightFogSettings.directionalInscatteringMultiplier; | ||
| } | ||
|
|
||
| fogColor += directionalInscattering; | ||
| return float4(fogColor, 1.0f - expFogFactor); | ||
| } | ||
|
|
||
| float GetSunlightFogAttenuation(float3 positionWS, float3 cameraWS) | ||
| { | ||
| float fogHeightFalloff = SharedData::exponentialHeightFogSettings.fogHeightFalloff * 0.001f; | ||
| float fogDensity = SharedData::exponentialHeightFogSettings.fogDensity * 0.001f; | ||
| if (fogDensity <= 0.0f) | ||
| { | ||
| return 1.0f; | ||
| } | ||
|
|
||
| float exponent = fogHeightFalloff * max(positionWS.z + cameraWS.z - SharedData::exponentialHeightFogSettings.fogHeight, 0.0f); | ||
| float localDensity = fogDensity * exp2(-exponent); | ||
|
|
||
| float3 lightDir = SharedData::DirLightDirection.xyz; | ||
| float lightDirZ = lightDir.z; | ||
|
|
||
| float exponentialHeightLineIntegral = 0.0f; | ||
|
|
||
| // Integral = Density * (1 - exp2(-slope * inf)) / slope | ||
| if (lightDirZ > 0.001f) | ||
| { | ||
| float slope = max(fogHeightFalloff * lightDirZ, 1e-8f); | ||
| exponentialHeightLineIntegral = localDensity / slope; | ||
| } | ||
| else | ||
| { | ||
| return 0.0f; | ||
| } | ||
|
|
||
| return saturate(exp2(-exponentialHeightLineIntegral)); | ||
| } | ||
| } | ||
| #endif | ||
2 changes: 2 additions & 0 deletions
2
features/Exponential Height Fog/Shaders/Features/ExponentialHeightFog.ini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [Info] | ||
| Version = 1-0-0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.