Repository navigation
Description
Summary
Add a differentiable PyTorch module implementing the GGX (Trowbridge-Reitz) microfacet BRDF for dielectrics, with a UV-space optimizer that fits per-texel (basecolor, roughness) to the multi-view observation tensor produced by #20.
Dielectric only, fixed F₀≈0.04, metallic=0 — covers the vast majority of real-world objects and avoids the mode-collapse pathologies of jointly fitting metallic.
Split from #11 Stage 2.5b — the BRDF kernel and its UV-space training loop.
Motivation
Once #20 produces T → [(view_dir, radiance, visibility)] per texel, the missing piece is the analysis-by-synthesis kernel: a differentiable GGX forward model, a loss against observations, and TV/smoothness regularization in UV space. That's the minimum viable inverse-rendering pipeline.
Standalone module + standalone optimizer means it can be unit-tested against synthetic GGX renders before any lighting estimation lands.
Module surface
# mini_mesh/pbr/ggx.py
def ggx_dielectric(
basecolor: Tensor, # [..., 3] linear
roughness: Tensor, # [..., 1] in [0, 1], remapped α = roughness²
n: Tensor, # [..., 3] surface normal
l: Tensor, # [..., 3] light direction
v: Tensor, # [..., 3] view direction
f0: float = 0.04,
) -> Tensor: # [..., 3] outgoing radiance scale (no L_in multiplier)UV-space optimizer:
# scripts/pbr_uv_fit.py
# inputs: observations.h5 (#20), env-light prior (constant white init OK for v0)
# outputs: basecolor_linear.exr, roughness.exr, residual.exrTasks
- Implement
ggx_dielectric()with Schlick Fresnel, Smith G2 height-correlated masking-shadowing, GGX D - Unit tests:
- Energy: Monte Carlo integral of
f_r · (n·l)over hemisphere ≤ 1 across roughness range - Reciprocity:
f_r(l, v) == f_r(v, l) - Limit checks: roughness=1 ≈ Lambertian + small specular floor; roughness→0 → narrow specular lobe
- Gradient check:
torch.autograd.gradcheckon basecolor and roughness
- Energy: Monte Carlo integral of
- Implement UV-space optimizer (
scripts/pbr_uv_fit.py):- Per-texel
(basecolor, roughness)parameters - L1 photometric loss against observations (linear radiance)
- TV regularization on basecolor + roughness in UV space
- Smoothness regularization on roughness only (basecolor stays sharp)
- Adam optimizer, configurable iterations + learning rates
- Optional: load Stage 1 basecolor/roughness from feat(export): glTF/GLB exporter with PBR channels (basecolor / ORM / normal) #19 export as initialization
- Per-texel
- CLI:
python scripts/pbr_uv_fit.py --observations observations.h5 --uv-resolution 2048 --output <dir> - End-to-end test: synthetic Blender scene with known GGX materials → fit → measure roughness MAE, basecolor MAE
Validation criteria
- Recovers GT roughness within MAE 0.05 on synthetic dielectric scene under known white lighting
- Recovers GT basecolor within ΔE76 < 5 in linear space
- Refuses to fit metallic regions cleanly (expected — flag for future #N metallic extension)
Non-goals
- Lighting estimation (separate, see follow-up issue) — v0 assumes constant white environment or pre-fit SH
- Metallic / multi-lobe BRDFs (single dielectric lobe only)
- Volumetric / training-time integration (operates entirely on the UV product of feat(export): UV-space multi-view texel observation builder #20)
References
- Walter, Marschner, Li, Torrance — Microfacet Models for Refraction through Rough Surfaces, EGSR 2007 — GGX D term
- Heitz — Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, JCGT 2014 — height-correlated G2
- Karis — Real Shading in Unreal Engine 4, SIGGRAPH 2013 — practical formulation, α = roughness²
- Schlick — An Inexpensive BRDF Model for Physically-based Rendering, EG 1994
Related
- feat(export): PBR-ish texture extraction and BRDF-aware training #11 — parent PBR tracking issue
- feat(export): UV-space multi-view texel observation builder #20 — observation builder (input)
- feat(export): glTF/GLB exporter with PBR channels (basecolor / ORM / normal) #19 — glTF exporter (consumes the fit output)
Activity