ModelTranslator: 修复 base 贴图双重 sRGB 编码变亮(well10k 内嵌贴图案例)

实测 blender 5.0:Image.pixels 对 8-bit 图返回原始字节值(不做色彩变换),
sRGB PNG 读出已是编码值,再 linear->sRGB 就双重 gamma(均值 0.33->0.60)。
现仅对线性来源编码:float 图(EXR)与 Principled 常量色。
端到端验证:well10k 重转后 base 与源贴图平均差 0.0013。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ud18010
2026-07-15 20:54:21 +08:00
co-authored by Claude Opus 4.8
parent 60c83a3765
commit c682ab4ebe
2 changed files with 34 additions and 8 deletions
+11 -6
View File
@@ -39,11 +39,13 @@ def linear_to_srgb(c):
1.055 * np.power(c, 1.0 / 2.4) - 0.055)
def encode_base_texture_for_png(rgba):
"""Base RGB is stored for Unity sRGB import; alpha keeps AO/opacity data."""
out = np.array(rgba, dtype=np.float32, copy=True)
out[..., :3] = linear_to_srgb(out[..., :3])
out[..., 3] = np.clip(out[..., 3], 0.0, 1.0)
def encode_base_texture_for_png(rgba, rgb_is_linear):
"""Base RGB is stored for Unity sRGB import; alpha keeps AO/opacity data.
Image.pixels 对 8-bit 图返回原始字节值(已是 sRGB 编码,实测 blender 5.0),
仅线性来源(float 图 / Principled 常量色)需要 linear->sRGB,否则双重 gamma 变亮。"""
out = np.clip(np.array(rgba, dtype=np.float32, copy=True), 0.0, 1.0)
if rgb_is_linear:
out[..., :3] = linear_to_srgb(out[..., :3])
return out
@@ -280,8 +282,11 @@ def _convert_material(mat, model_name, outdir, max_size, used_snames, src_dir):
base = np.empty((th, tw, 4), dtype=np.float32)
if maps["basecolor"]:
base[..., :3] = _scaled_pixels(maps["basecolor"][0], tw, th)[..., :3]
# 8-bit 图 pixels 已是 sRGB 编码值;float 图(EXR 等)才是线性
rgb_is_linear = bool(maps["basecolor"][0].is_float)
else:
base[..., :3] = np.array(consts["basecolor"][:3], dtype=np.float32)
rgb_is_linear = True # Principled 常量色是线性值
if maps["alpha"]:
img, ch = maps["alpha"]
base[..., 3] = _channel(_scaled_pixels(img, tw, th), ch)
@@ -330,7 +335,7 @@ def _convert_material(mat, model_name, outdir, max_size, used_snames, src_dir):
stem = model_name if sname == model_name else "%s_%s" % (model_name, sname)
base_png = os.path.abspath(os.path.join(outdir, "%s_base.png" % stem))
mix_png = os.path.abspath(os.path.join(outdir, "%s_mix.png" % stem))
_save_png(base_png, encode_base_texture_for_png(base))
_save_png(base_png, encode_base_texture_for_png(base, rgb_is_linear))
_save_png(mix_png, mix)
return {