File size: 10,500 Bytes
4d0d400 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | import { useRef, useState, Suspense, useEffect } from 'react';
import { Canvas, useFrame } from '@react-three/fiber';
import { OrbitControls, Environment, Stars, Effects } from '@react-three/drei';
import { Box, Disc, Hexagon, Octagon, Pentagon, Star, Palette, Zap, Maximize } from 'lucide-react';
import * as THREE from 'three';
import { EffectComposer, Bloom, ChromaticAberration } from '@react-three/postprocessing';
interface ShapeProps {
geometry: 'box' | 'sphere' | 'torus' | 'icosahedron' | 'octahedron' | 'dodecahedron';
color: string;
textured: boolean;
speed: number;
scale: number;
effects: boolean;
}
function createVoronoiTexture() {
const size = 512;
const data = new Uint8Array(size * size * 4);
const points = Array(16).fill(0).map(() => ({
x: Math.random() * size,
y: Math.random() * size,
color: Math.random() * 255
}));
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
let minDist = Infinity;
let cellColor = 0;
// Find nearest point for voronoi cell
points.forEach(point => {
const dx = x - point.x;
const dy = y - point.y;
const dist = dx * dx + dy * dy;
if (dist < minDist) {
minDist = dist;
cellColor = point.color;
}
});
// Add some noise to break up the pattern
const noise = Math.random() * 30 - 15;
const finalColor = Math.max(0, Math.min(255, cellColor + noise));
const i = (y * size + x) * 4;
data[i] = finalColor; // r
data[i + 1] = finalColor; // g
data[i + 2] = finalColor; // b
data[i + 3] = 255; // a
}
}
const texture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.needsUpdate = true;
return texture;
}
function Shape({ geometry, color, textured, speed, scale, effects }: ShapeProps) {
const meshRef = useRef<THREE.Mesh>(null);
const matRef = useRef<THREE.MeshPhysicalMaterial>(null);
const [normalMap] = useState(() => createVoronoiTexture());
useFrame((state, delta) => {
if (meshRef.current) {
meshRef.current.rotation.x += delta * speed;
meshRef.current.rotation.y += delta * speed * 0.5;
}
if (matRef.current && effects) {
matRef.current.roughness = Math.sin(state.clock.elapsedTime) * 0.3 + 0.5;
}
});
const getGeometry = () => {
switch (geometry) {
case 'box':
return <boxGeometry args={[1, 1, 1]} />;
case 'sphere':
return <sphereGeometry args={[0.7, 64, 64]} />;
case 'torus':
return <torusGeometry args={[0.7, 0.3, 64, 128]} />;
case 'icosahedron':
return <icosahedronGeometry args={[0.8, 1]} />;
case 'octahedron':
return <octahedronGeometry args={[0.8, 2]} />;
case 'dodecahedron':
return <dodecahedronGeometry args={[0.8, 1]} />;
default:
return <boxGeometry args={[1, 1, 1]} />;
}
};
return (
<mesh ref={meshRef} scale={scale} castShadow receiveShadow>
{getGeometry()}
<meshPhysicalMaterial
ref={matRef}
color={color}
wireframe={!textured}
roughness={textured ? 0.5 : 0}
metalness={textured ? 0.9 : 0}
clearcoat={textured ? 0.5 : 0}
clearcoatRoughness={textured ? 0.2 : 0}
normalMap={textured ? normalMap : null}
normalScale={new THREE.Vector2(1.0, 1.0)}
envMapIntensity={2.5}
transmission={textured ? 0 : 0}
thickness={0}
/>
</mesh>
);
}
function SceneContent(props: ShapeProps) {
return (
<>
<color attach="background" args={['#000000']} />
<fog attach="fog" args={['#000000', 5, 15]} />
<ambientLight intensity={0.4} />
<pointLight position={[10, 10, 10]} intensity={1} castShadow />
<spotLight
position={[-10, -10, -10]}
angle={0.3}
penumbra={1}
intensity={2}
castShadow
/>
<Suspense fallback={null}>
<Shape {...props} />
<Environment preset="sunset" />
<Stars radius={100} depth={50} count={5000} factor={4} saturation={0} fade speed={1} />
</Suspense>
{props.effects && (
<EffectComposer>
<Bloom
intensity={1.5}
luminanceThreshold={0.8}
luminanceSmoothing={0.5}
height={200}
/>
<ChromaticAberration offset={[0.001, 0.001]} />
</EffectComposer>
)}
<OrbitControls makeDefault />
</>
);
}
export function Scene() {
const [settings, setSettings] = useState({
geometry: 'box' as const,
color: '#ff6b6b',
textured: false,
speed: 1,
scale: 1,
effects: true,
});
const geometryOptions = [
{ value: 'box', label: 'Cube', icon: Box },
{ value: 'sphere', label: 'Sphere', icon: Disc },
{ value: 'torus', label: 'Torus', icon: Hexagon },
{ value: 'icosahedron', label: 'Icosahedron', icon: Pentagon },
{ value: 'octahedron', label: 'Octahedron', icon: Octagon },
{ value: 'dodecahedron', label: 'Dodecahedron', icon: Star },
];
return (
<div className="w-full h-screen flex bg-black">
<div className="w-3/4 h-full">
<Canvas
camera={{ position: [3, 3, 3], fov: 50 }}
shadows
gl={{ antialias: true }}
dpr={[1, 2]}
className="w-full h-full"
>
<SceneContent {...settings} />
</Canvas>
</div>
<div className="w-1/4 bg-gray-900 p-8 overflow-y-auto border-l border-gray-800">
<h2 className="text-2xl font-bold mb-8 text-white flex items-center">
<Palette className="w-6 h-6 mr-2" /> Scene Settings
</h2>
<div className="space-y-8">
<div>
<label className="block text-sm font-medium text-gray-300 mb-4 flex items-center">
<Box className="w-4 h-4 mr-2" /> Geometry
</label>
<div className="grid grid-cols-2 gap-4">
{geometryOptions.map((option) => {
const Icon = option.icon;
return (
<button
key={option.value}
onClick={() => setSettings({ ...settings, geometry: option.value as ShapeProps['geometry'] })}
className={`flex flex-col items-center justify-center p-4 rounded-lg transition-all ${
settings.geometry === option.value
? 'bg-blue-600 text-white shadow-lg shadow-blue-500/30'
: 'bg-gray-800 text-gray-400 hover:bg-gray-700 hover:text-gray-200'
}`}
>
<Icon className="w-8 h-8 mb-2" strokeWidth={1.5} />
<span className="text-sm font-medium">{option.label}</span>
</button>
);
})}
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-3 flex items-center">
<Palette className="w-4 h-4 mr-2" /> Color
</label>
<input
type="color"
value={settings.color}
onChange={(e) => setSettings({ ...settings, color: e.target.value })}
className="w-full h-12 rounded-lg cursor-pointer bg-gray-800 border-2 border-gray-700"
/>
</div>
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<label className="text-sm font-medium text-gray-300 flex items-center">
<Zap className="w-4 h-4 mr-2" /> Effects
</label>
<button
onClick={() => setSettings({ ...settings, effects: !settings.effects })}
className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${
settings.effects ? 'bg-blue-600' : 'bg-gray-700'
}`}
>
<span
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${
settings.effects ? 'translate-x-6' : 'translate-x-1'
}`}
/>
</button>
</div>
<div className="flex items-center space-x-3">
<label className="text-sm font-medium text-gray-300 flex items-center">
Metallic
</label>
<button
onClick={() => setSettings({ ...settings, textured: !settings.textured })}
className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${
settings.textured ? 'bg-blue-600' : 'bg-gray-700'
}`}
>
<span
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${
settings.textured ? 'translate-x-6' : 'translate-x-1'
}`}
/>
</button>
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-3 flex items-center">
<Zap className="w-4 h-4 mr-2" /> Rotation Speed: {settings.speed.toFixed(1)}
</label>
<input
type="range"
min="0"
max="5"
step="0.1"
value={settings.speed}
onChange={(e) => setSettings({ ...settings, speed: parseFloat(e.target.value) })}
className="w-full accent-blue-600 bg-gray-700 h-2 rounded-lg appearance-none cursor-pointer"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-3 flex items-center">
<Maximize className="w-4 h-4 mr-2" /> Scale: {settings.scale.toFixed(1)}
</label>
<input
type="range"
min="0.1"
max="3"
step="0.1"
value={settings.scale}
onChange={(e) => setSettings({ ...settings, scale: parseFloat(e.target.value) })}
className="w-full accent-blue-600 bg-gray-700 h-2 rounded-lg appearance-none cursor-pointer"
/>
</div>
</div>
</div>
</div>
);
} |