three follows world camera now

This commit is contained in:
azykov@mail.ru 2026-06-02 14:31:37 +03:00
parent 765412f9a9
commit e520eb07c5
No known key found for this signature in database
2 changed files with 20 additions and 2 deletions

View File

@ -1,11 +1,24 @@
import { useRef } from 'react'; import { useLayoutEffect, useRef } from 'react';
import type React from 'react'; import type React from 'react';
import { useFrame } from '@react-three/fiber'; import { useFrame, useThree } from '@react-three/fiber';
import { OrbitControls } from '@react-three/drei'; import { OrbitControls } from '@react-three/drei';
import { observer } from 'mobx-react-lite'; import { observer } from 'mobx-react-lite';
import { state } from '../state'; import { state } from '../state';
import { SceneView } from './SceneView'; import { SceneView } from './SceneView';
const CameraSync = observer(function () {
const { camera } = useThree();
const cam = state.world.currentCamera; // MobX tracks this; re-renders on change
useLayoutEffect(() => {
camera.position.set(cam.position[0], cam.position[1], cam.position[2]);
camera.rotation.set(cam.look[0], cam.look[1], cam.look[2]);
camera.updateProjectionMatrix();
});
return null;
});
export const WorldView = observer(function () { export const WorldView = observer(function () {
const world = state.world; const world = state.world;
const controlsRef = useRef<React.ComponentRef<typeof OrbitControls>>(null); const controlsRef = useRef<React.ComponentRef<typeof OrbitControls>>(null);
@ -33,6 +46,7 @@ export const WorldView = observer(function () {
makeDefault makeDefault
enableDamping={false} enableDamping={false}
/> />
<CameraSync />
<SceneView scene={state.world.currentScene} /> <SceneView scene={state.world.currentScene} />
</>) </>)
}); });

View File

@ -62,6 +62,10 @@ export class WorldEditorState {
return this.world.data.initialScene; return this.world.data.initialScene;
} }
public get camera(): Pos3 {
return this.world.data.editorCamera;
}
public mutateWorld(mutation: Partial<World>): void { public mutateWorld(mutation: Partial<World>): void {
this.world.data = { this.world.data = {
...this.world.data, ...this.world.data,