Guest User

ToonSnowBuildup.shader

a guest
Dec 18th, 2017
5,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1.  
  2. Shader "Toon/Lit Snow Buildup" {
  3. Properties{
  4. _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  5. _MainTex("Base (RGB)", 2D) = "white" {}
  6. _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
  7. _SnowRamp("Snow Toon Ramp (RGB)", 2D) = "gray" {}
  8. _SnowAngle("Angle of snow buildup", Vector) = (0,1,0)
  9. _SnowColor("Snow Base Color", Color) = (0.5,0.5,0.5,1)
  10. _TColor("Snow Top Color", Color) = (0.5,0.5,0.5,1)
  11. _RimColor("Snow Rim Color", Color) = (0.5,0.5,0.5,1)
  12. _RimPower("Snow Rim Power", Range(0,4)) = 3
  13. _SnowSize("Snow Amount", Range(-2,2)) = 1
  14. _Height("Snow Height", Range(0,0.2)) = 0.1
  15.  
  16.  
  17.  
  18. }
  19.  
  20. SubShader{
  21. Tags{ "RenderType" = "Opaque" }
  22. LOD 200
  23. Cull Off
  24. CGPROGRAM
  25.  
  26. #pragma surface surf ToonRamp vertex:disp addshadow
  27. sampler2D _Ramp;
  28.  
  29. // custom lighting function that uses a texture ramp based
  30. // on angle between light direction and normal
  31. #pragma lighting ToonRamp exclude_path:prepass
  32. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  33. {
  34. #ifndef USING_DIRECTIONAL_LIGHT
  35. lightDir = normalize(lightDir);
  36. #endif
  37.  
  38. half d = dot(s.Normal, lightDir)*0.5 + 0.5;
  39. half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
  40.  
  41. half4 c;
  42. c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
  43. c.a = 0;
  44. return c;
  45. }
  46.  
  47.  
  48. sampler2D _MainTex;
  49. sampler2D _SnowRamp;
  50.  
  51. float4 _Color;
  52. float4 _SnowColor;
  53. float4 _TColor;
  54. float4 _SnowAngle;
  55. float4 _RimColor;
  56.  
  57. float _SnowSize;
  58. float _Height;
  59. float _RimPower;
  60.  
  61. struct Input {
  62. float2 uv_MainTex : TEXCOORD0;
  63. float3 worldPos;
  64. float3 viewDir;
  65. float3 lightDir;
  66.  
  67. };
  68.  
  69. struct appdata {
  70. float4 vertex : POSITION;
  71. float3 normal : NORMAL;
  72. };
  73.  
  74. void disp(inout appdata_full v, out Input o)
  75. {
  76. UNITY_INITIALIZE_OUTPUT(Input, o);
  77. o.lightDir = WorldSpaceLightDir(v.vertex); // light direction for snow ramp
  78. float4 snowC = mul(_SnowAngle , unity_ObjectToWorld); // snow direction convertion to worldspace
  79. if (dot(v.normal, snowC.xyz) >= _SnowSize ) {
  80. v.vertex.xyz += v.normal * _Height;// scale vertices along normal
  81. }
  82.  
  83. }
  84.  
  85. void surf(Input IN, inout SurfaceOutput o) {
  86.  
  87. float3 localPos = (IN.worldPos - mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz); // local position for snow color blend
  88. half d = dot(o.Normal, IN.lightDir)*0.5 + 0.5; // light value for snow toon ramp
  89. half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; // main texture
  90. half3 rampS = tex2D(_SnowRamp, float2(d, d)).rgb; // snow toon ramp
  91. o.Albedo = c.rgb * _Color;// base color
  92. half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal)); // rimlight
  93. if (dot( o.Normal, _SnowAngle.xyz) >= _SnowSize -0.4) { // if dot product result is higher than snow amount, we turn it into snow
  94. o.Albedo = (lerp(_SnowColor * rampS, _TColor * rampS, saturate(localPos.y))); // blend base snow with top snow based on position
  95. o.Emission = _RimColor.rgb *pow(rim, _RimPower);// add glow rimlight to snow
  96. }
  97. o.Alpha = c.a;
  98. }
  99. ENDCG
  100.  
  101. }
  102.  
  103. Fallback "Diffuse"
  104. }
Add Comment
Please, Sign In to add comment