Advertisement
Guest User

TriplanarInteractiveSnow.shader

a guest
Nov 28th, 2024
500
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.    
  5.        
  6.         _Noise("Snow Noise", 2D) = "gray" {}
  7.        
  8.         _NoiseScale("Noise Scale", Range(0,2)) = 0.1
  9.         _NoiseWeight("Noise Weight", Range(0,2)) = 0.1
  10.         [HDR]_ShadowColor("Shadow Color", Color) = (0.5,0.5,0.5,1)
  11.  
  12.         [Space]
  13.         [Header(Tesselation)]
  14.         _MaxTessDistance("Max Tessellation Distance", Range(10,100)) = 50
  15.         _Tess("Tessellation", Range(1,500)) = 20
  16.    
  17.  
  18.         [Space]
  19.         [Header(Snow)]
  20.         [HDR]_Color("Snow Color", Color) = (0.5,0.5,0.5,1)
  21.         [HDR]_PathColorIn("Snow Path Color In", Color) = (0.5,0.5,0.7,1)
  22.         [HDR]_PathColorOut("Snow Path Color Out", Color) = (0.5,0.5,0.7,1)
  23.         _PathBlending("Snow Path Blending", Range(0,3)) = 0.3
  24.         _MainTex("Snow Texture", 2D) = "white" {}
  25.         _SnowHeight("Snow Height", Range(0,2)) = 0.3
  26.         _SnowDepth("Snow Path Depth", Range(-2,2)) = 0.3
  27.         _SnowTextureOpacity("Snow Texture Opacity", Range(0,1)) = 0.3
  28.         _SnowTextureScale("Snow Texture Scale", Range(0,2)) = 0.3
  29.         _Normal("Snow Normal", 2D) = "bump" {}
  30.         _SnowNormalStrength("Snow Normal Strength", Range(0,1)) = 0.3
  31.  
  32.         [Space]
  33.         [Header(Sparkles)]
  34.         _SparkleScale("Sparkle Scale", Range(0,10)) = 10
  35.         _SparkCutoff("Sparkle Cutoff", Range(0,2)) = 0.8
  36.         _SparkleNoise("Sparkle Noise", 2D) = "gray" {}
  37.  
  38.         [Space]
  39.         [Header(Rim)]
  40.         _RimPower("Rim Power", Range(0,20)) = 20
  41.         [HDR]_RimColor("Rim Color Snow", Color) = (0.5,0.5,0.5,1)
  42.     }
  43.     HLSLINCLUDE
  44.  
  45.     // Includes
  46.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  47.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  48.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  49.     #include "SnowTessellation.hlsl"
  50.     #pragma vertex TessellationVertexProgram
  51.     #pragma hull hull
  52.     #pragma domain domain
  53.     #pragma instancing_options renderinglayer
  54.     // Keywords
  55.    
  56.     #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
  57.     #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  58.     #pragma multi_compile _ _SHADOWS_SOFT
  59.     #pragma multi_compile_fragment _ _LIGHT_LAYERS
  60.     #pragma multi_compile_fog
  61.     #pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3
  62.  
  63.     ControlPoint TessellationVertexProgram(Attributes2 v)
  64.     {
  65.         ControlPoint p;
  66.         p.vertex = v.vertex;
  67.         p.uv = v.uv;
  68.         p.normal = v.normal;
  69.         p.tangent = v.tangent;
  70.         return p;
  71.     }
  72.     ENDHLSL
  73.  
  74.     SubShader{
  75.         Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  76.  
  77.         Pass{
  78.             Tags { "LightMode" = "UniversalForward" }
  79.  
  80.             HLSLPROGRAM
  81.             // vertex happens in snowtessellation.hlsl
  82.             #pragma require tessellation tessHW
  83.             #pragma fragment frag
  84.             #pragma target 4.0
  85.            
  86.  
  87.             sampler2D _MainTex, _SparkleNoise;
  88.             float4 _Color, _RimColor;
  89.             float _RimPower;
  90.             float4 _PathColorIn, _PathColorOut;
  91.             float _PathBlending;
  92.             float _SparkleScale, _SparkCutoff;
  93.             float _SnowTextureOpacity, _SnowTextureScale;
  94.             float4 _ShadowColor;
  95.             float _SnowNormalStrength;
  96.            
  97.  
  98.             half4 frag(Varyings2 IN) : SV_Target{
  99.  
  100.                 // Effects RenderTexture Reading
  101.                 float3 worldPosition = mul(unity_ObjectToWorld, IN.vertex).xyz;
  102.                 float2 uv = IN.worldPos.xz - _Position.xz;
  103.                 uv /= (_OrthographicCamSize * 2);
  104.                 uv += 0.5;
  105.  
  106.                 // effects texture             
  107.                 float4 effect = tex2D(_GlobalEffectRT, uv);
  108.  
  109.                 // mask to prevent bleeding
  110.                 effect *=  smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  111.                 effect *=  smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  112.  
  113.                
  114.                 // worldspace Snow texture
  115.                 float3 snowtexture = tex2D(_MainTex, IN.worldPos.xz * _SnowTextureScale).rgb;
  116.  
  117.                 // snow normal
  118.                 float3 snownormal = UnpackNormal(tex2D(_Normal, IN.worldPos.xz * _NoiseScale)).rgb;
  119.                 snownormal = snownormal.r * IN.tangent + snownormal.g * IN.bitangent + snownormal.b * IN.normal;
  120.                
  121.                 //lerp between snow color and snow texture
  122.                 float3 snowTex = lerp(_Color.rgb,snowtexture * _Color.rgb, _SnowTextureOpacity);
  123.                
  124.                
  125.                 //lerp the colors using the RT effect path
  126.                 float3 path = lerp(_PathColorOut.rgb * effect.g, _PathColorIn.rgb, saturate(effect.g * _PathBlending));
  127.                 float3 mainColors = lerp(snowTex,path, saturate(effect.g));
  128.  
  129.                 // lighting and shadow information
  130.                 float shadow = 0;
  131.                 half4 shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  132.                
  133.                 #if _MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS
  134.                     Light mainLight = GetMainLight(shadowCoord);
  135.                     shadow = mainLight.shadowAttenuation;
  136.                 #else
  137.                     Light mainLight = GetMainLight();
  138.                 #endif
  139.  
  140.                 // extra point lights support
  141.                 float3 extraLights;
  142.                 int pixelLightCount = GetAdditionalLightsCount();
  143.                 for (int j = 0; j < pixelLightCount; ++j) {
  144.                     Light light = GetAdditionalLight(j, IN.worldPos, half4(1, 1, 1, 1));
  145.                     float3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  146.                     extraLights += attenuatedLightColor;           
  147.                 }
  148.  
  149.                 float4 litMainColors = float4(mainColors,1) ;
  150.                 extraLights *= litMainColors.rgb;
  151.                 // add in the sparkles
  152.                 float sparklesStatic = tex2D(_SparkleNoise, IN.worldPos.xz * _SparkleScale).r;
  153.                 float cutoffSparkles = step(_SparkCutoff,sparklesStatic);              
  154.                 litMainColors += lerp(cutoffSparkles * 4, 0,saturate(effect.g * 2));
  155.                
  156.                
  157.                 // add rim light
  158.                 half rim = 1.0 - dot(lerp(snownormal, IN.normal, _SnowNormalStrength), normalize(IN.viewDir)) ;
  159.                 // no rim inside of the path
  160.                 rim = lerp(rim, 0, saturate(effect.g));
  161.                 litMainColors += _RimColor * pow(rim, _RimPower);
  162.  
  163.                 // ambient and mainlight colors added
  164.                 half4 extraColors;
  165.                 extraColors.rgb = litMainColors.rgb * mainLight.color.rgb * (shadow + unity_AmbientSky.rgb);
  166.                 extraColors.a = 1;
  167.                
  168.                 // colored shadows
  169.                 float3 coloredShadows = shadow + lerp(_ShadowColor, 0, shadow);
  170.                 litMainColors.rgb = litMainColors.rgb * mainLight.color * (coloredShadows);
  171.                 // everything together
  172.                 float4 final = litMainColors+ extraColors + float4(extraLights,0);
  173.                 // add in fog
  174.                 final.rgb = MixFog(final.rgb, IN.fogFactor);
  175.                 return final;
  176.  
  177.             }
  178.             ENDHLSL
  179.  
  180.         }
  181.  
  182.  
  183.         // depth only pass to fix invisiblity when turning on Depth Priming mode
  184.            Pass
  185.         {
  186.             Name "DepthOnly"
  187.             Tags
  188.             {
  189.                 "LightMode" = "DepthOnly"
  190.             }
  191.  
  192.             // -------------------------------------
  193.             // Render State Commands
  194.             ZWrite On
  195.             ColorMask R
  196.             Cull[_Cull]
  197.  
  198.             HLSLPROGRAM
  199.             #pragma target 2.0
  200.  
  201.             // -------------------------------------
  202.             // Shader Stages
  203.             #pragma vertex DepthOnlyVertex
  204.             #pragma fragment DepthOnlyFragment
  205.  
  206.             // -------------------------------------
  207.             // Material Keywords
  208.             #pragma shader_feature_local _ALPHATEST_ON
  209.             #pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA
  210.  
  211.             // -------------------------------------
  212.             // Unity defined keywords
  213.             #pragma multi_compile _ LOD_FADE_CROSSFADE
  214.  
  215.             //--------------------------------------
  216.             // GPU Instancing
  217.             #pragma multi_compile_instancing
  218.             #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
  219.  
  220.             // -------------------------------------
  221.             // Includes
  222.             //#include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
  223.             #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  224.             ENDHLSL
  225.         }
  226.  
  227.         // casting shadows is a little glitchy, I've turned it off, but maybe in future urp versions it works better?
  228.         // Shadow Casting Pass
  229.         Pass
  230.         {
  231.                 Name "ShadowCaster"
  232.                 Tags { "LightMode" = "ShadowCaster" }
  233.                 ZWrite On
  234.             ZTest LEqual
  235.             ColorMask 0
  236.                 HLSLPROGRAM
  237.                 #pragma target 3.0
  238.            
  239.                 // Support all the various light  ypes and shadow paths
  240.                 #pragma multi_compile_shadowcaster
  241.                 // Unity defined keywords
  242.                 #pragma multi_compile _ LOD_FADE_CROSSFADE
  243.                    // This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
  244.                    #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
  245.                 // Register our functions
  246.            
  247.                 #pragma fragment frag
  248.                 // A custom keyword to modify logic during the shadow caster pass
  249.  
  250.                 half4 frag(Varyings2 IN) : SV_Target{
  251.                         return 0;
  252.                 }
  253.            
  254.                 ENDHLSL
  255.         }
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement