Files
Spellcast3d/code/tests/bake/test_real_assets.py
Igor AherneandClaude Opus 5.5 56d85f6eeb Spellcast3D 0.1.0
A free, open-source Windows app that takes an artist from an idea to game-ready 3D props on their own PC: pictures with Qwen-Image 2.1, 3D models with Pixal3D, an optional repaint of their textures, retopology with TriFlow, LATO.2 or Instant Meshes, and a baker for the final textures. See README.md, and DEVELOPERS.md for building from source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 02:38:33 +01:00

166 lines
8.0 KiB
Python

"""L4 (design section 7): real assets, where no analytic truth exists.
* Self-bake identity: Pixal3D's output.glb baked onto its own UVs at the source size with
S = 1 must reproduce its texture (PSNR > 40 dB on covered texels) and give a flat normal
map, within 1 LSB of (128, 128, 255) on 99% of texels.
* E2E prop p_025d38 at 2048^2, S = 2, repack off, against Blender 5.1 Cycles
"selected to active" on the same retopo with a +-2% envelope: normal < 1 deg on >= 90%
of texels, base colour <= 3/255 on >= 90%, misses <= 0.5%, <= 60 s on 4 threads, peak
RSS <= 3 GB (measured with psutil when it is installed). Blender's bake used smooth
normals and a 2% envelope, so this bake does too (hard_edge_angle -1, ray distance 2%).
* Round trip: that bake baked back onto the high poly's own UVs: PSNR > 24 dB on the texels
of high triangles open to the outside (measured 25.1 dB on 43% of the texels). The design's
30 dB over every texel cannot be met by any projection on this asset: the other 57% lie on
an enclosed inner shell that no ray from outside reaches. On the texels Blender 5.1 Cycles
covers, Blender's own round trip scores 18.7 dB over all and 26.0 dB on the open ones; this
baker 20.7 and 26.5.
They need the real baker (``spellbake.bake``), run it in a child process (so time and
memory are the bake's alone) on COPIES of the inputs, and skip when the files are absent.
SPELLCAST_PIXAL3D_GLB, SPELLCAST_E2E_PROP and SPELLCAST_BLENDER_REF override the paths of
the development PC.
"""
from __future__ import annotations
import json
import os
import shutil
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import numpy as np
import pytest
from bakecheck import bakecheck as bc
from bakecheck import fixtures as fx
from spellbake.ao import ambient_occlusion
from spellbake.rays import Scene
pytestmark = [pytest.mark.realasset, pytest.mark.slow]
PIXAL3D_GLB = Path(os.environ.get("SPELLCAST_PIXAL3D_GLB",
r"C:\_myDrive\repos\Pixal3D-stableprojectorz\output.glb"))
E2E_PROP = Path(os.environ.get("SPELLCAST_E2E_PROP", r"C:\sc3d\spellcast3d\projects\E2E Props\props\p_025d38"))
BLENDER_REF = Path(os.environ.get(
"SPELLCAST_BLENDER_REF",
Path(tempfile.gettempdir()) / "claude" / "spellcast3d-scratch" / "texres" / "bake" / "blender_out"))
GIB = 1024 ** 3
def need(*paths: Path) -> None:
missing = [str(p) for p in paths if not p.exists()]
if missing:
pytest.skip(f"real asset not on this PC: {', '.join(missing)}")
if not fx.spellbake_available():
pytest.skip("spellbake.bake is not importable yet (bake-pipeline)")
def covered(g: bc.BakedGlb, res: int) -> np.ndarray:
"""Texels whose centre lies inside a UV triangle of the baked mesh."""
return bc.raster_uv(g.mesh.uv, g.mesh.faces, res)[0] >= 0
def psnr(a: np.ndarray, b: np.ndarray) -> float:
mse = float(((a.astype(np.float64) - b.astype(np.float64)) ** 2).mean())
return float("inf") if mse == 0 else 10 * np.log10(255.0 ** 2 / mse)
def bake(high: Path, low: Path, out: Path, recipe: fx.Recipe, threads: int) -> tuple[bc.BakedGlb, dict[str, Any]]:
run = fx.bake_in_child(high, low, out, recipe, threads)
run["stats"] = json.loads((out / "bake.json").read_text(encoding="utf-8"))["stats"]
return bc.read_glb(out / "projection.glb"), run
def test_self_bake_identity(tmp_path: Path) -> None:
need(PIXAL3D_GLB)
source_path = tmp_path / "output.glb"
shutil.copyfile(PIXAL3D_GLB, source_path)
source = bc.read_glb(source_path)
size = source.images["baseColor"].shape[0]
g, _ = bake(source_path, source_path, tmp_path / "bake",
fx.Recipe(texture_size=size, quality=1, repack_uvs=False), os.cpu_count() or 4)
mask = covered(g, size)
base_psnr = psnr(g.images["baseColor"][mask][:, :3], source.images["baseColor"][mask][:, :3])
assert base_psnr > 40, f"self-bake base colour PSNR {base_psnr:.1f} dB"
flat = (np.abs(g.images["normal"][mask][:, :3].astype(np.int64) - (128, 128, 255)) <= 1).all(-1).mean()
assert flat >= 0.99, f"only {flat:.2%} of texels within 1 LSB of the flat normal"
@dataclass(frozen=True)
class PropBake:
high: Path
glb: bc.BakedGlb
run: dict[str, Any]
@pytest.fixture(scope="module")
def prop_bake(tmp_path_factory: pytest.TempPathFactory) -> PropBake:
need(E2E_PROP / "mesh" / "mesh.glb", E2E_PROP / "retopo" / "retopo.obj")
work = tmp_path_factory.mktemp("prop")
high = Path(shutil.copyfile(E2E_PROP / "mesh" / "mesh.glb", work / "mesh.glb"))
low = Path(shutil.copyfile(E2E_PROP / "retopo" / "retopo.obj", work / "retopo.obj"))
recipe = fx.Recipe(texture_size=2048, quality=2, repack_uvs=False, hard_edge_angle=-1, ray_distance=2.0,
ao=True)
glb, run = bake(high, low, work / "bake", recipe, threads=4)
return PropBake(high, glb, run)
def test_e2e_prop_matches_blender(prop_bake: PropBake) -> None:
need(BLENDER_REF / "blender_normal.npy", BLENDER_REF / "blender_emit.npy")
ref_normal = np.load(BLENDER_REF / "blender_normal.npy", mmap_mode="r")
ref_emit = np.load(BLENDER_REF / "blender_emit.npy", mmap_mode="r")
g = prop_bake.glb
mask = covered(g, 2048) & (np.asarray(ref_emit[..., 3]) > 0)
ours = g.images["normal"][mask][:, :3] / 255.0 * 2 - 1
theirs = np.asarray(ref_normal[..., :3])[mask] * 2 - 1
within_1deg = (bc.angle_deg(ours, theirs) < 1.0).mean()
ref_colour = bc.linear_to_srgb(np.asarray(ref_emit[..., :3])[mask]) * 255
colour = np.abs(g.images["baseColor"][mask][:, :3] - ref_colour)
within_3lsb = (colour.max(-1) <= 3.0).mean()
assert mask.sum() > 1_000_000
assert within_1deg >= 0.90, f"normal within 1 deg of Blender on {within_1deg:.1%} of texels"
assert within_3lsb >= 0.90, f"base colour within 3/255 of Blender on {within_3lsb:.1%} of texels"
assert prop_bake.run["stats"]["miss_fraction"] <= 0.005
def test_e2e_prop_time_and_memory(prop_bake: PropBake) -> None:
assert prop_bake.run["seconds"] <= 60, f"bake took {prop_bake.run['seconds']:.1f} s on 4 threads"
peak = prop_bake.run["peak_rss"]
if peak is None:
pytest.skip("psutil is not installed: peak memory not measured")
assert peak <= 3 * GIB, f"peak RSS {peak / GIB:.2f} GB"
def test_round_trip(prop_bake: PropBake, tmp_path: Path) -> None:
"""The low-poly bake, baked back onto the high poly's own UVs, gives its texture back."""
source = bc.read_glb(prop_bake.high)
size = source.images["baseColor"].shape[0]
back_high = Path(shutil.copyfile(prop_bake.high.parent / "bake" / "projection.glb", tmp_path / "low.glb"))
g, _ = bake(back_high, prop_bake.high, tmp_path / "back",
fx.Recipe(texture_size=size, quality=2, repack_uvs=False), os.cpu_count() or 4)
triangle = bc.raster_uv(source.mesh.uv, source.mesh.faces, size)[0]
mask = triangle >= 0
covered_texels = int(mask.sum())
mask[mask] = open_to_outside(source.mesh)[triangle[mask]]
assert mask.sum() > 0.25 * covered_texels, f"only {mask.sum()} of {covered_texels} texels are open"
value = psnr(g.images["baseColor"][mask][:, :3], source.images["baseColor"][mask][:, :3])
assert value > 24, f"round-trip base colour PSNR {value:.1f} dB on the open triangles"
def open_to_outside(mesh: bc.LowMesh) -> np.ndarray:
"""(nF,) bool: on one side or the other, at least 2% of the cosine-weighted hemisphere
(32 rays from the triangle's centroid) travels half the model's diagonal without hitting
it. False on an inner shell that nothing outside can see."""
normals = mesh.face_normals()
centroids = mesh.pos[mesh.faces].mean(axis=1)
index = np.arange(len(mesh.faces))
with Scene(mesh.pos, mesh.faces, threads=os.cpu_count() or 4) as scene:
fraction = np.maximum(*(ambient_occlusion(scene, centroids, side * normals, side * normals, index,
np.zeros_like(index), supersample=1, max_distance=0.5 * scene.diag)
for side in (1.0, -1.0)))
return fraction >= 0.02