Guest User

ToonTriplanarVertexColor.shader

a guest
Aug 30th, 2018
999
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Toon/Lit TriPlanar Vertex" {
  2. Properties{
  3. [Header(Main)]
  4. _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  5. _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
  6. _Normal("Noise", 2D) = "gray" {}
  7. _NoiseScale("Noise Scale", Range(-2,2)) = 1
  8.  
  9. [Space]
  10. [Header(Extra Textures)]
  11. _MainTexBase("Base Texture", 2D) = "white" {}
  12. _MainTex("Primary Texture", 2D) = "white" {}
  13. _MainTex2("Secondary Texture", 2D) = "white" {}
  14. _Scale("Base Scale", Range(-2,2)) = 1
  15. _PrimaryScale("Primary Scale", Range(-2,2)) = 1
  16. _SecondaryScale("Secondary Scale", Range(-2,2)) = 1
  17. _EdgeColor("Primary Edge Color", Color) = (0.5,0.5,0.5,1)
  18. _EdgeColor2("Secondary Edge Color", Color) = (0.5,0.5,0.5,1)
  19. _Edgewidth("Edge Width", Range(0,0.2)) = 0.1
  20.  
  21. [Space]
  22. [Header(Rim)]
  23. _RimPower("Rim Power", Range(-2,20)) = 1
  24. _RimColor("Rim Color Top", Color) = (0.5,0.5,0.5,1)
  25. _RimColor2("Rim Color Side/Bottom", Color) = (0.5,0.5,0.5,1)
  26. }
  27.  
  28. SubShader{
  29. Tags{ "RenderType" = "Opaque" }
  30. LOD 200
  31.  
  32. CGPROGRAM
  33. #pragma surface surf ToonRamp
  34.  
  35. sampler2D _Ramp;
  36.  
  37. // custom lighting function that uses a texture ramp based
  38. // on angle between light direction and normal
  39. #pragma lighting ToonRamp exclude_path:prepass
  40. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  41. {
  42. #ifndef USING_DIRECTIONAL_LIGHT
  43. lightDir = normalize(lightDir);
  44. #endif
  45.  
  46. half d = dot(s.Normal, lightDir)*0.5 + 0.5;
  47. half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
  48.  
  49. half4 c;
  50. c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
  51. c.a = 0;
  52. return c;
  53. }
  54.  
  55.  
  56. sampler2D _MainTex, _MainTexBase, _Normal, _MainTex2;
  57. float4 _Color, _RimColor, _RimColor2;
  58. float _RimPower;
  59. float _Scale, _PrimaryScale, _SecondaryScale, _NoiseScale;
  60. float4 _EdgeColor, _EdgeColor2;
  61. float _Edgewidth;
  62.  
  63. struct Input {
  64. float2 uv_MainTex : TEXCOORD0;
  65. float3 worldPos; // world position built-in value
  66. float3 worldNormal; // world normal built-in value
  67. float3 viewDir;// view direction built-in value we're using for rimlight
  68. float4 vertexColor : COLOR;
  69. };
  70.  
  71. void surf(Input IN, inout SurfaceOutput o) {
  72.  
  73. // clamp (saturate) and increase(pow) the worldnormal value to use as a blend between the projected textures
  74. float3 blendNormal = saturate(pow(IN.worldNormal * 1.4,4));
  75.  
  76. // normal noise triplanar for x, y, z sides
  77. float3 xn = tex2D(_Normal, IN.worldPos.zy * _NoiseScale);
  78. float3 yn = tex2D(_Normal, IN.worldPos.zx * _NoiseScale);
  79. float3 zn = tex2D(_Normal, IN.worldPos.xy * _NoiseScale);
  80.  
  81. // lerped together all sides for noise texture
  82. float3 noisetexture = zn;
  83. noisetexture = lerp(noisetexture, xn, blendNormal.x);
  84. noisetexture = lerp(noisetexture, yn, blendNormal.y);
  85.  
  86. // triplanar for primary texture for x, y, z sides
  87. float3 xm = tex2D(_MainTex, IN.worldPos.zy * _PrimaryScale);
  88. float3 zm = tex2D(_MainTex, IN.worldPos.xy * _PrimaryScale);
  89. float3 ym = tex2D(_MainTex, IN.worldPos.zx * _PrimaryScale);
  90.  
  91. // lerped together all sides for primary texture
  92. float3 toptexture = zm;
  93. toptexture = lerp(toptexture, xm, blendNormal.x);
  94. toptexture = lerp(toptexture, ym, blendNormal.y);
  95.  
  96. // triplanar for secondary texture for x, y, z sides
  97. float3 xm2 = tex2D(_MainTex2, IN.worldPos.zy * _SecondaryScale);
  98. float3 zm2 = tex2D(_MainTex2, IN.worldPos.xy * _SecondaryScale);
  99. float3 ym2 = tex2D(_MainTex2, IN.worldPos.zx * _SecondaryScale);
  100.  
  101. // lerped together all sides for secondary texture
  102. float3 toptexture2 = zm2;
  103. toptexture2 = lerp(toptexture2, xm2, blendNormal.x);
  104. toptexture2 = lerp(toptexture2, ym2, blendNormal.y);
  105.  
  106. // triplanar for base texture, x,y,z sides
  107. float3 x = tex2D(_MainTexBase, IN.worldPos.zy * _Scale);
  108. float3 y = tex2D(_MainTexBase, IN.worldPos.zx * _Scale);
  109. float3 z = tex2D(_MainTexBase, IN.worldPos.xy * _Scale);
  110.  
  111. // lerped together all sides for base texture
  112. float3 baseTexture = z;
  113. baseTexture = lerp(baseTexture, x, blendNormal.x);
  114. baseTexture = lerp(baseTexture, y, blendNormal.y);
  115.  
  116. // rim light for fuzzy top texture
  117. half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal * noisetexture));
  118.  
  119. // rim light for side/bottom texture
  120. half rim2 = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal* noisetexture));
  121.  
  122. // primary texture only on red vertex color with the noise texture
  123. float vertexColoredPrimary = step(0.6* noisetexture,IN.vertexColor.r );
  124. float3 primaryTextureResult = vertexColoredPrimary * toptexture;
  125. // secondary texture only on blue vertex color with the noise texture
  126. float vertexColoredSecondary = step(0.6* noisetexture + saturate(IN.vertexColor.r),IN.vertexColor.b );
  127. float3 secondaryTextureResult = vertexColoredSecondary * toptexture2;
  128. // edge for primary texture
  129. float vertexColorEdge = (step((0.6 - _Edgewidth)* noisetexture,IN.vertexColor.r )) * (1- vertexColoredPrimary);
  130. // edge for secondary texture
  131. float vertexColorEdge2 = (step((0.6 - _Edgewidth)* noisetexture+ saturate(IN.vertexColor.r),IN.vertexColor.b )) * (1-vertexColoredSecondary);
  132.  
  133. // basetexture only where there is no red or blue vertex paint
  134. float3 sideTextureResult = baseTexture * (1-(vertexColoredPrimary + vertexColorEdge + vertexColoredSecondary+ vertexColorEdge2));
  135.  
  136. // final albedo color by adding everything together
  137. o.Albedo = sideTextureResult + primaryTextureResult + (vertexColorEdge * _EdgeColor) + secondaryTextureResult + (vertexColorEdge2 * _EdgeColor2) ; //+ topTextureEdgeResult;
  138. o.Albedo *= _Color;
  139.  
  140. // adding the fuzzy rimlight(rim) on the top texture, and the harder rimlight (rim2) on the side/bottom texture
  141. o.Emission = (vertexColoredSecondary * pow(rim2, _RimPower)* _RimColor2) + (vertexColoredPrimary * _RimColor * pow(rim, _RimPower));
  142.  
  143.  
  144. }
  145. ENDCG
  146.  
  147. }
  148.  
  149. Fallback "Diffuse"
  150. }
RAW Paste Data