Skip to content

Commit b005735

Browse files
committed
Fix HDRI preview updates after first light add
Track whether the scene has lights in `EnvMapPlane` and key `RenderCubeTexture` by that state to force a remount when transitioning from no lights to lights. This recreates Drei’s event portal so interaction state is refreshed instead of staying stuck in the initial empty-environment state until reload.
1 parent e060e06 commit b005735

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/components/HDRIPreview/EnvMapPlane.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { ComputeFunction, useFrame, useThree } from "@react-three/fiber";
44
import { Env } from "../Env";
55
import { CubeMaterial } from "./CubeMaterial";
66
import { useEffect, useRef } from "react";
7-
import { useSetAtom } from "jotai";
8-
import { envMapTextureAtom, sceneRendererAtom } from "../../store";
7+
import { useAtomValue, useSetAtom } from "jotai";
8+
import {
9+
envMapTextureAtom,
10+
lightIdsAtom,
11+
sceneRendererAtom,
12+
} from "../../store";
913
import { OnDemandCubemapCapture } from "../Env/OnDemandCubemapCapture";
1014
import {
1115
ENV_CAPTURE_FAR,
@@ -55,6 +59,7 @@ export function EnvMapPlane() {
5559
const lastTextureRef = useRef<THREE.CubeTexture | null>(null);
5660
const viewport = useThree((state) => state.viewport);
5761
const renderer = useThree((state) => state.gl);
62+
const hasLights = useAtomValue(lightIdsAtom).length > 0;
5863
const setTexture = useSetAtom(envMapTextureAtom);
5964
const setRenderer = useSetAtom(sceneRendererAtom);
6065

@@ -125,6 +130,10 @@ export function EnvMapPlane() {
125130
<planeGeometry />
126131
<CubeMaterial>
127132
<RenderCubeTexture
133+
// Recreate Drei's event portal when an empty environment gains its
134+
// first light. Its interaction registry otherwise retains the
135+
// empty state until the page is reloaded.
136+
key={hasLights ? "with-lights" : "without-lights"}
128137
ref={cubeCaptureRef}
129138
attach="map"
130139
compute={compute}

0 commit comments

Comments
 (0)