Debian Codegraph @

code/renderergl2/glsl/lightall_fp.glsl

raw
1 uniform sampler2D u_DiffuseMap;
2
3 #if defined(USE_LIGHTMAP)
4 uniform sampler2D u_LightMap;
5 #endif
6
7 #if defined(USE_NORMALMAP)
8 uniform sampler2D u_NormalMap;
9 #endif
10
11 #if defined(USE_DELUXEMAP)
12 uniform sampler2D u_DeluxeMap;
13 #endif
14
15 #if defined(USE_SPECULARMAP)
16 uniform sampler2D u_SpecularMap;
17 #endif
18
19 #if defined(USE_SHADOWMAP)
20 uniform sampler2D u_ShadowMap;
21 #endif
22
23 #if defined(USE_CUBEMAP)
24 uniform samplerCube u_CubeMap;
25 #endif
26
27 #if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
28 // y = deluxe, w = cube
29 uniform vec4 u_EnableTextures;
30 #endif
31
32 #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
33 uniform vec3 u_PrimaryLightColor;
34 uniform vec3 u_PrimaryLightAmbient;
35 #endif
36
37 #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
38 uniform vec4 u_NormalScale;
39 uniform vec4 u_SpecularScale;
40 #endif
41
42 #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
43 #if defined(USE_CUBEMAP)
44 uniform vec4 u_CubeMapInfo;
45 #endif
46 #endif
47
48 uniform int u_AlphaTest;
49
50 varying vec4 var_TexCoords;
51
52 varying vec4 var_Color;
53 #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
54 varying vec4 var_ColorAmbient;
55 #endif
56
57 #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
58 varying vec4 var_Normal;
59 varying vec4 var_Tangent;
60 varying vec4 var_Bitangent;
61 #endif
62
63 #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
64 varying vec4 var_LightDir;
65 #endif
66
67 #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
68 varying vec4 var_PrimaryLightDir;
69 #endif
70
71
72 #define EPSILON 0.00000001
73
74 #if defined(USE_PARALLAXMAP)
75 float SampleDepth(sampler2D normalMap, vec2 t)
76 {
77 #if defined(SWIZZLE_NORMALMAP)
78 return 1.0 - texture2D(normalMap, t).r;
79 #else
80 return 1.0 - texture2D(normalMap, t).a;
81 #endif
82 }
83
84 float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap)
85 {
86 const int linearSearchSteps = 16;
87 const int binarySearchSteps = 6;
88
89 // current size of search window
90 float size = 1.0 / float(linearSearchSteps);
91
92 // adjust position if offset above surface
93 dp -= ds * r_parallaxMapOffset;
94
95 // current depth position
96 float depth = 0.0;
97
98 // best match found (starts with last position 1.0)
99 float bestDepth = 1.0;
100
101 // texture depth at best depth
102 float texDepth = 0.0;
103
104 float prevT = SampleDepth(normalMap, dp);
105 float prevTexDepth = prevT;
106
107 // search front to back for first point inside object
108 for(int i = 0; i < linearSearchSteps - 1; ++i)
109 {
110 depth += size;
111
112 float t = SampleDepth(normalMap, dp + ds * depth);
113
114 if(bestDepth > 0.996) // if no depth found yet
115 if(depth >= t)
116 {
117 bestDepth = depth; // store best depth
118 texDepth = t;
119 prevTexDepth = prevT;
120 }
121 prevT = t;
122 }
123
124 depth = bestDepth;
125
126 #if !defined (USE_RELIEFMAP)
127 float div = 1.0 / (1.0 + (prevTexDepth - texDepth) * float(linearSearchSteps));
128 bestDepth -= (depth - size - prevTexDepth) * div;
129 #else
130 // recurse around first point (depth) for closest match
131 for(int i = 0; i < binarySearchSteps; ++i)
132 {
133 size *= 0.5;
134
135 float t = SampleDepth(normalMap, dp + ds * depth);
136
137 if(depth >= t)
138 {
139 bestDepth = depth;
140 depth -= 2.0 * size;
141 }
142
143 depth += size;
144 }
145 #endif
146
147 return bestDepth - r_parallaxMapOffset;
148 }
149
150 float LightRay(vec2 dp, vec2 ds, sampler2D normalMap)
151 {
152 const int linearSearchSteps = 16;
153
154 // current size of search window
155 float size = 1.0 / float(linearSearchSteps);
156
157 // current height from initial texel depth
158 float height = 0.0;
159
160 float startDepth = SampleDepth(normalMap, dp);
161
162 // find a collision or escape
163 for(int i = 0; i < linearSearchSteps - 1; ++i)
164 {
165 height += size;
166
167 if (startDepth < height)
168 return 1.0;
169
170 float t = SampleDepth(normalMap, dp + ds * height);
171
172 if (startDepth > t + height)
173 return 0.0;
174 }
175
176 return 1.0;
177 }
178 #endif
179
180 vec3 CalcDiffuse(vec3 diffuseAlbedo, float NH, float EH, float roughness)
181 {
182 #if defined(USE_BURLEY)
183 // modified from https://disney-animation.s3.amazonaws.com/library/s2012_pbs_disney_brdf_notes_v2.pdf
184 float fd90 = -0.5 + EH * EH * roughness;
185 float burley = 1.0 + fd90 * 0.04 / NH;
186 burley *= burley;
187 return diffuseAlbedo * burley;
188 #else
189 return diffuseAlbedo;
190 #endif
191 }
192
193 vec3 EnvironmentBRDF(float roughness, float NE, vec3 specular)
194 {
195 // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf
196 float v = 1.0 - max(roughness, NE);
197 v *= v * v;
198 return vec3(v) + specular;
199 }
200
201 vec3 CalcSpecular(vec3 specular, float NH, float EH, float roughness)
202 {
203 // from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf
204 float rr = roughness*roughness;
205 float rrrr = rr*rr;
206 float d = (NH * NH) * (rrrr - 1.0) + 1.0;
207 float v = (EH * EH) * (roughness + 0.5) + EPSILON;
208 return specular * (rrrr / (4.0 * d * d * v));
209 }
210
211
212 float CalcLightAttenuation(float point, float normDist)
213 {
214 // zero light at 1.0, approximating q3 style
215 // also don't attenuate directional light
216 float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
217
218 // clamp attenuation
219 #if defined(NO_LIGHT_CLAMP)
220 attenuation = max(attenuation, 0.0);
221 #else
222 attenuation = clamp(attenuation, 0.0, 1.0);
223 #endif
224
225 return attenuation;
226 }
227
228 #if defined(USE_BOX_CUBEMAP_PARALLAX)
229 vec4 hitCube(vec3 ray, vec3 pos, vec3 invSize, float lod, samplerCube tex)
230 {
231 // find any hits on cubemap faces facing the camera
232 vec3 scale = (sign(ray) - pos) / ray;
233
234 // find the nearest hit
235 float minScale = min(min(scale.x, scale.y), scale.z);
236
237 // if the nearest hit is behind the camera, ignore
238 // should not be necessary as long as pos is inside the cube
239 //if (minScale < 0.0)
240 //return vec4(0.0);
241
242 // calculate the hit position, that's our texture coordinates
243 vec3 tc = pos + ray * minScale;
244
245 // if the texture coordinates are outside the cube, ignore
246 // necessary since we're not fading out outside the cube
247 if (any(greaterThan(abs(tc), vec3(1.00001))))
248 return vec4(0.0);
249
250 // fade out when approaching the cubemap edges
251 //vec3 fade3 = abs(pos);
252 //float fade = max(max(fade3.x, fade3.y), fade3.z);
253 //fade = clamp(1.0 - fade, 0.0, 1.0);
254
255 //return vec4(textureCubeLod(tex, tc, lod).rgb * fade, fade);
256 return vec4(textureCubeLod(tex, tc, lod).rgb, 1.0);
257 }
258 #endif
259
260 void main()
261 {
262 vec3 viewDir, lightColor, ambientColor, reflectance;
263 vec3 L, N, E, H;
264 float NL, NH, NE, EH, attenuation;
265
266 #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
267 vec3 surfNormal = (!gl_FrontFacing ? var_Normal : -var_Normal).xyz;
268 mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, surfNormal);
269 viewDir = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
270 E = normalize(viewDir);
271 #endif
272
273 lightColor = var_Color.rgb;
274
275 #if defined(USE_LIGHTMAP)
276 vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
277 #if defined(RGBM_LIGHTMAP)
278 lightmapColor.rgb *= lightmapColor.a;
279 #endif
280 #if defined(USE_PBR) && !defined(USE_FAST_LIGHT)
281 lightmapColor.rgb *= lightmapColor.rgb;
282 #endif
283 lightColor *= lightmapColor.rgb;
284 #endif
285
286 vec2 texCoords = var_TexCoords.xy;
287
288 #if defined(USE_PARALLAXMAP)
289 vec3 offsetDir = E * tangentToWorld;
290
291 offsetDir.xy *= -u_NormalScale.a / offsetDir.z;
292
293 texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
294 #endif
295
296 vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
297
298 float alpha = diffuse.a * var_Color.a;
299 if (u_AlphaTest == 1)
300 {
301 if (alpha == 0.0)
302 discard;
303 }
304 else if (u_AlphaTest == 2)
305 {
306 if (alpha >= 0.5)
307 discard;
308 }
309 else if (u_AlphaTest == 3)
310 {
311 if (alpha < 0.5)
312 discard;
313 }
314
315 #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
316 L = var_LightDir.xyz;
317 #if defined(USE_DELUXEMAP)
318 L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
319 #endif
320 float sqrLightDist = dot(L, L);
321 L /= sqrt(sqrLightDist);
322
323 #if defined(USE_LIGHT_VECTOR)
324 attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
325 #else
326 attenuation = 1.0;
327 #endif
328
329 #if defined(USE_NORMALMAP)
330 #if defined(SWIZZLE_NORMALMAP)
331 N.xy = texture2D(u_NormalMap, texCoords).ag - vec2(0.5);
332 #else
333 N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
334 #endif
335 N.xy *= u_NormalScale.xy;
336 N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
337 N = tangentToWorld * N;
338 #else
339 N = surfNormal;
340 #endif
341
342 N = normalize(N);
343
344 #if defined(USE_SHADOWMAP)
345 vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
346 float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
347
348 // surfaces not facing the light are always shadowed
349 shadowValue *= clamp(dot(N, var_PrimaryLightDir.xyz), 0.0, 1.0);
350
351 #if defined(SHADOWMAP_MODULATE)
352 lightColor *= shadowValue * (1.0 - u_PrimaryLightAmbient.r) + u_PrimaryLightAmbient.r;
353 #endif
354 #endif
355
356 #if defined(USE_PARALLAXMAP) && defined(USE_PARALLAXMAP_SHADOWS)
357 offsetDir = L * tangentToWorld;
358 offsetDir.xy *= u_NormalScale.a / offsetDir.z;
359 lightColor *= LightRay(texCoords, offsetDir.xy, u_NormalMap);
360 #endif
361
362
363 #if !defined(USE_LIGHT_VECTOR)
364 ambientColor = lightColor;
365 float surfNL = clamp(dot(surfNormal, L), 0.0, 1.0);
366
367 // reserve 25% ambient to avoid black areas on normalmaps
368 lightColor *= 0.75;
369
370 // Scale the incoming light to compensate for the baked-in light angle
371 // attenuation.
372 lightColor /= max(surfNL, 0.25);
373
374 // Recover any unused light as ambient, in case attenuation is over 4x or
375 // light is below the surface
376 ambientColor = max(ambientColor - lightColor * surfNL, vec3(0.0));
377 #else
378 ambientColor = var_ColorAmbient.rgb;
379 #endif
380
381 NL = clamp(dot(N, L), 0.0, 1.0);
382 NE = clamp(dot(N, E), 0.0, 1.0);
383 H = normalize(L + E);
384 EH = clamp(dot(E, H), 0.0, 1.0);
385 NH = clamp(dot(N, H), 0.0, 1.0);
386
387 #if defined(USE_SPECULARMAP)
388 vec4 specular = texture2D(u_SpecularMap, texCoords);
389 #else
390 vec4 specular = vec4(1.0);
391 #endif
392 specular *= u_SpecularScale;
393
394 #if defined(USE_PBR)
395 diffuse.rgb *= diffuse.rgb;
396 #endif
397
398 #if defined(USE_PBR)
399 // diffuse rgb is base color
400 // specular red is gloss
401 // specular green is metallicness
402 float gloss = specular.r;
403 float metal = specular.g;
404 specular.rgb = metal * diffuse.rgb + vec3(0.04 - 0.04 * metal);
405 diffuse.rgb *= 1.0 - metal;
406 #else
407 // diffuse rgb is diffuse
408 // specular rgb is specular reflectance at normal incidence
409 // specular alpha is gloss
410 float gloss = specular.a;
411
412 // adjust diffuse by specular reflectance, to maintain energy conservation
413 diffuse.rgb *= vec3(1.0) - specular.rgb;
414 #endif
415
416 #if defined(GLOSS_IS_GLOSS)
417 float roughness = exp2(-3.0 * gloss);
418 #elif defined(GLOSS_IS_SMOOTHNESS)
419 float roughness = 1.0 - gloss;
420 #elif defined(GLOSS_IS_ROUGHNESS)
421 float roughness = gloss;
422 #elif defined(GLOSS_IS_SHININESS)
423 float roughness = pow(2.0 / (8190.0 * gloss + 2.0), 0.25);
424 #endif
425
426 reflectance = CalcDiffuse(diffuse.rgb, NH, EH, roughness);
427
428 #if defined(r_deluxeSpecular)
429 #if defined(USE_LIGHT_VECTOR)
430 reflectance += CalcSpecular(specular.rgb, NH, EH, roughness) * r_deluxeSpecular;
431 #else
432 reflectance += CalcSpecular(specular.rgb, NH, EH, pow(roughness, r_deluxeSpecular));
433 #endif
434 #endif
435
436 gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL);
437 gl_FragColor.rgb += ambientColor * diffuse.rgb;
438
439 #if defined(USE_CUBEMAP)
440 reflectance = EnvironmentBRDF(roughness, NE, specular.rgb);
441
442 vec3 R = reflect(E, N);
443
444 // parallax corrected cubemap (cheaper trick)
445 // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
446 vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
447
448 #if defined(USE_BOX_CUBEMAP_PARALLAX)
449 vec3 cubeLightColor = hitCube(R * u_CubeMapInfo.w, parallax, u_CubeMapInfo.www, ROUGHNESS_MIPS * roughness, u_CubeMap).rgb * u_EnableTextures.w;
450 #else
451 vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, ROUGHNESS_MIPS * roughness).rgb * u_EnableTextures.w;
452 #endif
453
454 // normalize cubemap based on last roughness mip (~diffuse)
455 // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation
456 //vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, ROUGHNESS_MIPS).rgb, 0.5 / 255.0);
457 //cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721));
458
459 #if defined(USE_PBR)
460 cubeLightColor *= cubeLightColor;
461 #endif
462
463 // multiply cubemap values by lighting
464 // not technically correct, but helps make reflections look less unnatural
465 //cubeLightColor *= lightColor * (attenuation * NL) + ambientColor;
466
467 gl_FragColor.rgb += cubeLightColor * reflectance;
468 #endif
469
470 #if defined(USE_PRIMARY_LIGHT) || defined(SHADOWMAP_MODULATE)
471 vec3 L2, H2;
472 float NL2, EH2, NH2;
473
474 L2 = var_PrimaryLightDir.xyz;
475
476 // enable when point lights are supported as primary lights
477 //sqrLightDist = dot(L2, L2);
478 //L2 /= sqrt(sqrLightDist);
479
480 NL2 = clamp(dot(N, L2), 0.0, 1.0);
481 H2 = normalize(L2 + E);
482 EH2 = clamp(dot(E, H2), 0.0, 1.0);
483 NH2 = clamp(dot(N, H2), 0.0, 1.0);
484
485 reflectance = CalcSpecular(specular.rgb, NH2, EH2, roughness);
486
487 // bit of a hack, with modulated shadowmaps, ignore diffuse
488 #if !defined(SHADOWMAP_MODULATE)
489 reflectance += CalcDiffuse(diffuse.rgb, NH2, EH2, roughness);
490 #endif
491
492 lightColor = u_PrimaryLightColor;
493
494 #if defined(USE_SHADOWMAP)
495 lightColor *= shadowValue;
496 #endif
497
498 // enable when point lights are supported as primary lights
499 //lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
500
501 #if defined(USE_PARALLAXMAP) && defined(USE_PARALLAXMAP_SHADOWS)
502 offsetDir = L2 * tangentToWorld;
503 offsetDir.xy *= u_NormalScale.a / offsetDir.z;
504 lightColor *= LightRay(texCoords, offsetDir.xy, u_NormalMap);
505 #endif
506
507 gl_FragColor.rgb += lightColor * reflectance * NL2;
508 #endif
509
510 #if defined(USE_PBR)
511 gl_FragColor.rgb = sqrt(gl_FragColor.rgb);
512 #endif
513
514 #else
515
516 gl_FragColor.rgb = diffuse.rgb * lightColor;
517
518 #endif
519
520 gl_FragColor.a = alpha;
521 }