selected object highlight

This commit is contained in:
azykov@mail.ru 2026-06-02 08:11:43 +03:00
parent 745ffe4f31
commit ca5dea5093
No known key found for this signature in database
2 changed files with 23 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import { observer } from "mobx-react-lite";
import type { ObjectInstance } from "../types"; import type { ObjectInstance } from "../types";
import { useRef } from "react"; import { useRef } from "react";
import type { Mesh } from "three"; import type { Mesh } from "three";
import { Edges } from "@react-three/drei";
import { state } from "../state"; import { state } from "../state";
type ObjectViewProps = { type ObjectViewProps = {
@ -15,14 +16,28 @@ export const ObjectView = observer(function ({ object }: ObjectViewProps) {
if (!objectType) if (!objectType)
return null; return null;
const isSelected = state.worldEditor.selectedObjectId === object.id;
const handleClick = (e: { stopPropagation: () => void }) => {
if (!state.worldEditor.isEnabled)
return;
e.stopPropagation();
state.worldEditor.setSelectedObjectId(object.id);
};
return (<> return (<>
<group position={object.position} rotation={object.rotation}> <group
position={object.position}
rotation={object.rotation}
onClick={handleClick}
>
{ {
objectType.voxels.map((v, idx) => { objectType.voxels.map((v, idx) => {
const vt = state.world.getVoxelTypeById(v.typeId); const vt = state.world.getVoxelTypeById(v.typeId);
return <mesh key={idx} ref={meshRef} position={v.position}> return <mesh key={idx} ref={meshRef} position={v.position}>
<boxGeometry args={[1, 1, 1]} /> <boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={(v.color ?? vt?.color) ?? 'white'} opacity={(v.opacity ?? vt?.opacity) ?? 1} /> <meshStandardMaterial color={(v.color ?? vt?.color) ?? 'white'} opacity={(v.opacity ?? vt?.opacity) ?? 1} />
{isSelected && <Edges color="white" lineWidth={3} stencilWrite={false} />}
</mesh> </mesh>
}) })
} }

View File

@ -3,11 +3,13 @@ import type { WorldState } from "./world";
import type { ObjectInstance } from "../types"; import type { ObjectInstance } from "../types";
import { createObjectInstance } from "../utils/object"; import { createObjectInstance } from "../utils/object";
import { randomId } from "../utils"; import { randomId } from "../utils";
import type { Pos3, V3 } from "../types/3d"; import type { Pos3 } from "../types/3d";
export class WorldEditorState { export class WorldEditorState {
private readonly world: WorldState; private readonly world: WorldState;
public selectedObjectId: string | undefined;
constructor(world: WorldState) { constructor(world: WorldState) {
this.world = world; this.world = world;
makeAutoObservable(this); makeAutoObservable(this);
@ -23,6 +25,10 @@ export class WorldEditorState {
this.world.save(); this.world.save();
} }
public setSelectedObjectId(value: string | undefined): void {
this.selectedObjectId = value;
}
public addObjectCloneAtRandomPosition(typeId: string): ObjectInstance { public addObjectCloneAtRandomPosition(typeId: string): ObjectInstance {
return this.addObjectClone( return this.addObjectClone(
typeId, typeId,