-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
default.fragment.fx
495 lines (385 loc) · 14.3 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include<__decl__defaultFragment>
#if defined(BUMP) || !defined(NORMAL)
#extension GL_OES_standard_derivatives : enable
#endif
#include<prePassDeclaration>[SCENE_MRT_COUNT]
#include<oitDeclaration>
#define CUSTOM_FRAGMENT_BEGIN
#ifdef LOGARITHMICDEPTH
#extension GL_EXT_frag_depth : enable
#endif
// Input
varying vec3 vPositionW;
#ifdef NORMAL
varying vec3 vNormalW;
#endif
#if defined(VERTEXCOLOR) || defined(INSTANCESCOLOR) && defined(INSTANCES)
varying vec4 vColor;
#endif
#include<mainUVVaryingDeclaration>[1..7]
// Helper functions
#include<helperFunctions>
// Lights
#include<__decl__lightFragment>[0..maxSimultaneousLights]
#include<lightsFragmentFunctions>
#include<shadowsFragmentFunctions>
// Samplers
#include<samplerFragmentDeclaration>(_DEFINENAME_,DIFFUSE,_VARYINGNAME_,Diffuse,_SAMPLERNAME_,diffuse)
#include<samplerFragmentDeclaration>(_DEFINENAME_,AMBIENT,_VARYINGNAME_,Ambient,_SAMPLERNAME_,ambient)
#include<samplerFragmentDeclaration>(_DEFINENAME_,OPACITY,_VARYINGNAME_,Opacity,_SAMPLERNAME_,opacity)
#include<samplerFragmentDeclaration>(_DEFINENAME_,EMISSIVE,_VARYINGNAME_,Emissive,_SAMPLERNAME_,emissive)
#include<samplerFragmentDeclaration>(_DEFINENAME_,LIGHTMAP,_VARYINGNAME_,Lightmap,_SAMPLERNAME_,lightmap)
#include<samplerFragmentDeclaration>(_DEFINENAME_,DECAL,_VARYINGNAME_,Decal,_SAMPLERNAME_,decal)
#ifdef REFRACTION
#ifdef REFRACTIONMAP_3D
uniform samplerCube refractionCubeSampler;
#else
uniform sampler2D refraction2DSampler;
#endif
#endif
#if defined(SPECULARTERM)
#include<samplerFragmentDeclaration>(_DEFINENAME_,SPECULAR,_VARYINGNAME_,Specular,_SAMPLERNAME_,specular)
#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<bumpFragmentMainFunctions>
#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.xyz - 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);
#if defined(ALPHATEST) && !defined(ALPHATEST_AFTERALLALPHACOMPUTATIONS)
if (baseColor.a < alphaCutOff)
discard;
#endif
#ifdef ALPHAFROMDIFFUSE
alpha *= baseColor.a;
#endif
#define CUSTOM_FRAGMENT_UPDATE_ALPHA
baseColor.rgb *= vDiffuseInfos.y;
#endif
#if defined(DECAL) && !defined(DECAL_AFTER_DETAIL)
vec4 decalColor = texture2D(decalSampler, vDecalUV + uvOffset);
#include<decalFragment>(surfaceAlbedo, baseColor, GAMMADECAL, _GAMMADECAL_NOTUSED_)
#endif
#include<depthPrePass>
#if defined(VERTEXCOLOR) || defined(INSTANCESCOLOR) && defined(INSTANCES)
baseColor.rgb *= vColor.rgb;
#endif
#ifdef DETAIL
baseColor.rgb = baseColor.rgb * 2.0 * mix(0.5, detailColor.r, vDetailInfos.y);
#endif
#if defined(DECAL) && defined(DECAL_AFTER_DETAIL)
vec4 decalColor = texture2D(decalSampler, vDecalUV + uvOffset);
#include<decalFragment>(surfaceAlbedo, baseColor, GAMMADECAL, _GAMMADECAL_NOTUSED_)
#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.;
float aggShadow = 0.;
float numLights = 0.;
#ifdef LIGHTMAP
vec4 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset);
#ifdef RGBDLIGHTMAP
lightmapColor.rgb = fromRGBD(lightmapColor);
#endif
lightmapColor.rgb *= vLightmapInfos.y;
#endif
#include<lightFragment>[0..maxSimultaneousLights]
aggShadow = aggShadow / numLights;
// Refraction
vec4 refractionColor = vec4(0., 0., 0., 1.);
#ifdef REFRACTION
vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
#ifdef REFRACTIONMAP_3D
#ifdef USE_LOCAL_REFRACTIONMAP_CUBIC
refractionVector = parallaxCorrectNormal(vPositionW, refractionVector, vRefractionSize, vRefractionPosition);
#endif
refractionVector.y = refractionVector.y * vRefractionInfos.w;
vec4 refractionLookup = textureCube(refractionCubeSampler, refractionVector);
if (dot(refractionVector, viewDirectionW) < 1.0) {
refractionColor = refractionLookup;
}
#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);
#endif
#ifdef RGBDREFRACTION
refractionColor.rgb = fromRGBD(refractionColor);
#endif
#ifdef IS_REFRACTION_LINEAR
refractionColor.rgb = toGammaSpace(refractionColor.rgb);
#endif
refractionColor.rgb *= vRefractionInfos.x;
#endif
// Reflection
vec4 reflectionColor = vec4(0., 0., 0., 1.);
#ifdef REFLECTION
vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
#ifdef REFLECTIONMAP_OPPOSITEZ
vReflectionUVW.z *= -1.0;
#endif
#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);
#else
reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW);
#endif
#else
vec2 coords = vReflectionUVW.xy;
#ifdef REFLECTIONMAP_PROJECTION
coords /= vReflectionUVW.z;
#endif
coords.y = 1.0 - coords.y;
reflectionColor = texture2D(reflection2DSampler, coords);
#endif
#ifdef RGBDREFLECTION
reflectionColor.rgb = fromRGBD(reflectionColor);
#endif
#ifdef IS_REFLECTION_LINEAR
reflectionColor.rgb = toGammaSpace(reflectionColor.rgb);
#endif
reflectionColor.rgb *= vReflectionInfos.x;
#ifdef REFLECTIONFRESNEL
float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
#ifdef REFLECTIONFRESNELFROMSPECULAR
#ifdef SPECULARTERM
reflectionColor.rgb *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#else
reflectionColor.rgb *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#endif
#else
reflectionColor.rgb *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
#endif
#endif
#endif
#ifdef REFRACTIONFRESNEL
float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
refractionColor.rgb *= 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
#if defined(VERTEXALPHA) || defined(INSTANCESCOLOR) && defined(INSTANCES)
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
#ifdef ALPHATEST
#ifdef ALPHATEST_AFTERALLALPHACOMPUTATIONS
if (alpha < alphaCutOff)
discard;
#endif
#ifndef ALPHABLEND
// Prevent to blend with the canvas.
alpha = 1.0;
#endif
#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.rgb, vec3(0.3, 0.59, 0.11)), 0., 1.);
#endif
// Composition
#ifdef EMISSIVEASILLUMINATION
vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor.rgb + emissiveColor + refractionColor.rgb, 0.0, 1.0), alpha);
#else
vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor.rgb + refractionColor.rgb, alpha);
#endif
//Old lightmap calculation method
#ifdef LIGHTMAP
#ifndef LIGHTMAPEXCLUDED
#ifdef USELIGHTMAPASSHADOWMAP
color.rgb *= lightmapColor.rgb;
#else
color.rgb += lightmapColor.rgb;
#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
#ifdef PREPASS
float writeGeometryInfo = color.a > 0.4 ? 1.0 : 0.0;
gl_FragData[0] = color; // We can't split irradiance on std material
#ifdef PREPASS_POSITION
gl_FragData[PREPASS_POSITION_INDEX] = vec4(vPositionW, writeGeometryInfo);
#endif
#ifdef PREPASS_VELOCITY
vec2 a = (vCurrentPosition.xy / vCurrentPosition.w) * 0.5 + 0.5;
vec2 b = (vPreviousPosition.xy / vPreviousPosition.w) * 0.5 + 0.5;
vec2 velocity = abs(a - b);
velocity = vec2(pow(velocity.x, 1.0 / 3.0), pow(velocity.y, 1.0 / 3.0)) * sign(a - b) * 0.5 + 0.5;
gl_FragData[PREPASS_VELOCITY_INDEX] = vec4(velocity, 0.0, writeGeometryInfo);
#endif
#ifdef PREPASS_IRRADIANCE
gl_FragData[PREPASS_IRRADIANCE_INDEX] = vec4(0.0, 0.0, 0.0, writeGeometryInfo); // We can't split irradiance on std material
#endif
#ifdef PREPASS_DEPTH
gl_FragData[PREPASS_DEPTH_INDEX] = vec4(vViewPos.z, 0.0, 0.0, writeGeometryInfo); // Linear depth
#endif
#ifdef PREPASS_NORMAL
#ifdef PREPASS_NORMAL_WORLDSPACE
gl_FragData[PREPASS_NORMAL_INDEX] = vec4(normalW, writeGeometryInfo); // Normal
#else
gl_FragData[PREPASS_NORMAL_INDEX] = vec4(normalize((view * vec4(normalW, 0.0)).rgb), writeGeometryInfo); // Normal
#endif
#endif
#ifdef PREPASS_ALBEDO_SQRT
gl_FragData[PREPASS_ALBEDO_SQRT_INDEX] = vec4(0.0, 0.0, 0.0, writeGeometryInfo); // We can't split albedo on std material
#endif
#ifdef PREPASS_REFLECTIVITY
#if defined(SPECULARTERM)
#if defined(SPECULAR)
gl_FragData[PREPASS_REFLECTIVITY_INDEX] = vec4(toLinearSpace(specularMapColor)) * writeGeometryInfo; // no specularity if no visibility
#else
gl_FragData[PREPASS_REFLECTIVITY_INDEX] = vec4(toLinearSpace(specularColor), 1.0) * writeGeometryInfo;
#endif
#else
gl_FragData[PREPASS_REFLECTIVITY_INDEX] = vec4(0.0, 0.0, 0.0, 1.0) * writeGeometryInfo;
#endif
#endif
#endif
#if !defined(PREPASS) || defined(WEBGL2)
gl_FragColor = color;
#endif
#include<oitFragment>
#if ORDER_INDEPENDENT_TRANSPARENCY
if (fragDepth == nearestDepth) {
frontColor.rgb += color.rgb * color.a * alphaMultiplier;
frontColor.a = 1.0 - alphaMultiplier * (1.0 - color.a);
} else {
backColor += color;
}
#endif
#define CUSTOM_FRAGMENT_MAIN_END
}