feat: added texture switcher for task 2 in r3f

This commit is contained in:
divyap 2026-04-02 19:16:29 +05:30
parent 5b88f5c625
commit b3133aae0e
2 changed files with 90 additions and 11 deletions

View File

@ -1,10 +1,41 @@
* { .app-container {
margin: 0; width: 100vw;
padding: 0; height: 100vh;
box-sizing: border-box; position: relative;
background-color: white;
overflow: hidden;
} }
body { #controls {
overflow: hidden; position: absolute;
background-color: #111; top: 20px;
font-family: sans-serif; 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%;
}

View File

@ -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 ( 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>
);
} }