-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSilent FLT Iridescent.shader
236 lines (192 loc) · 9.24 KB
/
Silent FLT Iridescent.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
Shader "Okano/Silent FLT Iridescent"
{
Properties
{
_MainTex("MainTex", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_ColorMask("ColorMask", 2D) = "black" {}
_Shadow("Shadow", Range(0, 1)) = 0.4
_Fresnel ("Fresnel Power", Range(0, 100)) = 20
_LightingRamp ("Lighting Ramp", 2D) = "white" {}
_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" {}
[HDR]_EmissionColor("Emission Color", Color) = (0,0,0,1)
_SpecularMap ("Specular Map", 2D) = "black" {}
_SpecularPower ("Specular Power", Range(0, 100)) = 20.0
_BumpMap("BumpMap", 2D) = "bump" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
_IridescentMap("Iridescent Cubemap", Cube) = "_Skybox" {}
_IridescentMix("Iridescent Mix", Range(0,1)) = 0.3
_IridescentMask("Iridescent Mask", 2D) = "white" {}
_Instability("Instability", Range(0,1)) = 1
// Blending state
[HideInInspector] _Mode ("__mode", 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
}
SubShader
{
Tags
{
"RenderType" = "Opaque"
}
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }
Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]
CGPROGRAM
#include "Libraries/SilentFlatLitToonCore.cginc"
#pragma shader_feature NO_OUTLINE TINTED_OUTLINE COLORED_OUTLINE
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
samplerCUBE _IridescentMap; float4 _IridescentMap_ST;
sampler2D _IridescentMask; float4 _IridescentMask_ST;
float _IridescentMix;
float _Instability;
float4 frag(VertexOutput i) : COLOR
{
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 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float4 _EmissionMap_var = tex2D(_EmissionMap,TRANSFORM_TEX(i.uv0, _EmissionMap));
float3 emissive = (_EmissionMap_var.rgb*_EmissionColor.rgb);
float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
baseColor *= float4(i.col.rgb, 1);
float4 _SpecularMap_var = tex2D(_SpecularMap,TRANSFORM_TEX(i.uv0, _SpecularMap));
float3 specular = (_SpecularMap_var.rgb);
#if COLORED_OUTLINE
if(i.is_outline)
{
baseColor.rgb = i.col.rgb;
}
#endif
#if defined(_ALPHATEST_ON)
//float mask = saturate(interleaved_gradient(i.pos.xy)+_Cutoff);
//float mask = saturate(interleaved_gradient(i.pos.xy)*(_Cutoff*2));f
float mask = _Cutoff;
clip (baseColor.a - mask);
//clip (baseColor.a - _Cutoff);
#endif
float3 lightmap = float4(1.0,1.0,1.0,1.0);
#ifdef LIGHTMAP_ON
lightmap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1 * unity_LightmapST.xy + unity_LightmapST.zw));
#endif
float3 reflectionMap = DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, normalize((_WorldSpaceCameraPos - objPos.rgb)), 7), unity_SpecCube0_HDR)* 0.02;
float grayscalelightcolor = dot(_LightColor0.rgb, grayscale_vector);
float bottomIndirectLighting = grayscaleSH9(float3(0.0, -1.0, 0.0));
float topIndirectLighting = grayscaleSH9(float3(0.0, 1.0, 0.0));
float grayscaleDirectLighting = dot(lightDirection, normalDirection)*grayscalelightcolor*attenuation + grayscaleSH9(normalDirection);
float grayscaleDirectLightingSpecular = dot(viewDirection, normalDirection)*grayscalelightcolor*attenuation + grayscaleSH9(normalDirection);
// Fresnel
float normalDotEye = dot(normalDirection, viewDirection); // ndotv
float fresnelEffect = (pow(1.0-max(0,normalDotEye),_Fresnel));
float tdotv = normalize(dot(i.tangentDir, viewDirection));
float3 iriMapPos = normalize(reflect(tdotv, objPos.xyz) + _WorldSpaceCameraPos);
float3 iriS2Offset = lerp(float3(-iriMapPos.x, iriMapPos.yz), iriMapPos.yxz, _Instability);
float3 iridescentMap = texCUBE(_IridescentMap, iriMapPos) + texCUBE(_IridescentMap, normalDotEye - iriS2Offset);
float lightDifference = topIndirectLighting + grayscalelightcolor - bottomIndirectLighting;
float remappedLight = (grayscaleDirectLighting - bottomIndirectLighting) / lightDifference;
float remappedLightSpecular = (grayscaleDirectLightingSpecular - bottomIndirectLighting) / lightDifference;
float3 indirectLighting = ((ShadeSH9(half4(0.0, -1.0, 0.0, 1.0)) + reflectionMap));
float3 directLighting = ((ShadeSH9(half4(0.0, 1.0, 0.0, 1.0)) + reflectionMap + _LightColor0.rgb));
// MMDs add fresnel through sphere textures, so this isn't that unusual.
// It also works better than running it through the lightramp.
indirectLighting *= 1+fresnelEffect;
directLighting *= 1+fresnelEffect;
//float3 directContribution = saturate((1.0 - _Shadow) + floor(saturate(remappedLight) * 2.0));
//float3 finalColor = emissive + (baseColor * lerp(indirectLighting, directLighting, directContribution));
remappedLight = mad(remappedLight,(1.0-_Shadow), _Shadow);
float3 directContribution = tex2D(_LightingRamp, saturate(float2( remappedLight, 0.0)) );
directContribution = baseColor * lerp(indirectLighting, directLighting, directContribution);
remappedLightSpecular = pow(remappedLightSpecular, _SpecularPower)+fresnelEffect;
float3 specularContribution = tex2D(_LightingRamp, saturate(float2( remappedLightSpecular, 0.0)) );
//specularContribution = specular.rgb*lerp(indirectLighting, directLighting, specularContribution);
specularContribution = specular.rgb*indirectLighting*specularContribution;
float3 iridescentContribution = (iridescentMap * _IridescentMix) * remappedLight;
float3 finalColor = emissive + directContribution + specularContribution + iridescentContribution;
fixed4 finalRGBA = fixed4(finalColor * lightmap, baseColor.a);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass
{
Name "FORWARD_DELTA"
Tags { "LightMode" = "ForwardAdd" }
Blend [_SrcBlend] One
CGPROGRAM
#pragma shader_feature NO_OUTLINE TINTED_OUTLINE COLORED_OUTLINE
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#include "Libraries/SilentFlatLitToonCore.cginc"
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
float4 frag(VertexOutput i) : COLOR
{
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 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
baseColor *= float4(i.col.rgb, 1);
#if COLORED_OUTLINE
if(i.is_outline) {
baseColor.rgb = i.col.rgb;
}
#endif
#if defined(_ALPHATEST_ON)
clip (baseColor.a - interleaved_gradient(i.pos.xy));
#endif
float lightContribution = dot(normalize(_WorldSpaceLightPos0.xyz - i.posWorld.xyz),normalDirection)*attenuation;
float3 directContribution = floor(saturate(lightContribution) * 2.0);
float3 finalColor = baseColor * lerp(0, _LightColor0.rgb, saturate(directContribution + ((1 - _Shadow) * attenuation)));
fixed4 finalRGBA = fixed4(finalColor,1) * i.col;
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass
{
Name "SHADOW_CASTER"
Tags{ "LightMode" = "ShadowCaster" }
ZWrite On ZTest LEqual
CGPROGRAM
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#include "Libraries/FlatLitToonShadows.cginc"
#pragma multi_compile_shadowcaster
#pragma vertex vertShadowCaster
#pragma fragment fragShadowCaster
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "OkanoFlatLitToonSIridescentInspector"
}