EnviroVolumetricsURP.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. Shader "Hidden/VolumetricsURP"
  2. {
  3. Properties
  4. {
  5. //_MainTex ("Texture", any) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Off ZWrite Off ZTest Always
  11. Pass
  12. {
  13. HLSLPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  17. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  18. float4x4 _LeftWorldFromView;
  19. float4x4 _RightWorldFromView;
  20. float4x4 _LeftViewFromScreen;
  21. float4x4 _RightViewFromScreen;
  22. sampler2D _DitherTexture;
  23. //UNITY_DECLARE_SCREENSPACE_TEXTURE(_CloudsTex);
  24. TEXTURE2D_X_FLOAT(_CameraDepthTexture);
  25. SAMPLER(sampler_CameraDepthTexture);
  26. uniform sampler3D _NoiseTexture;
  27. uniform int _Steps;
  28. uniform float3 _CameraPosition;
  29. uniform float4 _VolumetricLight;
  30. uniform float4 _HeightFog;
  31. uniform float4 _HeightParams;
  32. uniform float4 _NoiseData;
  33. uniform float3 _WindDirection;
  34. uniform float4 _MieG;
  35. uniform float _MaxRayLength;
  36. uniform float4 _AmbientColor;
  37. uniform float3 _DirLightDir;
  38. uniform float4 _Randomness;
  39. struct PointLight
  40. {
  41. float3 pos;
  42. float range;
  43. float3 color;
  44. float padding;
  45. };
  46. StructuredBuffer<PointLight> _PointLights;
  47. float _PointLightsCount;
  48. struct SpotLight
  49. {
  50. float3 pos;
  51. float range;
  52. float3 color;
  53. float3 lightDirection;
  54. float lightCosHalfAngle;
  55. //float2 angularFalloffParameters;
  56. //float2 distanceFalloffParameters;
  57. float padding;
  58. };
  59. StructuredBuffer<SpotLight> _SpotLights;
  60. float _SpotLightsCount;
  61. struct v2f
  62. {
  63. float2 uv : TEXCOORD0;
  64. float4 position : SV_POSITION;
  65. UNITY_VERTEX_INPUT_INSTANCE_ID
  66. UNITY_VERTEX_OUTPUT_STEREO
  67. };
  68. struct appdata
  69. {
  70. float4 vertex : POSITION;
  71. float2 texcoord : TEXCOORD0;
  72. UNITY_VERTEX_INPUT_INSTANCE_ID
  73. };
  74. v2f vert (appdata v)
  75. {
  76. v2f o = (v2f)0;
  77. UNITY_SETUP_INSTANCE_ID(v);
  78. //UNITY_TRANSFER_INSTANCE_ID(v, o);
  79. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  80. //VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
  81. o.position = float4(v.vertex.xyz,1);
  82. #if UNITY_UV_STARTS_AT_TOP
  83. o.position.y *= -1;
  84. #endif
  85. //o.position = v.vertex * float4(2,2,1,1) + float4(-1,-1,0,0);
  86. o.uv = v.texcoord;
  87. return o;
  88. }
  89. float anisotropy(float costheta)
  90. {
  91. float g = _MieG;
  92. float gsq = g*g;
  93. float denom = 1 + gsq - 2.0 * g * costheta;
  94. denom = denom * denom * denom;
  95. denom = sqrt(max(0, denom));
  96. return (1 - gsq) / denom;
  97. }
  98. float anisotropyPointSpot(float costheta)
  99. {
  100. float g = 0.8;
  101. float gsq = g*g;
  102. float denom = 1 + gsq - 2.0 * g * costheta;
  103. denom = denom * denom * denom;
  104. denom = sqrt(max(0, denom));
  105. return (1 - gsq) / denom;
  106. }
  107. float Attenuation(float distNorm)
  108. {
  109. return 1.0 / (1.0 + 25.0 * distNorm);
  110. }
  111. float DirectionalLight(float3 wpos)
  112. {
  113. float atten = 1.0f;
  114. half cascadeIndex = ComputeCascadeIndex(wpos);
  115. bool inside = dot(cascadeIndex, 1) < 4;
  116. float4 coords = mul(_MainLightWorldToShadow[cascadeIndex], float4(wpos, 1.0));
  117. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
  118. half4 shadowParams = GetMainLightShadowParams();
  119. float shadows = SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), coords, shadowSamplingData, shadowParams, false).r;
  120. atten = inside ? shadows : 1.0f;
  121. if(shadows > 0.0f)
  122. atten = 1.0f;
  123. return atten;
  124. }
  125. float3 PointLights(float3 pos)
  126. {
  127. float3 color = 0;
  128. for (int i = 0; i < _PointLightsCount; i++)
  129. {
  130. float3 posToLight = _PointLights[i].pos - pos;
  131. float distNorm = dot(posToLight, posToLight) * _PointLights[i].range;
  132. float att = Attenuation(distNorm);
  133. //#if ANISOTROPY
  134. float3 cameraToPos = normalize(pos - _WorldSpaceCameraPos.xyz);
  135. float costheta = dot(cameraToPos, normalize(posToLight));
  136. att *= anisotropyPointSpot(costheta);
  137. //#endif
  138. color += _PointLights[i].color * att;
  139. }
  140. return color;
  141. }
  142. float3 SpotLights(float3 pos)
  143. {
  144. float3 color = 0;
  145. for (int i = 0; i < _SpotLightsCount; i++)
  146. {
  147. float3 posToLight = _SpotLights[i].pos - pos;
  148. float distNorm = dot(posToLight, posToLight) * _SpotLights[i].range;
  149. float att = Attenuation(distNorm);
  150. half3 lightVector = normalize(pos - _SpotLights[i].pos);
  151. half cosAngle = dot(_SpotLights[i].lightDirection.xyz, lightVector);
  152. half angleAttenuation = 1;
  153. angleAttenuation = smoothstep(_SpotLights[i].lightCosHalfAngle, lerp(1, _SpotLights[i].lightCosHalfAngle, 0.8f), cosAngle);
  154. angleAttenuation = pow(angleAttenuation, 2.0f);
  155. att *= angleAttenuation;
  156. #if ANISOTROPY
  157. float3 cameraToPos = normalize(pos - _CameraPos.xyz);
  158. float costheta = dot(cameraToPos, normalize(posToLight));
  159. att *= anisotropyPointSpot(costheta);
  160. #endif
  161. color += _SpotLights[i].color * att;
  162. }
  163. return color;
  164. }
  165. //-----------------------------------------------------------------------------------------
  166. // GetDensity
  167. //-----------------------------------------------------------------------------------------
  168. float GetDensity(float3 wpos, inout float density, float depth, float3 rayDir)
  169. {
  170. density = 1.0f;
  171. // #ifdef NOISE
  172. // float4 noise = tex3D(_NoiseTexture, frac(wpos * _NoiseData.x + float3(_Time.y * _WindDirection.x, 0, _Time.y * _WindDirection.y)));
  173. // float noiseFbm = (noise.g * 0.625) + (noise.b * 0.25) + (noise.a * 0.125);
  174. // noiseFbm = saturate(noiseFbm - _NoiseData.y);
  175. // density *= saturate(noiseFbm);
  176. // #endif
  177. return density;
  178. }
  179. float2 squareUV(float2 uv)
  180. {
  181. float width = _ScreenParams.x;
  182. float height =_ScreenParams.y;
  183. float scale = 1000;
  184. float x = uv.x * width;
  185. float y = uv.y * height;
  186. return float2 (x/scale, y/scale);
  187. }
  188. float4 RayMarch(float2 uv,float2 screenPos, float3 rayStart, float3 rayDir, float rayLength, float linearDepth)
  189. {
  190. float2 interleavedPos = (fmod(floor(screenPos.xy), 8.0));
  191. #if UNITY_SINGLE_PASS_STEREO
  192. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
  193. interleavedPos = (interleavedPos - scaleOffset.zw) / scaleOffset.xy;
  194. #endif
  195. float offset = tex2D(_DitherTexture, interleavedPos / 8.0 + float2(0.5 / 8.0, 0.5 / 8.0)).w;
  196. //float offset = tex2D(_DitherTexture, squareUV(uv + _Randomness.xy)).x;
  197. int stepCount = _Steps;
  198. float stepSize = rayLength / stepCount;
  199. float3 step = rayDir * stepSize;
  200. float3 currentPositionDithered = rayStart + step * offset;
  201. float3 currentPosition = rayStart + step;
  202. float4 color = float4(0.0,0.0,0.0,0);
  203. float cosAngle;
  204. float extinction = 0;
  205. float transmitance = 0;
  206. float ambient = 0;
  207. cosAngle = dot(_DirLightDir.xyz, -rayDir);
  208. float ani = anisotropy(cosAngle);
  209. float4 lightsColor;
  210. [loop]
  211. for (int i = 0; i < stepCount; ++i)
  212. {
  213. float density = GetDensity(currentPosition, density, linearDepth, rayDir);
  214. float atten = DirectionalLight(currentPositionDithered);
  215. //Cloud Shadows
  216. //float cloudShadows = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_CloudsTex,uv).b;
  217. //atten *= (1-cloudShadows);
  218. float scattering = _VolumetricLight.x * density;
  219. extinction += _VolumetricLight.y * density;
  220. transmitance += atten * scattering * exp(-extinction);
  221. lightsColor.rgb += PointLights(currentPositionDithered) * density;
  222. lightsColor.rgb += SpotLights(currentPositionDithered) * density;
  223. currentPosition += step;
  224. currentPositionDithered += step;
  225. }
  226. color.a = transmitance * ani;
  227. color.rgb += lightsColor.rgb;
  228. color = max(0, color);
  229. return color;
  230. }
  231. float4 frag (v2f i) : SV_Target
  232. {
  233. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  234. float2 uv = i.uv.xy;
  235. float depth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(uv)).r;
  236. float linearDepth = Linear01Depth(depth,_ZBufferParams);
  237. float4x4 proj, eyeToWorld;
  238. if (unity_StereoEyeIndex == 0)
  239. {
  240. proj = _LeftViewFromScreen;
  241. eyeToWorld = _LeftWorldFromView;
  242. }
  243. else
  244. {
  245. proj = _RightViewFromScreen;
  246. eyeToWorld = _RightWorldFromView;
  247. }
  248. //bit of matrix math to take the screen space coord (u,v,depth) and transform to world space
  249. float2 uvClip = i.uv * 2.0 - 1.0;
  250. float clipDepth = depth; // Fix for OpenGl Core thanks to Lars Bertram
  251. clipDepth = (UNITY_NEAR_CLIP_VALUE < 0) ? clipDepth * 2 - 1 : clipDepth;
  252. float4 clipPos = float4(uvClip, clipDepth, 1.0);
  253. float4 viewPos = mul(proj, clipPos); // inverse projection by clip position
  254. viewPos /= viewPos.w; // perspective division
  255. float3 wpos = mul(eyeToWorld, viewPos).xyz;
  256. float3 rayStart = _WorldSpaceCameraPos;
  257. float3 rayDir = wpos - _WorldSpaceCameraPos;
  258. //rayDir *= linearDepth;
  259. float rayLength = length(rayDir);
  260. rayDir /= rayLength;
  261. rayLength = min(rayLength, _MaxRayLength);
  262. float4 color = RayMarch(uv, i.position.xy, rayStart, rayDir, rayLength, linearDepth);
  263. return color;
  264. }
  265. ENDHLSL
  266. }
  267. }
  268. }