Hello Guys, so basically im developing a web app and want to render 3d models in my react app. I am totally noob in 3d modelling and threejs so i don’t understand the technicality, but i managed to get things running so far. This is my code,
const Model = ({ model }) => {
const gltf = useLoader(GLTFLoader, model);
const [centeredPosition, setCenteredPosition] = useState()
var box = new Box3().setFromObject(gltf.scene);
var center = new Vector3();
box.getCenter(center);
useEffect(() => {
setCenteredPosition(gltf.scene.position.sub(center))
gltf.scene.rotation.y = Math.PI;
})
return (
<>
<primitive object={gltf.scene} scale={2} position={centeredPosition} />
</>
);
};
const RenderModel = ({ model }) => {
return (
<div className={styles["globe"]}>
<Canvas>
<color attach="background" args={["violet"]} />
<directionalLight intensity={1} />
<ambientLight intensity={1} />
<spotLight position={[10, 15, 10]} angle={0.9} />
<Suspense fallback={null}>
<Model model={model} />
<OrbitControls autoRotate />
{/* <Environment preset="sunset" background /> */}
</Suspense>
</Canvas>
</div>
)
}
So basically, in my code you can see that i have set scale={2}
statically and there are big and small models uploaded, i want to automatically scale them (zoom into them) according to their size upon rendering.
Help would be deeply appreciated.