Skip to content

feat(export): Differentiable GGX dielectric BRDF + UV-space optimizer #23

Open

Description

@hummat

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.exr

Tasks

  • 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.gradcheck on basecolor and roughness
  • 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
  • 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


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

Activity

  1. added
    enhancement
    export
    pbr
    brdf
    P2: later
    on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2: laterbrdfenhancementexportpbr

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions