Skip to content

Commit b5cb821

Browse files
committed
Add bent normal map support for the compatibility renderer
1 parent 63227bb commit b5cb821

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
@@ -56,7 +56,7 @@ RENDER_MOTION_VECTORS = false
5656

5757
#include "stdlib_inc.glsl"
5858

59-
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
59+
#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)
6060
#ifndef NORMAL_USED
6161
#define NORMAL_USED
6262
#endif
@@ -511,7 +511,7 @@ out vec2 uv_interp;
511511
out vec2 uv2_interp;
512512
#endif
513513

514-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
514+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
515515
out vec3 tangent_interp;
516516
out vec3 binormal_interp;
517517
#endif
@@ -604,7 +604,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
604604
model_normal_matrix = mat3(model_matrix);
605605
}
606606

607-
#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
607+
#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
608608

609609
vec3 binormal;
610610
float binormal_sign;
@@ -679,7 +679,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
679679
normal = model_normal_matrix * normal;
680680
#endif
681681

682-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
682+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
683683

684684
tangent = model_normal_matrix * tangent;
685685
binormal = model_normal_matrix * binormal;
@@ -712,7 +712,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
712712
normal = modelview_normal * normal;
713713
#endif
714714

715-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
715+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
716716

717717
binormal = modelview_normal * binormal;
718718
tangent = modelview_normal * tangent;
@@ -727,7 +727,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
727727
normal = (scene_data_input.view_matrix * vec4(normal, 0.0)).xyz;
728728
#endif
729729

730-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
730+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
731731
binormal = (scene_data_input.view_matrix * vec4(binormal, 0.0)).xyz;
732732
tangent = (scene_data_input.view_matrix * vec4(tangent, 0.0)).xyz;
733733
#endif
@@ -741,7 +741,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
741741
normal_interp = normalize(normal);
742742
#endif
743743

744-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
744+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
745745
tangent_interp = normalize(tangent);
746746
binormal_interp = normalize(binormal);
747747
#endif
@@ -984,7 +984,7 @@ void main() {
984984
#define SPECULAR_SCHLICK_GGX
985985
#endif
986986

987-
#if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
987+
#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)
988988
#ifndef NORMAL_USED
989989
#define NORMAL_USED
990990
#endif
@@ -1053,7 +1053,7 @@ in vec2 uv2_interp;
10531053
#endif
10541054
#endif
10551055

1056-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
1056+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
10571057
in vec3 tangent_interp;
10581058
in vec3 binormal_interp;
10591059
#endif
@@ -2029,7 +2029,7 @@ void main() {
20292029

20302030
float alpha = 1.0;
20312031

2032-
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
2032+
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
20332033
vec3 binormal = binormal_interp;
20342034
vec3 tangent = tangent_interp;
20352035
#else
@@ -2064,6 +2064,7 @@ void main() {
20642064
#endif
20652065

20662066
#if defined(BENT_NORMAL_MAP_USED)
2067+
vec3 bent_normal_vector;
20672068
vec3 bent_normal_map = vec3(0.5);
20682069
#endif
20692070

@@ -2145,6 +2146,13 @@ void main() {
21452146

21462147
#endif // !USE_SHADOW_TO_OPACITY
21472148

2149+
#ifdef BENT_NORMAL_MAP_USED
2150+
bent_normal_map.xy = bent_normal_map.xy * 2.0 - 1.0;
2151+
bent_normal_map.z = sqrt(max(0.0, 1.0 - dot(bent_normal_map.xy, bent_normal_map.xy)));
2152+
2153+
bent_normal_vector = normalize(tangent * bent_normal_map.x + binormal * bent_normal_map.y + normal * bent_normal_map.z);
2154+
#endif
2155+
21482156
#if defined(NORMAL_MAP_USED)
21492157
normal_map.xy = normal_map.xy * 2.0 - 1.0;
21502158
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.
@@ -2199,8 +2207,13 @@ void main() {
21992207
/////////////////////// LIGHTING //////////////////////////////
22002208

22012209
#ifndef AMBIENT_LIGHT_DISABLED
2210+
#ifdef BENT_NORMAL_MAP_USED
2211+
vec3 indirect_normal = bent_normal_vector;
2212+
#else
2213+
vec3 indirect_normal = normal;
2214+
#endif // BENT_NORMAL_MAP_USED
22022215
// IBL precalculations
2203-
float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2216+
float ndotv = clamp(dot(indirect_normal, view), 0.0, 1.0);
22042217
vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
22052218

22062219
#ifdef USE_RADIANCE_MAP
@@ -2210,13 +2223,13 @@ void main() {
22102223
vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
22112224
vec3 anisotropic_tangent = cross(anisotropic_direction, view);
22122225
vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
2213-
vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
2226+
vec3 bent_normal = normalize(mix(indirect_normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
22142227
vec3 ref_vec = reflect(-view, bent_normal);
22152228
#else
2216-
vec3 ref_vec = reflect(-view, normal);
2229+
vec3 ref_vec = reflect(-view, indirect_normal);
22172230
#endif
2218-
ref_vec = mix(ref_vec, normal, roughness * roughness);
2219-
float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
2231+
ref_vec = mix(ref_vec, indirect_normal, roughness * roughness);
2232+
float horizon = min(1.0 + dot(ref_vec, indirect_normal), 1.0);
22202233
ref_vec = mat3(scene_data_block.data.radiance_inverse_xform) * ref_vec;
22212234
specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
22222235
specular_light = srgb_to_linear(specular_light);
@@ -2231,14 +2244,14 @@ void main() {
22312244
{
22322245
vec4 reflection_accum = vec4(0.0);
22332246

2234-
reflection_process(refprobe1_texture, normal, vertex_interp, refprobe1_local_matrix,
2247+
reflection_process(refprobe1_texture, indirect_normal, vertex_interp, refprobe1_local_matrix,
22352248
refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
22362249
refprobe1_exterior, refprobe1_intensity, refprobe1_blend_distance, refprobe1_ambient_mode, refprobe1_ambient_color,
22372250
roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
22382251

22392252
#ifdef SECOND_REFLECTION_PROBE
22402253

2241-
reflection_process(refprobe2_texture, normal, vertex_interp, refprobe2_local_matrix,
2254+
reflection_process(refprobe2_texture, indirect_normal, vertex_interp, refprobe2_local_matrix,
22422255
refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
22432256
refprobe2_exterior, refprobe2_intensity, refprobe2_blend_distance, refprobe2_ambient_mode, refprobe2_ambient_color,
22442257
roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
@@ -2262,7 +2275,7 @@ void main() {
22622275

22632276
#ifdef USE_RADIANCE_MAP
22642277
if (scene_data_block.data.use_ambient_cubemap) {
2265-
vec3 ambient_dir = mat3(scene_data_block.data.radiance_inverse_xform) * normal;
2278+
vec3 ambient_dir = mat3(scene_data_block.data.radiance_inverse_xform) * indirect_normal;
22662279
vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
22672280
cubemap_ambient = srgb_to_linear(cubemap_ambient);
22682281
ambient_light = mix(ambient_light, cubemap_ambient * scene_data_block.data.ambient_light_color_energy.a, scene_data_block.data.ambient_color_sky_mix);
@@ -2285,7 +2298,7 @@ void main() {
22852298
#ifdef USE_LIGHTMAP_CAPTURE
22862299
{
22872300
// The world normal.
2288-
vec3 wnormal = mat3(scene_data_block.data.inv_view_matrix) * normal;
2301+
vec3 wnormal = mat3(scene_data_block.data.inv_view_matrix) * indirect_normal;
22892302

22902303
// The SH coefficients used for evaluating diffuse data from SH probes.
22912304
const float c0 = 0.886227; // l0 sqrt(1.0/(4.0*PI)) * PI
@@ -2327,7 +2340,7 @@ void main() {
23272340
vec3 lm_light_l1p1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0;
23282341
#endif
23292342

2330-
vec3 n = normalize(lightmap_normal_xform * normal);
2343+
vec3 n = normalize(lightmap_normal_xform * indirect_normal);
23312344

23322345
ambient_light += lm_light_l0 * lightmap_exposure_normalization;
23332346
ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
@@ -2347,6 +2360,12 @@ void main() {
23472360

23482361
ambient_light *= ao;
23492362
#ifndef SPECULAR_OCCLUSION_DISABLED
2363+
#ifdef BENT_NORMAL_MAP_USED
2364+
float cos_b = max(dot(reflect(-view, normal), bent_normal_vector), 0.0);
2365+
float specular_occlusion = clamp((ao - (1.0 - cos_b)) / roughness, 0.0, 1.0);
2366+
specular_occlusion = mix(specular_occlusion, cos_b * (1.0 - ao), roughness);
2367+
specular_light *= specular_occlusion;
2368+
#else // BENT_NORMAL_MAP_USED
23502369
float specular_occlusion = (ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11) * 2.0; // Luminance of ambient light.
23512370
specular_occlusion = min(specular_occlusion * 4.0, 1.0); // This multiplication preserves speculars on bright areas.
23522371

@@ -2355,6 +2374,7 @@ void main() {
23552374
// Low enough for occlusion, high enough for reaction to lights and shadows.
23562375
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
23572376
specular_light *= specular_occlusion;
2377+
#endif // BENT_NORMAL_MAP_USED
23582378
#endif // !SPECULAR_OCCLUSION_DISABLED
23592379
ambient_light *= albedo.rgb;
23602380

@@ -2376,7 +2396,7 @@ void main() {
23762396
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
23772397
const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
23782398
vec4 r = roughness * c0 + c1;
2379-
float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2399+
float ndotv = clamp(dot(indirect_normal, view), 0.0, 1.0);
23802400

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

0 commit comments

Comments
 (0)