minor voxel editor fixes

This commit is contained in:
azykov@mail.ru 2026-06-06 10:24:13 +03:00
parent 52caee642e
commit 1d13cfac36
No known key found for this signature in database
1 changed files with 9 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { OrbitControls } from '@react-three/drei'; import { OrbitControls } from '@react-three/drei';
import { useState, useRef } from 'react'; import { useState } from 'react';
import type { ThreeEvent } from '@react-three/fiber'; import type { ThreeEvent } from '@react-three/fiber';
import type { Voxel, V3 } from '../../types'; import type { Voxel, V3 } from '../../types';
@ -12,7 +12,7 @@ type Props = {
onRemove: (position: V3) => void; onRemove: (position: V3) => void;
}; };
const FLOOR_Y = -0.5; const FLOOR_Y = 0;
function adjPosition(pos: V3, nx: number, ny: number, nz: number): V3 { function adjPosition(pos: V3, nx: number, ny: number, nz: number): V3 {
return [pos[0] + Math.round(nx), pos[1] + Math.round(ny), pos[2] + Math.round(nz)]; return [pos[0] + Math.round(nx), pos[1] + Math.round(ny), pos[2] + Math.round(nz)];
@ -22,12 +22,13 @@ function posEq(a: V3, b: V3) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
} }
const DRAG_THRESHOLD = 5;
export function VoxelEditorScene({ voxels, mode, color, typeId, onAdd, onRemove }: Props) { export function VoxelEditorScene({ voxels, mode, color, typeId, onAdd, onRemove }: Props) {
const [ghost, setGhost] = useState<V3 | null>(null); const [ghost, setGhost] = useState<V3 | null>(null);
const isDragging = useRef(false);
function handleVoxelClick(e: ThreeEvent<MouseEvent>, voxel: Voxel) { function handleVoxelClick(e: ThreeEvent<MouseEvent>, voxel: Voxel) {
if (isDragging.current) return; if (e.delta > DRAG_THRESHOLD) return;
e.stopPropagation(); e.stopPropagation();
if (mode === 'remove') { if (mode === 'remove') {
onRemove(voxel.position); onRemove(voxel.position);
@ -40,7 +41,8 @@ export function VoxelEditorScene({ voxels, mode, color, typeId, onAdd, onRemove
} }
} }
function handleVoxelMove(_e: ThreeEvent<PointerEvent>, voxel: Voxel) { function handleVoxelMove(e: ThreeEvent<PointerEvent>, voxel: Voxel) {
e.stopPropagation();
if (mode === 'remove') { if (mode === 'remove') {
setGhost(voxel.position); setGhost(voxel.position);
} else { } else {
@ -49,7 +51,7 @@ export function VoxelEditorScene({ voxels, mode, color, typeId, onAdd, onRemove
} }
function handleFloorClick(e: ThreeEvent<MouseEvent>) { function handleFloorClick(e: ThreeEvent<MouseEvent>) {
if (isDragging.current || mode !== 'add') return; if (e.delta > DRAG_THRESHOLD || mode !== 'add') return;
e.stopPropagation(); e.stopPropagation();
const pos: V3 = [Math.floor(e.point.x), 0, Math.floor(e.point.z)]; const pos: V3 = [Math.floor(e.point.x), 0, Math.floor(e.point.z)];
onAdd({ typeId, position: pos, color }); onAdd({ typeId, position: pos, color });
@ -67,11 +69,7 @@ export function VoxelEditorScene({ voxels, mode, color, typeId, onAdd, onRemove
<ambientLight intensity={0.7} /> <ambientLight intensity={0.7} />
<directionalLight position={[8, 12, 6]} intensity={1} castShadow /> <directionalLight position={[8, 12, 6]} intensity={1} castShadow />
<OrbitControls <OrbitControls makeDefault />
makeDefault
onStart={() => { isDragging.current = false; }}
onChange={() => { isDragging.current = true; }}
/>
{/* Floor plane */} {/* Floor plane */}
<mesh <mesh