Fix LightmapGI shading sometimes being unlit or black

(cherry picked from commit dda8846dea)
This commit is contained in:
bitsawer 2023-09-20 01:41:32 +03:00 committed by Yuri Sizov
parent 299413157a
commit 4f04d3191e
2 changed files with 10 additions and 12 deletions

View File

@ -1225,9 +1225,10 @@ void fragment_shader(in SceneData scene_data) {
} else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap } else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
bool uses_sh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP); bool uses_sh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
uint ofs = instances.data[instance_index].gi_offset & 0xFFFF; uint ofs = instances.data[instance_index].gi_offset & 0xFFFF;
uint slice = instances.data[instance_index].gi_offset >> 16;
vec3 uvw; vec3 uvw;
uvw.xy = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy; uvw.xy = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy;
uvw.z = float((instances.data[instance_index].gi_offset >> 16) & 0xFFFF); uvw.z = float(slice);
if (uses_sh) { if (uses_sh) {
uvw.z *= 4.0; //SH textures use 4 times more data uvw.z *= 4.0; //SH textures use 4 times more data
@ -1236,9 +1237,8 @@ void fragment_shader(in SceneData scene_data) {
vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb; vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb;
vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb; vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb;
uint idx = instances.data[instance_index].gi_offset >> 20; vec3 n = normalize(lightmaps.data[ofs].normal_xform * normal);
vec3 n = normalize(lightmaps.data[idx].normal_xform * normal); float en = lightmaps.data[ofs].exposure_normalization;
float en = lightmaps.data[idx].exposure_normalization;
ambient_light += lm_light_l0 * 0.282095f * en; ambient_light += lm_light_l0 * 0.282095f * en;
ambient_light += lm_light_l1n1 * 0.32573 * n.y * en; ambient_light += lm_light_l1n1 * 0.32573 * n.y * en;
@ -1252,8 +1252,7 @@ void fragment_shader(in SceneData scene_data) {
} }
} else { } else {
uint idx = instances.data[instance_index].gi_offset >> 20; ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;
ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb * lightmaps.data[idx].exposure_normalization;
} }
} }
#else #else

View File

@ -1113,11 +1113,10 @@ void main() {
} else if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap } else if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
bool uses_sh = bool(draw_call.flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP); bool uses_sh = bool(draw_call.flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
uint ofs = draw_call.gi_offset & 0xFFFF; uint ofs = draw_call.gi_offset & 0xFFFF;
uint slice = draw_call.gi_offset >> 16;
vec3 uvw; vec3 uvw;
uvw.xy = uv2 * draw_call.lightmap_uv_scale.zw + draw_call.lightmap_uv_scale.xy; uvw.xy = uv2 * draw_call.lightmap_uv_scale.zw + draw_call.lightmap_uv_scale.xy;
uvw.z = float((draw_call.gi_offset >> 16) & 0xFFFF); uvw.z = float(slice);
uint idx = draw_call.gi_offset >> 20;
if (uses_sh) { if (uses_sh) {
uvw.z *= 4.0; //SH textures use 4 times more data uvw.z *= 4.0; //SH textures use 4 times more data
@ -1126,8 +1125,8 @@ void main() {
vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb; vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb;
vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb; vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb;
vec3 n = normalize(lightmaps.data[idx].normal_xform * normal); vec3 n = normalize(lightmaps.data[ofs].normal_xform * normal);
float exposure_normalization = lightmaps.data[idx].exposure_normalization; float exposure_normalization = lightmaps.data[ofs].exposure_normalization;
ambient_light += lm_light_l0 * 0.282095f; ambient_light += lm_light_l0 * 0.282095f;
ambient_light += lm_light_l1n1 * 0.32573 * n.y * exposure_normalization; ambient_light += lm_light_l1n1 * 0.32573 * n.y * exposure_normalization;
@ -1141,7 +1140,7 @@ void main() {
} }
} else { } else {
ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb * lightmaps.data[idx].exposure_normalization; ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;
} }
} }