Guest User

ToonLitVertexIce.shader

a guest
Dec 19th, 2018
2,926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. Shader "Toon/Lit Ice VertexColor Tessellated" {
  2. Properties{
  3. [Header(Main)]
  4. _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  5. _MainTex("Base (RGB)", 2D) = "white" {}
  6. _Noise("Noise", 2D) = "white" {}
  7. [Header(Tesselation)]
  8. _Tess("Tessellation", Range(1,32)) = 4
  9.  
  10. [Header(Toon Ramp)]
  11. _RampColor("ToonRamp Color", Color) = (0,0,0.5,1)
  12. _RampOffset("ToonRamp Offset", Range(0,1)) = 0.6
  13. _ToonRampSoftNess("ToonRamp Softness", Range(0,1)) = 0.1
  14.  
  15. [Header(Snow)]
  16. _ColorSnow("Snow Color", Color) = (0.7,1,1,1)
  17. _NoiseScale("Noise Scale Snow", Range(0,1)) = 1
  18. _SnowHeight("Snow Height", Range(0,0.1)) = 0.05
  19. _SnowCutoff("Snow Cutoff", Range(0.1,1)) = 0.5
  20. _RimColorSnow("Snow Rim Color", Color) = (0,0.6,0.8,1)
  21. _RimPowerSnow("Snow Rim Power", Range(0,20)) = 5
  22.  
  23.  
  24. [Header(Ice Colors)]
  25. _ColorIce("Ice Base Color", Color) = (0.2,0.5,0.5,1)
  26. _RimColor("Ice Rim Color", Color) = (0,0.6,0.8,1)
  27. _RimPowerIce("Ice Rim Power", Range(0,20)) = 5
  28.  
  29. [Header(Ice Displacement)]
  30. _Scale("Noise Scale Ice", Range(0,1)) = 1
  31. _IceDisplacement("Ice Displacement Cutoff", Range(-2,0)) = -1
  32. _Sharpness("Ice Sharpness", Range(0,1)) = 1
  33. _Height("Ice Length", Range(0,2)) = 0.7
  34. _IceCutoff("Ice Cutoff", Range(0.1,1)) = 0.1
  35. }
  36.  
  37. SubShader{
  38. Tags{ "RenderType" = "Opaque" }
  39. LOD 200
  40. Cull Off
  41. CGPROGRAM
  42.  
  43. #pragma surface surf ToonRamp vertex:vert addshadow nolightmap tessellate:tessDistance
  44. #pragma target 4.0
  45. #pragma require tessellation tessHW
  46. #include "Tessellation.cginc"
  47.  
  48. float _ToonRampSoftNess,_RampOffset;
  49. float4 _RampColor;
  50. // custom lighting function that uses a texture ramp based
  51. // on angle between light direction and normal
  52. #pragma lighting ToonRamp exclude_path:prepass
  53. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  54. {
  55. #ifndef USING_DIRECTIONAL_LIGHT
  56. lightDir = normalize(lightDir);
  57. #endif
  58. float d = dot(s.Normal, lightDir);
  59. float dOffset = fwidth(d) + _RampOffset;
  60. float ramp = smoothstep(dOffset, dOffset + _ToonRampSoftNess, d);
  61. half4 c;
  62. c.rgb = s.Albedo * _LightColor0.rgb * (ramp + _RampColor)* (atten * 2);
  63. c.a = s.Alpha;
  64. return c;
  65. }
  66.  
  67. float _Tess;
  68. // big thanks to Cone Wars devs for explaining tesselation http://diary.conewars.com/tessellation-melt-shader-part-3/
  69. float MeltCalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess, float4 color)
  70. {
  71. float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
  72. float dist = distance(worldPosition, _WorldSpaceCameraPos);
  73. float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0);
  74.  
  75. if (color.r < 0.4 ) {
  76. f = 0.001;
  77. }
  78. return f * tess;
  79. }
  80.  
  81. float4 MeltDistanceBasedTess(float4 v0, float4 v1, float4 v2, float minDist, float maxDist, float tess, float4 v0c, float4 v1c, float4 v2c)
  82. {
  83. float3 f;
  84. f.x = MeltCalcDistanceTessFactor(v0, minDist, maxDist, tess, v0c);
  85. f.y = MeltCalcDistanceTessFactor(v1, minDist, maxDist, tess, v1c);
  86. f.z = MeltCalcDistanceTessFactor(v2, minDist, maxDist, tess, v2c);
  87.  
  88. return UnityCalcTriEdgeTessFactors(f);
  89. }
  90.  
  91. float4 tessDistance(appdata_full v0, appdata_full v1, appdata_full v2)
  92. {
  93. float minDist = 10.0;
  94. float maxDist = 25.0;
  95.  
  96. return MeltDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess, v0.color, v1.color, v2.color);
  97. }
  98.  
  99. sampler2D _MainTex, _Noise;
  100. float4 _Color, _ColorIce, _ColorSnow, _RimColor, _RimColorSnow;
  101. float _IceDisplacement, _Sharpness, _Height, _RimPowerIce, _RimPowerSnow;
  102. float _Scale, _NoiseScale, _SnowHeight;
  103. float _SnowCutoff, _IceCutoff, IceVertexCutoff;
  104.  
  105. struct Input {
  106. float2 uv_MainTex : TEXCOORD0;
  107. float3 worldPos;
  108. float3 viewDir;
  109. float3 worldNormal; INTERNAL_DATA
  110. float4 color : COLOR;
  111. };
  112.  
  113. void vert(inout appdata_full v)
  114. {
  115. // world position
  116. float3 worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
  117. // icicle texture based on world position
  118. float4 icicleNoise = tex2Dlod(_Noise, float4(worldPosition.xz * _Scale, 0, 0));
  119.  
  120. float3 WorldNormal = UnityObjectToWorldNormal(v.normal);
  121. // facing bottom
  122. float3 Bottom = mul(float3(0,-1,0), unity_ObjectToWorld);
  123. // smooth falloff of parts facing bottom
  124. float edge = smoothstep(WorldNormal.y, WorldNormal.y + _Sharpness, _IceDisplacement *icicleNoise.r);
  125. // pull down icicles
  126. v.vertex.xyz += (Bottom * edge * v.color.r) * _Height;
  127. // move vertices outward where snow is
  128. v.vertex.xyz += normalize(v.normal) * (saturate(v.color.r * (1-edge)* _SnowHeight * icicleNoise.r));
  129. }
  130.  
  131. void surf(Input IN, inout SurfaceOutput o) {
  132. // triplanar noise
  133. float3 blendNormal = saturate(pow(IN.worldNormal * 1.4, 4));
  134.  
  135. // normal noise triplanar for x, y, z sides
  136. float3 xn = tex2D(_Noise, IN.worldPos.zy * _NoiseScale);
  137. float3 yn = tex2D(_Noise, IN.worldPos.zx * _NoiseScale);
  138. float3 zn = tex2D(_Noise, IN.worldPos.xy * _NoiseScale);
  139.  
  140. // lerped together all sides for noise texture
  141. float3 noisetexture = zn;
  142. noisetexture = lerp(noisetexture, xn, blendNormal.x);
  143. noisetexture = lerp(noisetexture, yn, blendNormal.y);
  144.  
  145. // main texture
  146. half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  147.  
  148. // part of model facing bottom
  149. float facingBottom =1- smoothstep(_IceCutoff * noisetexture.r, (_IceCutoff + 0.01)* noisetexture.r, IN.worldNormal.y);
  150. // part of model with red vertex color
  151. float snowEdge = step(_SnowCutoff * noisetexture.r, IN.color.r);
  152.  
  153. // base texture, not on red vertex color
  154. float3 baseAlbedo = c.rgb * (1- snowEdge);
  155.  
  156. // snow , only on red vertex color, not on bottom facing part
  157. float3 snowAlbedo = _ColorSnow * (1 - facingBottom) * snowEdge;
  158.  
  159. // ice, only on red vertex color, and bottom facing part
  160. float3 iceAlbedo = _ColorIce * facingBottom * snowEdge;
  161. // combined
  162. o.Albedo = baseAlbedo + snowAlbedo + iceAlbedo;
  163.  
  164. // rim lighting
  165. half rimTextured = 1.0 - (saturate(dot(normalize(IN.viewDir), o.Normal)) * noisetexture.r); //+ IN.test.r; // rimlight
  166. half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal)); //+ IN.test.r; // rimlight
  167.  
  168. // ice rim
  169. float3 iceRim = _RimColor.rgb *pow(rimTextured, _RimPowerIce)* facingBottom* (step(0.25, IN.color.r));
  170. // snow rim
  171. float3 snowRim = _RimColorSnow.rgb *pow(rim, _RimPowerSnow) * (1 - facingBottom) * (step(0.25, IN.color.r));
  172. //combined
  173. o.Emission = snowRim + iceRim ;// add glow rimlight to snow
  174.  
  175. o.Alpha = 1;
  176. }
  177. ENDCG
  178.  
  179. }
  180.  
  181. Fallback "Diffuse"
  182. }
Advertisement
Add Comment
Please, Sign In to add comment