feat: added texture switcher for task 2 in r3f
This commit is contained in:
parent
5b88f5c625
commit
b3133aae0e
@ -1,10 +1,41 @@
|
||||
* {
|
||||
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;
|
||||
#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,53 @@
|
||||
export default function App() {
|
||||
import React, { useState} from 'react';
|
||||
import { Canvas } from '@react-three/fiber';
|
||||
import { OrbitControls, PerspectiveCamera, useTexture } from '@react-three/drei';
|
||||
import './App.css';
|
||||
|
||||
const planetUrls = {
|
||||
earth: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQZVsp7bSmsdGhM1GouOYgZ6l06Za__Z1ZY8A&s',
|
||||
moon: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcROh1go667NHsMdzLyvI-0tt9Mn0eugRp0xhQ&s',
|
||||
sun: 'https://upload.wikimedia.org/wikipedia/commons/a/a4/Solarsystemscope_texture_8k_sun.jpg',
|
||||
};
|
||||
|
||||
function Planet({ variant }) {
|
||||
const textures = useTexture(planetUrls);
|
||||
|
||||
return (
|
||||
<></>
|
||||
)
|
||||
<mesh rotation={[0, 0, 0]}>
|
||||
<sphereGeometry args={[1.5, 100, 100]} />
|
||||
<meshStandardMaterial map={textures[variant]} roughness={0.6} />
|
||||
</mesh>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const [activeVariant, setActiveVariant] = useState('earth');
|
||||
|
||||
return (
|
||||
<div className="app-container">
|
||||
<div id="controls">
|
||||
{Object.keys(planetUrls).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} />
|
||||
<Planet variant={activeVariant} />
|
||||
|
||||
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
|
||||
</Canvas>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user