Guest User

TriplanarInteractiveSnowHlsl

a guest
Mar 8th, 2026
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Snow Interactive Normals" {
  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,500)) = 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.         _PathBlendOffset("Snow Path Blending Offset", Range(-3,3)) = 0.3
  21.         _PathCutoff("Snow Path Cutoff", Range(0,3)) = 0
  22.         _PathSmooth("Snow Path Smooth", Range(0,3)) = 1
  23.         _MainTex("Snow Texture", 2D) = "white" {}
  24.         _SnowHeight("Snow Height", Range(0,2)) = 0.3
  25.         _SnowDepth("Snow Path Depth", Range(-2,2)) = 0.3
  26.         _SnowTextureOpacity("Snow Texture Opacity", Range(0,2)) = 0.3
  27.         _SnowTextureScale("Snow Texture Scale", Range(0,2)) = 0.3
  28.  
  29.         [Space]
  30.         [Header(Sparkles)]
  31.         _SparkleScale("Sparkle Scale", Range(0,10)) = 10
  32.         _SparkCutoff("Sparkle Cutoff", Range(0,10)) = 0.8
  33.         _SparkleNoise("Sparkle Noise", 2D) = "gray" {}
  34.  
  35.         [Space]
  36.         [Header(Rim)]
  37.         _RimPower("Rim Power", Range(0,20)) = 20
  38.         [HDR]_RimColor("Rim Color Snow", Color) = (0.5,0.5,0.5,1)
  39.  
  40.         [Header(Rendering Layer Decal)]
  41.         _RenderingLayer("Rendering Layer", Range(0,10)) = 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 "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceData.hlsl"
  50.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl"
  51.     #include "SnowTessellation.hlsl"
  52.    
  53.     #pragma require tessellation tessHW
  54.     #pragma instancing_options renderinglayer
  55.     #pragma multi_compile_instancing
  56.     #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl"
  57.     #pragma vertex TessellationVertexProgram
  58.     #pragma hull hull
  59.     #pragma domain domain
  60.     // Keywords
  61.    
  62.     #pragma multi_compile _ _CLUSTER_LIGHT_LOOP
  63.     #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
  64.     #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  65.     #pragma multi_compile _ _SHADOWS_SOFT
  66.     #pragma multi_compile _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3
  67.     #pragma multi_compile_fog
  68.     #pragma target 5.0
  69.    
  70.  
  71.  
  72.     ControlPoint TessellationVertexProgram(Attributes2 v)
  73.     {
  74.         ControlPoint p;
  75.        
  76.         p.vertex = v.vertex;
  77.         p.uv = v.uv;
  78.         p.normal = v.normal;
  79.         UNITY_SETUP_INSTANCE_ID(v);
  80.         UNITY_TRANSFER_INSTANCE_ID(v, p);
  81.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(p);
  82.        
  83.         return p;
  84.     }
  85.     ENDHLSL
  86.  
  87.     SubShader{
  88.         Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  89.  
  90.         Pass{
  91.             Tags { "LightMode" = "UniversalForward" }
  92.  
  93.             HLSLPROGRAM
  94.             // vertex happens in snowtessellation.hlsl
  95.             #pragma fragment frag
  96.             #pragma require tessellation tessHW
  97.             #pragma target 2.0
  98.             sampler2D _MainTex, _SparkleNoise;
  99.             float4 _Color, _RimColor;
  100.             float _RimPower;
  101.             float4 _PathColorIn, _PathColorOut;
  102.             float _PathBlending, _PathBlendOffset;
  103.             float _SparkleScale, _SparkCutoff;
  104.             float _SnowTextureOpacity, _SnowTextureScale;
  105.             float4 _ShadowColor;
  106.            
  107.             half4 frag(Varyings2 IN) : SV_Target{
  108.                 UNITY_SETUP_INSTANCE_ID(IN);
  109.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
  110.            
  111.                 // Effects RenderTexture Reading
  112.                 float3 worldPosition = mul(unity_ObjectToWorld, IN.vertex).xyz;
  113.                 float2 uv = IN.worldPos.xz - _Position.xz;
  114.                 uv /= (_OrthographicCamSize * 2);
  115.                 uv += 0.5;
  116.  
  117.                 // effects texture             
  118.                 float4 effect = tex2D(_GlobalEffectRT, uv);
  119.  
  120.                 // mask to prevent bleeding
  121.                 effect *=  smoothstep(0.99, 0.9, uv.x) * smoothstep(0.99, 0.9,1- uv.x);
  122.                 effect *=  smoothstep(0.99, 0.9, uv.y) * smoothstep(0.99, 0.9,1- uv.y);
  123.  
  124.                 // worldspace Noise texture
  125.                 float3 topdownNoise = tex2D(_Noise, IN.worldPos.xz * _NoiseScale).rgb;
  126.  
  127.                 // worldspace Snow texture
  128.                 float3 snowtexture = tex2D(_MainTex, IN.worldPos.xz * _SnowTextureScale).rgb;
  129.                
  130.                 //lerp between snow color and snow texture
  131.                 float3 snowTex = lerp(_Color.rgb,snowtexture * _Color.rgb, _SnowTextureOpacity);
  132.                
  133.                 //lerp the colors using the RT effect path
  134.                 float pathShape = smoothstep(_PathCutoff,_PathCutoff + _PathSmooth,effect.g);
  135.                 float blendPath = (effect.g * _PathBlending) + _PathBlendOffset;
  136.                 float3 path = lerp(_PathColorOut.rgb, _PathColorIn.rgb,  saturate(blendPath));
  137.                 float3 mainColors = lerp(snowTex,path, pathShape);
  138.  
  139.                 // surface and inputdate are needed here to apply decals
  140.                 SurfaceData surfaceData = (SurfaceData)0;
  141.                 surfaceData.albedo = mainColors;
  142.                 surfaceData.alpha = 1;
  143.                 surfaceData.normalTS = IN.normal;
  144.                 surfaceData.smoothness = 0.5;
  145.                 surfaceData.occlusion = 1;
  146.  
  147.                 InputData inputData = (InputData)0;
  148.                 inputData.positionWS = IN.worldPos;
  149.                 inputData.positionCS = IN.vertex;
  150.                
  151.                 inputData.normalWS = normalize(IN.normal);
  152.                 inputData.viewDirectionWS = normalize(IN.viewDir);
  153.                 inputData.shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  154.                 inputData.fogCoord = IN.fogFactor;
  155.                
  156.                 inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(IN.vertex);
  157.  
  158. #if defined(_DBUFFER_MRT1) || defined(_DBUFFER_MRT2) || defined(_DBUFFER_MRT3) || defined(_DBUFFER)
  159.                 ApplyDecalToSurfaceData(inputData.positionCS, surfaceData, inputData);
  160.    
  161. #endif
  162.  
  163.                 // main colors with decals applied
  164.                 mainColors = surfaceData.albedo;
  165.                
  166.  
  167.                 // lighting and shadow information
  168.                 float shadow = 0;
  169.                 half4 shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  170.                
  171.                 #if _MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS
  172.                     Light mainLight = GetMainLight(shadowCoord);
  173.                     shadow = mainLight.shadowAttenuation;
  174.                 #else
  175.                     Light mainLight = GetMainLight();
  176.                 #endif
  177.  
  178.                     float3 extraLights;
  179.                 // extra lights forward+
  180. uint lightsCount = GetAdditionalLightsCount();
  181.                 LIGHT_LOOP_BEGIN(lightsCount)
  182.            
  183.                     Light aLight = GetAdditionalLight(lightIndex, IN.worldPos, half4(1,1,1,1));
  184.                     float3 attenuatedLightColor = aLight.color * (aLight.distanceAttenuation * aLight.shadowAttenuation);
  185.                     extraLights += attenuatedLightColor;
  186.                    
  187.                 LIGHT_LOOP_END
  188.  
  189.                 // extra point lights support forward
  190.            
  191.                 int pixelLightCount = GetAdditionalLightsCount();
  192.                 for (int j = 0; j < pixelLightCount; ++j) {
  193.                     Light light = GetAdditionalLight(j, IN.worldPos, half4(1, 1, 1, 1));
  194.                     float3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  195.                     extraLights += attenuatedLightColor;           
  196.                 }
  197.  
  198.                 float4 litMainColors = float4(mainColors,1) ;
  199.                 extraLights *= litMainColors.rgb;
  200.                 // add in the sparkles
  201.                 float sparklesStatic = tex2D(_SparkleNoise, IN.worldPos.xz * _SparkleScale).r;
  202.                 float cutoffSparkles = step(_SparkCutoff,sparklesStatic);              
  203.                 litMainColors += cutoffSparkles  *saturate(1- (effect.g * 2)) * 4;
  204.    
  205.                 // Rim using recalculated normal instead of vertex normal
  206.                 half rim = 1.0 - saturate(dot(inputData.viewDirectionWS, inputData.normalWS));
  207.                 litMainColors += _RimColor * pow(abs(rim), _RimPower);
  208.  
  209.                 // ambient and mainlight colors added
  210.                 half4 extraColors;
  211.                 extraColors.rgb = litMainColors.rgb * mainLight.color.rgb * (shadow + unity_AmbientSky.rgb);
  212.                 extraColors.a = 1;
  213.                
  214.                 // colored shadows
  215.                 float3 coloredShadows = (shadow + (_ShadowColor.rgb * (1-shadow)));
  216.                 litMainColors.rgb = litMainColors.rgb * mainLight.color * (coloredShadows);
  217.                 // everything together
  218.                 float4 final = litMainColors+ extraColors + float4(extraLights,0);
  219.                
  220.                 // add in fog
  221.                
  222.                 final.rgb = MixFog(final.rgb, IN.fogFactor);
  223.                 return final;
  224.  
  225.             }
  226.             ENDHLSL
  227.  
  228.         }
  229.  
  230. Pass
  231. {
  232.     // pass needed for decals
  233.     Name "DepthNormals"
  234.     Tags { "LightMode" = "DepthNormals" }
  235.  
  236.     ZWrite On
  237.     Cull Back
  238.  
  239.     HLSLPROGRAM
  240.     #pragma target 5.0
  241.     #pragma require tessellation tessHW
  242.     #pragma fragment DepthNormalsFragment
  243.      // Universal Pipeline keywords
  244.             #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl"
  245.  
  246.             //--------------------------------------
  247.             // GPU Instancing
  248.             #pragma multi_compile_instancing
  249.             #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
  250.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  251.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  252.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
  253.    
  254.     struct DNAttributes
  255.     {
  256.         float4 positionOS : POSITION;
  257.         float3 normalOS   : NORMAL;
  258.     };
  259.  
  260.     struct DNVaryings
  261.     {
  262.         float4 positionCS : SV_POSITION;
  263.         float3 normalWS   : TEXCOORD0;
  264.     };
  265.  
  266.     DNVaryings DepthNormalsVertex(DNAttributes input)
  267.     {
  268.         DNVaryings output;
  269.         float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  270.         output.positionCS = TransformWorldToHClip(positionWS);
  271.         output.normalWS = TransformObjectToWorldNormal(input.normalOS);
  272.         return output;
  273.     }
  274.  
  275.     float _RenderingLayer;
  276.  
  277.     void DepthNormalsFragment(
  278.             DNVaryings input
  279.             , out half4 outNormalWS : SV_Target0,
  280.             out float outDepth : SV_Depth
  281.         #ifdef _WRITE_RENDERING_LAYERS
  282.             , out float4 outRenderingLayers : SV_Target1
  283.         #endif
  284.         )
  285. {
  286.          #ifdef _WRITE_RENDERING_LAYERS
  287.                 uint renderingLayers = GetMeshRenderingLayer();
  288.                 outRenderingLayers = _RenderingLayer;//float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  289.             #endif
  290.             outNormalWS = half4(NormalizeNormalPerPixel(input.normalWS) * 0.5 + 0.5, 0);
  291.             outDepth = input.positionCS.z;
  292.        // return half4(NormalizeNormalPerPixel(input.normalWS) * 0.5 + 0.5, 0);
  293.     }
  294.  
  295.     ENDHLSL
  296. }
  297.         // depth only pass to fix invisiblity when turning on Depth Priming mode
  298.            Pass
  299.         {
  300.             Name "DepthOnly"
  301.             Tags
  302.             {
  303.                 "LightMode" = "DepthOnly"
  304.             }
  305.  
  306.             // -------------------------------------
  307.             // Render State Commands
  308.             ZWrite On
  309.             ColorMask R
  310.             Cull[_Cull]
  311.  
  312.             HLSLPROGRAM
  313.             #pragma require tessellation tessHW
  314.             #pragma target 2.0
  315.  
  316.             // -------------------------------------
  317.             // Shader Stages
  318.  
  319.             #pragma fragment DepthOnlyFragment
  320.  
  321.             // -------------------------------------
  322.             // Material Keywords
  323.             #pragma shader_feature_local _ALPHATEST_ON
  324.             #pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA
  325.  
  326.             // -------------------------------------
  327.             // Unity defined keywords
  328.             #pragma multi_compile _ LOD_FADE_CROSSFADE
  329.  
  330.             //--------------------------------------
  331.             // GPU Instancing
  332.             #pragma multi_compile_instancing
  333.             #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
  334.  
  335.             // -------------------------------------
  336.             // Includes
  337.             #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  338.             ENDHLSL
  339.         }
  340.         // casting shadows is a little glitchy, I've turned it off, but maybe in future urp versions it works better?
  341.         // Shadow Casting Pass
  342.         Pass
  343.         {
  344.                 Name "ShadowCaster"
  345.                 Tags { "LightMode" = "ShadowCaster" }
  346.                 ZWrite On
  347.                 ZTest LEqual
  348.                 Cull Off
  349.            
  350.                 HLSLPROGRAM
  351.                 #pragma target 3.0
  352.                 #pragma require tessellation tessHW
  353.                 // support all the various lightypes and shadow paths
  354.                 #pragma multi_compile_shadowcaster
  355.            
  356.                
  357.                 #pragma fragment frag
  358.                
  359.  
  360.                 half4 frag(Varyings2 IN) : SV_Target{
  361.                         return 0;
  362.                 }
  363.            
  364.                 ENDHLSL
  365.         }
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment