feat: added material variant switcher for task 1 in r3f
This commit is contained in:
parent
044ee26de8
commit
ea2edd0ac9
@ -0,0 +1,41 @@
|
||||
.app-container {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
position: relative;
|
||||
background-color: white;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#controls {
|
||||
position: absolute;
|
||||
top: 20px;
|
||||
left: 20px;
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
cursor: pointer;
|
||||
background: #f0f0f0;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
font-weight: 500;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #e0e0e0;
|
||||
}
|
||||
|
||||
button.active {
|
||||
background: #333;
|
||||
color: white;
|
||||
border-color: #333;
|
||||
}
|
||||
|
||||
.canvas-wrapper {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
@ -1,5 +1,55 @@
|
||||
export default function App() {
|
||||
import React, { useState } from 'react';
|
||||
import { Canvas } from '@react-three/fiber';
|
||||
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
|
||||
import './App.css';
|
||||
|
||||
const materials = {
|
||||
black: { color: "black", roughness: 1, metalness: 0 },
|
||||
silver: { color: "#C0C0C0", roughness: 0.3, metalness: 1 },
|
||||
gold: { color: "#FFD700", roughness: 0.4, metalness: 1 },
|
||||
};
|
||||
|
||||
function Scene({ variant }) {
|
||||
return (
|
||||
<></>
|
||||
)
|
||||
<>
|
||||
<color attach="background" args={["white"]} />
|
||||
|
||||
<ambientLight intensity={0.6 * Math.PI} />
|
||||
<pointLight position={[5, 5, 5]} intensity={1.0 * Math.PI} />
|
||||
|
||||
<mesh rotation={[0, 0, 0]}>
|
||||
<torusKnotGeometry args={[0.8, 0.3, 100, 16]} />
|
||||
<meshStandardMaterial {...materials[variant]}/>
|
||||
</mesh>
|
||||
|
||||
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const [activeVariant, setActiveVariant] = useState('black');
|
||||
|
||||
return (
|
||||
<div className="app-container">
|
||||
<div id="controls">
|
||||
{Object.keys(materials).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]}>
|
||||
<PerspectiveCamera makeDefault position={[0, 0, 5]} />
|
||||
<Scene variant={activeVariant} />
|
||||
</Canvas>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body, html, #root {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
background-color: #111;
|
||||
}
|
||||
@ -1,8 +1,6 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import './index.css'
|
||||
import App from './App.jsx'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user