-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triplanar Test.shader
322 lines (259 loc) · 10.5 KB
/
Triplanar Test.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Shader "Rhy Custom Shaders/Flat Lit Toon + MMD/Basic"
{
Properties
{
_MainTex("MainTex", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_ColorMask("ColorMask", 2D) = "black" {}
_ColorIntensity("Intensity", Range(0, 5)) = 1.0
_SphereAddTex("Sphere Add Texture", 2D) = "black" {}
_SphereAddIntensity("Add Sphere Texture Intensity", Range(0, 500)) = 1.0
_SphereMulTex("Sphere Multiply Texture", 2D) = "white" {}
_SphereMulIntensity("Multiply Sphere Texture Intensity", Range(0, 500)) = 1.0
_DefaultLightDir("Default Light Direction", Vector) = (1,1,1,0)
_ToonTex("Toon Texture", 2D) = "white" {}
_ShadowTex("Shadow Texture", 2D) = "white" {}
_ShadowMask("Shadow Mask", 2D) = "black" {}
_outline_width("outline_width", Float) = 0.2
_outline_color("outline_color", Color) = (0.5,0.5,0.5,1)
_outline_tint("outline_tint", Range(0, 1)) = 0.5
_EmissionMap("Emission Map", 2D) = "white" {}
_EmissionMask("Emission Mask", 2D) = "white" {}
_EmissionIntensity("Emission Intensity", Range(0, 20)) = 0.0
_SpeedX("Emission X speed", Float) = 1.0
_SpeedY("Emission Y speed", Float) = 1.0
_SphereMap("Sphere Mask", 2D) = "white" {}
[HDR]_EmissionColor("Emission Color", Color) = (0,0,0,1)
[HDR]_EmissionAltColor("Emission Alt Color", Color) = (0,0,0,1)
_BumpMap("Normal Map", 2D) = "bump" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
_Opacity("Opacity", Range(1,0)) = 0
_SpecularBleed("Specular Bleedthrough", Range(0,1)) = 0.1
_ClampMin("Minimum Light Intensity", Range(0,3)) = 0
_ClampMax("Maximum Light Intensity", Range(1,5)) = 5
_EmissionToggle("Emission Toggle", Float) = 0
// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _Cull ("__cull", Float) = 0.0
[HideInInspector] _OutlineMode("__outline_mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
[HideInInspector] _StencilRef("Stencil Reference Value", Range(0, 255)) = 2
}
SubShader
{
Tags
{
"Queue"="Geometry"
"RenderType" = "Opaque"
"IgnoreProjector"="True"
}
Pass
{
Name "FORWARD"
Tags {
"LightMode" = "ForwardBase"
}
Blend [_SrcBlend] [_DstBlend]
BlendOp Add
ZWrite On
ZTest LEqual
LOD 200
Cull [_Cull]
//ColorMask RGB
CGPROGRAM
#include "UnityCG.cginc"
#include "FlatLitToonCoreMMD Extra.cginc"
#include "RhyShaderHelperFunction.cginc"
#pragma target 5.0
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#pragma only_renderers d3d11 glcore gles
//#pragma multi_compile_instancing
//#pragma fragmentoption ARB_precision_hint_fastest
float2 emissionUV;
float2 emissionMovement;
float emissionMin;
float emissionMax;
LightContainer Lighting;
MatcapContainer Matcap;
float4 frag(VertexOutput i, float facing : VFACE) : COLOR
{
float faceSign = ( facing >= 0 ? 1 : -1 );
float3 bf = normalize(abs(i.normalDir));
bf /= dot(bf, (float3)1);
float2 tx = i.posWorld.yz;
float2 ty = i.posWorld.zx;
float2 tz = i.posWorld.xy;
float4 cx = tex2D(_MainTex, tx) * bf.x;
float4 cy = tex2D(_MainTex, ty) * bf.y;
float4 cz = tex2D(_MainTex, tz) * bf.z;
float4 white = float4(1,1,1,1);
float4 black = float4(0,0,0,0);
float light_Env = float(any(_WorldSpaceLightPos0.xyz));
float4 lightColor = float4(_LightColor0.rgb, _LightColor0.w);
//float4 lightColor = unity_LightColor[0] + unity_LightColor[1] + unity_LightColor[2] + unity_LightColor[3];
float AvgIntensity = (_LightColor0.r + _LightColor0.g + _LightColor0.b)/3;
emissionMin = 0;
emissionMax = 2;
emissionUV = i.uv0;
emissionUV.x += _Time.x * _SpeedX;
emissionUV.y += _Time.x * _SpeedY;
float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
i.normalDir = normalize(i.normalDir);
float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
float3 normalDirection = CalculateNormal(TRANSFORM_TEX(i.uv0, _BumpMap), _BumpMap, tangentTransform);
float4 baseColor = (cx + cy + cz) * _Color;
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
//attenuation = FadeShadows(attenuation, i.posWorld.xyz);
//if(attenuation == 0)
// attenuation = .5;
Lighting = CalculateLight(_WorldSpaceLightPos0, _LightColor0, normalDirection, attenuation, _ClampMin, _ClampMax);
/*
if(AvgIntensity >= emissionMin)
if(AvgIntensity <= emissionMax)
_EmissionIntensity = _EmissionIntensity * clamp((1 - (AvgIntensity/emissionMax)),0,1);
else
_EmissionIntensity = 0;
*/
float4 _EmissionMap_var = tex2D(_EmissionMap,TRANSFORM_TEX(i.uv0, _EmissionMap));
float4 emissionMask_var = tex2D(_EmissionMask,TRANSFORM_TEX(emissionUV, _EmissionMask));
float3 emissive = _EmissionMap_var.rgb;
if(_EmissionToggle == 1)
emissive.rgb *= _EmissionColor;
else
emissive.rgb *= _EmissionAltColor;
emissive.rgb *= emissionMask_var.rgb;
emissive.rgb *= _EmissionIntensity;
float rampValue = smoothstep(0, Lighting.bw_lightDif, 0 - dot(ShadeSH9(float4(0, 0, 0, 1)), grayscale_vector));
float tempValue = (0.5 * dot(normalDirection, Lighting.lightDir) + 0.5);
float3 toonTexColor = tex2D(_ToonTex, tempValue);
float3 shadowTexColor = tex2D(_ShadowTex, rampValue);
float4 shadowMask_var = tex2D(_ShadowMask, TRANSFORM_TEX(i.uv0, _ShadowMask));
Lighting.indirectLit += (shadowTexColor * Lighting.lightCol);
Matcap = CalculateSphere(normalDirection, i, _SphereAddTex, _SphereMulTex, _SphereMap, TRANSFORM_TEX(i.uv0, _SphereMap), _SpecularBleed, faceSign, attenuation);
if(light_Env == 1)
Matcap.Add.rgb *= (Matcap.Mask * _SphereAddIntensity) * Matcap.Shadow;
else
Matcap.Add.rgb *= (Matcap.Mask * _SphereAddIntensity);
Matcap.Mul.rgb *= _SphereMulIntensity;
float finalAlpha = baseColor.a;
if(_Mode == 1)
{
if(finalAlpha - _Cutoff < 0)
clip (finalAlpha - _Cutoff);
else
finalAlpha = 1;
}
if(_Mode == 3)
finalAlpha = _Opacity;
float3 finalColor = emissive + (Matcap.Add + ((_ColorIntensity / 2) * (baseColor.rgb * toonTexColor) * Matcap.Mul)) * (lerp(Lighting.indirectLit, Lighting.directLit, attenuation));
if(light_Env != 1)
finalColor = emissive + (Matcap.Add + ((_ColorIntensity / 2) * (baseColor.rgb * toonTexColor) * Matcap.Mul)) * Lighting.lightCol;
fixed4 finalRGBA = fixed4(baseColor.rgb, finalAlpha);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass
{
Name "FORWARD_DELTA"
Tags
{
"LightMode" = "ForwardAdd"
}
Blend [_SrcBlend] One
ZWrite On
ZTest LEqual
LOD 200
Cull [_Cull]
Fog { Color (0,0,0,0) } // in additive pass fog should be black
CGPROGRAM
#include "FlatLitToonCoreMMD Extra.cginc"
#include "RhyShaderHelperFunction.cginc"
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma only_renderers d3d11 glcore gles
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
LightContainer Lighting;
MatcapContainer Matcap;
float4 frag(VertexOutput i, float facing : VFACE) : COLOR
{
float faceSign = ( facing >= 0 ? 1 : -1 );
float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
float4 white = float4(1,1,1,1);
float4 black = float4(0,0,0,0);
float light_Env = float(any(_WorldSpaceLightPos0.xyz));
//float4 lightColor = _LightColor0.rgb;
i.normalDir = normalize(i.normalDir);
float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
float3 normalDirection = CalculateNormal(TRANSFORM_TEX(i.uv0, _BumpMap), _BumpMap, tangentTransform);
float4 baseColor = CalculateColor(_MainTex, TRANSFORM_TEX(i.uv0, _MainTex), _Color);
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
//attenuation = FadeShadows(attenuation, i.posWorld.xyz);
//if(attenuation == 0)
// attenuation = .5;
Lighting = CalculateLight(_DefaultLightDir, _LightColor0, normalDirection, attenuation, _ClampMin, _ClampMax);
float rampValue = smoothstep(0, Lighting.bw_lightDif, 0 - dot(ShadeSH9(float4(0, 0, 0, 1)), grayscale_vector));
float tempValue = (0.5 * dot(normalDirection, Lighting.lightDir) + 0.5);
float3 toonTexColor = tex2D(_ToonTex, tempValue);
float3 shadowTexColor = tex2D(_ShadowTex, rampValue);
float4 shadowMask_var = tex2D(_ShadowMask,TRANSFORM_TEX(i.uv0, _ShadowMask));
//shadowTexColor += shadowMask_var;
Lighting.indirectLit += (shadowTexColor * Lighting.directLit);
Matcap = CalculateSphere(normalDirection, i, _SphereAddTex, _SphereMulTex, _SphereMap, TRANSFORM_TEX(i.uv0, _SphereMap), _SpecularBleed, faceSign, attenuation);
if(light_Env == 1)
Matcap.Add.rgb *= (Matcap.Mask * _SphereAddIntensity) * Matcap.Shadow;
else
Matcap.Add.rgb *= (Matcap.Mask * _SphereAddIntensity);
Matcap.Mul.rgb *= _SphereMulIntensity;
float finalAlpha = baseColor.a;
if(_Mode == 1)
{
if(finalAlpha - _Cutoff < 0)
clip (finalAlpha - _Cutoff);
else
finalAlpha = 1;
}
if(_Mode == 3)
finalAlpha = _Opacity;
float3 finalColor = (Matcap.Add + ((_ColorIntensity / 2) * (baseColor.rgb * toonTexColor) * Matcap.Mul)) * (lerp(0, Lighting.directLit, attenuation));
if(light_Env != 1)
finalColor = (Matcap.Add + ((_ColorIntensity / 2) * (baseColor.rgb * toonTexColor) * Matcap.Mul)) * Lighting.lightCol;
#if defined(DIRECTIONAL)
finalColor = finalColor * 0.00001;
#endif
fixed4 finalRGBA = fixed4(finalColor, finalAlpha);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass
{
Name "SHADOW_CASTER"
Tags{ "LightMode" = "ShadowCaster" }
Blend [_SrcBlend] [_DstBlend]
ZWrite On
ZTest LEqual
Cull [_Cull]
CGPROGRAM
#include "FlatLitToonShadows.cginc"
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#pragma only_renderers d3d11 glcore gles
#pragma vertex vertShadowCaster
#pragma fragment fragShadowCaster
ENDCG
}
}
Fallback "Toon/Lit (Double)"
CustomEditor "RhyFlatLitMMDEditor"
}