Skip to content

Commit

Permalink
Darken bakemap if exceeding the LDR range (breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbousquet committed Mar 23, 2024
1 parent 911d677 commit 51859c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion addons/vpx_lightmapper/vlm_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def append_structure(src_path, mode, hashed):
writer.close()
dst_stream = dst_gamestg.CreateStream(f'Image{n_images}', storagecon.STGM_DIRECT | storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE | storagecon.STGM_CREATE, 0, 0)
dst_stream.Write(writer.get_data())
logger.info(f'. Adding Nestmap #{nestmap_index} as a {width:>4} x {height:>4} image (HDR: {is_hdr})')
logger.info(f'. Adding NestNormalMap #{nestmap_index} as a {width:>4} x {height:>4} image (HDR: {is_hdr})')
n_images += 1
nestmap_index += 1

Expand Down
11 changes: 9 additions & 2 deletions addons/vpx_lightmapper/vlm_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,13 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest
uniform sampler2D render;
uniform vec2 src_size;
uniform int padding;
uniform float hdr_scale;
void main() {
if (uv.x < 0.0 || uv.x >= 1.0 || uv.y < 0.0 || uv.y >= 1.0)
FragColor = vec4(0.0);
else
{
vec4 rescale = vec4(hdr_scale, hdr_scale, hdr_scale, 1.0);
vec2 min_uv = vec2(0.5/src_size);
vec2 max_uv = vec2(1.0 - 0.5/src_size);
float seam_sum = 0.0;
Expand All @@ -547,7 +549,7 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest
}
else
{ // Only accumulate outside of border (mask < 1.0) since it would contain border fade as well mixed with part transparency
padding_accum += texture(render, uv_ofs) * dist_factor;
padding_accum += texture(render, uv_ofs) * rescale * dist_factor;
padding_sum += dist_factor;
}
}
Expand All @@ -561,7 +563,7 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest
float inside = smoothstep(0.0, 2.0, sqrt(distance_to_outside)); // Fixed 2 pixel border/interior fading
seam = seam / seam_sum;
seam.a = step(0.001, seam.a); // binary island mask
FragColor = seam * mix(padding_accum / padding_sum, texture(render, uv), inside);
FragColor = seam * mix(padding_accum / padding_sum, texture(render, uv) * rescale, inside);
// Clamp to avoid overflows in VPX shaders
FragColor = clamp(FragColor, vec4(0.), vec4(1000., 1000., 1000., 1000.));
}
Expand Down Expand Up @@ -695,6 +697,10 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest
if not island_render is None:
with offscreen_renders[n].bind():
render_shader.bind()
if not obj.vlmSettings.is_lightmap and obj.vlmSettings.bake_hdr_range > 1.0:
render_shader.uniform_float("hdr_scale", 1.0 / obj.vlmSettings.bake_hdr_range)
else:
render_shader.uniform_float("hdr_scale", 1.0)
render_shader.uniform_float("src_size", (src_w, src_h))
render_shader.uniform_float("dst_size", (target_w, target_h))
render_shader.uniform_float("ref_width", mask_w)
Expand All @@ -718,6 +724,7 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest
with_normalmap = True
with offscreen_normalmaps[n].bind():
render_shader.bind()
render_shader.uniform_float("hdr_scale", 1.0)
render_shader.uniform_float("src_size", (src_w, src_h))
render_shader.uniform_float("dst_size", (target_w, target_h))
render_shader.uniform_float("ref_width", mask_w)
Expand Down
9 changes: 7 additions & 2 deletions addons/vpx_lightmapper/vlm_nestmap_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def render_nestmaps(op, context):
to_nest_hdr = []
to_nest_ldr_nm = []
to_nest_hdr_nm = []
bakemap_hdr_range = 0.0
for obj in to_nest:
uvmap = next((uv for uv in obj.data.uv_layers if uv.name == 'UVMap'), None)
if uvmap is None:
Expand All @@ -55,8 +56,8 @@ def render_nestmaps(op, context):
has_normalmap = next((mat for mat in obj.data.materials if mat.get('VLM.HasNormalMap') == True and mat['VLM.IsLightmap'] == False), None) is not None
# VPX only supports opaque HDR therefore we pack all non lightmaps as LDR (luckily base bake is usually LDR, and we don't really need this for lightmaps which are RGB only)
if not obj.vlmSettings.is_lightmap or obj.vlmSettings.bake_hdr_range <= 1.0:
if obj.vlmSettings.bake_hdr_range > 1.0:
logger.error('ERROR: Object {obj.name} is packed to an LDR nestmap while it has an HDR range of {obj.vlmSettings.bake_hdr_range}. Render will be wrongly clamped. You need to reduce bake lighting strength to avoid this.')
if not obj.vlmSettings.is_lightmap and obj.vlmSettings.bake_hdr_range > bakemap_hdr_range:
bakemap_hdr_range = obj.vlmSettings.bake_hdr_range
if has_normalmap:
to_nest_ldr_nm.append(obj)
else:
Expand All @@ -72,6 +73,10 @@ def render_nestmaps(op, context):
max_tex_size = min(8192, int(context.scene.vlmSettings.tex_size))
if len(to_nest_ldr) > 0:
logger.info('\nNesting all LDR parts')
logger.info(f'> Bakemap HDR range: {bakemap_hdr_range} (render lighting will be rescaled accordingly)')
for obj in to_nest_ldr:
if not obj.vlmSettings.is_lightmap:
obj.vlmSettings.bake_hdr_range = bakemap_hdr_range
n_ldr_nestmaps, splitted_objects = vlm_nest.nest(context, to_nest_ldr, 'UVMap', 'UVMap Nested', max_tex_size, max_tex_size, 'Nestmap', n_nestmaps)
n_nestmaps += n_ldr_nestmaps
if len(to_nest_hdr) > 0:
Expand Down

0 comments on commit 51859c3

Please sign in to comment.