The code creates a fully interactive 3D room with React, Three.js, and Tailwind CSS.
Key Features:
- 3D Navigation: Use your mouse to rotate (Left Click), pan (Right Click), and zoom (Scroll) around the room.
- Dynamic Lighting: Toggle between Day Mode (bright sun, blue sky) and Night Mode (dark sky, stars, warm lamp lighting).
- Furniture: Includes a bed, table, chairs, a lamp, and a rug, all built from basic geometric shapes (Boxes, Planes).
- Shadows: Real-time soft shadows that update with the light source.
Visuals:
- Day: Cheerful, bright atmosphere with a light theme UI.
- Night: Cozy, dark atmosphere with a starry background and emissive lamp glow.
Usage:
The application is contained entirely within src/App.tsx. The build_project command successfully compiles the project into a single index.html file ready for deployment.
// src/App.tsx
import { useState, useRef } from 'react'
import { Canvas } from '@react-three/fiber'
import { OrbitControls, Stars, Box, Plane } from '@react-three/drei'
import { Moon, Sun, Move3d } from 'lucide-react'
import * as THREE from 'three'
// --- Types ---
type Theme = 'day' | 'night'
// --- Components ---
function Floor() {
return (
<group>
<Plane args={[20, 20]} rotation={[-Math.PI / 2, 0, 0]} receiveShadow>
<meshStandardMaterial color="#e5e5e5" roughness={0.8} metalness={0.1} />
</Plane>
<gridHelper args={[20, 20, '#cccccc', '#cccccc']} position={[0, 0.01, 0]} />
</group>
)
}
function Walls() {
return (
<group>
{/* Back Wall */}
<Plane args={[20, 10]} position={[0, 5, -10]} receiveShadow>
<meshStandardMaterial color="#f0f0f0" />
</Plane>
{/* Left Wall */}
<Plane args={[20, 10]} rotation={[0, Math.PI / 2, 0]} position={[-10, 5, 0]} receiveShadow>
<meshStandardMaterial color="#e8e8e8" />
</Plane>
{/* Right Wall (Window Wall simplified) */}
<Plane args={[20, 10]} rotation={[0, -Math.PI / 2, 0]} position={[10, 5, 0]} receiveShadow>
<meshStandardMaterial color="#e8e8e8" />
</Plane>
</group>
)
}
function Bed({ position }: { position: [number, number, number] }) {
return (
<group position={position}>
{/* Frame */}
<Box args={[4, 0.5, 6]} position={[0, 0.25, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#5c4033" />
</Box>
{/* Mattress */}
<Box args={[3.8, 0.6, 5.8]} position={[0, 0.8, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#ffffff" />
</Box>
{/* Blanket */}
<Box args={[3.9, 0.65, 3.5]} position={[0, 0.82, 1.1]} castShadow receiveShadow>
<meshStandardMaterial color="#60a5fa" />
</Box>
{/* Pillow */}
<Box args={[2.5, 0.3, 1]} position={[0, 1.15, -2.2]} castShadow receiveShadow>
<meshStandardMaterial color="#eeeeee" />
</Box>
{/* Headboard */}
<Box args={[4.2, 2, 0.2]} position={[0, 1, -3]} castShadow receiveShadow>
<meshStandardMaterial color="#5c4033" />
</Box>
</group>
)
}
function Table({ position }: { position: [number, number, number] }) {
return (
<group position={position}>
{/* Top */}
<Box args={[3, 0.2, 3]} position={[0, 1.5, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#d4a373" />
</Box>
{/* Legs */}
<Box args={[0.2, 1.5, 0.2]} position={[-1.3, 0.75, -1.3]} castShadow receiveShadow>
<meshStandardMaterial color="#8d6e63" />
</Box>
<Box args={[0.2, 1.5, 0.2]} position={[1.3, 0.75, -1.3]} castShadow receiveShadow>
<meshStandardMaterial color="#8d6e63" />
</Box>
<Box args={[0.2, 1.5, 0.2]} position={[-1.3, 0.75, 1.3]} castShadow receiveShadow>
<meshStandardMaterial color="#8d6e63" />
</Box>
<Box args={[0.2, 1.5, 0.2]} position={[1.3, 0.75, 1.3]} castShadow receiveShadow>
<meshStandardMaterial color="#8d6e63" />
</Box>
</group>
)
}
function Chair({ position, rotation }: { position: [number, number, number], rotation?: number }) {
return (
<group position={position} rotation={[0, rotation || 0, 0]}>
{/* Seat */}
<Box args={[1.5, 0.2, 1.5]} position={[0, 1, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#a1887f" />
</Box>
{/* Back */}
<Box args={[1.5, 1.5, 0.2]} position={[0, 1.75, -0.65]} castShadow receiveShadow>
<meshStandardMaterial color="#8d6e63" />
</Box>
{/* Legs */}
<Box args={[0.15, 1, 0.15]} position={[-0.6, 0.5, -0.6]} castShadow receiveShadow>
<meshStandardMaterial color="#5d4037" />
</Box>
<Box args={[0.15, 1, 0.15]} position={[0.6, 0.5, -0.6]} castShadow receiveShadow>
<meshStandardMaterial color="#5d4037" />
</Box>
<Box args={[0.15, 1, 0.15]} position={[-0.6, 0.5, 0.6]} castShadow receiveShadow>
<meshStandardMaterial color="#5d4037" />
</Box>
<Box args={[0.15, 1, 0.15]} position={[0.6, 0.5, 0.6]} castShadow receiveShadow>
<meshStandardMaterial color="#5d4037" />
</Box>
</group>
)
}
function LampStand({ position }: { position: [number, number, number] }) {
return (
<group position={position}>
{/* Base */}
<Box args={[0.8, 0.1, 0.8]} position={[0, 0.05, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#333" />
</Box>
{/* Pole */}
<Box args={[0.1, 3, 0.1]} position={[0, 1.5, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#555" />
</Box>
{/* Shade */}
<Box args={[1.5, 1.5, 1.5]} position={[0, 3, 0]} castShadow receiveShadow>
<meshStandardMaterial color="#fef3c7" transparent opacity={0.9} emissive="#f59e0b" emissiveIntensity={0.2} />
</Box>
</group>
)
}
function Scene({ theme }: { theme: Theme }) {
const sunRef = useRef<THREE.DirectionalLight>(null)
// We can useFrame here for animations if needed in the future
return (
<>
{/* Ambient Light */}
<ambientLight intensity={theme === 'day' ? 0.6 : 0.1} />
{/* Main Directional Light (Sun/Moon) */}
<directionalLight
ref={sunRef}
position={theme === 'day' ? [10, 20, 10] : [-10, 10, -10]}
intensity={theme === 'day' ? 1.5 : 0.3}
color={theme === 'day' ? '#ffffff' : '#818cf8'}
castShadow
shadow-mapSize-width={2048}
shadow-mapSize-height={2048}
shadow-camera-far={50}
shadow-camera-left={-20}
shadow-camera-right={20}
shadow-camera-top={20}
shadow-camera-bottom={-20}
/>
{/* Lamp Point Light (Only visible at night) */}
{theme === 'night' && (
<pointLight position={[-6, 4, -6]} intensity={10} distance={15} color="#fbbf24" castShadow />
)}
{/* Sky/Stars */}
{theme === 'night' && <Stars radius={100} depth={50} count={5000} factor={4} saturation={0} fade speed={1} />}
{theme === 'day' && (
<color attach="background" args={['#87CEEB']} />
)}
{theme === 'night' && (
<color attach="background" args={['#0f172a']} />
)}
<Floor />
<Walls />
{/* Furniture Placement */}
<Bed position={[-5, 0, -5]} />
<Table position={[5, 0, -5]} />
<Chair position={[5, 0, -1]} rotation={Math.PI} />
<Chair position={[2, 0, -5]} rotation={Math.PI / 2} />
<LampStand position={[-6, 0, -6]} />
{/* Decorative Rug */}
<Box args={[8, 0.05, 6]} position={[0, 0.03, 2]} receiveShadow>
<meshStandardMaterial color="#fca5a5" />
</Box>
<OrbitControls
enablePan={true}
enableZoom={true}
enableRotate={true}
minDistance={5}
maxDistance={30}
maxPolarAngle={Math.PI / 2 - 0.05} // Prevent going below ground
/>
</>
)
}
// --- UI Components ---
function ToggleButton({
active,
onClick,
icon: Icon,
label
}: {
active: boolean;
onClick: () => void;
icon: any;
label: string
}) {
return (
<button
onClick={onClick}
className={`
flex items-center gap-2 px-4 py-2 rounded-full font-medium transition-all duration-300 shadow-lg
${active
? 'bg-white text-slate-900 scale-105 ring-2 ring-blue-400'
: 'bg-slate-800/50 text-slate-300 hover:bg-slate-700/50 backdrop-blur-md'
}
`}
>
<Icon size={18} />
<span>{label}</span>
</button>
)
}
// --- Main App ---
export default function App() {
const [theme, setTheme] = useState<Theme>('day')
return (
<div className="relative w-full h-screen overflow-hidden font-sans">
{/* 3D Canvas */}
<div className="absolute inset-0 z-0">
<Canvas shadows camera={{ position: [15, 10, 15], fov: 45 }}>
<Scene theme={theme} />
</Canvas>
</div>
{/* UI Overlay */}
<div className="absolute top-0 left-0 w-full h-full pointer-events-none z-10 flex flex-col justify-between p-6">
{/* Header */}
<header className="flex justify-between items-start pointer-events-auto">
<div className="bg-white/10 backdrop-blur-md border border-white/20 p-4 rounded-2xl shadow-xl">
<h1 className="text-2xl font-bold text-white flex items-center gap-2">
<Move3d className="text-blue-400" />
Room Architect 3D
</h1>
<p className="text-slate-300 text-sm mt-1">Interactive Interior Visualization</p>
</div>
<div className="flex flex-col gap-3 items-end">
<div className="bg-black/20 backdrop-blur-md p-4 rounded-xl text-white/80 text-xs max-w-xs">
<p className="font-semibold mb-1">Controls:</p>
<ul className="list-disc list-inside space-y-1">
<li>Left Click + Drag to Rotate</li>
<li>Right Click + Drag to Pan</li>
<li>Scroll to Zoom</li>
</ul>
</div>
</div>
</header>
{/* Bottom Controls */}
<footer className="flex justify-center items-end pb-8 pointer-events-auto">
<div className="flex gap-4 bg-slate-900/40 backdrop-blur-xl p-2 rounded-full border border-white/10 shadow-2xl">
<ToggleButton
active={theme === 'day'}
onClick={() => setTheme('day')}
icon={Sun}
label="Day Mode"
/>
<ToggleButton
active={theme === 'day' === false}
onClick={() => setTheme('night')}
icon={Moon}
label="Night Mode"
/>
</div>
</footer>
</div>
</div>
)
}
Files built: src/App.tsx, index.html