"""合并 5 个乳几何为单一紧凑 JSON,供内联进 sky-pool.html (单文件离线)。
约定: **乳根圆盘在 z=0(胸腔贴附点), 乳尖在 z≈+baseR**。
  不强行拉长乳轴——乳的 x/y(宽) 本就是 baseR 量级, z 保持 baseR 量级即可成为「半球/水滴」
  而非细长条。mesh.position 的 z 即「乳根贴胸点」。
做三件事:
  1. 去除 normals(网页 computeVertexNormals 重算, 省体积)
  2. z 缩放: 乳尖 depth→baseR (zscale=baseR/depth), 乳根 0→0
  3. x/y 保持原比例; 数值压缩(3位小数)
输出: assets/models/breasts_inline.json  {gxy:{positions,uvs,indices,meta},...}
"""
import bpy, json, os, sys, math

SRC="/home/gordon/Documents/repo/fiction/my-mother-is-a-god-emperor/assets/models"
OUT=os.path.join(SRC,"breasts_inline.json")

params=json.load(open(os.path.join(SRC,"all_breast_params.json")))
result={}
for cid in ["gxy","jnx","yji","wz","fy"]:
    d=json.load(open(os.path.join(SRC,f"breast_{cid}.json")))
    depth=d["meta"]["depth"]; baseR=d["meta"]["baseR"]; shade=d["meta"]
    positions=d["positions"]; uvs=d["uvs"]; indices=d["indices"]
    # 原几何(单乳): 乳根 z≈0, 乳尖 z≈depth. 缩放使乳根→0, 乳尖→baseR
    zscale=baseR/depth if depth>1e-6 else 1.0
    newpos=[]
    for i in range(0,len(positions),3):
        x=positions[i]; y=positions[i+1]; z=positions[i+2]
        z2=z*zscale
        newpos+= [round(x,3), round(y,3), round(z2,3)]
    result[cid]={
        "positions":newpos,
        "uvs":[round(u,3) for u in uvs],
        "indices":indices,
        "meta":{"baseR":baseR,"depth":shade["depth"],"verts":shade["verts"],"tris":shade["tris"]}
    }
with open(OUT,"w") as f:
    json.dump(result,f,ensure_ascii=False,separators=(",",":"))
tot=os.path.getsize(OUT)
print("WRITTEN",OUT,tot,"bytes")
for cid in result:
    zs=result[cid]["positions"][2::3]
    print(cid,"verts",result[cid]["meta"]["verts"],"tris",result[cid]["meta"]["tris"],"zRange",round(min(zs),3),round(max(zs),3))
