Hi all!
I am trying to add 3D models (converted from .gltf to .jsx file) to Scene on Button click. I am using Vite, React Three Fiber, Drei and leva. For now I have got 4 objects and I want to make them appear on scene when I click on button for each individually, for example:
<button type="submit">Add chair</button>
<button type="submit">Add table</button>
<button type="submit">Add barrier</button>
<button type="submit">Add tree</button>
My code is spaghetti code for now, later I am going to refactor it (seperate files, loop over etc.) I have tried to make it works like adding task in simple todolist but it does not work. Do you know how to solve this issue?
import {
DragControls,
Grid,
OrbitControls,
PivotControls,
} from "@react-three/drei";
import Table from "../../public/temporary/Table";
import Barrier from "../../public/temporary/Barrier";
import Chair from "../../public/temporary/Chair";
import Tree from "../../public/temporary/Tree";
import { useControls } from "leva";
function Scene() {
const { scale, Pivot } = useControls("models", {
scale: { options: [0.2, 0.4, 0.6, 0.8] },
Pivot: true,
});
return (
<>
<ambientLight intensity={2} />
<OrbitControls
makeDefault
maxPolarAngle={Math.PI / 2.5}
minDistance={5}
maxDistance={25}
/>
<directionalLight />
<Grid
args={[64, 64]}
fadeDistance={32}
fadeStrength={1.5}
cellThickness={0.75}
sectionThickness={1.5}
cellSize={0.25}
/>
<DragControls axisLock="y">
<PivotControls
activeAxes={[true, false, true]}
anchor={[0, 1, 0]}
depthTest={false}
axisColors={["red", "green", "blue"]}
lineWidth={7}
scale={2}
visible={Pivot}
>
<Table position={[-3, 0, -2]} scale={scale} />
</PivotControls>
</DragControls>
<DragControls axisLock="y">
<PivotControls
activeAxes={[true, false, true]}
anchor={[0, -0.9, 0]}
depthTest={false}
axisColors={["red", "green", "blue"]}
lineWidth={7}
scale={2}
visible={Pivot}
>
<Tree position={[2, 0.075, -2]} scale={scale} />
</PivotControls>
</DragControls>
<DragControls axisLock="y">
<PivotControls
activeAxes={[true, false, true]}
anchor={[0, 1, 0]}
depthTest={false}
axisColors={["red", "green", "blue"]}
lineWidth={7}
scale={1.5}
visible={Pivot}
>
<Chair position={[-3, 0, 4]} scale={scale} />
</PivotControls>
</DragControls>
<DragControls axisLock="y">
<PivotControls
activeAxes={[true, false, true]}
anchor={[0, 1, 0]}
depthTest={false}
axisColors={["red", "green", "blue"]}
lineWidth={7}
scale={2}
visible={Pivot}
>
<Barrier position={[1, 0, 4]} scale={scale} pos />
</PivotControls>
</DragControls>
</>
);
}
export default Scene;