-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCModelShaders.frag.metal
389 lines (355 loc) · 15.8 KB
/
CModelShaders.frag.metal
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
constant float3 kRGBToYPrime = float3(0.299, 0.587, 0.114);
/*
#if defined(ALPHA_%s)
#define SampleTexture_%s() %s.sample(samp, vtf.%sUv).aaa
#define SampleTextureAlpha_%s() %s.sample(samp, vtf.%sUv).a
#else
#define SampleTexture_%s() %s.sample(samp, vtf.%sUv).rgb
#define SampleTextureAlpha_%s() dot(%s.sample(samp, vtf.%sUv).rgb, kRGBToYPrime)
#endif
*/
#if defined(ALPHA_lightmap)
#define SampleTexture_lightmap() lightmap.sample(samp, vtf.lightmapUv).aaa
#define SampleTextureAlpha_lightmap() lightmap.sample(samp, vtf.lightmapUv).a
#else
#define SampleTexture_lightmap() lightmap.sample(samp, vtf.lightmapUv).rgb
#define SampleTextureAlpha_lightmap() dot(lightmap.sample(samp, vtf.lightmapUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_diffuse)
#define SampleTexture_diffuse() diffuse.sample(samp, vtf.diffuseUv).aaa
#define SampleTextureAlpha_diffuse() diffuse.sample(samp, vtf.diffuseUv).a
#else
#define SampleTexture_diffuse() diffuse.sample(samp, vtf.diffuseUv).rgb
#define SampleTextureAlpha_diffuse() dot(diffuse.sample(samp, vtf.diffuseUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_emissive)
#define SampleTexture_emissive() emissive.sample(samp, vtf.emissiveUv).aaa
#define SampleTextureAlpha_emissive() emissive.sample(samp, vtf.emissiveUv).a
#else
#define SampleTexture_emissive() emissive.sample(samp, vtf.emissiveUv).rgb
#define SampleTextureAlpha_emissive() dot(emissive.sample(samp, vtf.emissiveUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_specular)
#define SampleTexture_specular() specular.sample(samp, vtf.specularUv).aaa
#define SampleTextureAlpha_specular() specular.sample(samp, vtf.specularUv).a
#else
#define SampleTexture_specular() specular.sample(samp, vtf.specularUv).rgb
#define SampleTextureAlpha_specular() dot(specular.sample(samp, vtf.specularUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_extendedSpecular)
#define SampleTexture_extendedSpecular() extendedSpecular.sample(samp, vtf.extendedSpecularUv).aaa
#define SampleTextureAlpha_extendedSpecular() extendedSpecular.sample(samp, vtf.extendedSpecularUv).a
#else
#define SampleTexture_extendedSpecular() extendedSpecular.sample(samp, vtf.extendedSpecularUv).rgb
#define SampleTextureAlpha_extendedSpecular() dot(extendedSpecular.sample(samp, vtf.extendedSpecularUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_reflection)
#define SampleTexture_reflection() reflection.sample(samp, vtf.reflectionUv).aaa
#define SampleTextureAlpha_reflection() reflection.sample(samp, vtf.reflectionUv).a
#else
#define SampleTexture_reflection() reflection.sample(samp, vtf.reflectionUv).rgb
#define SampleTextureAlpha_reflection() dot(reflection.sample(samp, vtf.reflectionUv).rgb, kRGBToYPrime)
#endif
#if defined(ALPHA_alpha)
#define SampleTexture_alpha() alpha.sample(samp, vtf.alphaUv).aaa
#define SampleTextureAlpha_alpha() alpha.sample(samp, vtf.alphaUv).a
#else
#define SampleTexture_alpha() alpha.sample(samp, vtf.alphaUv).rgb
#define SampleTextureAlpha_alpha() dot(alpha.sample(samp, vtf.alphaUv).rgb, kRGBToYPrime)
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW) || defined(URDE_DISINTEGRATE)
struct Fog {
float4 color;
float A;
float B;
float C;
int mode;
};
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
struct Light {
float4 pos;
float4 dir;
float4 color;
float4 linAtt;
float4 angAtt;
};
struct LightingUniform {
Light lights[URDE_MAX_LIGHTS];
float4 ambient;
float4 colorReg0;
float4 colorReg1;
float4 colorReg2;
float4 mulColor;
float4 addColor;
Fog fog;
};
#define colorReg0 lu.colorReg0
#define colorReg1 lu.colorReg1
#define colorReg2 lu.colorReg2
#else
constant float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);
constant float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);
constant float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_CUBE_REFLECTION)
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
float4 ret = lu.ambient;
for (int i = 0; i < URDE_MAX_LIGHTS; ++i) {
float3 delta = vtf.mvPos.xyz - lu.lights[i].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lu.lights[i].dir.xyz), 0.0);
float att = 1.0 / (lu.lights[i].linAtt[2] * dist * dist +
lu.lights[i].linAtt[1] * dist +
lu.lights[i].linAtt[0]);
float angAtt = lu.lights[i].angAtt[2] * angDot * angDot +
lu.lights[i].angAtt[1] * angDot +
lu.lights[i].angAtt[0];
ret += lu.lights[i].color * angAtt * att * max(dot(-deltaNorm, vtf.mvNorm.xyz), 0.0);
}
return saturate(ret.rgb);
}
#endif
#if defined(URDE_THERMAL_MODEL)
struct LightingUniform {
float4 tmulColor;
float4 taddColor;
};
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_THERMAL_COLD) || defined(URDE_THERMAL_STATIC)
struct LightingUniform {};
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_SOLID)
struct LightingUniform {
float4 solidColor;
};
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_MB_SHADOW)
struct LightingUniform {
float4 shadowUp;
float shadowId;
};
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
float2 shadowUV = vtf.extUvs0;
shadowUV.y = 1.0 - shadowUV.y;
float4 ret = lu.ambient;
float3 delta = vtf.mvPos.xyz - lu.lights[0].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lu.lights[0].dir.xyz), 0.0);
float att = 1.0 / (lu.lights[0].linAtt[2] * dist * dist +
lu.lights[0].linAtt[1] * dist +
lu.lights[0].linAtt[0]);
float angAtt = lu.lights[0].angAtt[2] * angDot * angDot +
lu.lights[0].angAtt[1] * angDot +
lu.lights[0].angAtt[0];
ret += lu.lights[0].color * angAtt * att * max(dot(-deltaNorm, vtf.mvNorm.xyz), 0.0) *
extTex0.sample(clampSamp, shadowUV).r;
for (int i = 1; i < URDE_MAX_LIGHTS; ++i) {
float3 delta = vtf.mvPos.xyz - lu.lights[i].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lu.lights[i].dir.xyz), 0.0);
float att = 1.0 / (lu.lights[i].linAtt[2] * dist * dist +
lu.lights[i].linAtt[1] * dist +
lu.lights[i].linAtt[0]);
float angAtt = lu.lights[i].angAtt[2] * angDot * angDot +
lu.lights[i].angAtt[1] * angDot +
lu.lights[i].angAtt[0];
ret += lu.lights[i].color * angAtt * att * max(dot(-deltaNorm, vtf.mvNorm.xyz), 0.0);
}
return saturate(ret.rgb);
}
#endif
#if defined(URDE_DISINTEGRATE)
struct LightingUniform {
float4 daddColor;
Fog fog;
};
float3 LightingFunc(thread VertToFrag& vtf, constant LightingUniform& lu, texture2d<float> extTex0, sampler clampSamp) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW) || defined(URDE_DISINTEGRATE)
float4 FogFunc(thread VertToFrag& vtf, constant LightingUniform& lu, float4 colorIn) {
float fogZ;
float fogF = saturate((lu.fog.A / (lu.fog.B - (1.0 - vtf.mvpPos.z))) - lu.fog.C);
switch (lu.fog.mode) {
case 2:
fogZ = fogF;
break;
case 4:
fogZ = 1.0 - exp2(-8.0 * fogF);
break;
case 5:
fogZ = 1.0 - exp2(-8.0 * fogF * fogF);
break;
case 6:
fogZ = exp2(-8.0 * (1.0 - fogF));
break;
case 7:
fogF = 1.0 - fogF;
fogZ = exp2(-8.0 * fogF * fogF);
break;
default:
fogZ = 0.0;
break;
}
#ifdef BLEND_DST_ONE
return float4(mix(colorIn, float4(0.0, 0.0, 0.0, 0.0), saturate(fogZ)).rgb, colorIn.a);
#else
return float4(mix(colorIn, lu.fog.color, saturate(fogZ)).rgb, colorIn.a);
#endif
}
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
return FogFunc(vtf, lu, colorIn) * lu.mulColor + lu.addColor;
}
#endif
#if defined(URDE_THERMAL_MODEL)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
return extTex0.sample(samp, vtf.extUvs0).rrrr * lu.tmulColor + lu.taddColor;
}
#endif
#if defined(URDE_THERMAL_COLD)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
return colorIn * float4(0.75, 0.75, 0.75, 0.75);
}
#endif
#if defined(URDE_THERMAL_STATIC)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
return colorIn;
}
#endif
#if defined(URDE_SOLID)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
return lu.solidColor;
}
#endif
#if defined(URDE_MB_SHADOW)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
float idTexel = extTex0.sample(samp, vtf.extUvs0).a;
float sphereTexel = extTex1.sample(clampEdgeSamp, vtf.extUvs1).r;
float fadeTexel = extTex2.sample(clampEdgeSamp, vtf.extUvs2).a;
float val = ((abs(idTexel - lu.shadowId) < 0.001) ?
(dot(vtf.mvNorm.xyz, lu.shadowUp.xyz) * lu.shadowUp.w) : 0.0) *
sphereTexel * fadeTexel;
return float4(0.0, 0.0, 0.0, val);
}
#endif
#if defined(URDE_DISINTEGRATE)
float4 PostFunc(thread VertToFrag& vtf, constant LightingUniform& lu,
texture2d<float> extTex0, texture2d<float> extTex1, texture2d<float> extTex2,
sampler samp, sampler clampSamp, sampler clampEdgeSamp, float4 colorIn) {
float4 texel0 = extTex0.sample(samp, vtf.extUvs0);
float4 texel1 = extTex0.sample(samp, vtf.extUvs1);
colorIn = mix(float4(0.0, 0.0, 0.0, 0.0), texel1, texel0);
colorIn.rgb += lu.daddColor.rgb;
return FogFunc(vtf, lu, colorIn);
}
#endif
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
#define ReflectionFunc(roughness) \
(reflectionTex.sample(reflectSamp, float3(reflectionCoords.x, -reflectionCoords.y, reflectionCoords.z), bias(roughness)).rgb)
#elif defined(URDE_REFLECTION_SIMPLE)
#define ReflectionFunc() \
(reflectionTex.sample(reflectSamp, vtf.dynReflectionUvs1).rgb * vtf.dynReflectionAlpha)
#elif defined(URDE_REFLECTION_INDIRECT)
#define ReflectionFunc() \
(reflectionTex.sample(reflectSamp, (reflectionIndTex.sample(samp, vtf.dynReflectionUvs0).ab - \
float2(0.5, 0.5)) * float2(0.5, 0.5) + vtf.dynReflectionUvs1).rgb * vtf.dynReflectionAlpha)
#else
#define ReflectionFunc() float3(0.0, 0.0, 0.0)
#endif
#if !defined(URDE_ALPHA_TEST)
[[ early_fragment_tests ]]
#endif
fragment float4 fmain(VertToFrag vtf [[ stage_in ]],
sampler samp [[ sampler(0) ]],
sampler clampSamp [[ sampler(1) ]],
sampler reflectSamp [[ sampler(2) ]],
sampler clampEdgeSamp [[ sampler(3) ]],
texture2d<float> lightmap [[ texture(0) ]],
texture2d<float> diffuse [[ texture(1) ]],
texture2d<float> emissive [[ texture(2) ]],
texture2d<float> specular [[ texture(3) ]],
texture2d<float> extendedSpecular [[ texture(4) ]],
texture2d<float> reflection [[ texture(5) ]],
texture2d<float> alpha [[ texture(6) ]],
texture2d<float> reflectionIndTex [[ texture(7) ]],
texture2d<float> extTex0 [[ texture(8) ]],
texture2d<float> extTex1 [[ texture(9) ]],
texture2d<float> extTex2 [[ texture(10) ]],
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
texturecube<float> reflectionTex [[ texture(11) ]],
#else
texture2d<float> reflectionTex [[ texture(11) ]],
#endif
constant LightingUniform& lu [[ buffer(4) ]]) {
float3 lighting = LightingFunc(vtf, lu, extTex0, clampSamp);
float4 tmp;
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
float3 reflectionCoords = reflect(vtf.mvPos.xyz, vtf.mvNorm.xyz);
tmp.rgb = (SampleTexture_lightmap() * colorReg1.rgb + lighting) * SampleTexture_diffuse() +
SampleTexture_emissive() + (SampleTexture_specular() + SampleTexture_extendedSpecular() * lighting) *
(SampleTexture_reflection() * ReflectionFunc(saturate(0.5 - SampleTextureAlpha_specular())) * 2.0);
tmp.a = SampleTextureAlpha_alpha();
#elif defined(URDE_DIFFUSE_ONLY)
tmp.rgb = SampleTexture_diffuse();
tmp.a = SampleTextureAlpha_alpha();
#elif defined(RETRO_SHADER)
tmp.rgb = (SampleTexture_lightmap() * colorReg1.rgb + lighting) * SampleTexture_diffuse() +
SampleTexture_emissive() + (SampleTexture_specular() + SampleTexture_extendedSpecular() * lighting) *
SampleTexture_reflection() + ReflectionFunc();
tmp.a = SampleTextureAlpha_alpha();
#elif defined(RETRO_DYNAMIC_SHADER)
tmp.rgb = (SampleTexture_lightmap() * colorReg1.rgb + lighting) * SampleTexture_diffuse() * colorReg1.rgb +
SampleTexture_emissive() * colorReg1.rgb + (SampleTexture_specular() + SampleTexture_extendedSpecular() * lighting) *
SampleTexture_reflection() + ReflectionFunc();
tmp.a = SampleTextureAlpha_alpha();
#elif defined(RETRO_DYNAMIC_ALPHA_SHADER)
tmp.rgb = (SampleTexture_lightmap() * colorReg1.rgb + lighting) * SampleTexture_diffuse() * colorReg1.rgb +
SampleTexture_emissive() * colorReg1.rgb + (SampleTexture_specular() + SampleTexture_extendedSpecular() * lighting) *
SampleTexture_reflection() + ReflectionFunc();
tmp.a = SampleTextureAlpha_alpha() * colorReg1.a;
#elif defined(RETRO_DYNAMIC_CHARACTER_SHADER)
tmp.rgb = (SampleTexture_lightmap() + lighting) * SampleTexture_diffuse() +
SampleTexture_emissive() * colorReg1.rgb + (SampleTexture_specular() + SampleTexture_extendedSpecular() * lighting) *
SampleTexture_reflection() + ReflectionFunc();
tmp.a = SampleTextureAlpha_alpha();
#endif
float4 colorOut = PostFunc(vtf, lu, extTex0, extTex1, extTex2, samp, clampSamp, clampEdgeSamp, tmp);
#if defined(URDE_ALPHA_TEST)
if (colorOut.a < 0.25)
discard_fragment();
#endif
return colorOut;
}