-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathdefault.fragment.fx
452 lines (351 loc) · 11 KB
/
default.fragment.fx
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include<__decl__defaultFragment>
#if defined(BUMP) || !defined(NORMAL)
#extension GL_OES_standard_derivatives : enable
#endif
#define CUSTOM_FRAGMENT_BEGIN
#ifdef LOGARITHMICDEPTH
#extension GL_EXT_frag_depth : enable
#endif
// Constants
#define RECIPROCAL_PI2 0.15915494
uniform vec3 vEyePosition;
uniform vec3 vAmbientColor;
// Input
varying vec3 vPositionW;
#ifdef NORMAL
varying vec3 vNormalW;
#endif
#ifdef VERTEXCOLOR
varying vec4 vColor;
#endif
#ifdef MAINUV1
varying vec2 vMainUV1;
#endif
#ifdef MAINUV2
varying vec2 vMainUV2;
#endif
// Helper functions
#include<helperFunctions>
// Lights
#include<__decl__lightFragment>[0..maxSimultaneousLights]
#include<lightsFragmentFunctions>
#include<shadowsFragmentFunctions>
// Samplers
#ifdef DIFFUSE
#if DIFFUSEDIRECTUV == 1
#define vDiffuseUV vMainUV1
#elif DIFFUSEDIRECTUV == 2
#define vDiffuseUV vMainUV2
#else
varying vec2 vDiffuseUV;
#endif
uniform sampler2D diffuseSampler;
#endif
#ifdef AMBIENT
#if AMBIENTDIRECTUV == 1
#define vAmbientUV vMainUV1
#elif AMBIENTDIRECTUV == 2
#define vAmbientUV vMainUV2
#else
varying vec2 vAmbientUV;
#endif
uniform sampler2D ambientSampler;
#endif
#ifdef OPACITY
#if OPACITYDIRECTUV == 1
#define vOpacityUV vMainUV1
#elif OPACITYDIRECTUV == 2
#define vOpacityUV vMainUV2
#else
varying vec2 vOpacityUV;
#endif
uniform sampler2D opacitySampler;
#endif
#ifdef EMISSIVE
#if EMISSIVEDIRECTUV == 1
#define vEmissiveUV vMainUV1
#elif EMISSIVEDIRECTUV == 2
#define vEmissiveUV vMainUV2
#else
varying vec2 vEmissiveUV;
#endif
uniform sampler2D emissiveSampler;
#endif
#ifdef LIGHTMAP
#if LIGHTMAPDIRECTUV == 1
#define vLightmapUV vMainUV1
#elif LIGHTMAPDIRECTUV == 2
#define vLightmapUV vMainUV2
#else
varying vec2 vLightmapUV;
#endif
uniform sampler2D lightmapSampler;
#endif
#ifdef REFRACTION
#ifdef REFRACTIONMAP_3D
uniform samplerCube refractionCubeSampler;
#else
uniform sampler2D refraction2DSampler;
#endif
#endif
#if defined(SPECULAR) && defined(SPECULARTERM)
#if SPECULARDIRECTUV == 1
#define vSpecularUV vMainUV1
#elif SPECULARDIRECTUV == 2
#define vSpecularUV vMainUV2
#else
varying vec2 vSpecularUV;
#endif
uniform sampler2D specularSampler;
#endif
#ifdef ALPHATEST
uniform float alphaCutOff;
#endif
// Fresnel
#include<fresnelFunction>
// Reflection
#ifdef REFLECTION
#ifdef REFLECTIONMAP_3D
uniform samplerCube reflectionCubeSampler;
#else
uniform sampler2D reflection2DSampler;
#endif
#ifdef REFLECTIONMAP_SKYBOX
varying vec3 vPositionUVW;
#else
#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
varying vec3 vDirectionW;
#endif
#endif
#include<reflectionFunction>
#endif
#include<imageProcessingDeclaration>
#include<imageProcessingFunctions>
#include<bumpFragmentFunctions>
#include<clipPlaneFragmentDeclaration>
#include<logDepthDeclaration>
#include<fogFragmentDeclaration>
#define CUSTOM_FRAGMENT_DEFINITIONS
void main(void) {
#define CUSTOM_FRAGMENT_MAIN_BEGIN
#include<clipPlaneFragment>
vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
// Base color
vec4 baseColor = vec4(1., 1., 1., 1.);
vec3 diffuseColor = vDiffuseColor.rgb;
// Alpha
float alpha = vDiffuseColor.a;
// Bump
#ifdef NORMAL
vec3 normalW = normalize(vNormalW);
#else
vec3 normalW = normalize(-cross(dFdx(vPositionW), dFdy(vPositionW)));
#endif
#include<bumpFragment>
#ifdef TWOSIDEDLIGHTING
normalW = gl_FrontFacing ? normalW : -normalW;
#endif
#ifdef DIFFUSE
baseColor = texture2D(diffuseSampler, vDiffuseUV + uvOffset);
#ifdef ALPHATEST
if (baseColor.a < alphaCutOff)
discard;
#endif
#ifdef ALPHAFROMDIFFUSE
alpha *= baseColor.a;
#endif
#define CUSTOM_FRAGMENT_UPDATE_ALPHA
baseColor.rgb *= vDiffuseInfos.y;
#endif
#include<depthPrePass>
#ifdef VERTEXCOLOR
baseColor.rgb *= vColor.rgb;
#endif
#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE
// Ambient color
vec3 baseAmbientColor = vec3(1., 1., 1.);
#ifdef AMBIENT
baseAmbientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
#endif
#define CUSTOM_FRAGMENT_BEFORE_LIGHTS
// Specular map
#ifdef SPECULARTERM
float glossiness = vSpecularColor.a;
vec3 specularColor = vSpecularColor.rgb;
#ifdef SPECULAR
vec4 specularMapColor = texture2D(specularSampler, vSpecularUV + uvOffset);
specularColor = specularMapColor.rgb;
#ifdef GLOSSINESS
glossiness = glossiness * specularMapColor.a;
#endif
#endif
#else
float glossiness = 0.;
#endif
// Lighting
vec3 diffuseBase = vec3(0., 0., 0.);
lightingInfo info;
#ifdef SPECULARTERM
vec3 specularBase = vec3(0., 0., 0.);
#endif
float shadow = 1.;
#ifdef LIGHTMAP
vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
#endif
#include<lightFragment>[0..maxSimultaneousLights]
// Refraction
vec3 refractionColor = vec3(0., 0., 0.);
#ifdef REFRACTION
vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
#ifdef REFRACTIONMAP_3D
refractionVector.y = refractionVector.y * vRefractionInfos.w;
if (dot(refractionVector, viewDirectionW) < 1.0) {
refractionColor = textureCube(refractionCubeSampler, refractionVector).rgb;
}
#else
vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
refractionCoords.y = 1.0 - refractionCoords.y;
refractionColor = texture2D(refraction2DSampler, refractionCoords).rgb;
#endif
#ifdef IS_REFRACTION_LINEAR
refractionColor = toGammaSpace(refractionColor);
#endif
refractionColor *= vRefractionInfos.x;
#endif
// Reflection
vec3 reflectionColor = vec3(0., 0., 0.);
#ifdef REFLECTION
vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
#ifdef REFLECTIONMAP_3D
#ifdef ROUGHNESS
float bias = vReflectionInfos.y;
#ifdef SPECULARTERM
#ifdef SPECULAR
#ifdef GLOSSINESS
bias *= (1.0 - specularMapColor.a);
#endif
#endif
#endif
reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb;
#else
reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb;
#endif
#else
vec2 coords = vReflectionUVW.xy;
#ifdef REFLECTIONMAP_PROJECTION
coords /= vReflectionUVW.z;
#endif
coords.y = 1.0 - coords.y;
reflectionColor = texture2D(reflection2DSampler, coords).rgb;
#endif
#ifdef IS_REFLECTION_LINEAR
reflectionColor = toGammaSpace(reflectionColor);
#endif
reflectionColor *= vReflectionInfos.x;
#ifdef REFLECTIONFRESNEL
float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
#ifdef REFLECTIONFRESNELFROMSPECULAR
#ifdef SPECULARTERM
reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#else
reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#endif
#else
reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#endif
#endif
#endif
#ifdef REFRACTIONFRESNEL
float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
refractionColor *= refractionLeftColor.rgb * (1.0 - refractionFresnelTerm) + refractionFresnelTerm * refractionRightColor.rgb;
#endif
#ifdef OPACITY
vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
#ifdef OPACITYRGB
opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
#else
alpha *= opacityMap.a * vOpacityInfos.y;
#endif
#endif
#ifdef VERTEXALPHA
alpha *= vColor.a;
#endif
#ifdef OPACITYFRESNEL
float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
#endif
// Emissive
vec3 emissiveColor = vEmissiveColor;
#ifdef EMISSIVE
emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
#endif
#ifdef EMISSIVEFRESNEL
float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
#endif
// Fresnel
#ifdef DIFFUSEFRESNEL
float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
#endif
// Composition
#ifdef EMISSIVEASILLUMINATION
vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
#else
#ifdef LINKEMISSIVEWITHDIFFUSE
vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
#else
vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
#endif
#endif
#ifdef SPECULARTERM
vec3 finalSpecular = specularBase * specularColor;
#ifdef SPECULAROVERALPHA
alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
#endif
#else
vec3 finalSpecular = vec3(0.0);
#endif
#ifdef REFLECTIONOVERALPHA
alpha = clamp(alpha + dot(reflectionColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
#endif
// Composition
#ifdef EMISSIVEASILLUMINATION
vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
#else
vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
#endif
//Old lightmap calculation method
#ifdef LIGHTMAP
#ifndef LIGHTMAPEXCLUDED
#ifdef USELIGHTMAPASSHADOWMAP
color.rgb *= lightmapColor;
#else
color.rgb += lightmapColor;
#endif
#endif
#endif
#define CUSTOM_FRAGMENT_BEFORE_FOG
color.rgb = max(color.rgb, 0.);
#include<logDepthFragment>
#include<fogFragment>
// Apply image processing if relevant. As this applies in linear space,
// We first move from gamma to linear.
#ifdef IMAGEPROCESSINGPOSTPROCESS
color.rgb = toLinearSpace(color.rgb);
#else
#ifdef IMAGEPROCESSING
color.rgb = toLinearSpace(color.rgb);
color = applyImageProcessing(color);
#endif
#endif
color.a *= visibility;
#ifdef PREMULTIPLYALPHA
// Convert to associative (premultiplied) format if needed.
color.rgb *= color.a;
#endif
#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
gl_FragColor = color;
}