EnviroSkyboxHDRP.shader 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. Shader "Enviro/HDRP/Sky"
  2. {
  3. //Properties
  4. //{
  5. // _MoonTex("Moon Tex", 2D) = "black" {}
  6. // _MoonGlowTex("Moon Glow Tex", 2D) = "black" {}
  7. // _SunTex("Sun Tex", 2D) = "black" {}
  8. // _StarsTex ("Stars Tex", Cube) = "black" {}
  9. // _GalaxyTex ("Galaxy Tex", Cube) = "black" {}
  10. //}
  11. HLSLINCLUDE
  12. #pragma editor_sync_compilation
  13. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  14. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  15. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl"
  16. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
  17. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyUtils.hlsl"
  18. #include "../Includes/SkyIncludeHLSL.hlsl"
  19. uniform float4 _SkyMoonParameters;
  20. uniform float4 _SkySunParameters;
  21. uniform sampler2D _MoonTex;
  22. //uniform sampler2D _MoonGlowTex;
  23. uniform sampler2D _SunTex;
  24. uniform float4 _MoonColor;
  25. uniform float _MoonGlowIntensity;
  26. uniform float _StarIntensity;
  27. uniform float _GalaxyIntensity;
  28. uniform float _CirrusClouds;
  29. uniform float _FlatClouds;
  30. uniform float _Aurora;
  31. uniform samplerCUBE _StarsTex;
  32. uniform samplerCUBE _GalaxyTex;
  33. uniform float4x4 _StarsMatrix;
  34. uniform float4 _AmbientColorTintHDRP;
  35. uniform float _EnviroSkyIntensity;
  36. struct VertexInput
  37. {
  38. uint vertexID : SV_VertexID;
  39. float3 texcoord : TEXCOORD0;
  40. float3 worldPos : TEXCOORD1;
  41. UNITY_VERTEX_INPUT_INSTANCE_ID
  42. };
  43. struct v2f
  44. {
  45. float4 position : SV_POSITION;
  46. float3 texcoord : TEXCOORD0;
  47. float3 worldPos : TEXCOORD1;
  48. UNITY_VERTEX_OUTPUT_STEREO
  49. };
  50. v2f vert(VertexInput v)
  51. {
  52. v2f o;
  53. UNITY_SETUP_INSTANCE_ID(v);
  54. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  55. o.position = GetFullScreenTriangleVertexPosition(v.vertexID, UNITY_RAW_FAR_CLIP_VALUE);
  56. o.texcoord = v.texcoord;
  57. //o.worldPos = mul(unity_ObjectToWorld, v.vertex ).xyz;
  58. return o;
  59. }
  60. float MoonPhaseFactor(float2 uv, float phase)
  61. {
  62. float alpha = 1.0;
  63. float srefx = uv.x - 0.5;
  64. float refx = abs(uv.x - 0.5);
  65. if (phase > 0)
  66. {
  67. srefx = (1 - uv.x) - 0.5;
  68. refx = abs((1 - uv.x) - 0.5);
  69. }
  70. phase = abs(_SkyMoonParameters.x);
  71. float refy = abs(uv.y - 0.5);
  72. float refxfory = sqrt(0.25 - refy * refy);
  73. float xmin = -refxfory;
  74. float xmax = refxfory;
  75. float xmin1 = (xmax - xmin) * (phase / 2) + xmin;
  76. float xmin2 = (xmax - xmin) * phase + xmin;
  77. if (srefx < xmin1)
  78. {
  79. alpha = 0;
  80. }
  81. else if (srefx < xmin2 && xmin1 != xmin2)
  82. {
  83. alpha = (srefx - xmin1) / (xmin2 - xmin1);
  84. }
  85. return alpha;
  86. }
  87. float3 ScreenSpaceDither(float2 vScreenPos, float3 clr)
  88. {
  89. float _DitheringIntensity = 0.25;
  90. float d = dot(float2(131.0, 312.0), vScreenPos.xy + _Time.y);
  91. float3 vDither = float3(d, d, d);
  92. vDither.rgb = frac(vDither.rgb / float3(103.0, 71.0, 97.0)) - float3(0.5, 0.5, 0.5);
  93. return (vDither.rgb / 15.0) * _DitheringIntensity;
  94. }
  95. float4 frag(v2f i) : SV_Target
  96. {
  97. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  98. float3 viewDirWS = GetSkyViewDirWS(i.position.xy);
  99. float3 dir = -viewDirWS;
  100. float3 wpos = normalize(mul((float4x4)UNITY_MATRIX_M, dir)).xyz;
  101. float4 skyColor = float4(0, 0, 0, 1);
  102. float3 viewDir = normalize(dir);
  103. //float3 viewDir = normalize(i.texcoord);
  104. skyColor = GetSkyColor(viewDir, 0.005f);
  105. //Stars
  106. float3 starsUV = mul((float3x3)_StarsMatrix, dir);
  107. float4 starsTex = texCUBE(_StarsTex, starsUV) * saturate(viewDir.y);
  108. float4 stars = starsTex * _StarIntensity;
  109. //skyColor += stars;
  110. //Galaxy
  111. float4 galaxyTex = texCUBE(_GalaxyTex, starsUV) * saturate(viewDir.y);
  112. float4 galaxy = galaxyTex * _GalaxyIntensity;
  113. //skyColor += galaxy;
  114. //Sun and Moon UV
  115. float3 rSun = normalize(cross(_SunDir.xyz, float3(0, -1, 0)));
  116. float3 uSun = cross(_SunDir.xyz, rSun);
  117. float2 sunUV = float2(dot(rSun, dir), dot(uSun, dir)) * (21.0 - _SkySunParameters.y) + 0.5;
  118. float3 rMoon = normalize(cross(_MoonDir.xyz, float3(0, -1, 0)));
  119. float3 uMoon = cross(_MoonDir.xyz, rMoon);
  120. float2 moonUV = float2(dot(rMoon, dir), dot(uMoon, dir)) * (20.7 - _SkyMoonParameters.z) + 0.5;
  121. //Sun
  122. float4 sun = float4(0,0,0,1);
  123. float hideBackSun = saturate(dot(_SunDir.xyz, viewDir));
  124. float4 sunDisk = tex2D(_SunTex, sunUV) * hideBackSun;
  125. sun = sunDisk * _SunColor * 10;
  126. skyColor += sun;
  127. //Moon
  128. if(_SkyMoonParameters.w > 0.0)
  129. {
  130. float hideBackMoon = saturate(dot(-_MoonDir.xyz, viewDir));
  131. float4 moon = tex2D(_MoonTex, moonUV) * hideBackMoon;
  132. float alpha = MoonPhaseFactor(moonUV, _SkyMoonParameters.x);
  133. float3 moonArea = clamp(moon * 10, 0, 1);
  134. float starsBehindMoon = 1 - clamp((moonArea * 5), 0, 1);
  135. moon = lerp(float4(0, 0, 0, 0), moon, alpha);
  136. moon = moon * _MoonColor;
  137. //float4 moonGlow = tex2D(_MoonGlowTex, i.moonGlowPos.xy) * hideBackMoon;
  138. //moonGlow = moonGlow * _MoonColor * _MoonGlowIntensity;
  139. skyColor += stars * starsBehindMoon;
  140. skyColor += galaxy * starsBehindMoon;
  141. skyColor += moon;
  142. }
  143. else
  144. {
  145. skyColor += stars;
  146. skyColor += galaxy;
  147. }
  148. //Aurora
  149. if(_Aurora > 0.0)
  150. {
  151. float4 aurora = Aurora(wpos);
  152. skyColor.rgb += aurora.rgb;
  153. }
  154. //Dithering
  155. //skyColor.rgb += ScreenSpaceDither(i.position.xy,skyColor.rgb);
  156. float3 cloudsDir = normalize(wpos + float3(0,1,0));
  157. //Cirrus
  158. if(_CirrusClouds > 0.0)
  159. {
  160. float3 cirrusUV = wpos;
  161. cirrusUV.y *= 1 - dot(cloudsDir.y + 10, float3(0,-0.15,0));
  162. float4 cirrus = CirrusClouds(cirrusUV);
  163. skyColor.rgb = skyColor.rgb * (1 - cirrus.a) + cirrus.rgb * cirrus.a;
  164. }
  165. //2D Clouds
  166. if(_FlatClouds > 0.0)
  167. {
  168. float3 flatCloudsUV = wpos;
  169. flatCloudsUV.y *= 1 - dot(cloudsDir.y + 200, float3(0,-0.1,0));
  170. float4 clouds = Clouds2D(flatCloudsUV, wpos);
  171. skyColor.rgb = skyColor.rgb * (1 - clouds.a) + clouds.rgb * clouds.a;
  172. }
  173. return float4(skyColor.rgb * _EnviroSkyIntensity * GetCurrentExposureMultiplier(), 1);
  174. }
  175. float4 fragBaking(v2f i) : SV_Target
  176. {
  177. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  178. float3 viewDirWS = GetSkyViewDirWS(i.position.xy);
  179. float3 dir = -viewDirWS;
  180. float3 wpos = normalize(mul((float4x4)UNITY_MATRIX_M, dir)).xyz;
  181. float4 skyColor = float4(0, 0, 0, 1);
  182. float3 viewDir = normalize(dir);
  183. //float3 viewDir = normalize(i.texcoord);
  184. skyColor = GetSkyColor(viewDir, 0.005f) * _AmbientColorTintHDRP;
  185. //Stars
  186. float3 starsUV = mul((float3x3)_StarsMatrix, dir);
  187. float4 starsTex = texCUBE(_StarsTex, starsUV) * saturate(viewDir.y);
  188. float4 stars = starsTex * _StarIntensity;
  189. //skyColor += stars;
  190. //Galaxy
  191. float4 galaxyTex = texCUBE(_GalaxyTex, starsUV) * saturate(viewDir.y);
  192. float4 galaxy = galaxyTex * _GalaxyIntensity;
  193. //skyColor += galaxy;
  194. //Sun and Moon UV
  195. float3 rSun = normalize(cross(_SunDir.xyz, float3(0, -1, 0)));
  196. float3 uSun = cross(_SunDir.xyz, rSun);
  197. float2 sunUV = float2(dot(rSun, dir), dot(uSun, dir)) * (21.0 - _SkySunParameters.y) + 0.5;
  198. float3 rMoon = normalize(cross(_MoonDir.xyz, float3(0, -1, 0)));
  199. float3 uMoon = cross(_MoonDir.xyz, rMoon);
  200. float2 moonUV = float2(dot(rMoon, dir), dot(uMoon, dir)) * (20.7 - _SkyMoonParameters.z) + 0.5;
  201. //Sun
  202. float4 sun = float4(0,0,0,1);
  203. float hideBackSun = saturate(dot(_SunDir.xyz, viewDir));
  204. float4 sunDisk = tex2D(_SunTex, sunUV) * hideBackSun;
  205. sun = sunDisk * _SunColor * 10;
  206. skyColor += sun;
  207. //Moon
  208. if(_SkyMoonParameters.w > 0.0)
  209. {
  210. float hideBackMoon = saturate(dot(-_MoonDir.xyz, viewDir));
  211. float4 moon = tex2D(_MoonTex, moonUV) * hideBackMoon;
  212. float alpha = MoonPhaseFactor(moonUV, _SkyMoonParameters.x);
  213. float3 moonArea = clamp(moon * 10, 0, 1);
  214. float starsBehindMoon = 1 - clamp((moonArea * 5), 0, 1);
  215. moon = lerp(float4(0, 0, 0, 0), moon, alpha);
  216. moon = moon * _MoonColor;
  217. //float4 moonGlow = tex2D(_MoonGlowTex, i.moonGlowPos.xy) * hideBackMoon;
  218. //moonGlow = moonGlow * _MoonColor * _MoonGlowIntensity;
  219. skyColor += stars * starsBehindMoon;
  220. skyColor += galaxy * starsBehindMoon;
  221. skyColor += moon;
  222. }
  223. else
  224. {
  225. skyColor += stars;
  226. skyColor += galaxy;
  227. }
  228. //Aurora
  229. if(_Aurora > 0.0)
  230. {
  231. float4 aurora = Aurora(wpos);
  232. skyColor.rgb += aurora.rgb;
  233. }
  234. //Dithering
  235. //skyColor.rgb += ScreenSpaceDither(i.position.xy,skyColor.rgb);
  236. float3 cloudsDir = normalize(wpos + float3(0,1,0));
  237. //Cirrus
  238. if(_CirrusClouds > 0.0)
  239. {
  240. float3 cirrusUV = wpos;
  241. cirrusUV.y *= 1 - dot(cloudsDir.y + 10, float3(0,-0.15,0));
  242. float4 cirrus = CirrusClouds(cirrusUV);
  243. skyColor.rgb = skyColor.rgb * (1 - cirrus.a) + cirrus.rgb * cirrus.a;
  244. }
  245. //2D Clouds
  246. if(_FlatClouds > 0.0)
  247. {
  248. float3 flatCloudsUV = wpos;
  249. flatCloudsUV.y *= 1 - dot(cloudsDir.y + 200, float3(0,-0.1,0));
  250. float4 clouds = Clouds2D(flatCloudsUV, wpos);
  251. skyColor.rgb = skyColor.rgb * (1 - clouds.a) + clouds.rgb * clouds.a;
  252. }
  253. return float4(skyColor.rgb * _EnviroSkyIntensity, 1);
  254. }
  255. ENDHLSL
  256. SubShader
  257. {
  258. Tags{ "RenderPipeline" = "HDRenderPipeline" }
  259. Pass
  260. {
  261. ZWrite Off
  262. ZTest Always
  263. Blend Off
  264. Cull Off
  265. HLSLPROGRAM
  266. #pragma vertex vert
  267. #pragma fragment fragBaking
  268. ENDHLSL
  269. }
  270. // For fullscreen Sky
  271. Pass
  272. {
  273. ZWrite Off
  274. ZTest LEqual
  275. Blend Off
  276. Cull Off
  277. HLSLPROGRAM
  278. #pragma vertex vert
  279. #pragma fragment frag
  280. ENDHLSL
  281. }
  282. }
  283. }