Cel Shader.shader 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Max820/Cel Shader" {
  3. Properties{
  4. // _Color("Color", Color) = (1,1,1,1)
  5. _MainTex("Base Texture (RGB)", 2D) = "white" {}
  6. _SSSTex("SSS Texture (RGB)", 2D) = "white" {}
  7. _ILMTex("ILM Texture (RGB)", 2D) = "white" {}
  8. _OutlineColor("Outline Color", Color) = (0,0,0,1)
  9. _Outline("Outline Width", Range(.0, 2)) = .5
  10. _ShadowContrast("Shadow Contrast", Range(0, 20)) = 1
  11. _DarkenInnerLine("Darken Inner Line", Range(0, 1)) = 0.2
  12. // _LightDirection("Light Direction", Vector) = (0,0,1)
  13. }
  14. CGINCLUDE
  15. #include "UnityCG.cginc"
  16. sampler2D _MainTex;
  17. sampler2D _SSSTex;
  18. struct appdata {
  19. float4 vertex : POSITION;
  20. float3 normal : NORMAL;
  21. float4 texCoord : TEXCOORD0;
  22. //float4 vertexColor : COLOR;
  23. };
  24. struct v2f {
  25. float4 pos : POSITION;
  26. float4 color : COLOR;
  27. float4 tex : TEXCOORD0;
  28. };
  29. uniform float _Outline;
  30. uniform float4 _OutlineColor;
  31. uniform float _ShadowContrast;
  32. uniform float _DarkenInnerLine;
  33. // uniform half3 _LightDirection;
  34. v2f vert(appdata v) {
  35. // just make a copy of incoming vertex data but scaled according to normal direction
  36. v2f o;
  37. o.pos = UnityObjectToClipPos(v.vertex);
  38. float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
  39. float2 offset = TransformViewToProjection(norm.xy);
  40. o.pos.xy += offset * _Outline * 0.5 ;
  41. o.tex = v.texCoord;
  42. o.color = _OutlineColor ;
  43. return o;
  44. }
  45. ENDCG
  46. SubShader
  47. {
  48. //Tags {"Queue" = "Geometry+100" }
  49. CGPROGRAM
  50. #pragma surface surfA Lambert
  51. //fixed4 _Color;
  52. struct Input {
  53. float2 uv_MainTex;
  54. };
  55. void surfA(Input IN, inout SurfaceOutput o) {
  56. half4 c2 = half4(1, 0, 1, 1);
  57. o.Albedo = c2.rgb ;
  58. o.Alpha = c2.a;
  59. }
  60. ENDCG
  61. // note that a vertex shader is specified here but its using the one above
  62. Pass{
  63. Name "OUTLINE"
  64. Tags{ "LightMode" = "Always" }
  65. Cull Front
  66. ZWrite On
  67. ColorMask RGB
  68. Blend SrcAlpha OneMinusSrcAlpha
  69. //Offset 50,50
  70. //Lighting Off
  71. CGPROGRAM
  72. #pragma vertex vert
  73. #pragma fragment frag
  74. half4 frag(v2f i) :COLOR {
  75. fixed4 cLight = tex2D(_MainTex, i.tex.xy);
  76. fixed4 cSSS = tex2D(_SSSTex, i.tex.xy);
  77. fixed4 cDark = cLight * cSSS;
  78. cDark = cDark *0.5f;// *cDark * cDark;
  79. cDark.a = 1; // weapon had alpha?
  80. //return cDark;
  81. return (i.color + cDark );
  82. }
  83. ENDCG
  84. } // Pass
  85. // ###############################
  86. CGPROGRAM
  87. // noforwardadd: important to remove multiple light passes
  88. #pragma surface surf CelShadingForward vertex:vertB
  89. #pragma target 3.0
  90. sampler2D _ILMTex;
  91. struct Input {
  92. float2 uv_MainTex;
  93. float3 vertexColor; // Vertex color stored here by vert() method
  94. };
  95. struct v2fB {
  96. float4 pos : SV_POSITION;
  97. fixed4 color : COLOR;
  98. };
  99. void vertB(inout appdata_full v, out Input o)
  100. {
  101. UNITY_INITIALIZE_OUTPUT(Input, o);
  102. o.vertexColor = v.color ; // Save the Vertex Color in the Input for the surf() method
  103. }
  104. struct SurfaceOutputCustom
  105. {
  106. fixed3 Albedo;
  107. fixed3 Normal;
  108. fixed3 Emission;
  109. fixed Alpha;
  110. half3 BrightColor;
  111. half3 ShadowColor;
  112. half3 InnerLineColor;
  113. half ShadowThreshold;
  114. half SpecularIntensity;
  115. half SpecularSize;
  116. //fixed4 ILM;
  117. };
  118. half4 LightingCelShadingForward(SurfaceOutputCustom s, half3 lightDir, /*half3 viewDir,*/ half atten) {
  119. // lightDir = _LightDirection;
  120. // Cell shading: Threshold (<=0, > 90 deg away from light), light vector, normal vector [control]
  121. half NdotL = dot(lightDir, s.Normal);
  122. // NdotL = smoothstep(0, 1.0f, NdotL);
  123. // NdotL = smoothstep(0, 0.025f, NdotL);
  124. half testDot = (NdotL + 1) / 2.0; // color 0 to 1. black = shadow, white = light
  125. half4 c = half4(0, 0, 0, 1);
  126. half4 specColor = half4(s.SpecularIntensity, s.SpecularIntensity, s.SpecularIntensity, 1);
  127. half blendArea = 0.04;
  128. NdotL -= s.ShadowThreshold;
  129. half specStrength = s.SpecularIntensity;// = 0.1f + s.SpecularIntensity;// > 1 = brighter, < 1 = darker
  130. if (NdotL < 0) // <= s.ShadowThreshold)
  131. {
  132. if ( NdotL < - s.SpecularSize -0.5f && specStrength <= 0.5f) // -0.5f)
  133. {
  134. c.rgb = s.ShadowColor *(0.5f + specStrength);// (specStrength + 0.5f);// 0.5f; // *s.ShadowColor;
  135. }
  136. else
  137. {
  138. c.rgb = s.ShadowColor;
  139. }
  140. }
  141. else
  142. {
  143. if (s.SpecularSize < 1 && NdotL * 1.8f > s.SpecularSize && specStrength >= 0.5f) // 0.5f) // 1.0f)
  144. {
  145. c.rgb = s.BrightColor * (0.5f + specStrength);// 1.5f;// *(specStrength * 2);// 2; // lighter
  146. }
  147. else
  148. {
  149. c.rgb = s.BrightColor;
  150. }
  151. }
  152. // add inner lines
  153. c.rgb = c.rgb * s.InnerLineColor ;
  154. return c;
  155. }
  156. void surf(Input IN, inout SurfaceOutputCustom o) {
  157. // Albedo comes from a texture tinted by color
  158. fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
  159. fixed4 cSSS = tex2D(_SSSTex, IN.uv_MainTex);
  160. fixed4 cILM = tex2D(_ILMTex, IN.uv_MainTex);
  161. o.BrightColor = c.rgb;
  162. o.ShadowColor = c.rgb * cSSS.rgb;
  163. float clampedLineColor = cILM.a;
  164. if (clampedLineColor < _DarkenInnerLine)
  165. clampedLineColor = _DarkenInnerLine;
  166. o.InnerLineColor = half3(clampedLineColor, clampedLineColor, clampedLineColor);
  167. float vertColor = (IN.vertexColor.r - 0.5) * _ShadowContrast + 0.5; //IN.vertexColor.r;
  168. // easier to combine black dark areas
  169. o.ShadowThreshold = cILM.g;
  170. o.ShadowThreshold *= vertColor;
  171. o.ShadowThreshold = 1 - o.ShadowThreshold; // flip black / white
  172. o.SpecularIntensity = cILM.r;// 1 + (1 - cILM.r);// +cILM.r;// *2; // make whiter
  173. o.SpecularSize = 1-cILM.b;// * 0.25f);
  174. }
  175. ENDCG
  176. }
  177. FallBack "Diffuse"
  178. }