diff --git a/addons/vpx_lightmapper/vlm_export.py b/addons/vpx_lightmapper/vlm_export.py index 88baf42..1fc4ad6 100644 --- a/addons/vpx_lightmapper/vlm_export.py +++ b/addons/vpx_lightmapper/vlm_export.py @@ -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 diff --git a/addons/vpx_lightmapper/vlm_nest.py b/addons/vpx_lightmapper/vlm_nest.py index 700c6c9..a60bd84 100644 --- a/addons/vpx_lightmapper/vlm_nest.py +++ b/addons/vpx_lightmapper/vlm_nest.py @@ -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; @@ -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; } } @@ -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.)); } @@ -599,7 +601,10 @@ def render_nestmap(context, selection, uv_bake_name, nestmap, nestmap_name, nest with_normalmap = False for obj_name in sorted(list({obj.name for (obj, _, _, _) in selection}), key=lambda x:bpy.data.objects[x].vlmSettings.bake_lighting): obj = bpy.data.objects[obj_name] - logger.info(f'. Copying renders (HDR range={obj.vlmSettings.bake_hdr_range:>7.2f}) for object {obj.name} from {obj.vlmSettings.bake_lighting} renders') + msg = '' + if not obj.vlmSettings.is_lightmap and obj.vlmSettings.bake_hdr_range > 1.0: + msg = f'. WARNING: Darkening to avoid LDR overflow (darkening factor is HDR range)' + logger.info(f'. Copying renders (HDR range={obj.vlmSettings.bake_hdr_range:>7.2f}) for object {obj.name} from {obj.vlmSettings.bake_lighting} renders{msg}') # Render to the packed nest map for island in islands: @@ -695,6 +700,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) @@ -718,6 +727,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) diff --git a/addons/vpx_lightmapper/vlm_nestmap_baker.py b/addons/vpx_lightmapper/vlm_nestmap_baker.py index 27a120d..67a55fc 100644 --- a/addons/vpx_lightmapper/vlm_nestmap_baker.py +++ b/addons/vpx_lightmapper/vlm_nestmap_baker.py @@ -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: @@ -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: @@ -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: