wip: play around with imposters

This commit is contained in:
max_richter 2024-03-19 17:39:25 +01:00
parent b55d3d4217
commit 70d7c57e46
4 changed files with 47 additions and 22 deletions

View File

@ -4,39 +4,60 @@
import NodeHeader from "./NodeHeader.svelte"; import NodeHeader from "./NodeHeader.svelte";
import NodeParameter from "./NodeParameter.svelte"; import NodeParameter from "./NodeParameter.svelte";
import { activeNodeId, selectedNodes } from "./graph/stores"; import { activeNodeId, selectedNodes } from "./graph/stores";
import { T } from "@threlte/core";
import type { Mesh } from "three";
export let node: Node; export let node: Node;
export let inView = true; export let inView = true;
export let z = 2;
const updateNodePosition = const updateNodePosition =
getContext<(n: Node) => void>("updateNodePosition"); getContext<(n: Node) => void>("updateNodePosition");
const getNodeHeight = getContext<(n: Node) => number>("getNodeHeight");
const type = node?.tmp?.type; const type = node?.tmp?.type;
const parameters = Object.entries(type?.inputs || {}); const parameters = Object.entries(type?.inputs || {});
let ref: HTMLDivElement; let ref: HTMLDivElement;
let meshRef: Mesh;
$: if (node && ref) { const height = getNodeHeight(node.type);
console.log(node.type, height);
$: if (node && ref && meshRef) {
node.tmp = node.tmp || {}; node.tmp = node.tmp || {};
node.tmp.ref = ref; node.tmp.ref = ref;
node.tmp.mesh = meshRef;
updateNodePosition(node); updateNodePosition(node);
} }
onMount(() => { onMount(() => {
if (ref) {
node.tmp = node.tmp || {}; node.tmp = node.tmp || {};
node.tmp.ref = ref; node.tmp.ref = ref;
node.tmp.mesh = meshRef;
updateNodePosition(node); updateNodePosition(node);
}
}); });
</script> </script>
<T.Mesh
position.x={node.position.x + 10}
position.z={node.position.y + height / 2}
position.y={0.8}
rotation.x={-Math.PI / 2}
bind:ref={meshRef}
visible={true}
>
<T.PlaneGeometry args={[20, height]} />
<T.MeshBasicMaterial color="#151515" />
</T.Mesh>
<div <div
class="node" class="node"
class:active={$activeNodeId === node.id} class:active={$activeNodeId === node.id}
class:selected={!!$selectedNodes?.has(node.id)} class:selected={!!$selectedNodes?.has(node.id)}
class:in-view={inView} class:out-of-view={!inView}
data-node-id={node.id} data-node-id={node.id}
bind:this={ref} bind:this={ref}
> >
@ -63,9 +84,10 @@
transform: translate3d(var(--nx), var(--ny), 0); transform: translate3d(var(--nx), var(--ny), 0);
z-index: 1; z-index: 1;
font-weight: 300; font-weight: 300;
display: none; display: var(--node-display, "block");
--stroke: var(--background-color-lighter); --stroke: var(--background-color-lighter);
--stroke-width: 2px; --stroke-width: 2px;
opacity: var(--input-opacity);
} }
.node.active { .node.active {
@ -78,7 +100,7 @@
--stroke-width: 1px; --stroke-width: 1px;
} }
.node.in-view { .node.out-of-view {
display: unset; display: none;
} }
</style> </style>

View File

@ -26,7 +26,7 @@
const edges = graph.edges; const edges = graph.edges;
let camera: OrthographicCamera; let camera: OrthographicCamera;
const minZoom = 2; const minZoom = 1;
const maxZoom = 40; const maxZoom = 40;
let mousePosition = [0, 0]; let mousePosition = [0, 0];
let mouseDown: null | [number, number] = null; let mouseDown: null | [number, number] = null;
@ -71,11 +71,12 @@
}; };
function updateNodePosition(node: NodeType) { function updateNodePosition(node: NodeType) {
node.tmp = node.tmp || {};
if (node?.tmp?.ref) { if (node?.tmp?.ref) {
if (node.tmp["x"] !== undefined && node.tmp["y"] !== undefined) { if (node.tmp["x"] !== undefined && node.tmp["y"] !== undefined) {
node.tmp.ref.style.setProperty("--nx", `${node.tmp.x * 10}px`); node.tmp.ref.style.setProperty("--nx", `${node.tmp.x * 10}px`);
node.tmp.ref.style.setProperty("--ny", `${node.tmp.y * 10}px`); node.tmp.ref.style.setProperty("--ny", `${node.tmp.y * 10}px`);
node.tmp.mesh.position.x = node.tmp.x + 10;
node.tmp.mesh.position.z = node.tmp.y + getNodeHeight(node.type) / 2;
if (node.tmp.x === node.position.x && node.tmp.y === node.position.y) { if (node.tmp.x === node.position.x && node.tmp.y === node.position.y) {
delete node.tmp.x; delete node.tmp.x;
delete node.tmp.y; delete node.tmp.y;
@ -83,6 +84,9 @@
} else { } else {
node.tmp.ref.style.setProperty("--nx", `${node.position.x * 10}px`); node.tmp.ref.style.setProperty("--nx", `${node.position.x * 10}px`);
node.tmp.ref.style.setProperty("--ny", `${node.position.y * 10}px`); node.tmp.ref.style.setProperty("--ny", `${node.position.y * 10}px`);
node.tmp.mesh.position.x = node.position.x + 10;
node.tmp.mesh.position.z =
node.position.y + getNodeHeight(node.type) / 2;
} }
} }
} }
@ -99,8 +103,10 @@
} }
const height = 5 + 10 * Object.keys(node.inputs).length; const height = 5 + 10 * Object.keys(node.inputs).length;
nodeHeightCache[nodeTypeId] = height; nodeHeightCache[nodeTypeId] = height;
console.log(node, height);
return height; return height;
} }
setContext("getNodeHeight", getNodeHeight);
setContext("isNodeInView", (node: NodeType) => { setContext("isNodeInView", (node: NodeType) => {
const height = getNodeHeight(node.type); const height = getNodeHeight(node.type);

View File

@ -58,10 +58,14 @@
class="wrapper" class="wrapper"
class:zoom-small={cameraPosition[2] < 2} class:zoom-small={cameraPosition[2] < 2}
class:hovering-sockets={activeSocket} class:hovering-sockets={activeSocket}
style={`--cz: ${cameraPosition[2]};`} style={`--cz: ${cameraPosition[2]}; --node-display: ${cameraPosition[2] < 2 ? "none" : "block"};`}
> >
{#each $nodes.values() as node (node.id)} {#each $nodes.values() as node (node.id)}
<Node {node} inView={cameraPosition && isNodeInView(node)} /> <Node
{node}
inView={cameraPosition && isNodeInView(node)}
z={cameraPosition[2]}
/>
{/each} {/each}
</div> </div>
</HTML> </HTML>

View File

@ -14,7 +14,7 @@
if (graph) { if (graph) {
graphManager.load(JSON.parse(graph)); graphManager.load(JSON.parse(graph));
} else { } else {
graphManager.load(graphManager.createTemplate("grid", 5, 5)); graphManager.load(graphManager.createTemplate("grid", 10, 10));
} }
graphManager.on("save", (graph) => { graphManager.on("save", (graph) => {
@ -54,13 +54,6 @@
</div> </div>
<style> <style>
.wrapper {
position: absolute;
z-index: 100;
top: 10px;
left: 10px;
}
#canvas-wrapper { #canvas-wrapper {
height: 100vh; height: 100vh;
} }