Advertisement
Guest User

InteractiveSnowURP.shader

a guest
Jul 8th, 2022
2,946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. Shader "Custom/Snow Interactive" {
  2. Properties{
  3. [Header(Main)]
  4. _Noise("Snow Noise", 2D) = "gray" {}
  5. _NoiseScale("Noise Scale", Range(0,2)) = 0.1
  6. _NoiseWeight("Noise Weight", Range(0,2)) = 0.1
  7. [HDR]_ShadowColor("Shadow Color", Color) = (0.5,0.5,0.5,1)
  8.  
  9. [Space]
  10. [Header(Tesselation)]
  11. _MaxTessDistance("Max Tessellation Distance", Range(10,100)) = 50
  12. _Tess("Tessellation", Range(1,32)) = 20
  13.  
  14. [Space]
  15. [Header(Snow)]
  16. [HDR]_Color("Snow Color", Color) = (0.5,0.5,0.5,1)
  17. [HDR]_PathColorIn("Snow Path Color In", Color) = (0.5,0.5,0.7,1)
  18. [HDR]_PathColorOut("Snow Path Color Out", Color) = (0.5,0.5,0.7,1)
  19. _PathBlending("Snow Path Blending", Range(0,3)) = 0.3
  20. _MainTex("Snow Texture", 2D) = "white" {}
  21. _SnowHeight("Snow Height", Range(0,2)) = 0.3
  22. _SnowDepth("Snow Path Depth", Range(0,100)) = 0.3
  23. _SnowTextureOpacity("Snow Texture Opacity", Range(0,2)) = 0.3
  24. _SnowTextureScale("Snow Texture Scale", Range(0,2)) = 0.3
  25.  
  26. [Space]
  27. [Header(Sparkles)]
  28. _SparkleScale("Sparkle Scale", Range(0,10)) = 10
  29. _SparkCutoff("Sparkle Cutoff", Range(0,10)) = 0.8
  30. _SparkleNoise("Sparkle Noise", 2D) = "gray" {}
  31.  
  32. [Space]
  33. [Header(Rim)]
  34. _RimPower("Rim Power", Range(0,20)) = 20
  35. [HDR]_RimColor("Rim Color Snow", Color) = (0.5,0.5,0.5,1)
  36. }
  37. HLSLINCLUDE
  38.  
  39. // Includes
  40. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  41. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  42. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  43. #include "SnowTessellation.hlsl"
  44. #pragma require tessellation tessHW
  45. #pragma vertex TessellationVertexProgram
  46. #pragma hull hull
  47. #pragma domain domain
  48. // Keywords
  49.  
  50. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
  51. #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  52. #pragma multi_compile _ _SHADOWS_SOFT
  53. #pragma multi_compile_fog
  54.  
  55.  
  56. ControlPoint TessellationVertexProgram(Attributes v)
  57. {
  58. ControlPoint p;
  59. p.vertex = v.vertex;
  60. p.uv = v.uv;
  61. p.normal = v.normal;
  62. return p;
  63. }
  64. ENDHLSL
  65.  
  66. SubShader{
  67. Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  68.  
  69. Pass{
  70. Tags { "LightMode" = "UniversalForward" }
  71.  
  72. HLSLPROGRAM
  73. // vertex happens in snowtessellation.hlsl
  74. #pragma fragment frag
  75. #pragma target 4.0
  76.  
  77.  
  78. sampler2D _MainTex, _SparkleNoise;
  79. float4 _Color, _RimColor;
  80. float _RimPower;
  81. float4 _PathColorIn, _PathColorOut;
  82. float _PathBlending;
  83. float _SparkleScale, _SparkCutoff;
  84. float _SnowTextureOpacity, _SnowTextureScale;
  85. float4 _ShadowColor;
  86.  
  87.  
  88. half4 frag(Varyings IN) : SV_Target{
  89.  
  90. // Effects RenderTexture Reading
  91. float3 worldPosition = mul(unity_ObjectToWorld, IN.vertex).xyz;
  92. float2 uv = IN.worldPos.xz - _Position.xz;
  93. uv /= (_OrthographicCamSize * 2);
  94. uv += 0.5;
  95.  
  96. // effects texture
  97. float4 effect = tex2D(_GlobalEffectRT, uv);
  98.  
  99. // mask to prevent bleeding
  100. effect *= smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  101. effect *= smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  102.  
  103. // worldspace Noise texture
  104. float3 topdownNoise = tex2D(_Noise, IN.worldPos.xz * _NoiseScale).rgb;
  105.  
  106. // worldspace Snow texture
  107. float3 snowtexture = tex2D(_MainTex, IN.worldPos.xz * _SnowTextureScale).rgb;
  108.  
  109. //lerp between snow color and snow texture
  110. float3 snowTex = lerp(_Color.rgb,snowtexture * _Color.rgb, _SnowTextureOpacity);
  111.  
  112. //lerp the colors using the RT effect path
  113. float3 path = lerp(_PathColorOut.rgb * effect.g, _PathColorIn.rgb, saturate(effect.g * _PathBlending));
  114. float3 mainColors = lerp(snowTex,path, saturate(effect.g));
  115.  
  116. // lighting and shadow information
  117. float shadow = 0;
  118. half4 shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  119.  
  120. #if _MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS
  121. Light mainLight = GetMainLight(shadowCoord);
  122. shadow = mainLight.shadowAttenuation;
  123. #else
  124. Light mainLight = GetMainLight();
  125. #endif
  126.  
  127. // extra point lights support
  128. float3 extraLights;
  129. int pixelLightCount = GetAdditionalLightsCount();
  130. for (int j = 0; j < pixelLightCount; ++j) {
  131. Light light = GetAdditionalLight(j, IN.worldPos, half4(1, 1, 1, 1));
  132. float3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  133. extraLights += attenuatedLightColor;
  134. }
  135.  
  136. float4 litMainColors = float4(mainColors,1) ;
  137. extraLights *= litMainColors.rgb;
  138. // add in the sparkles
  139. float sparklesStatic = tex2D(_SparkleNoise, IN.worldPos.xz * _SparkleScale).r;
  140. float cutoffSparkles = step(_SparkCutoff,sparklesStatic);
  141. litMainColors += cutoffSparkles *saturate(1- (effect.g * 2)) * 4;
  142.  
  143.  
  144. // add rim light
  145. half rim = 1.0 - dot((IN.viewDir), IN.normal) * topdownNoise.r;
  146. litMainColors += _RimColor * pow(abs(rim), _RimPower);
  147.  
  148. // ambient and mainlight colors added
  149. half4 extraColors;
  150. extraColors.rgb = litMainColors.rgb * mainLight.color.rgb * (shadow + unity_AmbientSky.rgb);
  151. extraColors.a = 1;
  152.  
  153. // colored shadows
  154. float3 coloredShadows = (shadow + (_ShadowColor.rgb * (1-shadow)));
  155. litMainColors.rgb = litMainColors.rgb * mainLight.color * (coloredShadows);
  156. // everything together
  157. float4 final = litMainColors+ extraColors + float4(extraLights,0);
  158. // add in fog
  159. final.rgb = MixFog(final.rgb, IN.fogFactor);
  160. return final;
  161.  
  162. }
  163. ENDHLSL
  164.  
  165. }
  166.  
  167. // casting shadows is a little glitchy, I've turned it off, but maybe in future urp versions it works better?
  168. // Shadow Casting Pass
  169. // Pass
  170. // {
  171. // Name "ShadowCaster"
  172. // Tags { "LightMode" = "ShadowCaster" }
  173. // ZWrite On
  174. // ZTest LEqual
  175. // Cull Off
  176.  
  177. // HLSLPROGRAM
  178. // #pragma target 3.0
  179.  
  180. // // Support all the various light ypes and shadow paths
  181. // #pragma multi_compile_shadowcaster
  182.  
  183. // // Register our functions
  184.  
  185. // #pragma fragment frag
  186. // // A custom keyword to modify logic during the shadow caster pass
  187.  
  188. // half4 frag(Varyings IN) : SV_Target{
  189. // return 0;
  190. // }
  191.  
  192. // ENDHLSL
  193. // }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement