feat: added scene for task1 in r3f
This commit is contained in:
parent
9658508b1d
commit
78d516b3f2
@ -1,9 +1,95 @@
|
|||||||
|
|
||||||
function App() {
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
|
import { Canvas, useFrame, useThree } from '@react-three/fiber'
|
||||||
|
import * as THREE from 'three'
|
||||||
|
|
||||||
|
const PRESETS = {
|
||||||
|
front: [0, 1.2, 6.5],
|
||||||
|
side: [6.5, 1.4, 0],
|
||||||
|
topAngled: [3.8, 5.4, 4.2],
|
||||||
|
}
|
||||||
|
|
||||||
|
function CameraController({ preset }) {
|
||||||
|
const { camera } = useThree()
|
||||||
|
const target = useMemo(() => new THREE.Vector3(0, 0, 0), [])
|
||||||
|
const targetPosition = useMemo(() => new THREE.Vector3(), [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const [x, y, z] = PRESETS[preset]
|
||||||
|
targetPosition.set(x, y, z)
|
||||||
|
}, [preset, targetPosition])
|
||||||
|
|
||||||
|
useFrame(() => {
|
||||||
|
camera.position.lerp(targetPosition, 0.08)
|
||||||
|
camera.lookAt(target)
|
||||||
|
})
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function Scene({ preset }) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<ambientLight intensity={0.85} />
|
||||||
|
<directionalLight intensity={1} position={[3, 4, 5]} />
|
||||||
|
|
||||||
|
<mesh>
|
||||||
|
<boxGeometry args={[1.6, 1.6, 1.6]} />
|
||||||
|
<meshStandardMaterial color="#2a9d8f" roughness={0.45} metalness={0.1} />
|
||||||
|
</mesh>
|
||||||
|
|
||||||
|
<CameraController preset={preset} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [preset, setPreset] = useState('front')
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
function onKeyDown(event) {
|
||||||
|
if (event.key === '1') setPreset('front')
|
||||||
|
if (event.key === '2') setPreset('side')
|
||||||
|
if (event.key === '3') setPreset('topAngled')
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', onKeyDown)
|
||||||
|
return () => window.removeEventListener('keydown', onKeyDown)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="app-shell">
|
||||||
|
<div id="preset-panel">
|
||||||
|
<h2>Camera Presets</h2>
|
||||||
|
<div className="preset-buttons">
|
||||||
|
<button
|
||||||
|
className={`preset-btn ${preset === 'front' ? 'active' : ''}`}
|
||||||
|
onClick={() => setPreset('front')}
|
||||||
|
>
|
||||||
|
Front (1)
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`preset-btn ${preset === 'side' ? 'active' : ''}`}
|
||||||
|
onClick={() => setPreset('side')}
|
||||||
|
>
|
||||||
|
Side (2)
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`preset-btn ${preset === 'topAngled' ? 'active' : ''}`}
|
||||||
|
onClick={() => setPreset('topAngled')}
|
||||||
|
>
|
||||||
|
Top Angled (3)
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p id="hint">Press 1, 2, or 3 to switch view.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Canvas camera={{ fov: 55, near: 0.1, far: 100, position: PRESETS.front }}>
|
||||||
|
<color attach="background" args={["#f3f5f8"]} />
|
||||||
|
<Scene preset={preset} />
|
||||||
|
</Canvas>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|||||||
@ -12,4 +12,70 @@ body,
|
|||||||
|
|
||||||
body {
|
body {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
font-family: "Avenir Next", "Segoe UI", sans-serif;
|
||||||
|
background: #f3f5f8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-shell {
|
||||||
|
position: relative;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#preset-panel {
|
||||||
|
position: fixed;
|
||||||
|
top: 16px;
|
||||||
|
left: 16px;
|
||||||
|
z-index: 10;
|
||||||
|
background: rgba(255, 255, 255, 0.82);
|
||||||
|
border: 1px solid rgba(0, 0, 0, 0.12);
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
|
||||||
|
padding: 12px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
backdrop-filter: blur(6px);
|
||||||
|
}
|
||||||
|
|
||||||
|
#preset-panel h2 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 14px;
|
||||||
|
letter-spacing: 0.03em;
|
||||||
|
color: #1b2530;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn {
|
||||||
|
border: 1px solid #cfd8e3;
|
||||||
|
background: #ffffff;
|
||||||
|
color: #203448;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 8px 10px;
|
||||||
|
font-size: 13px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn:hover {
|
||||||
|
border-color: #8ea2b8;
|
||||||
|
transform: translateY(-1px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn.active {
|
||||||
|
background: #203448;
|
||||||
|
border-color: #203448;
|
||||||
|
color: #f4f8fb;
|
||||||
|
}
|
||||||
|
|
||||||
|
#hint {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #4f647a;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user