aboutsummaryrefslogtreecommitdiff
path: root/src/components/SceneBackground.tsx
blob: c2dd2e27f0d6934058f970521dccc00331761881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, { useEffect } from "react";
import { useThree } from "@react-three/fiber";
import * as THREE from "three";
import { useStore } from "../store/store.ts";

const textureLoader = new THREE.TextureLoader();

const SceneBackgroundInner: React.FC = () => {
  const backdropUrl = useStore((state) => state.backdropUrl);
  const { scene } = useThree();

  useEffect(() => {
    if (backdropUrl) {
      textureLoader.load(backdropUrl, (texture) => {
        texture.colorSpace = THREE.SRGBColorSpace;
        scene.background = texture;
      });
    } else {
      scene.background = new THREE.Color(0x4a4a4a);
    }

    return () => {
      if (scene.background instanceof THREE.Texture) {
        scene.background.dispose();
      }
    };
  }, [backdropUrl, scene]);

  return null;
};

export const SceneBackground = React.memo(SceneBackgroundInner);
SceneBackground.displayName = "SceneBackground";