Coder Social home page Coder Social logo

Comments (2)

alexkahndev avatar alexkahndev commented on July 30, 2024 1
import { useEffect } from "react";
import { useThree } from "@react-three/fiber";
import { useEnvironment } from "@react-three/drei";

type BackgroundProps = {
	backgroundUrl: string;
};

export const Background = ({ backgroundUrl }: BackgroundProps) => {
	const texture = useEnvironment({
		files: backgroundUrl,
	});

	const { scene } = useThree((state) => ({ scene: state.scene }));

	useEffect(() => {
		const previousTexture = scene.background;

		scene.environment = texture;
		scene.background = texture;

		return () => {
			if (previousTexture) {
				(previousTexture as any).dispose();
			}
		};
	}, [texture, scene]);

	return (
		<>
			<directionalLight
				position={[3.3, 1.0, 4.4]}
				castShadow
				intensity={1}
			/>
		</>
	);
};

Instead of using the Environment component, the useEnvironment hook works in a way for what I needed to dispose of the old texture.

After looking into the code for Environment I saw memoization which I assume was the root of my original issue. What is the reason for the Environment component to have memoization?

from drei.

tomires avatar tomires commented on July 30, 2024

I have experienced the same issue as @alexkahndev however as I needed the environment to be ground projected, my solution involved the use of three's GroundedSkybox addon

import { extend, useLoader, useThree } from '@react-three/fiber'
import { TextureLoader } from 'three/src/loaders/TextureLoader'
import { GroundedSkybox } from 'three/examples/jsm/Addons.js'
import { useEffect } from 'react'
import * as THREE from 'three'

extend({ GroundedSkybox })

export default function SceneEnvironment({ 
    texture, 
    envHeight = 2.4,
    envRadius = 7,
    envScale = 100,
    heightOffset = 35
}) {
    const loadedTexture = useLoader(TextureLoader, texture)

    const { scene } = useThree()

    useEffect(() => {
        loadedTexture.mapping = THREE.EquirectangularReflectionMapping
        loadedTexture.colorSpace = THREE.SRGBColorSpace
        scene.environment = loadedTexture
    }, [scene])

    useEffect(() => {
        return () => {
            if (loadedTexture) {
                loadedTexture.dispose()
            }
        }
    })

    return (
        <group scale={ envScale } position-y={ heightOffset }>
            <groundedSkybox
                args={ [loadedTexture, envHeight, envRadius, 40] }
            />  
        </group>
    )
}

from drei.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.