Skip to content

Commit 448f45b

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

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

drivers/gles3/shaders/scene.glsl

Lines changed: 32 additions & 12 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
@@ -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

@@ -2156,6 +2157,13 @@ void main() {
21562157
normal = geo_normal;
21572158
#endif // NORMAL_MAP_USED
21582159

2160+
#ifdef BENT_NORMAL_MAP_USED
2161+
bent_normal_map.xy = bent_normal_map.xy * 2.0 - 1.0;
2162+
bent_normal_map.z = sqrt(max(0.0, 1.0 - dot(bent_normal_map.xy, bent_normal_map.xy)));
2163+
2164+
bent_normal_vector = normalize(tangent * bent_normal_map.x + binormal * bent_normal_map.y + normal * bent_normal_map.z);
2165+
#endif
2166+
21592167
#ifdef LIGHT_ANISOTROPY_USED
21602168

21612169
if (anisotropy > 0.01) {
@@ -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)