Spaces:
Sleeping
Sleeping
File size: 6,200 Bytes
43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 43024e4 96c6832 | 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 | import React, { Suspense, useRef, useEffect } from 'react';
import { Canvas, useThree } from '@react-three/fiber';
import {
OrbitControls, Grid, GizmoHelper,
GizmoViewport, Sky, Stars, TransformControls,
} from '@react-three/drei';
import * as THREE from 'three';
import { useStudioStore } from '../store/useStudioStore';
import { Model, ModelHandle } from './Model';
import './Viewport.css';
// โโ One model + its optional TransformControls โโโโโโโโโโโโโโโโ
const ModelWithGizmo: React.FC<{
obj: any;
isSelected: boolean;
gizmoMode: 'translate' | 'rotate' | 'scale';
onOrbitEnable: (v: boolean) => void;
}> = ({ obj, isSelected, gizmoMode, onOrbitEnable }) => {
const modelRef = useRef<ModelHandle>(null);
const { updateObject, setSelectedId } = useStudioStore();
const syncTransform = () => {
const g = modelRef.current?.group;
if (!g) return;
updateObject(obj.id, {
position: [+g.position.x.toFixed(3), +g.position.y.toFixed(3), +g.position.z.toFixed(3)],
rotation: [
+THREE.MathUtils.radToDeg(g.rotation.x).toFixed(2),
+THREE.MathUtils.radToDeg(g.rotation.y).toFixed(2),
+THREE.MathUtils.radToDeg(g.rotation.z).toFixed(2),
],
scale: [+g.scale.x.toFixed(3), +g.scale.y.toFixed(3), +g.scale.z.toFixed(3)],
});
};
return (
<>
<Model
ref={modelRef}
obj={obj}
isSelected={isSelected}
onClick={() => setSelectedId(obj.id)}
/>
{isSelected && modelRef.current?.group && (
<TransformControls
object={modelRef.current.group}
mode={gizmoMode}
size={0.9}
onMouseDown={() => onOrbitEnable(false)}
onMouseUp={() => { onOrbitEnable(true); syncTransform(); }}
onChange={syncTransform}
/>
)}
</>
);
};
// โโ HDR / equirectangular skybox โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const SkyboxHDR: React.FC<{ url: string }> = ({ url }) => {
const { scene } = useThree();
useEffect(() => {
new THREE.TextureLoader().load(url, (tex) => {
tex.mapping = THREE.EquirectangularReflectionMapping;
scene.background = tex;
scene.environment = tex;
});
}, [url, scene]);
return null;
};
// โโ Main scene content โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const SceneContent: React.FC = () => {
const {
objects, selectedId,
ambientIntensity, directionalIntensity,
showGrid, showAxes,
skyboxType, skyboxUrl, bgColor,
mode, gizmoMode,
} = useStudioStore();
const { scene } = useThree();
const orbitRef = useRef<any>(null);
useEffect(() => {
if (skyboxType !== 'uploaded')
scene.background = new THREE.Color(bgColor);
}, [bgColor, skyboxType, scene]);
const setOrbit = (v: boolean) => {
if (orbitRef.current) orbitRef.current.enabled = v;
};
return (
<>
<ambientLight intensity={ambientIntensity} />
<directionalLight
position={[5, 10, 5]}
intensity={directionalIntensity}
castShadow
shadow-mapSize={[2048, 2048]}
/>
<directionalLight position={[-5, 5, -5]} intensity={directionalIntensity * 0.3} />
{skyboxType === 'gradient' && (
<Sky sunPosition={[100, 20, 100]} turbidity={8} rayleigh={2} />
)}
{skyboxType === 'uploaded' && skyboxUrl && <SkyboxHDR url={skyboxUrl} />}
{mode === 'render' && (
<Stars radius={100} depth={50} count={3000} factor={4} />
)}
<Suspense fallback={null}>
{objects.map((obj) => (
<ModelWithGizmo
key={obj.id}
obj={obj}
isSelected={selectedId === obj.id}
gizmoMode={gizmoMode}
onOrbitEnable={setOrbit}
/>
))}
</Suspense>
{showGrid && (
<Grid
position={[0, -0.01, 0]}
args={[20, 20]}
cellSize={1}
cellThickness={0.5}
cellColor="#444466"
sectionSize={5}
sectionThickness={1}
sectionColor="#6666aa"
fadeDistance={30}
infiniteGrid
/>
)}
{showAxes && <axesHelper args={[5]} />}
<OrbitControls ref={orbitRef} makeDefault />
<GizmoHelper alignment="bottom-right" margin={[80, 80]}>
<GizmoViewport
axisColors={['#f06292', '#66bb6a', '#42a5f5']}
labelColor="white"
/>
</GizmoHelper>
</>
);
};
// โโ Viewport root โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const Viewport: React.FC = () => {
const { gizmoMode, setGizmoMode, mode } = useStudioStore();
return (
<div className="viewport">
{/* Floating gizmo mode toolbar โ only in MODEL mode */}
{mode === 'model' && (
<div className="gizmo-toolbar">
{([
{ id: 'translate', icon: 'โ', label: 'Move', key: 'G' },
{ id: 'rotate', icon: 'โป', label: 'Rotate', key: 'R' },
{ id: 'scale', icon: 'โคข', label: 'Scale', key: 'S' },
] as const).map((g) => (
<button
key={g.id}
className={`gizmo-btn ${gizmoMode === g.id ? 'active' : ''}`}
onClick={() => setGizmoMode(g.id)}
title={`${g.label} (${g.key})`}
>
<span className="gizmo-icon">{g.icon}</span>
<span>{g.label}</span>
<span className="gizmo-key">{g.key}</span>
</button>
))}
</div>
)}
<Canvas
shadows
camera={{ position: [5, 5, 5], fov: 50 }}
gl={{
antialias: true,
toneMapping: THREE.ACESFilmicToneMapping,
toneMappingExposure: 1,
preserveDrawingBuffer: true, // required for video recording
}}
onPointerMissed={() => useStudioStore.getState().setSelectedId(null)}
>
<SceneContent />
</Canvas>
</div>
);
};
export default Viewport; |