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
|
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
#ifndef UNITY_STANDARD_SHADOW_INCLUDED
#define UNITY_STANDARD_SHADOW_INCLUDED
// NOTE: had to split shadow functions into separate file,
// otherwise compiler gives trouble with LIGHTING_COORDS macro (in UnityStandardCore.cginc)
#include "UnityCG.cginc"
#include "UnityShaderVariables.cginc"
#include "UnityInstancing.cginc"
#include "UnityStandardConfig.cginc"
#include "UnityStandardUtils.cginc"
#if (defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)) && defined(UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS)
#define UNITY_STANDARD_USE_DITHER_MASK 1
#endif
// Need to output UVs in shadow caster, since we need to sample texture and do clip/dithering based on it
#if defined(_ALPHATEST_ON) || defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)
#define UNITY_STANDARD_USE_SHADOW_UVS 1
#endif
// Has a non-empty shadow caster output struct (it's an error to have empty structs on some platforms...)
#if !defined(V2F_SHADOW_CASTER_NOPOS_IS_EMPTY) || defined(UNITY_STANDARD_USE_SHADOW_UVS)
#define UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT 1
#endif
#ifdef UNITY_STEREO_INSTANCING_ENABLED
#define UNITY_STANDARD_USE_STEREO_SHADOW_OUTPUT_STRUCT 1
#endif
half4 _Color;
half _Cutoff;
sampler2D _MainTex;
float4 _MainTex_ST;
#ifdef UNITY_STANDARD_USE_DITHER_MASK
sampler3D _DitherMaskLOD;
#endif
// Handle PremultipliedAlpha from Fade or Transparent shading mode
half4 _SpecColor;
half _Metallic;
#ifdef _SPECGLOSSMAP
sampler2D _SpecGlossMap;
#endif
#ifdef _METALLICGLOSSMAP
sampler2D _MetallicGlossMap;
#endif
#if defined(UNITY_STANDARD_USE_SHADOW_UVS) && defined(_PARALLAXMAP)
sampler2D _ParallaxMap;
half _Parallax;
#endif
//MFX
#include "Assets/MaterializeFX/Shaders/cginc/MFX.cginc"
half MetallicSetup_ShadowGetOneMinusReflectivity(half2 uv)
{
half metallicity = _Metallic;
#ifdef _METALLICGLOSSMAP
metallicity = tex2D(_MetallicGlossMap, uv).r;
#endif
return OneMinusReflectivityFromMetallic(metallicity);
}
half RoughnessSetup_ShadowGetOneMinusReflectivity(half2 uv)
{
half metallicity = _Metallic;
#ifdef _METALLICGLOSSMAP
metallicity = tex2D(_MetallicGlossMap, uv).r;
#endif
return OneMinusReflectivityFromMetallic(metallicity);
}
half SpecularSetup_ShadowGetOneMinusReflectivity(half2 uv)
{
half3 specColor = _SpecColor.rgb;
#ifdef _SPECGLOSSMAP
specColor = tex2D(_SpecGlossMap, uv).rgb;
#endif
return (1 - SpecularStrength(specColor));
}
// SHADOW_ONEMINUSREFLECTIVITY(): workaround to get one minus reflectivity based on UNITY_SETUP_BRDF_INPUT
#define SHADOW_JOIN2(a, b) a##b
#define SHADOW_JOIN(a, b) SHADOW_JOIN2(a,b)
#define SHADOW_ONEMINUSREFLECTIVITY SHADOW_JOIN(UNITY_SETUP_BRDF_INPUT, _ShadowGetOneMinusReflectivity)
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv0 : TEXCOORD0;
#if defined(UNITY_STANDARD_USE_SHADOW_UVS) && defined(_PARALLAXMAP)
half4 tangent : TANGENT;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
#ifdef UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT
struct VertexOutputShadowCaster
{
V2F_SHADOW_CASTER_NOPOS
#if defined(UNITY_STANDARD_USE_SHADOW_UVS)
float2 tex : TEXCOORD1;
#if defined(_PARALLAXMAP)
half3 viewDirForParallax : TEXCOORD2;
#endif
#endif
//MFX
float4 mfxUv : TEXCOORD5;
float3 worldPos : TEXCOORD6;
};
#endif
#ifdef UNITY_STANDARD_USE_STEREO_SHADOW_OUTPUT_STRUCT
struct VertexOutputStereoShadowCaster
{
UNITY_VERTEX_OUTPUT_STEREO
//MFX
float4 mfxUv : TEXCOORD5;
float3 worldPos : TEXCOORD6;
};
#endif
// We have to do these dances of outputting SV_POSITION separately from the vertex shader,
// and inputting VPOS in the pixel shader, since they both map to "POSITION" semantic on
// some platforms, and then things don't go well.
void vertShadowCaster (VertexInput v
, out float4 opos : SV_POSITION
#ifdef UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT
, out VertexOutputShadowCaster o
#endif
#ifdef UNITY_STANDARD_USE_STEREO_SHADOW_OUTPUT_STRUCT
, out VertexOutputStereoShadowCaster os
#endif
)
{
UNITY_SETUP_INSTANCE_ID(v);
#ifdef UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT
UNITY_INITIALIZE_OUTPUT(VertexOutputShadowCaster, o);
#endif
#ifdef UNITY_STANDARD_USE_STEREO_SHADOW_OUTPUT_STRUCT
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(os);
#endif
TRANSFER_SHADOW_CASTER_NOPOS(o,opos)
#if defined(UNITY_STANDARD_USE_SHADOW_UVS)
o.tex = TRANSFORM_TEX(v.uv0, _MainTex);
#ifdef _PARALLAXMAP
TANGENT_SPACE_ROTATION;
o.viewDirForParallax = mul (rotation, ObjSpaceViewDir(v.vertex));
#endif
#endif
#ifdef UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT
//MFX
PassMfxVertex2Fragment(v.uv0.xy, o.mfxUv);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
#endif
#ifdef UNITY_STANDARD_USE_STEREO_SHADOW_OUTPUT_STRUCT
//MFX
PassMfxVertex2Fragment(v.uv0.xy, os.mfxUv);
os.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
#endif
}
half4 fragShadowCaster (UNITY_POSITION(vpos)
#ifdef UNITY_STANDARD_USE_SHADOW_OUTPUT_STRUCT
, VertexOutputShadowCaster i
#endif
) : SV_Target
{
#if defined(UNITY_STANDARD_USE_SHADOW_UVS)
#if defined(_PARALLAXMAP) && (SHADER_TARGET >= 30)
half3 viewDirForParallax = normalize(i.viewDirForParallax);
fixed h = tex2D (_ParallaxMap, i.tex.xy).g;
half2 offset = ParallaxOffset1Step (h, _Parallax, viewDirForParallax);
i.tex.xy += offset;
#endif
//MFX
half alpha = GetMfxDissolve(i.mfxUv, i.worldPos);
MfxClip(alpha);
// half alpha = tex2D(_MainTex, i.tesx).a * _Color.a;
// #if defined(_ALPHATEST_ON)
// clip (alpha - _Cutoff);
// #endif
// #if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)
// #if defined(_ALPHAPREMULTIPLY_ON)
// half outModifiedAlpha;
// PreMultiplyAlpha(half3(0, 0, 0), alpha, SHADOW_ONEMINUSREFLECTIVITY(i.tex), outModifiedAlpha);
// alpha = outModifiedAlpha;
// #endif
// #if defined(UNITY_STANDARD_USE_DITHER_MASK)
// // Use dither mask for alpha blended shadows, based on pixel position xy
// // and alpha level. Our dither texture is 4x4x16.
// #ifdef LOD_FADE_CROSSFADE
// #define _LOD_FADE_ON_ALPHA
// alpha *= unity_LODFade.y;
// #endif
// half alphaRef = tex3D(_DitherMaskLOD, float3(vpos.xy*0.25,alpha*0.9375)).a;
// clip (alphaRef - 0.01);
// #else
// clip (alpha - _Cutoff);
// #endif
// #endif
#endif // #if defined(UNITY_STANDARD_USE_SHADOW_UVS)
#ifdef LOD_FADE_CROSSFADE
#ifdef _LOD_FADE_ON_ALPHA
#undef _LOD_FADE_ON_ALPHA
#else
UnityApplyDitherCrossFade(vpos.xy);
#endif
#endif
SHADOW_CASTER_FRAGMENT(i)
}
#endif // UNITY_STANDARD_SHADOW_INCLUDED
|