Advertisement
Guest User

ToonLitTriPlanar.shader

a guest
Jan 31st, 2018
7,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. Shader "Toon/Lit TriPlanar" {
  2. Properties{
  3. _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  4. _MainTex("Top Texture", 2D) = "white" {}
  5. _MainTexSide("Side/Bottom Texture", 2D) = "white" {}
  6. _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
  7. _Normal("Normal/Noise", 2D) = "bump" {}
  8. _Scale("Top Scale", Range(-2,2)) = 1
  9. _SideScale("Side Scale", Range(-2,2)) = 1
  10. _NoiseScale("Noise Scale", Range(-2,2)) = 1
  11. _TopSpread("TopSpread", Range(-2,2)) = 1
  12. _EdgeWidth("EdgeWidth", Range(0,0.5)) = 1
  13. _RimPower("Rim Power", Range(-2,20)) = 1
  14. _RimColor("Rim Color Top", Color) = (0.5,0.5,0.5,1)
  15. _RimColor2("Rim Color Side/Bottom", Color) = (0.5,0.5,0.5,1)
  16. }
  17.  
  18. SubShader{
  19. Tags{ "RenderType" = "Opaque" }
  20. LOD 200
  21.  
  22. CGPROGRAM
  23. #pragma surface surf ToonRamp
  24.  
  25. sampler2D _Ramp;
  26.  
  27. // custom lighting function that uses a texture ramp based
  28. // on angle between light direction and normal
  29. #pragma lighting ToonRamp exclude_path:prepass
  30. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  31. {
  32. #ifndef USING_DIRECTIONAL_LIGHT
  33. lightDir = normalize(lightDir);
  34. #endif
  35.  
  36. half d = dot(s.Normal, lightDir)*0.5 + 0.5;
  37. half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
  38.  
  39. half4 c;
  40. c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
  41. c.a = 0;
  42. return c;
  43. }
  44.  
  45.  
  46. sampler2D _MainTex, _MainTexSide, _Normal;
  47. float4 _Color, _RimColor, _RimColor2;
  48. float _RimPower;
  49. float _TopSpread, _EdgeWidth;
  50. float _Scale, _SideScale, _NoiseScale;
  51.  
  52. struct Input {
  53. float2 uv_MainTex : TEXCOORD0;
  54. float3 worldPos; // world position built-in value
  55. float3 worldNormal; // world normal built-in value
  56. float3 viewDir;// view direction built-in value we're using for rimlight
  57. };
  58.  
  59. void surf(Input IN, inout SurfaceOutput o) {
  60.  
  61. // clamp (saturate) and increase(pow) the worldnormal value to use as a blend between the projected textures
  62. float3 blendNormal = saturate(pow(IN.worldNormal * 1.4,4));
  63.  
  64. // normal noise triplanar for x, y, z sides
  65. float3 xn = tex2D(_Normal, IN.worldPos.zy * _NoiseScale);
  66. float3 yn = tex2D(_Normal, IN.worldPos.zx * _NoiseScale);
  67. float3 zn = tex2D(_Normal, IN.worldPos.xy * _NoiseScale);
  68.  
  69. // lerped together all sides for noise texture
  70. float3 noisetexture = zn;
  71. noisetexture = lerp(noisetexture, xn, blendNormal.x);
  72. noisetexture = lerp(noisetexture, yn, blendNormal.y);
  73.  
  74. // triplanar for top texture for x, y, z sides
  75. float3 xm = tex2D(_MainTex, IN.worldPos.zy * _Scale);
  76. float3 zm = tex2D(_MainTex, IN.worldPos.xy * _Scale);
  77. float3 ym = tex2D(_MainTex, IN.worldPos.zx * _Scale);
  78.  
  79. // lerped together all sides for top texture
  80. float3 toptexture = zm;
  81. toptexture = lerp(toptexture, xm, blendNormal.x);
  82. toptexture = lerp(toptexture, ym, blendNormal.y);
  83.  
  84. // triplanar for side and bottom texture, x,y,z sides
  85. float3 x = tex2D(_MainTexSide, IN.worldPos.zy * _SideScale);
  86. float3 y = tex2D(_MainTexSide, IN.worldPos.zx * _SideScale);
  87. float3 z = tex2D(_MainTexSide, IN.worldPos.xy * _SideScale);
  88.  
  89. // lerped together all sides for side bottom texture
  90. float3 sidetexture = z;
  91. sidetexture = lerp(sidetexture, x, blendNormal.x);
  92. sidetexture = lerp(sidetexture, y, blendNormal.y);
  93.  
  94. // rim light for fuzzy top texture
  95. half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal * noisetexture));
  96.  
  97. // rim light for side/bottom texture
  98. half rim2 = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
  99.  
  100. // dot product of world normal and surface normal + noise
  101. float worldNormalDotNoise = dot(o.Normal + (noisetexture.y + (noisetexture * 0.5)), IN.worldNormal.y);
  102.  
  103. // if dot product is higher than the top spread slider, multiplied by triplanar mapped top texture
  104. // step is replacing an if statement to avoid branching :
  105. // if (worldNormalDotNoise > _TopSpread{ o.Albedo = toptexture}
  106. float3 topTextureResult = step(_TopSpread, worldNormalDotNoise) * toptexture;
  107.  
  108. // if dot product is lower than the top spread slider, multiplied by triplanar mapped side/bottom texture
  109. float3 sideTextureResult = step(worldNormalDotNoise, _TopSpread) * sidetexture;
  110.  
  111. // if dot product is in between the two, make the texture darker
  112. float3 topTextureEdgeResult = step(_TopSpread, worldNormalDotNoise) * step(worldNormalDotNoise, _TopSpread + _EdgeWidth) * -0.15;
  113.  
  114. // final albedo color
  115. o.Albedo = topTextureResult + sideTextureResult + topTextureEdgeResult;
  116. o.Albedo *= _Color;
  117. // adding the fuzzy rimlight(rim) on the top texture, and the harder rimlight (rim2) on the side/bottom texture
  118. o.Emission = step(_TopSpread, worldNormalDotNoise) * _RimColor.rgb * pow(rim, _RimPower) + step(worldNormalDotNoise, _TopSpread) * _RimColor2.rgb * pow(rim2, _RimPower);
  119.  
  120.  
  121. }
  122. ENDCG
  123.  
  124. }
  125.  
  126. Fallback "Diffuse"
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement