This repository has been archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathUnityMetaPass.cginc
366 lines (317 loc) · 11.4 KB
/
UnityMetaPass.cginc
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
#ifndef UNITY_META_PASS_INCLUDED
#define UNITY_META_PASS_INCLUDED
CBUFFER_START(UnityMetaPass)
// x = use uv1 as raster position
// y = use uv2 as raster position
bool4 unity_MetaVertexControl;
// x = return albedo
// y = return normal
bool4 unity_MetaFragmentControl;
// Control which VisualizationMode we will
// display in the editor
int unity_VisualizationMode;
CBUFFER_END
struct UnityMetaInput
{
half3 Albedo;
half3 Emission;
half3 SpecularColor;
#ifdef EDITOR_VISUALIZATION
float2 VizUV;
float4 LightCoord;
#endif
};
#ifdef EDITOR_VISUALIZATION
//Visualization defines
// Should be kept in sync with the EditorVisualizationMode enum in EditorCameraDrawing.cpp
#define EDITORVIZ_PBR_VALIDATION_ALBEDO 0
#define EDITORVIZ_PBR_VALIDATION_METALSPECULAR 1
#define EDITORVIZ_TEXTURE 2
#define EDITORVIZ_SHOWLIGHTMASK 3
// Old names...
#define PBR_VALIDATION_ALBEDO EDITORVIZ_PBR_VALIDATION_ALBEDO
#define PBR_VALIDATION_METALSPECULAR EDITORVIZ_PBR_VALIDATION_METALSPECULAR
uniform int _CheckPureMetal = 0;// flag to check only full metal, not partial metal, known because it has metallic features and pure black albedo
uniform int _CheckAlbedo = 0; // if 0, pass through untouched color
uniform half4 _AlbedoCompareColor = half4(0.0, 0.0, 0.0, 0.0);
uniform half _AlbedoMinLuminance = 0.0;
uniform half _AlbedoMaxLuminance = 1.0;
uniform half _AlbedoHueTolerance = 0.1;
uniform half _AlbedoSaturationTolerance = 0.1;
uniform sampler2D unity_EditorViz_Texture;
uniform half4 unity_EditorViz_Texture_ST;
uniform int unity_EditorViz_UVIndex;
uniform half4 unity_EditorViz_Decode_HDR;
uniform bool unity_EditorViz_ConvertToLinearSpace;
uniform half4 unity_EditorViz_ColorMul;
uniform half4 unity_EditorViz_ColorAdd;
uniform half unity_EditorViz_Exposure;
uniform sampler2D unity_EditorViz_LightTexture;
uniform sampler2D unity_EditorViz_LightTextureB;
#define unity_EditorViz_ChannelSelect unity_EditorViz_ColorMul
#define unity_EditorViz_Color unity_EditorViz_ColorAdd
#define unity_EditorViz_LightType unity_EditorViz_UVIndex
uniform float4x4 unity_EditorViz_WorldToLight;
uniform half4 unity_MaterialValidateLowColor = half4(1.0f, 0.0f, 0.0f, 0.0f);
uniform half4 unity_MaterialValidateHighColor = half4(0.0f, 0.0f, 1.0f, 0.0f);
uniform half4 unity_MaterialValidatePureMetalColor = half4(1.0f, 1.0f, 0.0f, 0.0f);
// Define bounds value in linear RGB for fresnel0 values
static const float dieletricMin = 0.02;
static const float dieletricMax = 0.07;
static const float gemsMin = 0.07;
static const float gemsMax = 0.22;
static const float conductorMin = 0.45;
static const float conductorMax = 1.00;
static const float albedoMin = 0.012;
static const float albedoMax = 0.9;
half3 UnityMeta_RGBToHSVHelper(float offset, half dominantColor, half colorone, half colortwo)
{
half H, S, V;
V = dominantColor;
if (V != 0.0)
{
half small = 0.0;
if (colorone > colortwo)
small = colortwo;
else
small = colorone;
half diff = V - small;
if (diff != 0)
{
S = diff / V;
H = offset + ((colorone - colortwo)/diff);
}
else
{
S = 0;
H = offset + (colorone - colortwo);
}
H /= 6.0;
if (H < 6.0)
{
H += 1.0;
}
}
else
{
S = 0;
H = 0;
}
return half3(H, S, V);
}
half3 UnityMeta_RGBToHSV(half3 rgbColor)
{
// when blue is highest valued
if((rgbColor.b > rgbColor.g) && (rgbColor.b > rgbColor.r))
return UnityMeta_RGBToHSVHelper(4.0, rgbColor.b, rgbColor.r, rgbColor.g);
//when green is highest valued
else if(rgbColor.g > rgbColor.r)
return UnityMeta_RGBToHSVHelper(2.0, rgbColor.g, rgbColor.b, rgbColor.r);
//when red is highest valued
else
return UnityMeta_RGBToHSVHelper(0.0, rgbColor.r, rgbColor.g, rgbColor.b);
}
// Pass 0 - Albedo
half4 UnityMeta_pbrAlbedo(UnityMetaInput IN)
{
half3 SpecularColor = IN.SpecularColor;
half3 baseColor = IN.Albedo;
if (IsGammaSpace())
{
baseColor = half3( GammaToLinearSpaceExact(baseColor.x), GammaToLinearSpaceExact(baseColor.y), GammaToLinearSpaceExact(baseColor.z) ); //GammaToLinearSpace(baseColor);
SpecularColor = GammaToLinearSpace(SpecularColor);
}
half3 unTouched = LinearRgbToLuminance(baseColor).xxx; // if no errors, leave color as it was in render
bool isMetal = dot(SpecularColor, float3(0.3333,0.3333,0.3333)) >= conductorMin;
// When checking full range we do not take the luminance but the mean because often in game blue color are highlight as too low whereas this is what we are looking for.
half value = _CheckAlbedo ? LinearRgbToLuminance(baseColor) : dot(baseColor, half3(0.3333, 0.3333, 0.3333));
// Check if we are pure metal with black albedo
if (_CheckPureMetal && isMetal && value != 0.0)
return unity_MaterialValidatePureMetalColor;
if (_CheckAlbedo == 0)
{
// If we have a metallic object, don't complain about low albedo
if (!isMetal && value < albedoMin)
{
return unity_MaterialValidateLowColor;
}
else if (value > albedoMax)
{
return unity_MaterialValidateHighColor;
}
else
{
return half4(unTouched, 0);
}
}
else
{
if (_AlbedoMinLuminance > value)
{
return unity_MaterialValidateLowColor;
}
else if (_AlbedoMaxLuminance < value)
{
return unity_MaterialValidateHighColor;
}
else
{
half3 hsv = UnityMeta_RGBToHSV(IN.Albedo);
half hue = hsv.r;
half sat = hsv.g;
half3 compHSV = UnityMeta_RGBToHSV(_AlbedoCompareColor.rgb);
half compHue = compHSV.r;
half compSat = compHSV.g;
if ((compSat - _AlbedoSaturationTolerance > sat) || ((compHue - _AlbedoHueTolerance > hue) && (compHue - _AlbedoHueTolerance + 1.0 > hue)))
{
return unity_MaterialValidateLowColor;
}
else if ((sat > compSat + _AlbedoSaturationTolerance) || ((hue > compHue + _AlbedoHueTolerance) && (hue > compHue + _AlbedoHueTolerance - 1.0)))
{
return unity_MaterialValidateHighColor;
}
else
{
return half4(unTouched, 0);
}
}
}
return half4(1.0, 0, 0, 1);
}
// Pass 1 - Metal Specular
half4 UnityMeta_pbrMetalspec(UnityMetaInput IN)
{
half3 SpecularColor = IN.SpecularColor;
half4 baseColor = half4(IN.Albedo, 0);
if (IsGammaSpace())
{
baseColor.xyz = GammaToLinearSpace(baseColor.xyz);
SpecularColor = GammaToLinearSpace(SpecularColor);
}
// Take the mean of three channel, works ok.
half value = dot(SpecularColor, half3(0.3333,0.3333,0.3333));
bool isMetal = value >= conductorMin;
half4 outColor = half4(LinearRgbToLuminance(baseColor.xyz).xxx, 1.0f);
if (value < conductorMin)
{
outColor = unity_MaterialValidateLowColor;
}
else if (value > conductorMax)
{
outColor = unity_MaterialValidateHighColor;
}
else if (isMetal)
{
// If we are here we supposed the users want to have a metal, so check if we have a pure metal (black albedo) or not
// if it is not a pure metal, highlight it
if (_CheckPureMetal)
outColor = dot(baseColor.xyz, half3(1,1,1)) == 0 ? outColor : unity_MaterialValidatePureMetalColor;
}
return outColor;
}
#endif // EDITOR_VISUALIZATION
float2 UnityMetaVizUV(int uvIndex, float2 uv0, float2 uv1, float2 uv2, float4 st)
{
if (uvIndex == 0)
return uv0 * st.xy + st.zw;
else if (uvIndex == 1)
return uv1 * st.xy + st.zw;
else
return uv2 * st.xy + st.zw;
}
float4 UnityMetaVertexPosition(float4 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST)
{
#if !defined(EDITOR_VISUALIZATION)
if (unity_MetaVertexControl.x)
{
vertex.xy = uv1 * lightmapST.xy + lightmapST.zw;
// OpenGL right now needs to actually use incoming vertex position,
// so use it in a very dummy way
vertex.z = vertex.z > 0 ? 1.0e-4f : 0.0f;
}
if (unity_MetaVertexControl.y)
{
vertex.xy = uv2 * dynlightmapST.xy + dynlightmapST.zw;
// OpenGL right now needs to actually use incoming vertex position,
// so use it in a very dummy way
vertex.z = vertex.z > 0 ? 1.0e-4f : 0.0f;
}
return mul(UNITY_MATRIX_VP, float4(vertex.xyz, 1.0));
#else
return UnityObjectToClipPos(vertex);
#endif
}
float unity_OneOverOutputBoost;
float unity_MaxOutputValue;
float unity_UseLinearSpace;
half4 UnityMetaFragment (UnityMetaInput IN)
{
half4 res = 0;
#if !defined(EDITOR_VISUALIZATION)
if (unity_MetaFragmentControl.x)
{
res = half4(IN.Albedo,1);
// d3d9 shader compiler doesn't like NaNs and infinity.
unity_OneOverOutputBoost = saturate(unity_OneOverOutputBoost);
// Apply Albedo Boost from LightmapSettings.
res.rgb = clamp(pow(res.rgb, unity_OneOverOutputBoost), 0, unity_MaxOutputValue);
}
if (unity_MetaFragmentControl.y)
{
half3 emission;
if (unity_UseLinearSpace)
emission = IN.Emission;
else
emission = GammaToLinearSpace(IN.Emission);
res = half4(emission, 1.0);
}
#else
if ( unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_ALBEDO)
{
res = UnityMeta_pbrAlbedo(IN);
}
else if (unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_METALSPECULAR)
{
res = UnityMeta_pbrMetalspec(IN);
}
else if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
{
res = tex2D(unity_EditorViz_Texture, IN.VizUV);
if (unity_EditorViz_Decode_HDR.x > 0)
res = half4(DecodeHDR(res, unity_EditorViz_Decode_HDR), 1);
if (unity_EditorViz_ConvertToLinearSpace)
res.rgb = LinearToGammaSpace(res.rgb);
res *= unity_EditorViz_ColorMul;
res += unity_EditorViz_ColorAdd;
res *= exp2(unity_EditorViz_Exposure);
}
else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
{
float result = dot(unity_EditorViz_ChannelSelect, tex2D(unity_EditorViz_Texture, IN.VizUV).rgba);
if (result == 0)
discard;
float atten = 1;
if (unity_EditorViz_LightType == 0)
{
// directional: no attenuation
}
else if (unity_EditorViz_LightType == 1)
{
// point
atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
}
else if (unity_EditorViz_LightType == 2)
{
// spot
atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
float cookie = tex2D(unity_EditorViz_LightTextureB, IN.LightCoord.xy / IN.LightCoord.w + 0.5).w;
atten *= (IN.LightCoord.z > 0) * cookie;
}
clip(atten - 0.001f);
res = float4(unity_EditorViz_Color.xyz * result, unity_EditorViz_Color.w);
}
#endif // EDITOR_VISUALIZATION
return res;
}
#endif // UNITY_META_PASS_INCLUDED