Skip to content

Support equirectangular HDRI environments in the native renderer - #179

Open
wangcan26 wants to merge 4 commits into
meshy-dev:mainfrom
wangcan26:codex/native-hdri-environment
Open

wangcan26 wants to merge 4 commits into
meshy-dev:mainfrom
wangcan26:codex/native-hdri-environment

Conversation

@wangcan26

Copy link
Copy Markdown

Closes #178.

Blender-exported equirectangular OpenEXR files can now drive the native renderer's lighting, reflections, and visible sky. The renderer decodes OpenEXR and Radiance HDR into linear float texels and uses the existing environment importance sampler. The panorama lookup and the importance sampler apply the same Y-axis rotation.

HDRIEnvironment.intensity scales the visible sky and the lighting. skyLightIntensity scales lighting and reflections independently. scene.background still accepts a solid color, and scene.backgroundPanorama accepts a second HDRIEnvironment with its own intensity and rotation for camera rays.

Verification

  • Rendered a Blender-exported OpenEXR and a synthetic Radiance HDR in the native renderer on Apple M4 Pro with Metal.
  • Rendered a directional OpenEXR against a rough metal sphere and a diffuse sphere. Verified in both still and headless live renders that rotating the sky changed both surfaces and that an independent background left the original lighting in place. At zero skyLightIntensity the spheres went dark and the camera sky retained its radiance.
  • cargo build --locked --bin threert-render, cargo test --locked --lib (200 passed, 6 ignored), and cargo check --locked --lib --target wasm32-unknown-unknown from renderer/.
  • npm run typecheck and npm test (165 passed, 1 skipped because the optional physics WASM artifact was absent).

@kvnloo

kvnloo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks for this. I checked out 23ddc29 and compared it against three.js's equirect conventions. The conventions match. I found one performance problem in the live editing path.

Conventions match three.js. I checked them numerically (a 1000-sample Node script) over random directions and random rotation values.

  • hdri_uv(d, panorama_offset(θ)) lands on the same texel as three.js. three.js computes equirectUv(R_y(-θ)·d), which is atan2(z, x)/2π + 0.5, and WebGLBackground negates backgroundRotation the same way. The largest difference in u was 3e-16.
  • sample_env followed by hdri_uv returns the sampled u to within 3e-16. The importance sampler and the lookup therefore agree under rotation, and env_pdf uses the same offset.
  • Row 0 of the image maps to +Y, and the image is not mirrored when viewed from inside the sphere (u increases to the right when looking down −Z). This matches three.js.
  • Colour space: float EXR and HDR files skip the sRGB decode and 8-bit files apply it, which is how three.js treats LinearSRGBColorSpace and SRGBColorSpace equirect maps. The 65000 clamp matches three.js too, since EXRLoader/RGBELoader default to HalfFloatType. One caveat: for unclipped HDRIs whose sun is brighter than 65000, the sun is capped in both the texture and the CDF, and three.js caps it the same way.

Changing rotation or intensity in the live view decodes the whole panorama again. Sky::Hdri { rotation, intensity, sky_light_intensity } is part of Sky's PartialEq, so a drag on any of them sets changes.sky at renderer/src/world.rs:988. That takes apply_sky (renderer/src/live/edits.rs:636, :792), which calls:

  1. scene::sky_data → panorama() → files::image_data: the EXR is read and decoded from disk again, and every texel is converted again (scene.rs:1347, :1388).
  2. build_env_cdf(&baked.env): the full W×H CDF is rebuilt (edits.rs:796).
  3. put_env: the full texture is converted to f16 and uploaded again.

None of these three values changes a texel or the CDF. They only change Params::hdri ([offset, intensity·skyLight, intensity, 1]) and the NEE gate in counts2[1]. For a 4K or 8K Blender export, that repeats a multi-megapixel decode, a CDF build and an upload on every committed edit. The atmosphere and gradient skies avoid this with the static LAST caches in bake_sky (lib.rs:1034, :1055).

Two ways to fix it:

  • In World, compare the HDRI's texture id separately from its scalars. When only the scalars changed, rewrite Params::hdri and counts2[1] through SkyBaked::write and skip put_env.
  • Alternatively, cache the decoded EnvMap (it is already an Arc<[[f32; 4]]>) and its CDF by texture id and file version (files::version), like the other skies do.

A small test would protect the fix: change only rotation on an HDRI sky and assert that Params::hdri[0] changes and the env texture is not replaced.

Checks I ran on 23ddc29:

  • npm test: 165 passed, 1 skipped.
  • npm run typecheck: fails only at packages/voxel/src/native.ts:175. That error is already present at this PR's merge base, 0778f4d, so this PR does not cause it.
  • I could not run cargo test in my environment, so the Rust-side points above come from reading the code.

Drafted with AI assistance.

@wangcan26

Copy link
Copy Markdown
Author

Thanks for checking the three.js conventions and catching the live-edit cost, @kvnloo! Fixed in 49b17f5. Live rotation, intensity, and skyLightIntensity edits now reuse the uploaded panorama and CDF. The regression test covers those edits with the source texture unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support equirectangular HDRI environments in the native renderer

2 participants