feat: 树渲染改用 apple/fattree 模型,修复 Cesium alpha 抠图丢失

tree_style 新增 apple(SpeedTree Red Delicious,4475 tris,alpha 抠图叶片
+ 法线贴图)和 fattree(低面数卡通树,2238 tris,实体几何)。

高度改为按模型自身包围盒归一化到目标高度,树根落在 z=0,不再用魔数;
每棵树按序号做确定性抖动(缩放 ±14%、黄金角偏航、±3° 倾斜),
tree_row 采样高度全部相同,不抖动就是同一棵树盖章 156 次。

移除 polyhaven 样式和 island_tree_01 资产(76MB)+ ingest_tree.py:
该样式 append 的 *_LOD1 并不是完整的树,枝干只有 0.41 单位高,叶片是
挂在原点下方的平面簇,本是给源文件几何节点散布用的碎片。

Cesium 侧修三处:

- make_export_material 把 Alpha 恒定写死为 1.0,叶片卡片整块导出,而
  SpeedTree 图集抠掉的区域是纯黑,在 Cesium 里就是黑色色块。
- Blender 4.2 起 glTF 导出不再读 blend_method(仍可写但已失效,写 CLIP
  读回来是 HASHED),改为从节点树推断 alpha 模式,alpha 直连 BSDF 会落到
  BLEND。新增 materials.link_alpha_clip() 构造导出器识别的
  1 - (alpha < cutoff) 结构,得到 alphaMode=MASK。
- 新增 alpha_dilated_image():把不透明像素颜色向抠图区域外扩 8 圈,
  避免 mipmap 把黑色平均进叶缘。叶缘相邻的纯黑像素 10.3% → 0.6%。

另修两个既有 bug:

- 材质槽在 mesh 上,181 棵树共享一个 datablock,第一棵替换后其余会把结果
  再包一层,产生 Cesium Cesium Cesium... 的材质名;烘焙图缓存按材质名索引,
  每轮再嵌一份同样的贴图。GLB 22.46MB → 20.76MB,materials 270 → 23。
- shrub_02 从 glTF 带进来一个 Math 节点接在 Alpha 上,但其 JPEG 贴图 alpha
  全是 1.0,只判断「连了 Alpha」会误判为抠图材质。

模型资产为第三方素材,已 gitignore,缺失时回落到 natural;
来源见 assets/models/SOURCES.md。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 11:30:16 +08:00
parent b35b094ed2
commit 2139990818
10 changed files with 564 additions and 257 deletions

View File

@@ -116,6 +116,11 @@ TUFT_LIMIT_PER_LAWN = 100
TUFT_TINT = (0.15, 0.52, 0.09)
TUFT_TINT_FACTOR = 0.66
# Tree styles. The two built from mesh batches live in this file; the rest are
# vendored models handled by osmassets.tree, which owns that list. A model style
# whose asset is missing falls back to "natural" rather than planting nothing.
TREE_STYLES = frozenset(("natural", "procedural")) | frozenset(_tree.MODEL_STYLES)
def cli_args():
values = {"osm": None, "geojson": None, "output": None, "render": None,
@@ -143,8 +148,9 @@ def cli_args():
values["office_overrides"] = set()
else:
values["office_overrides"] = set()
if values.get("tree_style") not in {"natural", "procedural", "polyhaven"}:
raise RuntimeError("--tree-style must be 'natural', 'procedural', or 'polyhaven'")
if values.get("tree_style") not in TREE_STYLES:
raise RuntimeError("--tree-style must be one of: "
+ ", ".join(sorted(TREE_STYLES)))
return values
@@ -712,20 +718,25 @@ def build(args):
height=parse_height(row_tags, 5.0))
trees.extend(row_samples)
row_tree_count += len(row_samples)
tree_style = args.get("tree_style")
tree_style_used = tree_style
if trees:
tree_style = args.get("tree_style")
if tree_style == "polyhaven":
_tree.assemble(trees, props_c)
elif tree_style == "natural":
# A model style returns 0 when its vendored asset is missing; that drops
# through to "natural" so a clean checkout still gets trees.
placed = (_tree.assemble(trees, props_c, tree_style)
if tree_style in _tree.MODEL_STYLES else 0)
if not placed:
tree_style_used = "procedural" if tree_style == "procedural" else "natural"
tree_trunk = material_from_spec(catalog.MATERIALS["tree_trunk"])
add_natural_tree_instances(
trees, props_c, tree_trunk,
material_from_spec(catalog.MATERIALS["tree_crown_dark"]),
material_from_spec(catalog.MATERIALS["tree_crown_light"]))
else:
tree_trunk = material_from_spec(catalog.MATERIALS["tree_trunk"])
add_tree_batch(trees, props_c, tree_trunk,
material_from_spec(catalog.MATERIALS["tree_crown"]))
if tree_style_used == "procedural":
add_tree_batch(
trees, props_c, tree_trunk,
material_from_spec(catalog.MATERIALS["tree_crown"]))
else:
add_natural_tree_instances(
trees, props_c, tree_trunk,
material_from_spec(catalog.MATERIALS["tree_crown_dark"]),
material_from_spec(catalog.MATERIALS["tree_crown_light"]))
for feature in point_features:
if feature["tags"].get("amenity") != "fountain":
@@ -793,6 +804,9 @@ def build(args):
scene["tree_node_count"] = individual_tree_count
scene["tree_row_count"] = row_tree_count
scene["tree_count"] = len(trees)
scene["tree_style"] = tree_style
# Differs from tree_style when a model style's asset was missing.
scene["tree_style_used"] = tree_style_used
scene["road_feature_counts"] = json.dumps(road_counts, ensure_ascii=True)
os.makedirs(os.path.dirname(args["output"]), exist_ok=True)
@@ -819,6 +833,7 @@ def build(args):
"tree_nodes": individual_tree_count,
"tree_row_instances": row_tree_count,
"trees": len(trees),
"tree_style": tree_style_used,
"road_features": road_counts}, ensure_ascii=True))