Skip to content

Commit 637ae4c

Browse files
committed
Merge pull request #114336 from LunaCapra/bent-normals-compatibility
Add bent normal map support for the compatibility renderer
2 parents 2adf91e + b5cb821 commit 637ae4c

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

drivers/gles3/shaders/scene.glsl

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ RENDER_MOTION_VECTORS = false
6262

6363
#include "stdlib_inc.glsl"
6464

65-
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
65+
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) || defined(LIGHT_CLEARCOAT_USED)
6666
#ifndef NORMAL_USED
6767
#define NORMAL_USED
6868
#endif
@@ -517,7 +517,7 @@ out vec2 uv_interp;
517517
out vec2 uv2_interp;
518518
#endif
519519

520-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
520+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
521521
out vec3 tangent_interp;
522522
out vec3 binormal_interp;
523523
#endif
@@ -610,7 +610,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
610610
model_normal_matrix = mat3(model_matrix);
611611
}
612612

613-
#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
613+
#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
614614

615615
vec3 binormal;
616616
float binormal_sign;
@@ -685,7 +685,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
685685
normal = model_normal_matrix * normal;
686686
#endif
687687

688-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
688+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
689689

690690
tangent = model_normal_matrix * tangent;
691691
binormal = model_normal_matrix * binormal;
@@ -718,7 +718,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
718718
normal = modelview_normal * normal;
719719
#endif
720720

721-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
721+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
722722

723723
binormal = modelview_normal * binormal;
724724
tangent = modelview_normal * tangent;
@@ -733,7 +733,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
733733
normal = (scene_data_input.view_matrix * vec4(normal, 0.0)).xyz;
734734
#endif
735735

736-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
736+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
737737
binormal = (scene_data_input.view_matrix * vec4(binormal, 0.0)).xyz;
738738
tangent = (scene_data_input.view_matrix * vec4(tangent, 0.0)).xyz;
739739
#endif
@@ -747,7 +747,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
747747
normal_interp = normalize(normal);
748748
#endif
749749

750-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
750+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
751751
tangent_interp = normalize(tangent);
752752
binormal_interp = normalize(binormal);
753753
#endif
@@ -990,7 +990,7 @@ void main() {
990990
#define SPECULAR_SCHLICK_GGX
991991
#endif
992992

993-
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
993+
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
994994
#ifndef NORMAL_USED
995995
#define NORMAL_USED
996996
#endif
@@ -1065,7 +1065,7 @@ in vec2 uv2_interp;
10651065
#endif
10661066
#endif
10671067

1068-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
1068+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
10691069
in vec3 tangent_interp;
10701070
in vec3 binormal_interp;
10711071
#endif
@@ -2044,7 +2044,7 @@ void main() {
20442044

20452045
float alpha = 1.0;
20462046

2047-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
2047+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
20482048
vec3 binormal = binormal_interp;
20492049
vec3 tangent = tangent_interp;
20502050
#else
@@ -2079,6 +2079,7 @@ void main() {
20792079
#endif
20802080

20812081
#if defined(BENT_NORMAL_MAP_USED)
2082+
vec3 bent_normal_vector;
20822083
vec3 bent_normal_map = vec3(0.5);
20832084
#endif
20842085

@@ -2160,6 +2161,13 @@ void main() {
21602161

21612162
#endif // !USE_SHADOW_TO_OPACITY
21622163

2164+
#ifdef BENT_NORMAL_MAP_USED
2165+
bent_normal_map.xy = bent_normal_map.xy * 2.0 - 1.0;
2166+
bent_normal_map.z = sqrt(max(0.0, 1.0 - dot(bent_normal_map.xy, bent_normal_map.xy)));
2167+
2168+
bent_normal_vector = normalize(tangent * bent_normal_map.x + binormal * bent_normal_map.y + normal * bent_normal_map.z);
2169+
#endif
2170+
21632171
#if defined(NORMAL_MAP_USED)
21642172
normal_map.xy = normal_map.xy * 2.0 - 1.0;
21652173
normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
@@ -2214,8 +2222,13 @@ void main() {
22142222
/////////////////////// LIGHTING //////////////////////////////
22152223

22162224
#ifndef AMBIENT_LIGHT_DISABLED
2225+
#ifdef BENT_NORMAL_MAP_USED
2226+
vec3 indirect_normal = bent_normal_vector;
2227+
#else
2228+
vec3 indirect_normal = normal;
2229+
#endif // BENT_NORMAL_MAP_USED
22172230
// IBL precalculations
2218-
float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2231+
float ndotv = clamp(dot(indirect_normal, view), 0.0, 1.0);
22192232
vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
22202233

22212234
#ifdef USE_RADIANCE_MAP
@@ -2225,13 +2238,13 @@ void main() {
22252238
vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
22262239
vec3 anisotropic_tangent = cross(anisotropic_direction, view);
22272240
vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
2228-
vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
2241+
vec3 bent_normal = normalize(mix(indirect_normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
22292242
vec3 ref_vec = reflect(-view, bent_normal);
22302243
#else
2231-
vec3 ref_vec = reflect(-view, normal);
2244+
vec3 ref_vec = reflect(-view, indirect_normal);
22322245
#endif
2233-
ref_vec = mix(ref_vec, normal, roughness * roughness);
2234-
float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
2246+
ref_vec = mix(ref_vec, indirect_normal, roughness * roughness);
2247+
float horizon = min(1.0 + dot(ref_vec, indirect_normal), 1.0);
22352248
ref_vec = mat3(scene_data_block.data.radiance_inverse_xform) * ref_vec;
22362249
specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
22372250
specular_light = srgb_to_linear(specular_light);
@@ -2246,14 +2259,14 @@ void main() {
22462259
{
22472260
vec4 reflection_accum = vec4(0.0);
22482261

2249-
reflection_process(refprobe1_texture, normal, vertex_interp, refprobe1_local_matrix,
2262+
reflection_process(refprobe1_texture, indirect_normal, vertex_interp, refprobe1_local_matrix,
22502263
refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
22512264
refprobe1_exterior, refprobe1_intensity, refprobe1_blend_distance, refprobe1_ambient_mode, refprobe1_ambient_color,
22522265
roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
22532266

22542267
#ifdef SECOND_REFLECTION_PROBE
22552268

2256-
reflection_process(refprobe2_texture, normal, vertex_interp, refprobe2_local_matrix,
2269+
reflection_process(refprobe2_texture, indirect_normal, vertex_interp, refprobe2_local_matrix,
22572270
refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
22582271
refprobe2_exterior, refprobe2_intensity, refprobe2_blend_distance, refprobe2_ambient_mode, refprobe2_ambient_color,
22592272
roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
@@ -2277,7 +2290,7 @@ void main() {
22772290

22782291
#ifdef USE_RADIANCE_MAP
22792292
if (scene_data_block.data.use_ambient_cubemap) {
2280-
vec3 ambient_dir = mat3(scene_data_block.data.radiance_inverse_xform) * normal;
2293+
vec3 ambient_dir = mat3(scene_data_block.data.radiance_inverse_xform) * indirect_normal;
22812294
vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
22822295
cubemap_ambient = srgb_to_linear(cubemap_ambient);
22832296
ambient_light = mix(ambient_light, cubemap_ambient * scene_data_block.data.ambient_light_color_energy.a, scene_data_block.data.ambient_color_sky_mix);
@@ -2300,7 +2313,7 @@ void main() {
23002313
#ifdef USE_LIGHTMAP_CAPTURE
23012314
{
23022315
// The world normal.
2303-
vec3 wnormal = mat3(scene_data_block.data.inv_view_matrix) * normal;
2316+
vec3 wnormal = mat3(scene_data_block.data.inv_view_matrix) * indirect_normal;
23042317

23052318
// The SH coefficients used for evaluating diffuse data from SH probes.
23062319
const float c0 = 0.886227; // l0 sqrt(1.0/(4.0*PI)) * PI
@@ -2342,7 +2355,7 @@ void main() {
23422355
vec3 lm_light_l1p1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0;
23432356
#endif
23442357

2345-
vec3 n = normalize(lightmap_normal_xform * normal);
2358+
vec3 n = normalize(lightmap_normal_xform * indirect_normal);
23462359

23472360
ambient_light += lm_light_l0 * lightmap_exposure_normalization;
23482361
ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
@@ -2362,6 +2375,12 @@ void main() {
23622375

23632376
ambient_light *= ao;
23642377
#ifndef SPECULAR_OCCLUSION_DISABLED
2378+
#ifdef BENT_NORMAL_MAP_USED
2379+
float cos_b = max(dot(reflect(-view, normal), bent_normal_vector), 0.0);
2380+
float specular_occlusion = clamp((ao - (1.0 - cos_b)) / roughness, 0.0, 1.0);
2381+
specular_occlusion = mix(specular_occlusion, cos_b * (1.0 - ao), roughness);
2382+
specular_light *= specular_occlusion;
2383+
#else // BENT_NORMAL_MAP_USED
23652384
float specular_occlusion = (ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11) * 2.0; // Luminance of ambient light.
23662385
specular_occlusion = min(specular_occlusion * 4.0, 1.0); // This multiplication preserves speculars on bright areas.
23672386

@@ -2370,6 +2389,7 @@ void main() {
23702389
// Low enough for occlusion, high enough for reaction to lights and shadows.
23712390
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
23722391
specular_light *= specular_occlusion;
2392+
#endif // BENT_NORMAL_MAP_USED
23732393
#endif // !SPECULAR_OCCLUSION_DISABLED
23742394
ambient_light *= albedo.rgb;
23752395

@@ -2391,7 +2411,7 @@ void main() {
23912411
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
23922412
const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
23932413
vec4 r = roughness * c0 + c1;
2394-
float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2414+
float ndotv = clamp(dot(indirect_normal, view), 0.0, 1.0);
23952415

23962416
float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
23972417
vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;

0 commit comments

Comments
 (0)