Hello, threejs novice here. Title: I am having trouble putting the HDRI as lighting for my model using react-three-fiber. My code is as following:
import React, {Suspense, useEffect, useRef} from 'react';
import {Canvas, useThree} from 'react-three-fiber';
import {PMREMGenerator} from "three/src/extras/PMREMGenerator";
import {RGBELoader} from "three/examples/jsm/loaders/RGBELoader";
import {UnsignedByteType} from "three";
import hdr from "path_to_.hdr";
const Model = ({ gltf }) => {
const groupRef = useRef();
const { gl, scene } = useThree();
const pmremGenerator = new PMREMGenerator(gl);
const loader = new RGBELoader();
loader.setDataType(UnsignedByteType);
pmremGenerator.compileEquirectangularShader();
useEffect(() => {
loader.load(
hdr,
texture => {
const textureData = pmremGenerator.fromEquirectangular(texture).texture;
scene.environment = textureData;
texture.dispose();
pmremGenerator.dispose();
}
)
}, [scene, loader, pmremGenerator]);
return (
<group ref={groupRef}>
<primitive object={gltf.scene} dispose={null} />
</group>
)
};
const ThreeDeeModel = (props) => {
return (
<Canvas>
<Suspense fallback={<mesh/>}>
<Model {...props}/>
</Suspense>
</Canvas>
)
};
export default (ThreeDeeModel);
I have been following the similar topics for a while but couldn’t figure where it go wrong. For me now the only thing I’m sure is that:
- I can load the textureData correctly
- My model appears on the browser (covered in black)
Appreciate every help, thanks a lot for reading.