ModelTranslator: 烘焙射线距离/命名/包围盒校验纯逻辑(TDD)

This commit is contained in:
ud18010
2026-07-16 10:44:33 +08:00
parent 65b4c3fe8e
commit 7c3b86a995
2 changed files with 87 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
"""Blender 内运行:高低模 Cycles 烘焙,低模 UV 上出 normal/ao/color/metallic/roughness。
调用:blender -b --factory-startup --python bl_bake.py -- \
<high.fbx> <low.fbx> <outdir> <size> <ray_distance|auto> <samples>
"""
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
DEFAULT_RAY_PCT = 0.02 # 自动射线距离 = 低模包围盒对角线 * 2%
BBOX_TOL = 0.10 # 高低模包围盒尺寸相对差警告阈值
# (输出名, bake type, EMIT 来源的 Principled 输入, 目标图颜色空间)
BAKE_PASSES = [
("normal", 'NORMAL', None, 'Non-Color'),
("ao", 'AO', None, 'Non-Color'),
("color", 'EMIT', 'Base Color', 'sRGB'),
("metallic", 'EMIT', 'Metallic', 'Non-Color'),
("roughness", 'EMIT', 'Roughness', 'Non-Color'),
]
def estimate_ray_distance(bbox_dims, pct=DEFAULT_RAY_PCT):
"""低模包围盒尺寸 (dx,dy,dz) -> 射线距离 = 对角线长 * pct。"""
return math.sqrt(sum(d * d for d in bbox_dims)) * pct
def output_stem(low_name):
"""低模名去掉 _low 后缀作为输出前缀(对齐工具3纯网格贴图命名约定)。"""
return low_name[:-4] if low_name.endswith("_low") else low_name
def bbox_mismatch(high_dims, low_dims, tol=BBOX_TOL):
"""任一轴尺寸相对差超 tol 返回 True;接近 0 的轴忽略(平面模型)。"""
for h, l in zip(high_dims, low_dims):
m = max(abs(h), abs(l))
if m > 1e-9 and abs(h - l) / m > tol:
return True
return False