Advertisement
Guest User

InteractiveSnowHLSL.shader

a guest
Mar 11th, 2021
394
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.         _Mask("Mask", 2D) = "white" {}// This mask prevents bleeding of the RT, make it white with a transparent edge around all sides, set to CLAMP, not repeat
  8.         [Space]
  9.         [Header(Tesselation)]
  10.         _MaxTessDistance("Max Tessellation Distance", Range(10,100)) = 50
  11.         _Tess("Tessellation", Range(1,32)) = 20
  12.         [Space]
  13.         [Header(Snow)]
  14.         _Color("Snow Color", Color) = (0.5,0.5,0.5,1)
  15.         _PathColor("Snow Path Color", Color) = (0.5,0.5,0.7,1)
  16.         _MainTex("Snow Texture", 2D) = "white" {}
  17.         _SnowHeight("Snow Height", Range(0,2)) = 0.3
  18.         _SnowDepth("Snow Path Depth", Range(-2,2)) = 0.3
  19.         _SnowTextureOpacity("Snow Texture Opacity", Range(0,2)) = 0.3
  20.         _SnowTextureScale("Snow Texture Scale", Range(0,2)) = 0.3
  21.         [Space]
  22.         [Header(Sparkles)]
  23.         _SparkleScale("Sparkle Scale", Range(0,10)) = 10
  24.         _SparkCutoff("Sparkle Cutoff", Range(0,10)) = 0.1
  25.         _SparkleNoise("Sparkle Noise", 2D) = "gray" {}
  26.         [Space]
  27.         [Header(NonSnow Textures)]
  28.         _MainTexBase("Base Texture", 2D) = "white" {}
  29.         _Scale("Base Scale", Range(0,2)) = 1
  30.         _EdgeColor("Snow Edge Color", Color) = (0.5,0.5,0.5,1)
  31.         _Edgewidth("Snow Edge Width", Range(0,0.2)) = 0.1
  32.         [Space]
  33.         [Header(Rim)]
  34.         _RimPower("Rim Power", Range(0,20)) = 20
  35.         _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. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  49. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  50. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  51. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  52. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  53. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  54. #pragma multi_compile _ SHADOWS_SHADOWMASK
  55. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  56. #pragma multi_compile_fog  
  57. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  58. #pragma multi_compile _ LIGHTMAP_ON
  59.  
  60.  
  61.             ControlPoint TessellationVertexProgram(Attributes v)
  62.         {
  63.             ControlPoint p;
  64.             p.vertex = v.vertex;
  65.             p.uv = v.uv;
  66.             p.normal = v.normal;
  67.             p.color = v.color;
  68.             return p;
  69.         }
  70.  
  71.         ENDHLSL
  72.  
  73.             SubShader{
  74.                 Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  75.  
  76.                 Pass{
  77.                        Tags { "LightMode" = "UniversalForward" }
  78.  
  79.             HLSLPROGRAM
  80.             // vertex happens in snowtessellation.hlsl
  81.             #pragma fragment frag
  82.  
  83.             sampler2D  _MainTexBase, _MainTex, _SparkleNoise;
  84.             float4 _Color, _RimColor;
  85.             float _RimPower;
  86.             float4 _EdgeColor;
  87.             float _Edgewidth;
  88.             float4 _PathColor;
  89.             float _SparkleScale, _SparkCutoff;
  90.             float _SnowTextureOpacity, _SnowTextureScale, _Scale;
  91.  
  92.             half4 frag(Varyings IN) : SV_Target{
  93.  
  94.                 // Effects RenderTexture Reading
  95.                 float3 worldPosition = mul(unity_ObjectToWorld, IN.vertex).xyz;
  96.                 float2 uv = IN.worldPos.xz - _Position.xz;
  97.                 uv /= (_OrthographicCamSize * 2);
  98.                 uv += 0.5;
  99.                 // mask to prevent bleeding
  100.                 float mask = tex2D(_Mask, uv).a;
  101.                 float4 effect = tex2D(_GlobalEffectRT, uv);
  102.                 effect *= mask;
  103.  
  104.                 float3 topdownNoise = tex2D(_Noise, IN.worldPos.xz * _NoiseScale).rgb;
  105.  
  106.                 float3 snowtexture = tex2D(_MainTex, IN.worldPos.xz * _SnowTextureScale).rgb;
  107.  
  108.                 float3 baseTexture = tex2D(_MainTexBase, IN.worldPos.xz * _Scale).rgb;
  109.  
  110.                 //// primary texture only on red vertex color with the noise texture
  111.                 float vertexColoredPrimary = step(0.6 * topdownNoise,IN.color.r).r;
  112.                 float3 snowTextureResult = vertexColoredPrimary * snowtexture;
  113.  
  114.                 //// edge for primary texture
  115.                 float vertexColorEdge = ((step((0.6 - _Edgewidth) * topdownNoise,IN.color.r)) * (1 - vertexColoredPrimary)).r;
  116.  
  117.                 //// basetexture only where there is no red vertex paint
  118.                 float3 baseTextureResult = baseTexture * (1 - (vertexColoredPrimary + vertexColorEdge));
  119.                 //// main colors by adding everything together
  120.                 float3 mainColors = (baseTextureResult + ((snowTextureResult * _SnowTextureOpacity) + (vertexColoredPrimary * _Color)) + (vertexColorEdge * _EdgeColor));
  121.                 ////lerp the colors using the RT effect path
  122.                 mainColors = saturate(lerp(mainColors, _PathColor * effect.g * 2, saturate(effect.g * 3 * vertexColoredPrimary))).rgb;
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                 // add shadows
  129.                 float4 shadowCoord = TransformWorldToShadowCoord(IN.worldPos);
  130.                 #if _MAIN_LIGHT_SHADOWS
  131.                                 Light mainLight = GetMainLight(shadowCoord);
  132.                 #else
  133.                                 Light mainLight = GetMainLight();
  134.                 #endif
  135.                                 float shadows = mainLight.shadowAttenuation;
  136.                                 float4 litMainColors = float4(mainColors,1) * (shadows);
  137.                                 // add in the sparkles
  138.                                 // sparkles, static multiplied by a simple screenpos version
  139.                                 float sparklesStatic = tex2D(_SparkleNoise, IN.worldPos.xz * _SparkleScale * 5).r;
  140.                                 float sparklesResult = tex2D(_SparkleNoise, (IN.worldPos.xz + IN.screenPos) * _SparkleScale) * sparklesStatic;
  141.                                 litMainColors += step(_SparkCutoff, sparklesResult) * vertexColoredPrimary;
  142.                                 // add rim light
  143.                                 half rim = 1.0 - dot((IN.viewDir), IN.normal);
  144.                                 litMainColors += vertexColoredPrimary * _RimColor * pow(rim, _RimPower);
  145.  
  146.  
  147.                                 // ambient and mainlight colors added
  148.                                 half4 extraColors;
  149.                                 extraColors.rgb = litMainColors * mainLight.color.rgb * (shadows + unity_AmbientSky);
  150.                                 extraColors.a = 1;
  151.  
  152.                                 // everything together
  153.                                 float4 final = litMainColors + extraColors;
  154.                                 // add in fog
  155.                                 final.rgb = MixFog(final.rgb, IN.fogFactor);
  156.                                 return final;
  157.  
  158.                                 }
  159.                             ENDHLSL
  160.  
  161.                             }
  162.  
  163.  
  164.             // shadow casting pass with empty fragment
  165.             Pass{
  166.  
  167.                 Tags{ "LightMode" = "ShadowCaster" }
  168.  
  169.  
  170.                 ZWrite On
  171.                 ZTest LEqual
  172.  
  173.                 HLSLPROGRAM
  174.                 #pragma fragment frag
  175.                 half4 frag(Varyings IN) : SV_Target{
  176.                     return 0;
  177.                 }
  178.  
  179.              ENDHLSL
  180.             }
  181.         }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement