feat: save camera location to localStorage

This commit is contained in:
max_richter 2024-04-23 15:41:07 +02:00
parent 00776b92d3
commit 3af7ebb672
2 changed files with 42 additions and 5 deletions

View File

@ -1,13 +1,34 @@
<script lang="ts"> <script lang="ts">
import { T } from "@threlte/core"; import { T } from "@threlte/core";
import { MeshLineGeometry, MeshLineMaterial, Text } from "@threlte/extras"; import { MeshLineGeometry, MeshLineMaterial, Text } from "@threlte/extras";
import type { BufferGeometry, Vector3 } from "three"; import type { BufferGeometry, PerspectiveCamera, Vector3 } from "three";
import type { OrbitControls as OrbitControlsType } from "three/addons/controls/OrbitControls.js";
import { OrbitControls } from "@threlte/extras"; import { OrbitControls } from "@threlte/extras";
import { AppSettings } from "../settings/app-settings"; import { AppSettings } from "../settings/app-settings";
import localStore from "$lib/helpers/localStore";
import { onMount } from "svelte";
export let geometries: BufferGeometry[]; export let geometries: BufferGeometry[];
export let lines: Vector3[][]; export let lines: Vector3[][];
$: console.log({ geometries, lines });
let camera: PerspectiveCamera;
let controls: OrbitControlsType;
const cameraTransform = localStore<{ camera: number[]; target: number[] }>(
"nodes.camera.transform",
{
camera: [0, 0, 10],
target: [0, 0, 0],
},
);
function saveCameraState() {
if (!camera) return;
$cameraTransform = {
camera: camera.position.toArray(),
target: controls.target.toArray(),
};
}
function getPosition(geo: BufferGeometry, i: number) { function getPosition(geo: BufferGeometry, i: number) {
const pos = [ const pos = [
@ -23,8 +44,17 @@
<T.GridHelper args={[20, 20]} /> <T.GridHelper args={[20, 20]} />
{/if} {/if}
<T.PerspectiveCamera position={[-10, 10, 10]} makeDefault fov={50}> <T.PerspectiveCamera
<OrbitControls /> bind:ref={camera}
position={$cameraTransform.camera}
makeDefault
fov={50}
>
<OrbitControls
bind:ref={controls}
on:change={saveCameraState}
target={$cameraTransform.target}
/>
</T.PerspectiveCamera> </T.PerspectiveCamera>
<T.DirectionalLight position={[0, 10, 10]} /> <T.DirectionalLight position={[0, 10, 10]} />
@ -37,7 +67,9 @@
<Text text={i / 3} fontSize={0.25} position={getPosition(geo, i)} /> <Text text={i / 3} fontSize={0.25} position={getPosition(geo, i)} />
{/if} {/if}
{/each} {/each}
{/if}
{#if $AppSettings.showVertices}
<T.Points visible={true}> <T.Points visible={true}>
<T is={geo} /> <T is={geo} />
<T.PointsMaterial size={0.25} /> <T.PointsMaterial size={0.25} />
@ -54,7 +86,7 @@
{#if $AppSettings.showStemLines && lines} {#if $AppSettings.showStemLines && lines}
{#each lines as line} {#each lines as line}
<T.Mesh scale={2}> <T.Mesh>
<MeshLineGeometry points={line} /> <MeshLineGeometry points={line} />
<MeshLineMaterial width={0.1} color="red" depthTest={false} /> <MeshLineMaterial width={0.1} color="red" depthTest={false} />
</T.Mesh> </T.Mesh>

View File

@ -58,6 +58,11 @@ export const AppSettingTypes = {
label: "Show Indices", label: "Show Indices",
value: false, value: false,
}, },
showVertices: {
type: "boolean",
label: "Show Vertices",
value: false,
},
showStemLines: { showStemLines: {
type: "boolean", type: "boolean",
label: "Show Stem Lines", label: "Show Stem Lines",