Comments (2)
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.
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)
- MeshTransmissionMaterial sandbox demo not working.
- Cached useEnvironment gainmap texture no longer works after renderer lost context HOT 1
- Question: How to draw many instance of plain with same texture but with different uvs.
- Expose MeshPortalMaterial RenderTarget
- R3F how can I get the camera controls through the hook get()
- Bounds Ignores Group Scale HOT 1
- Three.js outlinePass add-on not working when implemented with R3F
- Edges don't work when negative scaling is used (works in Drei <=0.101.0)
- Strange issues with MeshReflectorMaterial and GLB geometries HOT 1
- Float does not work with on demand rendering HOT 1
- e2e playright + docker support
- The reason for the very low performance of R3F with instances ?
- Billboard does not respect position after updating three to 0.166 HOT 1
- Using Outlines in React Native HOT 1
- MeshTransmissionMaterial heats up my macbook air m2
- The grass (instances) example in the readme / documentation should be updated.
- Unsupported GPOS/GSUB table LookupType with <Text>
- AdaptiveDpr min/max settings
- iOS production build crash: Calling the 'readAsStringAsync' function has failed in the latest react-native version
- iOS production build crash: Calling the 'readAsStringAsync' function has failed in the latest react-native version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drei.