-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCModelShaders.frag.hlsl
373 lines (337 loc) · 14.1 KB
/
CModelShaders.frag.hlsl
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
SamplerState samp : register(s0);
SamplerState clampSamp : register(s1);
SamplerState reflectSamp : register(s2);
SamplerState clampEdgeSamp : register(s3);
Texture2D lightmap : register(t0);
Texture2D diffuse : register(t1);
Texture2D emissive : register(t2);
Texture2D specular : register(t3);
Texture2D extendedSpecular : register(t4);
Texture2D reflection : register(t5);
Texture2D alpha : register(t6);
Texture2D reflectionIndTex : register(t7);
Texture2D extTex0 : register(t8);
Texture2D extTex1 : register(t9);
Texture2D extTex2 : register(t10);
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
TextureCube reflectionTex : register(t11);
#else
Texture2D reflectionTex : register(t11);
#endif
static const float3 kRGBToYPrime = float3(0.299, 0.587, 0.114);
/*
#if defined(ALPHA_%s)
float3 SampleTexture_%s(in VertToFrag vtf) { return %s.Sample(samp, vtf.%sUv).aaa; }
float SampleTextureAlpha_%s(in VertToFrag vtf) { return %s.Sample(samp, vtf.%sUv).a; }
#else
float3 SampleTexture_%s(in VertToFrag vtf) { return %s.Sample(samp, vtf.%sUv).rgb; }
float SampleTextureAlpha_%s(in VertToFrag vtf) { return dot(%s.Sample(samp, vtf.%sUv).rgb, kRGBToYPrime); }
#endif
*/
#if defined(ALPHA_lightmap)
float3 SampleTexture_lightmap(in VertToFrag vtf) { return lightmap.Sample(samp, vtf.lightmapUv).aaa; }
float SampleTextureAlpha_lightmap(in VertToFrag vtf) { return lightmap.Sample(samp, vtf.lightmapUv).a; }
#else
float3 SampleTexture_lightmap(in VertToFrag vtf) { return lightmap.Sample(samp, vtf.lightmapUv).rgb; }
float SampleTextureAlpha_lightmap(in VertToFrag vtf) { return dot(lightmap.Sample(samp, vtf.lightmapUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_diffuse)
float3 SampleTexture_diffuse(in VertToFrag vtf) { return diffuse.Sample(samp, vtf.diffuseUv).aaa; }
float SampleTextureAlpha_diffuse(in VertToFrag vtf) { return diffuse.Sample(samp, vtf.diffuseUv).a; }
#else
float3 SampleTexture_diffuse(in VertToFrag vtf) { return diffuse.Sample(samp, vtf.diffuseUv).rgb; }
float SampleTextureAlpha_diffuse(in VertToFrag vtf) { return dot(diffuse.Sample(samp, vtf.diffuseUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_emissive)
float3 SampleTexture_emissive(in VertToFrag vtf) { return emissive.Sample(samp, vtf.emissiveUv).aaa; }
float SampleTextureAlpha_emissive(in VertToFrag vtf) { return emissive.Sample(samp, vtf.emissiveUv).a; }
#else
float3 SampleTexture_emissive(in VertToFrag vtf) { return emissive.Sample(samp, vtf.emissiveUv).rgb; }
float SampleTextureAlpha_emissive(in VertToFrag vtf) { return dot(emissive.Sample(samp, vtf.emissiveUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_specular)
float3 SampleTexture_specular(in VertToFrag vtf) { return specular.Sample(samp, vtf.specularUv).aaa; }
float SampleTextureAlpha_specular(in VertToFrag vtf) { return specular.Sample(samp, vtf.specularUv).a; }
#else
float3 SampleTexture_specular(in VertToFrag vtf) { return specular.Sample(samp, vtf.specularUv).rgb; }
float SampleTextureAlpha_specular(in VertToFrag vtf) { return dot(specular.Sample(samp, vtf.specularUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_extendedSpecular)
float3 SampleTexture_extendedSpecular(in VertToFrag vtf) { return extendedSpecular.Sample(samp, vtf.extendedSpecularUv).aaa; }
float SampleTextureAlpha_extendedSpecular(in VertToFrag vtf) { return extendedSpecular.Sample(samp, vtf.extendedSpecularUv).a; }
#else
float3 SampleTexture_extendedSpecular(in VertToFrag vtf) { return extendedSpecular.Sample(samp, vtf.extendedSpecularUv).rgb; }
float SampleTextureAlpha_extendedSpecular(in VertToFrag vtf) { return dot(extendedSpecular.Sample(samp, vtf.extendedSpecularUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_reflection)
float3 SampleTexture_reflection(in VertToFrag vtf) { return reflection.Sample(samp, vtf.reflectionUv).aaa; }
float SampleTextureAlpha_reflection(in VertToFrag vtf) { return reflection.Sample(samp, vtf.reflectionUv).a; }
#else
float3 SampleTexture_reflection(in VertToFrag vtf) { return reflection.Sample(samp, vtf.reflectionUv).rgb; }
float SampleTextureAlpha_reflection(in VertToFrag vtf) { return dot(reflection.Sample(samp, vtf.reflectionUv).rgb, kRGBToYPrime); }
#endif
#if defined(ALPHA_alpha)
float3 SampleTexture_alpha(in VertToFrag vtf) { return alpha.Sample(samp, vtf.alphaUv).aaa; }
float SampleTextureAlpha_alpha(in VertToFrag vtf) { return alpha.Sample(samp, vtf.alphaUv).a; }
#else
float3 SampleTexture_alpha(in VertToFrag vtf) { return alpha.Sample(samp, vtf.alphaUv).rgb; }
float SampleTextureAlpha_alpha(in VertToFrag vtf) { return 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;
};
cbuffer LightingUniform : register(b2) {
Light lights[URDE_MAX_LIGHTS];
float4 ambient;
float4 colorReg0;
float4 colorReg1;
float4 colorReg2;
float4 mulColor;
float4 addColor;
Fog fog;
};
#else
static const float4 colorReg0 = float4(1.0, 1.0, 1.0, 1.0);
static const float4 colorReg1 = float4(1.0, 1.0, 1.0, 1.0);
static const float4 colorReg2 = float4(1.0, 1.0, 1.0, 1.0);
#endif
#if defined(URDE_LIGHTING) || defined(URDE_LIGHTING_CUBE_REFLECTION)
float3 LightingFunc(in VertToFrag vtf) {
float4 ret = ambient;
for (int i = 0; i < URDE_MAX_LIGHTS; ++i) {
float3 delta = vtf.mvPos.xyz - lights[i].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lights[i].dir.xyz), 0.0);
float att = 1.0 / (lights[i].linAtt[2] * dist * dist +
lights[i].linAtt[1] * dist +
lights[i].linAtt[0]);
float angAtt = lights[i].angAtt[2] * angDot * angDot +
lights[i].angAtt[1] * angDot +
lights[i].angAtt[0];
ret += lights[i].color * angAtt * att * max(dot(-deltaNorm, vtf.mvNorm.xyz), 0.0);
}
return saturate(ret.rgb);
}
#endif
#if defined(URDE_THERMAL_MODEL)
float3 LightingFunc(in VertToFrag vtf) {
return float3(1.0, 1.0, 1.0);
}
cbuffer ThermalUniform : register(b2) {
float4 tmulColor;
float4 taddColor;
};
#endif
#if defined(URDE_THERMAL_COLD) || defined(URDE_THERMAL_STATIC)
float3 LightingFunc(in VertToFrag vtf) {
return float3(1.0, 1.0, 1.0);
}
#endif
#if defined(URDE_SOLID)
float3 LightingFunc(in VertToFrag vtf) {
return float3(1.0, 1.0, 1.0);
}
cbuffer SolidUniform : register(b2) {
float4 solidColor;
};
#endif
#if defined(URDE_MB_SHADOW)
float3 LightingFunc(in VertToFrag vtf) {
return float3(1.0, 1.0, 1.0);
}
cbuffer MBShadowUniform : register(b2) {
float4 shadowUp;
float shadowId;
};
#endif
#if defined(URDE_LIGHTING_SHADOW) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
float3 LightingFunc(in VertToFrag vtf) {
float2 shadowUV = vtf.extUvs[0];
shadowUV.y = 1.0 - shadowUV.y;
float4 ret = ambient;
float3 delta = vtf.mvPos.xyz - lights[0].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lights[0].dir.xyz), 0.0);
float att = 1.0 / (lights[0].linAtt[2] * dist * dist +
lights[0].linAtt[1] * dist +
lights[0].linAtt[0]);
float angAtt = lights[0].angAtt[2] * angDot * angDot +
lights[0].angAtt[1] * angDot +
lights[0].angAtt[0];
ret += 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 - lights[i].pos.xyz;
float dist = length(delta);
float3 deltaNorm = delta / dist;
float angDot = max(dot(deltaNorm, lights[i].dir.xyz), 0.0);
float att = 1.0 / (lights[i].linAtt[2] * dist * dist +
lights[i].linAtt[1] * dist +
lights[i].linAtt[0]);
float angAtt = lights[i].angAtt[2] * angDot * angDot +
lights[i].angAtt[1] * angDot +
lights[i].angAtt[0];
ret += lights[i].color * angAtt * att * max(dot(-deltaNorm, vtf.mvNorm.xyz), 0.0);
}
return saturate(ret.rgb);
}
#endif
#if defined(URDE_DISINTEGRATE)
cbuffer DisintegrateUniform : register(b2) {
float4 daddColor;
Fog fog;
};
float3 LightingFunc(in VertToFrag vtf) {
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(in VertToFrag vtf, float4 colorIn) {
float fogZ;
float fogF = saturate((fog.A / (fog.B - (1.0 - vtf.mvpPos.z))) - fog.C);
switch (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(lerp(colorIn, float4(0.0, 0.0, 0.0, 0.0), saturate(fogZ)).rgb, colorIn.a);
#else
return float4(lerp(colorIn, 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(in VertToFrag vtf, float4 colorIn) {
return FogFunc(vtf, colorIn) * mulColor + addColor;
}
#endif
#if defined(URDE_THERMAL_MODEL)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
return extTex0.Sample(samp, vtf.extUvs[0]).rrrr * tmulColor + taddColor;
}
#endif
#if defined(URDE_THERMAL_COLD)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
return colorIn * float4(0.75, 0.75, 0.75, 0.75);
}
#endif
#if defined(URDE_THERMAL_STATIC)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
return colorIn;
}
#endif
#if defined(URDE_SOLID)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
return solidColor;
}
#endif
#if defined(URDE_MB_SHADOW)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
float idTexel = extTex0.Sample(samp, vtf.extUvs[0]).a;
float sphereTexel = extTex1.Sample(clampEdgeSamp, vtf.extUvs[1]).r;
float fadeTexel = extTex2.Sample(clampEdgeSamp, vtf.extUvs[2]).a;
float val = ((abs(idTexel - shadowId) < 0.001) ?
(dot(vtf.mvNorm.xyz, shadowUp.xyz) * shadowUp.w) : 0.0) *
sphereTexel * fadeTexel;
return float4(0.0, 0.0, 0.0, val);
}
#endif
#if defined(URDE_DISINTEGRATE)
float4 PostFunc(in VertToFrag vtf, float4 colorIn) {
float4 texel0 = extTex0.Sample(samp, vtf.extUvs[0]);
float4 texel1 = extTex0.Sample(samp, vtf.extUvs[1]);
colorIn = lerp(float4(0.0, 0.0, 0.0, 0.0), texel1, texel0);
colorIn.rgb += daddColor.rgb;
return FogFunc(vtf, colorIn);
}
#endif
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
float3 ReflectionFunc(in VertToFrag vtf, float roughness) {
float3 coords = reflect(vtf.mvPos.xyz, vtf.mvNorm.xyz);
return reflectionTex.SampleBias(reflectSamp, float3(coords.x, -coords.y, coords.z), roughness).rgb;
}
#elif defined(URDE_REFLECTION_SIMPLE)
float3 ReflectionFunc(in VertToFrag vtf)
{ return reflectionTex.Sample(reflectSamp, vtf.dynReflectionUvs[1]).rgb * vtf.dynReflectionAlpha; }
#elif defined(URDE_REFLECTION_INDIRECT)
float3 ReflectionFunc(in VertToFrag vtf)
{ return reflectionTex.Sample(reflectSamp, (reflectionIndTex.Sample(samp, vtf.dynReflectionUvs[0]).ab -
float2(0.5, 0.5)) * float2(0.5, 0.5) + vtf.dynReflectionUvs[1]).rgb * vtf.dynReflectionAlpha; }
#else
float3 ReflectionFunc(in VertToFrag vtf) { return float3(0.0, 0.0, 0.0); }
#endif
#if !defined(URDE_ALPHA_TEST)
[earlydepthstencil]
#endif
float4 main(in VertToFrag vtf) : SV_Target0 {
float3 lighting = LightingFunc(vtf);
float4 tmp;
#if defined(URDE_LIGHTING_CUBE_REFLECTION) || defined(URDE_LIGHTING_CUBE_REFLECTION_SHADOW)
tmp.rgb = (SampleTexture_lightmap(vtf) * colorReg1.rgb + lighting) * SampleTexture_diffuse(vtf) +
SampleTexture_emissive(vtf) + (SampleTexture_specular(vtf) + SampleTexture_extendedSpecular(vtf) * lighting) *
(SampleTexture_reflection(vtf) * ReflectionFunc(vtf, saturate(0.5 - SampleTextureAlpha_specular(vtf))) * 2.0);
tmp.a = SampleTextureAlpha_alpha(vtf);
#elif defined(URDE_DIFFUSE_ONLY)
tmp.rgb = SampleTexture_diffuse(vtf);
tmp.a = SampleTextureAlpha_alpha(vtf);
#elif defined(RETRO_SHADER)
tmp.rgb = (SampleTexture_lightmap(vtf) * colorReg1.rgb + lighting) * SampleTexture_diffuse(vtf) +
SampleTexture_emissive(vtf) + (SampleTexture_specular(vtf) + SampleTexture_extendedSpecular(vtf) * lighting) *
SampleTexture_reflection(vtf) + ReflectionFunc(vtf);
tmp.a = SampleTextureAlpha_alpha(vtf);
#elif defined(RETRO_DYNAMIC_SHADER)
tmp.rgb = (SampleTexture_lightmap(vtf) * colorReg1.rgb + lighting) * SampleTexture_diffuse(vtf) * colorReg1.rgb +
SampleTexture_emissive(vtf) * colorReg1.rgb + (SampleTexture_specular(vtf) + SampleTexture_extendedSpecular(vtf) * lighting) *
SampleTexture_reflection(vtf) + ReflectionFunc(vtf);
tmp.a = SampleTextureAlpha_alpha(vtf);
#elif defined(RETRO_DYNAMIC_ALPHA_SHADER)
tmp.rgb = (SampleTexture_lightmap(vtf) * colorReg1.rgb + lighting) * SampleTexture_diffuse(vtf) * colorReg1.rgb +
SampleTexture_emissive(vtf) * colorReg1.rgb + (SampleTexture_specular(vtf) + SampleTexture_extendedSpecular(vtf) * lighting) *
SampleTexture_reflection(vtf) + ReflectionFunc(vtf);
tmp.a = SampleTextureAlpha_alpha(vtf) * colorReg1.a;
#elif defined(RETRO_DYNAMIC_CHARACTER_SHADER)
tmp.rgb = (SampleTexture_lightmap(vtf) + lighting) * SampleTexture_diffuse(vtf) +
SampleTexture_emissive(vtf) * colorReg1.rgb + (SampleTexture_specular(vtf) + SampleTexture_extendedSpecular(vtf) * lighting) *
SampleTexture_reflection(vtf) + ReflectionFunc(vtf);
tmp.a = SampleTextureAlpha_alpha(vtf);
#endif
float4 colorOut = PostFunc(vtf, tmp);
#if defined(URDE_ALPHA_TEST)
if (colorOut.a < 0.25)
discard;
#endif
return colorOut;
}