FogIncludeHLSL.hlsl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "SkyInclude.cginc"
  2. TEXTURE2D_X(_EnviroVolumetricFogTex);
  3. float4 _EnviroVolumetricFogTex_TexelSize;
  4. float4 _Screen_TexelSize;
  5. uniform float4 _EnviroFogParameters; //x = rayorigin1, y = falloff1, z = density1, w = height1
  6. uniform float4 _EnviroFogParameters2; //x = rayorigin2, y = falloff2, z = density2, w = height2
  7. uniform float4 _EnviroFogParameters3; //x = maxDensity, y = startDistance, z = , w = sky blend
  8. uniform float4 _EnviroFogColor; //Fog color
  9. uniform float4 _EnviroDirLightColor;
  10. #if defined(SHADER_API_D3D11) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN)
  11. struct EllipsZones
  12. {
  13. float3 pos;
  14. float radius;
  15. float3 axis;
  16. float stretch;
  17. float density;
  18. float feather;
  19. float padding1;
  20. float padding2;
  21. };
  22. StructuredBuffer<EllipsZones> _EllipsZones;
  23. float _EllipsZonesCount;
  24. #endif
  25. int ihash(int n)
  26. {
  27. n = (n<<13)^n;
  28. return (n*(n*n*15731+789221)+1376312589) & 2147483647;
  29. }
  30. float frand(int n)
  31. {
  32. return ihash(n) / 2147483647.0;
  33. }
  34. float2 cellNoise(int2 p)
  35. {
  36. int i = p.y*256 + p.x;
  37. return float2(frand(i), frand(i + 57)) - 0.5;//*2.0-1.0;
  38. }
  39. float Pow2(float x)
  40. {
  41. return x * x;
  42. }
  43. // Calculate the line integral of the ray from the camera to the receiver position through the fog density function
  44. // The exponential fog density function is d = GlobalDensity * exp(-HeightFalloff * y)
  45. float CalculateLineIntegralShared(float FogHeightFalloff, float RayDirectionY, float RayOriginTerms)
  46. {
  47. float Falloff = max(-127.0f, FogHeightFalloff * RayDirectionY); // if it's lower than -127.0, then exp2() goes crazy in OpenGL's GLSL.
  48. float LineIntegral = (1.0f - exp2(-Falloff)) / Falloff;
  49. float LineIntegralTaylor = log(2.0) - (0.5 * Pow2(log(2.0))) * Falloff; // Taylor expansion around 0
  50. return RayOriginTerms * (abs(Falloff) > 0.01f ? LineIntegral : LineIntegralTaylor);
  51. }
  52. #if defined(SHADER_API_D3D11) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN)
  53. void FogZones(float3 pos, inout float density)
  54. {
  55. for (int i = 0; i < _EllipsZonesCount; i++)
  56. {
  57. float3 dir = _EllipsZones[i].pos - pos;
  58. float3 axis = _EllipsZones[i].axis;
  59. float3 dirAlongAxis = dot(dir, axis) * axis;
  60. //float scrollNoise = ScrollNoise(dir, _EllipsZones[i].noiseSpeed, _EllipsZones[i].noiseScale, axis, _EllipsZones[i].noiseAmount);
  61. dir = dir + dirAlongAxis * _EllipsZones[i].stretch;
  62. float distsq = dot(dir, dir);
  63. float radius = _EllipsZones[i].radius;
  64. float feather = _EllipsZones[i].feather;
  65. // float feather = 0.3;
  66. feather = (1.0 - smoothstep (radius * feather, radius, distsq));
  67. float contribution = feather * _EllipsZones[i].density;
  68. density = density + contribution;
  69. }
  70. }
  71. #endif
  72. half4 GetExponentialHeightFog(float3 wPos, float linearDepth)
  73. {
  74. const half MinFogOpacity = _EnviroFogParameters3.x;
  75. //Receiver
  76. float3 CameraToReceiver = wPos - _WorldSpaceCameraPos.xyz;
  77. float3 viewDirection = CameraToReceiver;
  78. float viewLength = length(viewDirection);
  79. viewDirection /= viewLength;
  80. float CameraToReceiverLengthSqr = dot(CameraToReceiver, CameraToReceiver);
  81. float CameraToReceiverLengthInv = rsqrt(CameraToReceiverLengthSqr);
  82. float CameraToReceiverLength = CameraToReceiverLengthSqr * CameraToReceiverLengthInv;
  83. half3 CameraToReceiverNormalized = CameraToReceiver * CameraToReceiverLengthInv;
  84. float RayOriginTerms = _EnviroFogParameters.x;
  85. float RayOriginTermsSecond = _EnviroFogParameters2.x;
  86. float RayLength = CameraToReceiverLength;
  87. float RayDirectionY = CameraToReceiver.y;
  88. // Factor in StartDistance
  89. float ExcludeDistance = _EnviroFogParameters3.y;
  90. if (ExcludeDistance > 0)
  91. {
  92. float ExcludeIntersectionTime = ExcludeDistance * CameraToReceiverLengthInv;
  93. float CameraToExclusionIntersectionY = ExcludeIntersectionTime * CameraToReceiver.y;
  94. float ExclusionIntersectionY = _WorldSpaceCameraPos.y + CameraToExclusionIntersectionY;
  95. float ExclusionIntersectionToReceiverY = CameraToReceiver.y - CameraToExclusionIntersectionY;
  96. RayLength = (1.0f - ExcludeIntersectionTime) * CameraToReceiverLength;
  97. RayDirectionY = ExclusionIntersectionToReceiverY;
  98. float Exponent = max(-127.0f, _EnviroFogParameters.y * (ExclusionIntersectionY - _EnviroFogParameters.w));
  99. RayOriginTerms = _EnviroFogParameters.z * exp2(-Exponent);
  100. float ExponentSecond = max(-127.0f, _EnviroFogParameters2.y * (ExclusionIntersectionY - _EnviroFogParameters2.w));
  101. RayOriginTermsSecond = _EnviroFogParameters2.z * exp2(-ExponentSecond);
  102. }
  103. // Calculate fog amount of both layers
  104. float fogAmount = (CalculateLineIntegralShared(_EnviroFogParameters.y, RayDirectionY, RayOriginTerms) + CalculateLineIntegralShared(_EnviroFogParameters2.y, RayDirectionY, RayOriginTermsSecond))* RayLength;
  105. //Fog Zones
  106. #if defined(SHADER_API_D3D11) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN)
  107. FogZones(wPos,fogAmount);
  108. #endif
  109. // Calculate the amount of light that made it through the fog using the transmission equation
  110. float fogfactor = max(saturate(exp2(-fogAmount)), MinFogOpacity);
  111. // Color
  112. float4 sky = GetSkyColor(viewDirection,0.005f);
  113. float3 inscatterColor = lerp(_EnviroFogColor.rgb,sky.rgb,_EnviroFogParameters3.w);
  114. float3 fogColor = inscatterColor * (1 - fogfactor);
  115. return float4(fogColor, fogfactor);
  116. }
  117. float3 ApplyVolumetricLights(float4 fogColor, float3 sceneColor, float2 uv)
  118. {
  119. float4 volumeLightsSample = LOAD_TEXTURE2D_X(_EnviroVolumetricFogTex, uv);
  120. //uvs += cellNoise(uvs.xy * _Screen_TexelSize.zw) * _VolumeScatter_TexelSize.xy * 0.8;
  121. float3 volumeLightsDirectional = volumeLightsSample.a * _EnviroDirLightColor.rgb;
  122. float3 volumeLights = volumeLightsSample.rgb;
  123. return (sceneColor.rgb * fogColor.a + fogColor.rgb * max(volumeLightsDirectional,0.75)) + volumeLights;
  124. }
  125. float3 ApplyFogAndVolumetricLights(float3 sceneColor, float2 uv, float3 wPos, float linearDepth)
  126. {
  127. float4 fog = GetExponentialHeightFog(wPos,linearDepth);
  128. return ApplyVolumetricLights(fog,sceneColor,uv);
  129. }