Advertisement
annapuppet

InteractiveSnowURP.shader

Jul 8th, 2022
1,966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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(-2,2)) = 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 vertex TessellationVertexProgram
  45.     #pragma hull hull
  46.     #pragma domain domain
  47.     // Keywords
  48.    
  49.     #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
  50.     #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  51.     #pragma multi_compile _ _SHADOWS_SOFT
  52.     #pragma multi_compile_fog
  53.  
  54.  
  55.     ControlPoint TessellationVertexProgram(Attributes v)
  56.     {
  57.         ControlPoint p;
  58.         p.vertex = v.vertex;
  59.         p.uv = v.uv;
  60.         p.normal = v.normal;
  61.         return p;
  62.     }
  63.     ENDHLSL
  64.  
  65.     SubShader{
  66.         Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  67.  
  68.         Pass{
  69.             Tags { "LightMode" = "UniversalForward" }
  70.  
  71.             HLSLPROGRAM
  72.             // vertex happens in snowtessellation.hlsl
  73.             #pragma fragment frag
  74.             #pragma target 4.0
  75.            
  76.  
  77.             sampler2D _MainTex, _SparkleNoise;
  78.             float4 _Color, _RimColor;
  79.             float _RimPower;
  80.             float4 _PathColorIn, _PathColorOut;
  81.             float _PathBlending;
  82.             float _SparkleScale, _SparkCutoff;
  83.             float _SnowTextureOpacity, _SnowTextureScale;
  84.             float4 _ShadowColor;
  85.            
  86.  
  87.             half4 frag(Varyings IN) : SV_Target{
  88.  
  89.                 // Effects RenderTexture Reading
  90.                 float3 worldPosition = mul(unity_ObjectToWorld, IN.vertex).xyz;
  91.                 float2 uv = IN.worldPos.xz - _Position.xz;
  92.                 uv /= (_OrthographicCamSize * 2);
  93.                 uv += 0.5;
  94.  
  95.                 // effects texture             
  96.                 float4 effect = tex2D(_GlobalEffectRT, uv);
  97.  
  98.                 // mask to prevent bleeding
  99.                 effect *=  smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  100.                 effect *=  smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  101.  
  102.                 // worldspace Noise texture
  103.                 float3 topdownNoise = tex2D(_Noise, IN.worldPos.xz * _NoiseScale).rgb;
  104.  
  105.                 // worldspace Snow texture
  106.                 float3 snowtexture = tex2D(_MainTex, IN.worldPos.xz * _SnowTextureScale).rgb;
  107.                
  108.                 //lerp between snow color and snow texture
  109.                 float3 snowTex = lerp(_Color.rgb,snowtexture * _Color.rgb, _SnowTextureOpacity);
  110.                
  111.                 //lerp the colors using the RT effect path
  112.                 float3 path = lerp(_PathColorOut.rgb * effect.g, _PathColorIn.rgb, saturate(effect.g * _PathBlending));
  113.                 float3 mainColors = lerp(snowTex,path, saturate(effect.g));
  114.  
  115.                 // lighting and shadow information
  116.                 float shadow = 0;
  117.                 half4 shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  118.                
  119.                 #if _MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS
  120.                     Light mainLight = GetMainLight(shadowCoord);
  121.                     shadow = mainLight.shadowAttenuation;
  122.                 #else
  123.                     Light mainLight = GetMainLight();
  124.                 #endif
  125.  
  126.                 // extra point lights support
  127.                 float3 extraLights;
  128.                 int pixelLightCount = GetAdditionalLightsCount();
  129.                 for (int j = 0; j < pixelLightCount; ++j) {
  130.                     Light light = GetAdditionalLight(j, IN.worldPos, half4(1, 1, 1, 1));
  131.                     float3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  132.                     extraLights += attenuatedLightColor;           
  133.                 }
  134.  
  135.                 float4 litMainColors = float4(mainColors,1) ;
  136.                 extraLights *= litMainColors.rgb;
  137.                 // add in the sparkles
  138.                 float sparklesStatic = tex2D(_SparkleNoise, IN.worldPos.xz * _SparkleScale).r;
  139.                 float cutoffSparkles = step(_SparkCutoff,sparklesStatic);              
  140.                 litMainColors += cutoffSparkles  *saturate(1- (effect.g * 2)) * 4;
  141.                
  142.                
  143.                 // add rim light
  144.                 half rim = 1.0 - dot((IN.viewDir), IN.normal) * topdownNoise;
  145.                 litMainColors += _RimColor * pow(abs(rim), _RimPower);
  146.  
  147.                 // ambient and mainlight colors added
  148.                 half4 extraColors;
  149.                 extraColors.rgb = litMainColors.rgb * mainLight.color.rgb * (shadow + unity_AmbientSky.rgb);
  150.                 extraColors.a = 1;
  151.                
  152.                 // colored shadows
  153.                 float3 coloredShadows = (shadow + (_ShadowColor.rgb * (1-shadow)));
  154.                 litMainColors.rgb = litMainColors.rgb * mainLight.color * (coloredShadows);
  155.                 // everything together
  156.                 float4 final = litMainColors+ extraColors + float4(extraLights,0);
  157.                 // add in fog
  158.                 final.rgb = MixFog(final.rgb, IN.fogFactor);
  159.                 return final;
  160.  
  161.             }
  162.             ENDHLSL
  163.  
  164.         }
  165.  
  166.         // casting shadows is a little glitchy, I've turned it off, but maybe in future urp versions it works better?
  167.         // Shadow Casting Pass
  168.         // Pass
  169.         // {
  170.             //  Name "ShadowCaster"
  171.             //  Tags { "LightMode" = "ShadowCaster" }
  172.             //  ZWrite On
  173.             //  ZTest LEqual
  174.             //  Cull Off
  175.            
  176.             //  HLSLPROGRAM
  177.             //  #pragma target 3.0
  178.            
  179.             //  // Support all the various light  ypes and shadow paths
  180.             //  #pragma multi_compile_shadowcaster
  181.            
  182.             //  // Register our functions
  183.            
  184.             //  #pragma fragment frag
  185.             //  // A custom keyword to modify logic during the shadow caster pass
  186.  
  187.             //  half4 frag(Varyings IN) : SV_Target{
  188.                 //      return 0;
  189.             //  }
  190.            
  191.             //  ENDHLSL
  192.         // }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement