saliacoel commited on
Commit
f956c99
·
verified ·
1 Parent(s): 9fc9ab1

Update toon_cutoff16.shader

Browse files
Files changed (1) hide show
  1. toon_cutoff16.shader +373 -216
toon_cutoff16.shader CHANGED
@@ -1,216 +1,373 @@
1
- Shader "Toon/Cutoff"
2
- {
3
- Properties
4
- {
5
- _Color("Color(RGBA)", Color) = (1,1,1,1)
6
- _MainTex("Texture", 2D) = "white" {}
7
- _Shininess ("Shininess(0.0:)", Float) = 1.0
8
-
9
- _ShadowThreshold ("Shadow Threshold(0.0:1.0)", Float) = 0.5
10
- _ShadowColor ("Shadow Color(RGBA)", Color) = (0,0,0,0.5)
11
- _ShadowSharpness ("Shadow Sharpness(0.0:)", Float) = 100
12
- _Cutoff ("Alpha cutoff", Range(0,1)) = 0.9
13
-
14
- }
15
-
16
- SubShader
17
- {
18
- // Settings
19
- Tags {"Queue" = "Transparent" "IgnoreProjector"="False" "RenderType" = "TransparentCutout"}
20
-
21
-
22
-
23
- // Surface Shader Pass ( Front )
24
- Cull Back
25
- ZWrite On
26
- AlphaTest Greater 0.9
27
- //AlphaTest Equal 1
28
- Blend SrcAlpha OneMinusSrcAlpha
29
- CGPROGRAM
30
- #pragma surface surf Toon alphatest:_Cutoff vertex:vert
31
- // Toon Surface Shader for Unity
32
- // File : ToonSurface.cginc
33
- // Title : Toon Surface Shader with Alpha
34
- // Language : Cg Language
35
- // Include : ToonSurface.cginc
36
- /*
37
- How To Use :
38
-
39
- CGPROGRAM
40
- #pragma surface surf Toon
41
- #include "ToonSurface.cginc"
42
- ENDCG
43
- */
44
-
45
- float4 _Color;
46
- sampler2D _MainTex;
47
- float _ShadowThreshold;
48
- float4 _ShadowColor;
49
- float _ShadowSharpness;
50
- float _Shininess;
51
-
52
-
53
- struct ToonSurfaceOutput
54
- {
55
- half3 Albedo;
56
- half3 Normal;
57
- half3 Emission;
58
- half3 Gloss;
59
- half Specular;
60
- half Alpha;
61
- half4 Color;
62
- };
63
-
64
- inline half4 LightingToon (ToonSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
65
- {
66
- //Lighting paramater
67
- float4 lightColor = _LightColor0 * atten;
68
- float lightStrength = dot(lightDir, s.Normal) * 0.5 + 0.5;
69
-
70
- //ToonMapping
71
- half shadowRate = abs( max( -1, ( min( lightStrength, _ShadowThreshold ) -_ShadowThreshold)*_ShadowSharpness ) )*_ShadowColor.a;
72
- float4 toon = float4(1,1,1,1) * (1-shadowRate) + _ShadowColor *shadowRate;
73
-
74
- //Output
75
- //float4 color = saturate( _Color * (lightColor*2) ) * s.Color;
76
- float4 color = _Color * (lightColor) * s.Color * (atten*2) * _Shininess;
77
-
78
- color *= toon;
79
- color.a = s.Alpha;
80
- return color;
81
- }
82
-
83
- struct Input
84
- {
85
- float2 uv_MainTex;
86
- };
87
-
88
-
89
- void vert (inout appdata_full v, out Input o) {
90
- UNITY_INITIALIZE_OUTPUT(Input,o);
91
- }
92
-
93
- void surf (Input IN, inout ToonSurfaceOutput o)
94
- {
95
-
96
- // Defaults
97
- o.Albedo = 0.0;
98
- o.Emission = 0.0;
99
- o.Gloss = 0.0;
100
- o.Specular = 0.0;
101
-
102
- half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
103
- //o.Albedo = c;
104
- o.Color = c;
105
- o.Alpha = c.a;
106
- }
107
-
108
- ENDCG
109
-
110
- // // Outline Pass
111
- // Pass
112
- // {
113
- // Cull Off
114
- // ZWrite On
115
- // Lighting Off
116
- // CGPROGRAM
117
- // #pragma vertex vert
118
- // #pragma fragment frag
119
- // #include "UnityCG.cginc"
120
- // #include "ToonOutlineVertFrag.cginc"
121
- // ENDCG
122
- // }
123
-
124
-
125
- // Surface Shader Pass ( Back )
126
- Cull Front
127
- ZWrite On
128
- //AlphaTest Equal 1
129
- AlphaTest Greater 0.9
130
- Blend SrcAlpha OneMinusSrcAlpha
131
- CGPROGRAM
132
- #pragma surface surf Toon alphatest:_Cutoff vertex:vert
133
- // Toon Surface Shader for Unity
134
- // File : ToonSurface.cginc
135
- // Title : Toon Surface Shader with Alpha
136
- // Language : Cg Language
137
- // Include : ToonSurface.cginc
138
- /*
139
- How To Use :
140
-
141
- CGPROGRAM
142
- #pragma surface surf Toon
143
- #include "ToonSurface.cginc"
144
- ENDCG
145
- */
146
-
147
- float4 _Color;
148
- sampler2D _MainTex;
149
- float _ShadowThreshold;
150
- float4 _ShadowColor;
151
- float _ShadowSharpness;
152
- float _Shininess;
153
-
154
-
155
- struct ToonSurfaceOutput
156
- {
157
- half3 Albedo;
158
- half3 Normal;
159
- half3 Emission;
160
- half3 Gloss;
161
- half Specular;
162
- half Alpha;
163
- half4 Color;
164
- };
165
-
166
- inline half4 LightingToon (ToonSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
167
- {
168
- //Lighting paramater
169
- float4 lightColor = _LightColor0 * atten;
170
- float lightStrength = dot(lightDir, s.Normal) * 0.5 + 0.5;
171
-
172
- //ToonMapping
173
- half shadowRate = abs( max( -1, ( min( lightStrength, _ShadowThreshold ) -_ShadowThreshold)*_ShadowSharpness ) )*_ShadowColor.a;
174
- float4 toon = float4(1,1,1,1) * (1-shadowRate) + _ShadowColor *shadowRate;
175
-
176
- //Output
177
- //float4 color = saturate( _Color * (lightColor*2) ) * s.Color;
178
- float4 color = _Color * (lightColor) * s.Color * (atten*2) * _Shininess;
179
-
180
- color *= toon;
181
- color.a = s.Alpha;
182
- return color;
183
- }
184
-
185
- struct Input
186
- {
187
- float2 uv_MainTex;
188
- };
189
-
190
-
191
- void vert (inout appdata_full v, out Input o) {
192
- UNITY_INITIALIZE_OUTPUT(Input,o);
193
- }
194
-
195
- void surf (Input IN, inout ToonSurfaceOutput o)
196
- {
197
-
198
- // Defaults
199
- o.Albedo = 0.0;
200
- o.Emission = 0.0;
201
- o.Gloss = 0.0;
202
- o.Specular = 0.0;
203
-
204
- half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
205
- //o.Albedo = c;
206
- o.Color = c;
207
- o.Alpha = c.a;
208
- }
209
-
210
- ENDCG
211
-
212
- }
213
-
214
- // Other Environment
215
- Fallback "Diffuse"
216
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Shader "Toon/Cutoff"
2
+ {
3
+ Properties
4
+ {
5
+ [MainColor] _Color("Color(RGBA)", Color) = (1,1,1,1)
6
+ [MainTexture] _MainTex("Texture", 2D) = "white" {}
7
+ _Shininess ("Shininess(0.0:)", Float) = 1.0
8
+
9
+ _ShadowThreshold ("Shadow Threshold(0.0:1.0)", Range(0,1)) = 0.5
10
+ _ShadowColor ("Shadow Color(RGBA)", Color) = (0,0,0,0.5)
11
+ _ShadowSharpness ("Shadow Sharpness(0.0:)", Float) = 100
12
+ _Cutoff ("Alpha cutoff", Range(0,1)) = 0.9
13
+ }
14
+
15
+ // ============================================================
16
+ // URP
17
+ // ============================================================
18
+ SubShader
19
+ {
20
+ Tags
21
+ {
22
+ "RenderPipeline" = "UniversalPipeline"
23
+ "Queue" = "AlphaTest"
24
+ "RenderType" = "AlphaTest"
25
+ }
26
+
27
+ Pass
28
+ {
29
+ Name "ForwardLit"
30
+ Tags { "LightMode" = "UniversalForward" }
31
+
32
+ Cull Off
33
+ ZWrite On
34
+ ZTest LEqual
35
+ Blend One Zero
36
+
37
+ HLSLPROGRAM
38
+ #pragma target 2.0
39
+ #pragma vertex vert
40
+ #pragma fragment frag
41
+
42
+ #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
43
+ #pragma multi_compile _ _ADDITIONAL_LIGHTS
44
+ #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
45
+ #pragma multi_compile_fog
46
+ #pragma multi_compile_instancing
47
+
48
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
49
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
50
+
51
+ struct Attributes
52
+ {
53
+ float4 positionOS : POSITION;
54
+ float3 normalOS : NORMAL;
55
+ float2 uv : TEXCOORD0;
56
+ UNITY_VERTEX_INPUT_INSTANCE_ID
57
+ };
58
+
59
+ struct Varyings
60
+ {
61
+ float4 positionCS : SV_POSITION;
62
+ float2 uv : TEXCOORD0;
63
+ float3 positionWS : TEXCOORD1;
64
+ float3 normalWS : TEXCOORD2;
65
+ float4 shadowCoord : TEXCOORD3;
66
+ half fogFactor : TEXCOORD4;
67
+ UNITY_VERTEX_INPUT_INSTANCE_ID
68
+ UNITY_VERTEX_OUTPUT_STEREO
69
+ };
70
+
71
+ CBUFFER_START(UnityPerMaterial)
72
+ float4 _Color;
73
+ float4 _ShadowColor;
74
+ float4 _MainTex_ST;
75
+ float _Shininess;
76
+ float _ShadowThreshold;
77
+ float _ShadowSharpness;
78
+ float _Cutoff;
79
+ CBUFFER_END
80
+
81
+ TEXTURE2D(_MainTex);
82
+ SAMPLER(sampler_MainTex);
83
+
84
+ Varyings vert(Attributes IN)
85
+ {
86
+ Varyings OUT;
87
+
88
+ UNITY_SETUP_INSTANCE_ID(IN);
89
+ UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
90
+ UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
91
+
92
+ VertexPositionInputs posInputs = GetVertexPositionInputs(IN.positionOS.xyz);
93
+ VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS);
94
+
95
+ OUT.positionCS = posInputs.positionCS;
96
+ OUT.positionWS = posInputs.positionWS;
97
+ OUT.normalWS = normalize(normalInputs.normalWS);
98
+ OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
99
+ OUT.shadowCoord = GetShadowCoord(posInputs);
100
+ OUT.fogFactor = ComputeFogFactor(posInputs.positionCS.z);
101
+
102
+ return OUT;
103
+ }
104
+
105
+ half3 ToonLight(half3 baseColor, half3 normalWS, Light light)
106
+ {
107
+ half atten = light.distanceAttenuation * light.shadowAttenuation;
108
+
109
+ half lightStrength = dot(normalWS, normalize(light.direction)) * 0.5h + 0.5h;
110
+
111
+ half shadowRate =
112
+ abs(max(-1.0h,
113
+ (min(lightStrength, (half)_ShadowThreshold) - (half)_ShadowThreshold) *
114
+ (half)_ShadowSharpness)) * _ShadowColor.a;
115
+
116
+ shadowRate = saturate(shadowRate);
117
+
118
+ half3 toonTint = lerp(half3(1.0h, 1.0h, 1.0h), _ShadowColor.rgb, shadowRate);
119
+
120
+ return baseColor * light.color * toonTint * saturate(atten * 2.0h) * max((half)_Shininess, 0.0h);
121
+ }
122
+
123
+ half4 frag(Varyings IN) : SV_Target
124
+ {
125
+ UNITY_SETUP_INSTANCE_ID(IN);
126
+
127
+ half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv) * _Color;
128
+ clip(texColor.a - _Cutoff);
129
+
130
+ half3 normalWS = normalize(IN.normalWS);
131
+ half3 color = 0;
132
+
133
+ Light mainLight = GetMainLight(IN.shadowCoord);
134
+ color += ToonLight(texColor.rgb, normalWS, mainLight);
135
+
136
+ #if defined(_ADDITIONAL_LIGHTS)
137
+ uint pixelLightCount = GetAdditionalLightsCount();
138
+ LIGHT_LOOP_BEGIN(pixelLightCount)
139
+ Light additionalLight = GetAdditionalLight(lightIndex, IN.positionWS, half4(1,1,1,1));
140
+ color += ToonLight(texColor.rgb, normalWS, additionalLight);
141
+ LIGHT_LOOP_END
142
+ #endif
143
+
144
+ color = MixFog(color, IN.fogFactor);
145
+ return half4(color, texColor.a);
146
+ }
147
+ ENDHLSL
148
+ }
149
+
150
+ Pass
151
+ {
152
+ Name "ShadowCaster"
153
+ Tags { "LightMode" = "ShadowCaster" }
154
+
155
+ ZWrite On
156
+ ZTest LEqual
157
+ ColorMask 0
158
+ Cull Off
159
+
160
+ HLSLPROGRAM
161
+ #pragma target 2.0
162
+ #pragma vertex ShadowPassVertex
163
+ #pragma fragment ShadowPassFragment
164
+ #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
165
+ #pragma multi_compile_instancing
166
+
167
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
168
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
169
+
170
+ struct Attributes
171
+ {
172
+ float4 positionOS : POSITION;
173
+ float3 normalOS : NORMAL;
174
+ float2 uv : TEXCOORD0;
175
+ UNITY_VERTEX_INPUT_INSTANCE_ID
176
+ };
177
+
178
+ struct Varyings
179
+ {
180
+ float4 positionCS : SV_POSITION;
181
+ float2 uv : TEXCOORD0;
182
+ UNITY_VERTEX_INPUT_INSTANCE_ID
183
+ UNITY_VERTEX_OUTPUT_STEREO
184
+ };
185
+
186
+ CBUFFER_START(UnityPerMaterial)
187
+ float4 _Color;
188
+ float4 _ShadowColor;
189
+ float4 _MainTex_ST;
190
+ float _Shininess;
191
+ float _ShadowThreshold;
192
+ float _ShadowSharpness;
193
+ float _Cutoff;
194
+ CBUFFER_END
195
+
196
+ TEXTURE2D(_MainTex);
197
+ SAMPLER(sampler_MainTex);
198
+
199
+ float3 _LightDirection;
200
+ float3 _LightPosition;
201
+
202
+ float4 GetShadowPositionHClip(Attributes input)
203
+ {
204
+ float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
205
+ float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
206
+
207
+ #if _CASTING_PUNCTUAL_LIGHT_SHADOW
208
+ float3 lightDirectionWS = normalize(_LightPosition - positionWS);
209
+ #else
210
+ float3 lightDirectionWS = _LightDirection;
211
+ #endif
212
+
213
+ float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
214
+ positionCS = ApplyShadowClamping(positionCS);
215
+ return positionCS;
216
+ }
217
+
218
+ Varyings ShadowPassVertex(Attributes IN)
219
+ {
220
+ Varyings OUT;
221
+
222
+ UNITY_SETUP_INSTANCE_ID(IN);
223
+ UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
224
+ UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
225
+
226
+ OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
227
+ OUT.positionCS = GetShadowPositionHClip(IN);
228
+ return OUT;
229
+ }
230
+
231
+ half4 ShadowPassFragment(Varyings IN) : SV_Target
232
+ {
233
+ UNITY_SETUP_INSTANCE_ID(IN);
234
+
235
+ half alpha = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv).a * _Color.a;
236
+ clip(alpha - _Cutoff);
237
+
238
+ return 0;
239
+ }
240
+ ENDHLSL
241
+ }
242
+
243
+ Pass
244
+ {
245
+ Name "DepthOnly"
246
+ Tags { "LightMode" = "DepthOnly" }
247
+
248
+ ZWrite On
249
+ ZTest LEqual
250
+ ColorMask 0
251
+ Cull Off
252
+
253
+ HLSLPROGRAM
254
+ #pragma target 2.0
255
+ #pragma vertex DepthOnlyVertex
256
+ #pragma fragment DepthOnlyFragment
257
+ #pragma multi_compile_instancing
258
+
259
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
260
+
261
+ struct Attributes
262
+ {
263
+ float4 positionOS : POSITION;
264
+ float2 uv : TEXCOORD0;
265
+ UNITY_VERTEX_INPUT_INSTANCE_ID
266
+ };
267
+
268
+ struct Varyings
269
+ {
270
+ float4 positionCS : SV_POSITION;
271
+ float2 uv : TEXCOORD0;
272
+ UNITY_VERTEX_INPUT_INSTANCE_ID
273
+ UNITY_VERTEX_OUTPUT_STEREO
274
+ };
275
+
276
+ CBUFFER_START(UnityPerMaterial)
277
+ float4 _Color;
278
+ float4 _ShadowColor;
279
+ float4 _MainTex_ST;
280
+ float _Shininess;
281
+ float _ShadowThreshold;
282
+ float _ShadowSharpness;
283
+ float _Cutoff;
284
+ CBUFFER_END
285
+
286
+ TEXTURE2D(_MainTex);
287
+ SAMPLER(sampler_MainTex);
288
+
289
+ Varyings DepthOnlyVertex(Attributes IN)
290
+ {
291
+ Varyings OUT;
292
+
293
+ UNITY_SETUP_INSTANCE_ID(IN);
294
+ UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
295
+ UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
296
+
297
+ OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
298
+ OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
299
+
300
+ return OUT;
301
+ }
302
+
303
+ half4 DepthOnlyFragment(Varyings IN) : SV_Target
304
+ {
305
+ UNITY_SETUP_INSTANCE_ID(IN);
306
+
307
+ half alpha = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv).a * _Color.a;
308
+ clip(alpha - _Cutoff);
309
+
310
+ return 0;
311
+ }
312
+ ENDHLSL
313
+ }
314
+ }
315
+
316
+ // ============================================================
317
+ // Built-in Render Pipeline fallback
318
+ // ============================================================
319
+ SubShader
320
+ {
321
+ Tags
322
+ {
323
+ "Queue" = "AlphaTest"
324
+ "RenderType" = "TransparentCutout"
325
+ }
326
+
327
+ Cull Off
328
+ ZWrite On
329
+
330
+ CGPROGRAM
331
+ #pragma surface surf Toon alphatest:_Cutoff addshadow fullforwardshadows
332
+
333
+ sampler2D _MainTex;
334
+ fixed4 _Color;
335
+ half _Shininess;
336
+ half _ShadowThreshold;
337
+ fixed4 _ShadowColor;
338
+ half _ShadowSharpness;
339
+ half _Cutoff;
340
+
341
+ struct Input
342
+ {
343
+ float2 uv_MainTex;
344
+ };
345
+
346
+ inline half4 LightingToon(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
347
+ {
348
+ half lightStrength = dot(s.Normal, lightDir) * 0.5h + 0.5h;
349
+
350
+ half shadowRate =
351
+ abs(max(-1.0h,
352
+ (min(lightStrength, _ShadowThreshold) - _ShadowThreshold) *
353
+ _ShadowSharpness)) * _ShadowColor.a;
354
+
355
+ shadowRate = saturate(shadowRate);
356
+
357
+ half3 toonTint = lerp(half3(1,1,1), _ShadowColor.rgb, shadowRate);
358
+ half3 rgb = s.Albedo * _LightColor0.rgb * toonTint * saturate(atten * 2.0h) * max(_Shininess, 0.0h);
359
+
360
+ return half4(rgb, s.Alpha);
361
+ }
362
+
363
+ void surf(Input IN, inout SurfaceOutput o)
364
+ {
365
+ half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
366
+ o.Albedo = c.rgb;
367
+ o.Alpha = c.a;
368
+ }
369
+ ENDCG
370
+ }
371
+
372
+ Fallback "Legacy Shaders/Transparent/Cutout/Diffuse"
373
+ }