feat: added task 3 in r3f
This commit is contained in:
parent
34f4bc743d
commit
3970740f88
@ -1,10 +1,12 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
.app-container {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
position: relative;
|
||||
background-color: white;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
body {
|
||||
overflow: hidden;
|
||||
background-color: #111;
|
||||
font-family: sans-serif;
|
||||
.canvas-wrapper {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
@ -1,5 +1,64 @@
|
||||
import React, { useState } from 'react';
|
||||
import { Canvas } from '@react-three/fiber';
|
||||
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
|
||||
import './App.css';
|
||||
|
||||
const variants = {
|
||||
black: {
|
||||
geometry: <boxGeometry args={[2, 2, 2]} />,
|
||||
material: { color: "black", roughness: 1, metalness: 0 }
|
||||
},
|
||||
silver: {
|
||||
geometry: <sphereGeometry args={[1.5, 32, 32]} />,
|
||||
material: { color: "#C0C0C0", roughness: 0.3, metalness: 1 }
|
||||
},
|
||||
gold: {
|
||||
geometry: <torusGeometry args={[1.2, 0.4, 16, 100]} />,
|
||||
material: { color: "#FFD700", roughness: 0.4, metalness: 1 }
|
||||
}
|
||||
};
|
||||
|
||||
function Product({ variant }) {
|
||||
const { geometry, material } = variants[variant];
|
||||
|
||||
return (
|
||||
<mesh>
|
||||
{geometry}
|
||||
<meshStandardMaterial {...material} />
|
||||
</mesh>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<></>
|
||||
)
|
||||
const [activeVariant, setActiveVariant] = useState('black');
|
||||
|
||||
return (
|
||||
<div className="app-container">
|
||||
<div id="controls">
|
||||
{Object.keys(variants).map((variant) => (
|
||||
<button
|
||||
key={variant}
|
||||
className={activeVariant === variant ? 'active' : ''}
|
||||
onClick={() => setActiveVariant(variant)}
|
||||
>
|
||||
{variant.charAt(0).toUpperCase() + variant.slice(1)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="canvas-wrapper">
|
||||
<Canvas dpr={[1, 2]}>
|
||||
<color attach="background" args={["white"]} />
|
||||
<PerspectiveCamera makeDefault position={[0, 0, 5]} />
|
||||
|
||||
<ambientLight intensity={0.6 * Math.PI} />
|
||||
<pointLight position={[5, 5, 5]} intensity={1.0 * Math.PI} />
|
||||
|
||||
<Product variant={activeVariant} />
|
||||
|
||||
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
|
||||
</Canvas>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user