Files
solvecraft/assets/render/starter/tools/materials.py
T
selune 9e6e0b8df1 Render workspace: complete experimental ribbon, resources and effects integration (#5493) (#5494)
* wip(render): reconcile completed material and image pipeline source with 0.20 (#5493)

Source-only integration reference for staged review, not a merge-ready aggregate.
Donor: 0xlillith/solidcraft@aea20b0115.
Upstream: 858e5995d6.

Retain original CPU/material contracts and explicit GPU donor provenance in
docs/render-materials-lab.md (#2067, #2191, #2780, #3306). Preserve released
format migrations and current navigation/startup/shell behavior. New saved
records use pending descriptors. The physical-card descriptor follows the
maintainer's material-engineering-cards naming request, with a legacy-lab
format-29 regression. Replace raw new panel hosts with shared containers.

Repository checks, complete workspace/all-target Clippy, and the independent
physical/resource authoring checks pass for this source. The fresh full local
CI invocation has reached test compilation and is not yet complete. Do not
transfer donor native output or prior test totals to this integration.

Generated review screenshots, video, package ZIP and machine logs remain at
the immutable personal evidence pin. Uncommitted geometric displacement is
backed up separately, not silently included. No kernel/vendor change, direct
main write, release or independent contributor-branch rewrite.

Co-authored-by: Christian Svensson <blue@cmd.nu>

* test(cli): pin the Compact camera-sample admission diagnostic (#5493)

The bounded request still refuses 1024x1024x64 samples before host initialization and writes no output. Update only the stale expected diagnostic to the existing Compact budget wording; production limits and code are unchanged. Exact-tree CLI regression suite: 14 passed, 0 failed. The full repository gate has been invoked separately and is not yet complete.

* Normalize UI container violation paths across platforms

Report repository-relative paths with forward slashes so diagnostics and the existing nested-package regression assertions agree on Windows and Unix. Keep the container detection rules unchanged.

Validation: official ui-containers gate and all seven focused xtask tests passed; rustfmt and diff checks passed. Only xtask/dependencies were compiled; no application build or heavy tests.
(cherry picked from commit 44b58d8e10)

* fix(render): preserve camera contracts and correct acceptance regressions (#5493)

Preserve Design view.apply semantics with a separate strict Render camera command. Retain existing translations when English-only aliases collide; add material-axis entries and camera-specific pitch labels. Compare complete validated engineering cards and retain job handles before cancellation.

Focused and native acceptance is pinned to tree f8a423b21f. Full CI has been invoked separately and is not claimed green. Landing follows the render integration policy in #5493.

* fix(render): resolve showcase licence and upstream resource hygiene (#5493)

Record SychPL's explicit MIT OR Apache-2.0 grant, retaining source/model bytes and the Ubuntu lettering notice. Remove 80 absent attribution rows, personal fork records, dead delivery attributes and three fork/resource-only workflows. Existing xtask assets now performs bounded offline hash checks and rejects orphan render attribution; no parallel draft CI or package installation. Original authoring reproduction remains explicit. Scoped xtask tests, assets, Clippy, formatting and prepared SELENE validation pass; no application or renderer-wide result is inferred.

* fix(render): align saved bounds and progressive sample resolution (#5493)

Address review r4364211669/r4364211677/r4364211681/r4364211691/r4364211695. Persisted light/frame domains respect the existing 1e9 document bound; related LED/rectangle/direction cases use the same bound. Workstation primary resolve shares the 4096 admission cap and film counters use checked conversions. Exact candidate: 485 CPU render tests, 21 render document tests and strict scoped Clippy pass, including positive boundary round trips and 2048/4096 end-to-end checkpoints. No released format/migration edits, relaxed test limits or full-stack acceptance claim.

* wip(render): isolate optional resources and harden sample handling (#5493)

Review work in progress: opt-in encoded render library with reproducible runtime catalog exports; native-only robust dependency through the workspace; checked bloom slices; per-sample point-light diagnostics; independent area-light spatial/film keys. Resource-module binaries in both configurations, generation, Cargo production feature graphs and attribution checks pass. Full engine/render regression and combined acceptance are pending: do not merge this donor as a constituent or treat scoped resource proof as runtime acceptance.

* fix(gpu): preserve donor allocations through the scope-safety repair (#5722)

Port the native-validated #5723 scope-cleanup correction into the experimental donor, retaining its adaptive, volume, aging, photographic and bloom allocations. Same reverse-drain helper and regression cases; the donor combination still needs its own compile/test gate and is not covered retroactively by render-base results.

* claim: #1369

* Reject unknown pending concepts after format folding (#1369)

* test(render): close bounded sampling and bloom review regressions (#5493)

Validate the optional-resource/lighting/dependency donor at its exact tree. Required Vulkan GPU 127/0/3; CPU renderer 487/0; engine Render 72/0; document render 21/0; lean library 7/0; strict scoped Clippy, assets/layers/lists/containers and lean-engine WASM pass. Preserve original failed controller/typing attempts. This is the draft donor/disposition source, not a replacement for constituent render PRs or final main acceptance.

* wip(render): preserve stable aging target validation candidate

Exact staged source from prior review work on donor 3b892eab5e. This is a recovery/review checkpoint, NOT accepted delivery. An actual command-path regression currently fails because the selected executor refuses ordered surface aging. No assertion, capability or admission limit is weakened. Native/UI/full combination acceptance remains pending. Saved/runtime split adds one pending descriptor and retains original file scope; no main/render branch is changed.

* test(render): preserve numeric setup intent at the admitted coordinate bound

Follow up jyviko review on #5494 without weakening the corrected saved-coordinate contract. The positive clicked fixture uses 1e9 instead of inadmissible 2e9; explicit out-of-range cases still refuse, and the full saved setup round-trips through JSON and the native container. Exact-source Render workspace 95/0 and strict all-target UI Clippy pass. This does not incorporate the separately failing stable-aging candidate or claim full current-render acceptance.

* test(render): use the real GPU executor for saved aging

Move successful native cold-reopen Check/Start coverage from the CPU-backed protocol fixture into the production GPU executor tests. Verify capture-index changes preserve the stable target and that enabling aging changes both PNG and raw EXR output. Retain explicit, allocation-free refusal coverage for executors without aging support. Production capabilities and admission checks are unchanged.

* fix(render): persist stable identities for targeted aging (#5493)

Separate saved RenderBodyTarget identities from captured mesh ordinals. Reject legacy ambiguous targets, resolve Check/Start after visibility changes and preserve native reopening, copies and undo. Unsupported executors still refuse; real AMD Vulkan regression completes saved aging after reordered capture.

Source tree 6b84c8ad passes document 23, engine Render 75, GPU effects 5 and Render UI 95 with no failures; scoped strict Clippy, WASM and repository gates pass. Fresh CLI/native reopen and clean/aged GPU exports pass with identical physical totals and inspected actual images. Whole current-render integration is separate.

* fix(render): reconcile combined CPU/GPU contracts on current upstream

Unify captured resources/status and CPU-vs-GPU request schemas after the real two-parent integration; remove obsolete duplicate Vulkan selector and retain combined platform-filter regression. Deduplicate only identical or superseded test definitions, retaining the stronger card metadata and reserved-executor assertions. No assertions, admission limits or feature refusals are weakened.

All targets of render dependencies, GPU, engine, UI, CLI and native app pass cargo check with raytrace-cpu and bundled-render-library at tree c70d00d1. Native executable tests and full current-source acceptance remain pending. This is a private integration rehearsal for subsequent constituent PRs, not an aggregate official render/main merge.

* test(render): retain one complete host lifetime regression set

Deduplicate byte-identical DropProbe/task-lifetime tests and retain the adapted environment/texture ownership regressions with every assertion and complete memory estimate. Keep the registered-geometry admission regression once; only struct field order differed. Replace a one-element diagnostic loop with its equivalent block, preserving all wrap/width assertions.

Test-only integration repair. Formatting and diff checks pass; refreshed executable validation is required. No production behavior, limits, assertions or ignores changed.

* fix(doc): recognize the proven format-29 card descriptor alias

Normalize only the historical format-29 physical-material-cards spelling, and only when material-engineering-cards is registered. Admission and migration de-duplication use the same identity. Unknown concepts, wrong versions and incapable readers still refuse before mutation. Six controlled-history Rust regressions and actual CLI migration/no-clobber acceptance pass. No version, released migration or CI changes. Refs #5793, #1369, #5493.

* test(render): preserve grounding refusal before explicit instance moves

Update the two Render binding fixtures for upstream first-occurrence grounding. Verify grounded movement refuses without changing document/history, then explicitly unground through the public command before the intended move. Retain every target, resource, copy and persistence assertion. Refs #5493.

---------

Co-authored-by: Christian Svensson <blue@cmd.nu>
Co-authored-by: Victor Santos Oliveira <56417388+Vittuu@users.noreply.github.com>
Co-authored-by: cfaulkingham <colin.faulkingham@gmail.com>
2026-10-24 14:21:59 +00:00

457 lines
23 KiB
Python

#!/usr/bin/env python3
"""Author original periodic PBR data fields; no application runtime dependency.
The recipe JSON is the editable authority. PNGs are required authoring exports,
not inferred material measurements. All signal construction is original maths.
"""
from __future__ import annotations
import argparse
import hashlib
import io
import json
import math
from pathlib import Path
import re
import sys
import numpy as np
from PIL import Image, ImageDraw
VERSION = "1.0.0"
PACK = Path(__file__).resolve().parents[1]
ROOT = PACK.parents[2]
RECIPES = PACK / "materials/source/recipes.json"
EXPORTS = PACK / "materials/exports"
MANIFEST = PACK / "materials/manifest.json"
KINDS = {"brushed", "cross_brushed", "grain", "galvanized", "milled",
"turned", "powder", "hammered", "polymer", "rubber", "grip",
"wood", "plywood", "fibre", "weave", "weave_twill", "pcb", "fdm", "fdm_top",
"foil", "regolith", "cork"}
ROLES = {"base_color": ("RGB", 8, "srgb"),
"normal_open_gl": ("RGB", 8, "linear_data"),
"roughness": ("L", 8, "linear_data"),
"metallic": ("L", 8, "linear_data"),
"height": ("I;16", 16, "linear_data")}
ITEM_KEYS = {"id", "name", "catalog_ids", "domains", "pattern", "seed", "tile_mm",
"base_linear_low", "base_linear_high", "roughness", "roughness_variation",
"metallic", "height_span_mm", "cycles_u", "cycles_v", "limitations"}
NORMAL_CONVENTION = "OpenGL +Y, +U right, +V image-up; renormalize after sampling"
AO = {"constant": 1.0, "reason": "No invented cavity occlusion or baked lighting."}
def digest(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"duplicate JSON key: {key}")
result[key] = value
return result
def load_json(path: Path):
if path.stat().st_size > 4_000_000:
raise ValueError("JSON source exceeds 4 MB authoring limit")
return json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=unique_object,
parse_constant=lambda x: (_ for _ in ()).throw(ValueError(f"nonfinite {x}")))
def number(value, lower, upper, label):
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise ValueError(f"{label}: expected finite number")
if not lower <= value <= upper or not math.isfinite(value):
raise ValueError(f"{label}: outside [{lower}, {upper}]")
def validate_source(data):
if not isinstance(data, dict):
raise ValueError("source must be a JSON object")
if data.get("schema") != "solidcraft-authored-surfaces/1":
raise ValueError("unknown authoring schema")
if data.get("generator_version") != VERSION:
raise ValueError("generator version mismatch")
size = data.get("resolution")
if type(size) is not int or size not in (256, 512, 1024, 2048):
raise ValueError("resolution must be an admitted power of two (256..2048)")
items = data.get("items")
if not isinstance(items, list) or not 1 <= len(items) <= 64:
raise ValueError("expected 1..64 surface recipes")
ids = set()
catalog = {x["id"] for x in load_json(ROOT / "docs/render-materials/materials.json")["items"]}
for item in items:
if not isinstance(item, dict) or set(item) != ITEM_KEYS:
raise ValueError("surface recipe must have exactly the documented fields")
rid = item.get("id", "")
if not isinstance(rid, str) or not re.fullmatch(r"SURF-[0-9]{3}", rid) or rid in ids:
raise ValueError("unsafe or duplicate resource id")
ids.add(rid)
if not isinstance(item.get("name"), str) or not item["name"].strip():
raise ValueError(f"{rid}: missing name")
if not isinstance(item.get("pattern"), str) or item["pattern"] not in KINDS:
raise ValueError(f"{rid}: unknown generator pattern")
if type(item.get("seed")) is not int or not 0 <= item["seed"] < 2**32:
raise ValueError(f"{rid}: invalid seed")
refs = item.get("catalog_ids")
if not isinstance(refs, list) or not refs or any(not isinstance(x, str) or x not in catalog for x in refs):
raise ValueError(f"{rid}: unknown material concept")
for key in ("tile_mm", "base_linear_low", "base_linear_high"):
values = item.get(key)
count = 2 if key == "tile_mm" else 3
if not isinstance(values, list) or len(values) != count:
raise ValueError(f"{rid}: invalid {key}")
for value in values:
number(value, 0.001 if key == "tile_mm" else 0,
10000 if key == "tile_mm" else 1, key)
number(item.get("height_span_mm"), 0, min(item["tile_mm"]) / 8, "height span")
for key in ("roughness", "roughness_variation", "metallic"):
number(item.get(key), 0, 1, key)
for key in ("cycles_u", "cycles_v"):
value = item.get(key)
if type(value) is not int or not 1 <= value <= size // 8:
raise ValueError(f"{rid}: {key} must be integer 1..resolution/8")
if item["pattern"] == "weave" and value % 2:
raise ValueError(f"{rid}: weave cycles must be even for a periodic crossing")
if item["pattern"] == "weave_twill" and value % 4:
raise ValueError(f"{rid}: 2/2 twill cycles must be multiples of four")
for key in ("domains", "limitations"):
values = item.get(key)
if not isinstance(values, list) or not values or any(not isinstance(v, str) or not v.strip() for v in values):
raise ValueError(f"{rid}: nonempty string {key} required")
return data
def noise(u, v, seed, frequency=1):
"""Bounded periodic Fourier field; integer frequencies guarantee repeat."""
rng = np.random.Generator(np.random.PCG64(seed))
result = np.zeros(np.broadcast_shapes(np.shape(u), np.shape(v)), dtype=np.float64)
total = 0.0
for octave in range(3):
for _ in range(4):
a, b = rng.integers(-3, 4, size=2)
if a == 0 and b == 0:
a = 1
phase = float(rng.integers(0, 2**32)) / 2**32
weight = 0.5**octave
result += weight * np.sin(2 * np.pi * (
(a * u + b * v) * frequency * 2**octave + phase))
total += weight
return result / total
def fields(item, u, v):
"""Continuous periodic height/tone/roughness fields in [0,1]."""
seed = item["seed"]
coarse = noise(u, v, seed, 1)
fine = noise(u, v, seed + 1, 8)
cu, cv = item["cycles_u"], item["cycles_v"]
kind = item["pattern"]
tone = 0.5 + 0.35 * coarse + 0.15 * fine
h = 0.5 + 0.25 * coarse + 0.25 * fine
if kind in ("brushed", "cross_brushed"):
a = np.sin(2 * np.pi * (cv * v + 0.08 * coarse))
b = np.sin(2 * np.pi * (cu * u + 0.05 * coarse))
h = 0.5 + 0.32 * a + 0.08 * fine
if kind == "cross_brushed":
h = 0.5 + 0.20 * a + 0.20 * b + 0.06 * fine
tone = 0.5 + 0.06 * coarse + 0.05 * fine
elif kind == "milled":
phase = cu * u + 0.5 * np.cos(2 * np.pi * cv * v)
h = 0.5 + 0.32 * np.sin(2 * np.pi * phase) + 0.08 * fine
tone = 0.5 + 0.08 * coarse
elif kind == "turned":
h = 0.5 + 0.38 * np.cos(2 * np.pi * cv * v) + 0.08 * fine
tone = 0.5 + 0.05 * coarse
elif kind in ("grain", "powder", "polymer", "rubber"):
h = 0.5 + 0.13 * coarse + 0.34 * fine
tone = 0.5 + 0.08 * coarse + 0.08 * fine
elif kind == "hammered":
a = np.sin(2 * np.pi * cu * u + 0.5 * coarse)
b = np.cos(2 * np.pi * cv * v + 0.5 * coarse)
h = 0.5 + 0.35 * a * b + 0.06 * fine
tone = 0.5 + 0.20 * coarse + 0.15 * a * b
elif kind in ("galvanized", "cork"):
# Original periodic Voronoi cells; no photographed spangle source.
rng = np.random.Generator(np.random.PCG64(seed + 3))
first = np.full(np.broadcast_shapes(np.shape(u), np.shape(v)), np.inf)
second = first.copy()
cell = np.zeros_like(first)
for _ in range(36):
x, y, color = rng.random(3)
dx = (u - x + 0.5) % 1 - 0.5
dy = (v - y + 0.5) % 1 - 0.5
distance = dx * dx + dy * dy
closer = distance < first
second = np.where(closer, first, np.minimum(second, distance))
cell = np.where(closer, color, cell)
first = np.minimum(first, distance)
edge = np.clip((second - first) * 400, 0, 1)
h = 0.3 + 0.35 * edge + 0.12 * fine
tone = 0.15 + 0.65 * cell + 0.08 * fine
elif kind == "grip":
a = np.cos(2 * np.pi * (cu * u + cv * v))
b = np.cos(2 * np.pi * (cu * u - cv * v))
h = 0.5 + 0.40 * a * b + 0.03 * fine
tone = 0.5 + 0.08 * coarse
elif kind == "wood":
# Continuous sinusoidal latewood; period and warp share the same torus.
warp = 0.22 * np.sin(2 * np.pi * v) + 0.12 * coarse
grain = 0.5 + 0.5 * np.cos(2 * np.pi * (cu * u + warp))
pores = np.power(grain, 8)
tone = 0.12 + 0.74 * grain + 0.10 * fine
h = 0.65 - 0.38 * pores + 0.05 * fine
elif kind == "plywood":
layers = 0.5 + 0.5 * np.cos(2 * np.pi * cv * v)
grain = np.sin(2 * np.pi * (cu * u + 0.12 * coarse))
tone = 0.15 + 0.65 * layers + 0.08 * grain
h = 0.5 + 0.20 * layers + 0.08 * fine
elif kind == "fibre":
fibres = np.sin(2 * np.pi * (cu * u + cv * v))
h = 0.5 + 0.20 * fine + 0.14 * fibres
tone = 0.5 + 0.23 * coarse + 0.18 * fine
elif kind in ("weave", "weave_twill"):
# Alternating crossing envelope; even cycle counts make repeat coherent.
a = 0.5 + 0.5 * np.cos(2 * np.pi * cu * u)
b = 0.5 + 0.5 * np.cos(2 * np.pi * cv * v)
crossing = (np.cos(np.pi / 2 * (cu * u - cv * v)) if kind == "weave_twill"
else np.sin(np.pi * cu * u) * np.sin(np.pi * cv * v))
weight = 0.5 + 0.5 * np.tanh(8 * crossing)
h = 0.20 + 0.58 * (weight * a + (1 - weight) * b) + 0.04 * fine
tone = 0.3 + 0.4 * (weight * a + (1 - weight) * b) + 0.05 * fine
elif kind == "pcb":
# Continuous solder-mask finish only: no fabricated circuit/marking.
h = 0.5 + 0.12 * coarse + 0.18 * fine
tone = 0.5 + 0.12 * coarse + 0.10 * fine
elif kind in ("fdm", "fdm_top"):
phase = cv * v if kind == "fdm" else cu * u + cv * v
bead = 0.5 + 0.5 * np.cos(2 * np.pi * phase + 0.03 * coarse)
h = 0.15 + 0.75 * np.sqrt(np.maximum(0.0, bead)) + 0.015 * fine
tone = 0.5 + 0.04 * fine
elif kind == "foil":
folds = (np.sin(2 * np.pi * (cu * u + v)) +
0.5 * np.sin(2 * np.pi * (u - cv * v))) / 1.5
h = 0.5 + 0.34 * np.tanh(2 * folds) + 0.05 * fine
tone = 0.5 + 0.03 * coarse
elif kind == "regolith":
h = 0.5 + 0.23 * coarse + 0.23 * fine
tone = 0.5 + 0.28 * coarse + 0.18 * fine
rough = np.clip(item["roughness"] + item["roughness_variation"] * (2 * h - 1),
0.02, 1.0)
return np.clip(h, 0, 1), np.clip(tone, 0, 1), rough
def srgb_encode(linear):
linear = np.asarray(linear, dtype=np.float64)
return np.where(linear <= 0.0031308, 12.92 * linear,
1.055 * np.power(linear, 1 / 2.4) - 0.055)
def normal_from_height(height_mm, tile_mm):
rows, cols = height_mm.shape
du = (np.roll(height_mm, -1, axis=1) - np.roll(height_mm, 1, axis=1)) / (2 * tile_mm[0] / cols)
# Image rows descend; +V is towards the top of the image.
dv = (np.roll(height_mm, 1, axis=0) - np.roll(height_mm, -1, axis=0)) / (2 * tile_mm[1] / rows)
normal = np.stack((-du, -dv, np.ones_like(height_mm)), axis=-1)
return normal / np.linalg.norm(normal, axis=-1, keepdims=True)
def make_maps(item, size):
u = (np.arange(size, dtype=np.float64)[None, :] + 0.5) / size
v = 1 - (np.arange(size, dtype=np.float64)[:, None] + 0.5) / size
height, tone, rough = fields(item, u, v)
height16 = np.rint(height * 65535).astype(np.uint16)
displacement = (height16.astype(np.float64) / 65535 - 0.5) * item["height_span_mm"]
normal = normal_from_height(displacement, item["tile_mm"])
low, high = np.asarray(item["base_linear_low"]), np.asarray(item["base_linear_high"])
color = low + (high - low) * tone[..., None]
return {"base_color": np.rint(np.clip(srgb_encode(color), 0, 1) * 255).astype(np.uint8),
"normal_open_gl": np.rint((normal * 0.5 + 0.5) * 255).astype(np.uint8),
"roughness": np.rint(rough * 255).astype(np.uint8),
"metallic": np.full((size, size), round(item["metallic"] * 255), dtype=np.uint8),
"height": height16}
def png_bytes(array):
out = io.BytesIO()
Image.fromarray(array).save(out, format="PNG", compress_level=9)
return out.getvalue()
def seams(item):
t = (np.arange(257, dtype=np.float64) + 0.5) / 257
residual = 0.0
for a, b in ((fields(item, 0.0, t), fields(item, 1.0, t)),
(fields(item, t, 0.0), fields(item, t, 1.0))):
for x, y in zip(a, b):
residual = max(residual, float(np.max(np.abs(x - y))))
return residual
def build():
data = validate_source(load_json(RECIPES))
records = []
for item in data["items"]:
maps = make_maps(item, data["resolution"])
record = {k: item[k] for k in ("id", "name", "catalog_ids", "domains", "tile_mm",
"height_span_mm", "limitations")}
record.update({"resource_version": VERSION, "authoring_status": "authored",
"runtime_status": "unbound", "appearance_fidelity": "illustrative_approximation",
"seed": item["seed"], "pattern": item["pattern"],
"ambient_occlusion": AO,
"height_zero": 0.5, "normal_convention": NORMAL_CONVENTION,
"repeat": ["repeat", "repeat"], "sample_locations": "pixel_centers",
"source_boundary_max_error": seams(item),
"maps": {}})
for role, array in maps.items():
target = EXPORTS / item["id"] / f"{role}.png"
target.parent.mkdir(parents=True, exist_ok=True)
raw = png_bytes(array)
target.write_bytes(raw)
record["maps"][role] = {"path": target.relative_to(ROOT).as_posix(),
"sha256": digest(raw), "decoded_sha256": digest(array.astype(array.dtype.newbyteorder("<")).tobytes()),
"size_bytes": len(raw), "width": data["resolution"], "height": data["resolution"],
"channels": 3 if array.ndim == 3 else 1, "bit_depth": ROLES[role][1],
"encoding": ROLES[role][2], "minimum": int(array.min()), "maximum": int(array.max()),
"consumer": "authoring_only_requires_explicit_height_adapter" if role == "height" else "bounded_texture_input_compatible_not_runtime_bound"}
records.append(record)
manifest = {"schema": "solidcraft-authored-surface-manifest/1", "generator_version": VERSION,
"source_path": RECIPES.relative_to(ROOT).as_posix(),
"source_sha256": digest(RECIPES.read_bytes()),
"generator_sha256": digest(Path(__file__).read_bytes()),
"license": "MIT OR Apache-2.0", "author": "Original SolidCraft contributor work",
"source_inputs": "Original numerical fields; no external imagery, material library, or commercial-product output.",
"items": records}
MANIFEST.write_bytes((json.dumps(manifest, indent=2, allow_nan=False) + "\n").encode("utf-8"))
return {"surfaces": len(records), "maps": len(records) * len(ROLES),
"bytes": sum(m["size_bytes"] for r in records for m in r["maps"].values())}
def validate(reproduce=False):
data = validate_source(load_json(RECIPES))
manifest = load_json(MANIFEST)
if not isinstance(manifest, dict) or manifest.get("schema") != "solidcraft-authored-surface-manifest/1":
raise ValueError("unknown manifest schema")
if manifest.get("source_sha256") != digest(RECIPES.read_bytes()):
raise ValueError("source receipt mismatch")
if manifest.get("generator_sha256") != digest(Path(__file__).read_bytes()):
raise ValueError("generator receipt mismatch: rebuild exports")
if manifest.get("generator_version") != VERSION or manifest.get("license") != "MIT OR Apache-2.0":
raise ValueError("manifest version or license mismatch")
if manifest.get("source_path") != RECIPES.relative_to(ROOT).as_posix():
raise ValueError("manifest source path mismatch")
records = manifest.get("items")
if not isinstance(records, list) or len(records) != len(data["items"]):
raise ValueError("manifest resource count mismatch")
checked = 0
for item, record in zip(data["items"], records):
if not isinstance(record, dict):
raise ValueError("manifest resource must be an object")
rid = item["id"]
for key in ("id", "name", "catalog_ids", "domains", "tile_mm", "height_span_mm", "limitations", "seed", "pattern"):
if record.get(key) != item[key]:
raise ValueError(f"{rid}: manifest/source mismatch {key}")
if record.get("runtime_status") != "unbound" or record.get("authoring_status") != "authored":
raise ValueError("unsupported runtime availability claim")
semantics = {"resource_version": VERSION, "appearance_fidelity": "illustrative_approximation",
"height_zero": 0.5, "normal_convention": NORMAL_CONVENTION,
"repeat": ["repeat", "repeat"], "sample_locations": "pixel_centers",
"ambient_occlusion": AO}
for key, value in semantics.items():
if record.get(key) != value:
raise ValueError(f"{rid}: changed surface semantics {key}")
if not isinstance(record.get("maps"), dict) or set(record["maps"]) != set(ROLES):
raise ValueError(f"{rid}: missing or extra map")
seam = seams(item)
claimed_seam = record.get("source_boundary_max_error")
number(claimed_seam, 0, 1e-10, "source boundary error")
if abs(claimed_seam - seam) > 1e-12:
raise ValueError(f"{rid}: boundary evidence mismatch")
if seam > 1e-10:
raise ValueError(f"{rid}: discontinuous source boundary")
expected = make_maps(item, data["resolution"]) if reproduce else None
decoded = {}
for role, spec in record["maps"].items():
if not isinstance(spec, dict):
raise ValueError("map metadata must be an object")
path = EXPORTS / rid / f"{role}.png"
if spec.get("path") != path.relative_to(ROOT).as_posix():
raise ValueError(f"{rid}: unexpected map path")
raw = path.read_bytes()
if len(raw) > 16 * 1024 * 1024 or digest(raw) != spec["sha256"] or len(raw) != spec["size_bytes"]:
raise ValueError(f"{rid}: corrupt {role}")
with Image.open(io.BytesIO(raw)) as image:
if image.format != "PNG" or image.size != (data["resolution"],) * 2:
raise ValueError("invalid PNG shape")
if any(x in image.info for x in ("icc_profile", "exif", "gamma")):
raise ValueError("unexpected input color/orientation metadata")
array = np.array(image)
wanted_shape = (data["resolution"],) * 2 + ((3,) if role in ("base_color", "normal_open_gl") else ())
if array.shape != wanted_shape or array.dtype != (np.uint16 if role == "height" else np.uint8):
raise ValueError(f"{rid}: wrong decoded type or channels")
if spec["bit_depth"] != ROLES[role][1] or spec["encoding"] != ROLES[role][2]:
raise ValueError("map semantics mismatch")
actual = {"width": data["resolution"], "height": data["resolution"],
"channels": 3 if array.ndim == 3 else 1,
"minimum": int(array.min()), "maximum": int(array.max()),
"consumer": "authoring_only_requires_explicit_height_adapter" if role == "height" else "bounded_texture_input_compatible_not_runtime_bound"}
if any(spec.get(key) != value for key, value in actual.items()):
raise ValueError(f"{rid}: map dimensions, range or consumer metadata mismatch")
if digest(array.astype(array.dtype.newbyteorder("<")).tobytes()) != spec["decoded_sha256"]:
raise ValueError("decoded pixel receipt mismatch")
if expected is not None and not np.array_equal(array, expected[role]):
raise ValueError(f"{rid}: regeneration changed pixels in {role}")
decoded[role] = array
checked += 1
normal = decoded["normal_open_gl"].astype(np.float64) / 255 * 2 - 1
lengths = np.linalg.norm(normal, axis=-1)
if float(np.max(np.abs(lengths - 1))) > 0.007 or float(normal[..., 2].min()) <= 0:
raise ValueError(f"{rid}: invalid normal vectors")
h = (decoded["height"].astype(np.float64) / 65535 - 0.5) * item["height_span_mm"]
computed = np.rint((normal_from_height(h, item["tile_mm"]) * 0.5 + 0.5) * 255).astype(np.uint8)
if not np.array_equal(computed, decoded["normal_open_gl"]):
raise ValueError(f"{rid}: normal/height physical-scale disagreement")
return {"status": "pass", "surfaces": len(records), "maps_checked": checked,
"pixel_reproduction": reproduce, "runtime_ready": False}
def preview():
data = validate_source(load_json(RECIPES))
tile, gap, label, columns = 160, 12, 44, 6
rows = math.ceil(len(data["items"]) / columns)
out = Image.new("RGB", (columns * (tile + gap) + gap, rows * (tile + label + gap) + 72), (19, 24, 32))
draw = ImageDraw.Draw(out)
draw.text((12, 12), "SolidCraft | Authored surface data, v1", fill=(237, 240, 245))
draw.text((12, 32), "Unlit base-color maps. No scene render or measured material claim.", fill=(171, 184, 201))
for index, item in enumerate(data["items"]):
x = gap + (index % columns) * (tile + gap)
y = 60 + (index // columns) * (tile + label + gap)
with Image.open(EXPORTS / item["id"] / "base_color.png") as image:
out.paste(image.resize((tile, tile)), (x, y))
draw.text((x, y + tile + 5), item["id"] + " " + item["name"][:21], fill=(228, 234, 240))
draw.text((x, y + tile + 21), str(item["tile_mm"]) + " mm", fill=(153, 168, 188))
target = ROOT / "target/render-starter-review/materials/base-color-contact-sheet.png"
target.parent.mkdir(parents=True, exist_ok=True)
out.save(target)
return {"preview": str(target)}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("command", choices=("build", "validate", "preview"))
parser.add_argument("--reproduce", action="store_true")
args = parser.parse_args()
try:
result = build() if args.command == "build" else validate(args.reproduce) if args.command == "validate" else preview()
except (OSError, ValueError, KeyError, TypeError) as error:
print(f"material authoring failed: {error}", file=sys.stderr)
return 1
print(json.dumps(result, indent=2, allow_nan=False))
return 0
if __name__ == "__main__":
sys.exit(main())