|
1 |
| -import { useEffect, useMemo } from "react"; |
2 |
| -import { useThree } from "@react-three/fiber"; |
3 |
| -import { NearestFilter, RGBAFormat, WebGLCubeRenderTarget } from "three"; |
4 |
| -import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader"; |
| 1 | +import { Suspense } from "react"; |
| 2 | +import { Environment } from "@react-three/drei"; |
5 | 3 |
|
6 | 4 | type HDRIProps = {
|
7 | 5 | src: string;
|
8 |
| - size?: number; |
9 | 6 | disableBackground?: boolean;
|
10 | 7 | disableEnvironment?: boolean;
|
11 | 8 | };
|
12 | 9 |
|
13 | 10 | export function HDRI(props: HDRIProps) {
|
14 |
| - const { src, size = 1204, disableBackground, disableEnvironment } = props; |
15 |
| - |
16 |
| - const { gl, scene } = useThree(); |
17 |
| - |
18 |
| - const loader = useMemo(() => new RGBELoader(), []); |
19 |
| - |
20 |
| - useEffect(() => { |
21 |
| - loader.load(src, (texture) => { |
22 |
| - const opts = { |
23 |
| - format: RGBAFormat, |
24 |
| - generateMipmaps: false, |
25 |
| - magFilter: NearestFilter, |
26 |
| - minFilter: NearestFilter, |
27 |
| - }; |
28 |
| - |
29 |
| - const envMap = new WebGLCubeRenderTarget( |
30 |
| - size, |
31 |
| - opts |
32 |
| - ).fromEquirectangularTexture(gl, texture).texture; |
33 |
| - |
34 |
| - // sent envmap onto scene env and background |
35 |
| - if (!disableBackground) { |
36 |
| - scene.background = envMap; |
37 |
| - } |
38 |
| - if (!disableEnvironment) { |
39 |
| - scene.environment = envMap; |
40 |
| - } |
41 |
| - |
42 |
| - texture.dispose(); |
43 |
| - |
44 |
| - return () => { |
45 |
| - scene.background = null; |
46 |
| - scene.environment = null; |
47 |
| - envMap.dispose(); |
48 |
| - }; |
49 |
| - }); |
50 |
| - }, [src, scene, loader, disableBackground, disableEnvironment]); |
51 |
| - |
52 |
| - return null; |
| 11 | + const { src, disableBackground, disableEnvironment } = props; |
| 12 | + |
| 13 | + return ( |
| 14 | + <Suspense fallback={null}> |
| 15 | + <Environment |
| 16 | + files={src} |
| 17 | + background={ |
| 18 | + !disableBackground && !disableEnvironment |
| 19 | + ? true |
| 20 | + : disableEnvironment && !disableBackground |
| 21 | + ? "only" |
| 22 | + : false |
| 23 | + } |
| 24 | + /> |
| 25 | + </Suspense> |
| 26 | + ); |
53 | 27 | }
|
0 commit comments