Spaces:
Build error
Build error
Upload components/GaussianSplatViewer.jsx with huggingface_hub
Browse files
components/GaussianSplatViewer.jsx
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useRef, useMemo, useState, useEffect } from 'react';
|
| 2 |
+
import { Canvas, useFrame } from '@react-three/fiber';
|
| 3 |
+
import { OrbitControls, Stars, useTexture } from '@react-three/drei';
|
| 4 |
+
import * as THREE from 'three';
|
| 5 |
+
|
| 6 |
+
function GaussianPoints({ worldData, isLoading }) {
|
| 7 |
+
const pointsRef = useRef();
|
| 8 |
+
const [hovered, setHovered] = useState(false);
|
| 9 |
+
|
| 10 |
+
const { positions, colors, sizes } = useMemo(() => {
|
| 11 |
+
if (isLoading || !worldData) {
|
| 12 |
+
return { positions: new Float32Array(0), colors: new Float32Array(0), sizes: new Float32Array(0) };
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const count = worldData.pointCount || 50000;
|
| 16 |
+
const positions = new Float32Array(count * 3);
|
| 17 |
+
const colors = new Float32Array(count * 3);
|
| 18 |
+
const sizes = new Float32Array(count);
|
| 19 |
+
|
| 20 |
+
const colorPalette = worldData.colorPalette || [[0.4, 0.6, 0.9], [0.2, 0.8, 0.4], [0.9, 0.3, 0.5]];
|
| 21 |
+
|
| 22 |
+
for (let i = 0; i < count; i++) {
|
| 23 |
+
const i3 = i * 3;
|
| 24 |
+
|
| 25 |
+
// Spherical distribution for 360 capture feel
|
| 26 |
+
const theta = Math.random() * Math.PI * 2;
|
| 27 |
+
const phi = Math.acos(2 * Math.random() - 1);
|
| 28 |
+
const r = 2 + Math.random() * 3;
|
| 29 |
+
|
| 30 |
+
positions[i3] = r * Math.sin(phi) * Math.cos(theta);
|
| 31 |
+
positions[i3 + 1] = r * Math.sin(phi) * Math.sin(theta);
|
| 32 |
+
positions[i3 + 2] = r * Math.cos(phi);
|
| 33 |
+
|
| 34 |
+
const color = colorPalette[Math.floor(Math.random() * colorPalette.length)];
|
| 35 |
+
colors[i3] = color[0] + (Math.random() - 0.5) * 0.2;
|
| 36 |
+
colors[i3 + 1] = color[1] + (Math.random() - 0.5) * 0.2;
|
| 37 |
+
colors[i3 + 2] = color[2] + (Math.random() - 0.5) * 0.2;
|
| 38 |
+
|
| 39 |
+
sizes[i] = Math.random() * 0.05 + 0.01;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
return { positions, colors, sizes };
|
| 43 |
+
}, [worldData, isLoading]);
|
| 44 |
+
|
| 45 |
+
useFrame((state) => {
|
| 46 |
+
if (pointsRef.current) {
|
| 47 |
+
pointsRef.current.rotation.y += 0.001;
|
| 48 |
+
pointsRef.current.rotation.x = Math.sin(state.clock.elapsedTime * 0.1) * 0.1;
|
| 49 |
+
}
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
if (isLoading) return null;
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
<points
|
| 56 |
+
ref={pointsRef}
|
| 57 |
+
onPointerOver={() => setHovered(true)}
|
| 58 |
+
onPointerOut={() => setHovered(false)}
|
| 59 |
+
>
|
| 60 |
+
<bufferGeometry>
|
| 61 |
+
<bufferAttribute
|
| 62 |
+
attach="attributes-position"
|
| 63 |
+
count={positions.length / 3}
|
| 64 |
+
array={positions}
|
| 65 |
+
itemSize={3}
|
| 66 |
+
/>
|
| 67 |
+
<bufferAttribute
|
| 68 |
+
attach="attributes-color"
|
| 69 |
+
count={colors.length / 3}
|
| 70 |
+
array={colors}
|
| 71 |
+
itemSize={3}
|
| 72 |
+
/>
|
| 73 |
+
<bufferAttribute
|
| 74 |
+
attach="attributes-size"
|
| 75 |
+
count={sizes.length}
|
| 76 |
+
array={sizes}
|
| 77 |
+
itemSize={1}
|
| 78 |
+
/>
|
| 79 |
+
</bufferGeometry>
|
| 80 |
+
<pointsMaterial
|
| 81 |
+
size={0.05}
|
| 82 |
+
vertexColors
|
| 83 |
+
transparent
|
| 84 |
+
opacity={0.8}
|
| 85 |
+
sizeAttenuation
|
| 86 |
+
blending={THREE.AdditiveBlending}
|
| 87 |
+
depthWrite={false}
|
| 88 |
+
/>
|
| 89 |
+
</points>
|
| 90 |
+
);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
function Scene({ worldData, isLoading }) {
|
| 94 |
+
return (
|
| 95 |
+
<>
|
| 96 |
+
<ambientLight intensity={0.5} />
|
| 97 |
+
<pointLight position={[10, 10, 10]} intensity={1} />
|
| 98 |
+
<Stars radius={100} depth={50} count={5000} factor={4} saturation={0} fade speed={1} />
|
| 99 |
+
<GaussianPoints worldData={worldData} isLoading={isLoading} />
|
| 100 |
+
<OrbitControls
|
| 101 |
+
enablePan={true}
|
| 102 |
+
enableZoom={true}
|
| 103 |
+
enableRotate={true}
|
| 104 |
+
zoomSpeed={0.5}
|
| 105 |
+
rotateSpeed={0.5}
|
| 106 |
+
minDistance={2}
|
| 107 |
+
maxDistance={20}
|
| 108 |
+
/>
|
| 109 |
+
</>
|
| 110 |
+
);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
export default function GaussianSplatViewer({ worldData, isLoading }) {
|
| 114 |
+
if (isLoading) {
|
| 115 |
+
return (
|
| 116 |
+
<div className="w-full h-full flex items-center justify-center bg-slate-950">
|
| 117 |
+
<div className="text-center">
|
| 118 |
+
<div className="w-16 h-16 border-4 border-indigo-500 border-t-transparent rounded-full animate-spin mx-auto mb-4"></div>
|
| 119 |
+
<p className="text-slate-400 animate-pulse">Loading Gaussian Splats...</p>
|
| 120 |
+
<p className="text-slate-600 text-sm mt-2">Processing 360° capture data</p>
|
| 121 |
+
</div>
|
| 122 |
+
</div>
|
| 123 |
+
);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
return (
|
| 127 |
+
<div className="w-full h-full relative">
|
| 128 |
+
<Canvas
|
| 129 |
+
camera={{ position: [0, 0, 8], fov: 60 }}
|
| 130 |
+
dpr={[1, 2]}
|
| 131 |
+
gl={{ antialias: true, alpha: true }}
|
| 132 |
+
style={{ background: 'radial-gradient(circle at center, #1e293b 0%, #020617 100%)' }}
|
| 133 |
+
>
|
| 134 |
+
<Scene worldData={worldData} isLoading={isLoading} />
|
| 135 |
+
</Canvas>
|
| 136 |
+
|
| 137 |
+
<div className="absolute bottom-4 left-4 glass-panel rounded-lg p-3 text-xs text-slate-400">
|
| 138 |
+
<p className="font-semibold text-slate-200 mb-1">Controls</p>
|
| 139 |
+
<p>Left Click: Rotate</p>
|
| 140 |
+
<p>Right Click: Pan</p>
|
| 141 |
+
<p>Scroll: Zoom</p>
|
| 142 |
+
</div>
|
| 143 |
+
</div>
|
| 144 |
+
);
|
| 145 |
+
}
|