"""Signed-normalized glTF endpoints through the real ped importer and CLI. The glTF accessor-data-types contract maps -128/-127 and -32768/-32767 to -1. KHR_mesh_quantization permits these POSITION/TEXCOORD component types: https://github.com/KhronosGroup/glTF/blob/5b00b913b74c8f0260c64578b98a09ef2dde0b41/extensions/2.0/Khronos/KHR_mesh_quantization/README.md """ from __future__ import annotations import json from pathlib import Path import struct import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tools")) import glb_skinning import ped_assemble as pa def make_glb(component_type, values, one, *, normalized, interleaved=False, sparse=None): """Repack a real writer triangle; every vertex row/offset is four-byte aligned.""" part = pa.MeshPart(name="quantized", coord_space="y-up", positions=[(0.0, 0.0, 0.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)], triangles=[(0, 1, 2)], uvs=[(0.0, 0.0), (0.0, 1.0), (1.0, 0.0)]) original, _ = pa.build_assembled_glb([part], apply_standing=False) doc, binary, _ = glb_skinning.parse_glb(original) data = bytearray(binary) fmt = {5120: "b", 5121: "B", 5122: "h", 5123: "H", 5126: "f"}[component_type] def view(raw, **fields): data.extend(bytes(-len(data) % 4)) index = len(doc["bufferViews"]) doc["bufferViews"].append(dict(buffer=0, byteOffset=len(data), byteLength=len(raw), **fields)) data.extend(raw) return index attrs = doc["meshes"][0]["primitives"][0]["attributes"] for name, width in (("POSITION", 3), ("TEXCOORD_0", 2)): accessor = doc["accessors"][attrs[name]] rows = [(v, one if i == 1 else 0, *([0] if width == 3 else [])) for i, v in enumerate(values)] if sparse is not None: rows = [(0, 0, *([0] if width == 3 else [])), (0, one, *([0] if width == 3 else [])), (one, 0, *([0] if width == 3 else []))] size = struct.calcsize("<" + fmt * width) stride = (size + 3) // 4 * 4 + (4 if interleaved else 0) offset = 4 if interleaved else 0 raw = bytearray(b"PAD!" if offset else b"") for row in rows: raw.extend(struct.pack("<" + fmt * width, *row)) raw.extend(bytes(stride - size)) accessor.update(bufferView=view(bytes(raw), byteStride=stride, target=34962), byteOffset=offset, componentType=component_type, normalized=normalized) if sparse is not None: if sparse == "implicit": del accessor["bufferView"] del accessor["byteOffset"] rows = [(0,) * width] * 3 replacement = (values[0],) + (0,) * (width - 1) accessor["sparse"] = dict(count=1, indices=dict(bufferView=view(b"\0"), componentType=5121), values=dict(bufferView=view(struct.pack("<" + fmt * width, *replacement)))) rows[0] = replacement if name == "POSITION": accessor["min"] = [min(row[c] for row in rows) for c in range(3)] accessor["max"] = [max(row[c] for row in rows) for c in range(3)] doc["buffers"][0]["byteLength"] = len(data) doc["extensionsUsed"] = ["KHR_mesh_quantization"] doc["extensionsRequired"] = ["KHR_mesh_quantization"] encoded = json.dumps(doc, separators=(",", ":")).encode() encoded += b" " * (-len(encoded) % 4) data.extend(bytes(-len(data) % 4)) return (struct.pack("<4sII", b"glTF", 2, 28 + len(encoded) + len(data)) + struct.pack("