"""Synthetic PNGs survive part JSON and the actual cull/assemble CLI pipeline.""" import base64 import copy import json from pathlib import Path import struct import subprocess import sys import tempfile import unittest import zlib ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tools")) import ped_assemble as pa SLOTS = ("base_color_png", "normal_png", "metallic_roughness_png") def png(r, g, b): def chunk(kind, payload): return struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", zlib.crc32(kind + payload)) return b"".join(( b"\x89PNG\r\n\x1a\n", chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 6, 0, 0, 0)), chunk(b"IDAT", zlib.compress(bytes([0, r, g, b, 255]))), chunk(b"IEND", b""), )) IMAGES = (png(30, 80, 150), png(128, 128, 255), png(0, 220, 10)) def part(material=None): return pa.MeshPart( "surface", [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)], [(0, 1, 2)], coord_space="y-up", material=material, ) def split_glb(data): length = struct.unpack_from(" 1 tris", result.stdout) self.cli("assemble", serialized, "-o", rebuilt, "--no-cull", "--no-standing-offset", "--source-space", "y-up") self.assertEqual(rebuilt.read_bytes(), direct.read_bytes()) doc, blob = split_glb(rebuilt.read_bytes()) self.assertEqual(len(doc["images"]), 1 if shared else 3) embedded = [] for image in doc["images"]: view = doc["bufferViews"][image["bufferView"]] start = view.get("byteOffset", 0) embedded.append(blob[start:start + view["byteLength"]]) self.assertEqual(set(embedded), set(payloads)) primitive = doc["meshes"][0]["primitives"][0] self.assertEqual(doc["accessors"][primitive["indices"]]["count"], 3) self.assertEqual(doc["accessors"][primitive["attributes"]["POSITION"]]["count"], 3) restored, _ = pa.load_parts_from_path(str(serialized)) self.assertEqual(restored[0].positions, [(2.0, 0.0, 0.0), (3.0, 0.0, 0.0), (2.0, 1.0, 0.0)]) self.assertEqual(restored[0].uvs, [(0.25, 0.25), (0.5, 0.25), (0.25, 0.5)]) self.assertEqual({path: path.read_bytes() for path in originals}, originals) def test_no_texture_serialization_is_unchanged(self): source = part() expected = {"name": "surface", "role": "other", "coord_space": "y-up", "translation": [0.0, 0.0, 0.0], "positions": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], "triangles": [[0, 1, 2]]} self.assertEqual(pa.part_to_dict(source), expected) source.material = pa.MaterialSpec() expected["material"] = {"name": "default", "base_color_factor": [0.8, 0.8, 0.8, 1.0], "metallic_factor": 0.0, "roughness_factor": 0.85, "alpha_mode": "OPAQUE", "double_sided": False} self.assertEqual(pa.part_to_dict(source), expected) if __name__ == "__main__": unittest.main()