Layer inclined orbits in flow and even-ring lock spacing

This commit is contained in:
Tessera Agent
2026-11-09 11:18:30 +00:00
parent 32493b640e
commit 5b40536e37
2 changed files with 315 additions and 172 deletions

View File

@@ -1,10 +1,12 @@
import { useEffect, useMemo, useRef, useState } from "react";
import type { Orbit, Snapshot } from "../../api";
import {
flowMoonOffsets,
flowOrbitPhysics,
lockedSlot,
lerpPos,
moonPositions,
orbitArcs,
positionOnEllipse,
relaxLockedMoons,
} from "../../lib/galaxyLayout";
@@ -154,124 +156,200 @@ export function BlackholeGalaxy({
{starOrbits.map((orbit, i) => {
const phys = flowOrbitPhysics(i, starOrbits.length);
const arcs = orbitArcs(phys);
return (
<ellipse
key={`path-${orbit.id}`}
cx="0"
cy="0"
rx={phys.rx}
ry={phys.ry}
<path
key={`path-back-${orbit.id}`}
d={arcs.back}
fill="none"
stroke="#4a5f9e"
strokeOpacity={flowing ? 0.07 : 0.16}
strokeWidth="0.7"
strokeDasharray="3 10"
transform={`rotate(${phys.tiltDeg})`}
stroke="#6d84c4"
strokeOpacity={flowing ? 0.08 : 0.1}
strokeWidth={0.7}
strokeDasharray="2 10"
/>
);
})}
<StarLayer
which="back"
t={t}
blend={blend}
flowing={flowing}
starOrbits={starOrbits}
snapsByOrbit={snapsByOrbit}
lockLayout={lockLayout}
selectedSnapshotId={selectedSnapshotId}
showLabels={showLabels}
onSelectOrbit={onSelectOrbit}
onSelectSnapshot={onSelectSnapshot}
/>
<BlackHoleCore t={t} flowing={flowing} onToggle={onToggleFlow} />
{starOrbits.map((orbit, i) => {
const phys = flowOrbitPhysics(i, starOrbits.length);
const flowAngle = phys.phase + t * phys.speed;
const flowPos = positionOnEllipse(
flowAngle,
phys.rx,
phys.ry,
phys.tiltDeg,
);
const lockPos = lockedSlot(i, starOrbits.length);
const { x, y } = lerpPos(flowPos, lockPos, blend);
const moons = snapsByOrbit.get(orbit.name) ?? [];
const color = statusColor(orbit.last_status);
const starR = flowing ? STAR_R_FLOW : STAR_R_LOCK;
const interactive = !flowing || blend < 0.15;
const label = lockLayout.labels[i];
const flowMoons = moonPositions(
x,
y,
moons.length,
t,
true,
36,
);
const lockMoons = lockLayout.moons[i] ?? [];
const arcs = orbitArcs(phys);
return (
<g key={orbit.id}>
{!flowing && (
<circle
cx={x}
cy={y}
r={HIT_R_LOCK}
fill="transparent"
className="cursor-pointer"
onClick={(e) => {
e.stopPropagation();
onSelectOrbit(orbit.id);
}}
/>
)}
<StarBody
id={orbit.id}
x={x}
y={y}
r={starR}
color={color}
locked={!flowing}
interactive={interactive}
onClick={(e) => {
e.stopPropagation();
if (!flowing) onSelectOrbit(orbit.id);
}}
/>
{moons.map((moon, mi) => {
const flowMp = flowMoons[mi];
const lockMp = lockMoons[mi];
const mp = lockMp
? lerpPos(
{ x: flowMp.x, y: flowMp.y },
lockMp,
blend,
)
: flowMp;
const sel = moon.id === selectedSnapshotId;
const mr = flowing ? 3.8 : sel ? 7.5 : 6;
const hit = flowing ? mr : MOON_HIT_LOCK;
return (
<MoonBody
key={moon.id}
x={mp.x}
y={mp.y}
r={mr}
hitR={hit}
rim={color}
selected={sel}
locked={!flowing}
onClick={(e) => {
e.stopPropagation();
if (!flowing) onSelectSnapshot(moon.id);
}}
/>
);
})}
{showLabels && (
<LockLabel
x={label.x}
y={label.y}
text={shortenOrbitName(orbit.name)}
/>
)}
</g>
<path
key={`path-front-${orbit.id}`}
d={arcs.front}
fill="none"
stroke="#9aafdf"
strokeOpacity={flowing ? 0.2 : 0.12}
strokeWidth={0.9}
strokeDasharray="2 8"
/>
);
})}
<StarLayer
which="front"
t={t}
blend={blend}
flowing={flowing}
starOrbits={starOrbits}
snapsByOrbit={snapsByOrbit}
lockLayout={lockLayout}
selectedSnapshotId={selectedSnapshotId}
showLabels={showLabels}
onSelectOrbit={onSelectOrbit}
onSelectSnapshot={onSelectSnapshot}
/>
</svg>
</div>
);
}
function StarLayer({
which,
t,
blend,
flowing,
starOrbits,
snapsByOrbit,
lockLayout,
selectedSnapshotId,
showLabels,
onSelectOrbit,
onSelectSnapshot,
}: {
which: "back" | "front";
t: number;
blend: number;
flowing: boolean;
starOrbits: Orbit[];
snapsByOrbit: Map<string, Snapshot[]>;
lockLayout: {
moons: { x: number; y: number }[][];
labels: { x: number; y: number }[];
};
selectedSnapshotId?: string | null;
showLabels: boolean;
onSelectOrbit: (id: string) => void;
onSelectSnapshot: (id: string) => void;
}) {
const drawn = starOrbits
.map((orbit, i) => {
const phys = flowOrbitPhysics(i, starOrbits.length);
const pose = positionOnEllipse(phys.phase + t * phys.speed, phys);
const depth = pose.z * blend;
const inFront = blend < 0.35 || depth >= 0;
return { orbit, i, pose, depth, inFront };
})
.filter((row) => (which === "front" ? row.inFront : !row.inFront))
.sort((a, b) => a.depth - b.depth);
return drawn.map(({ orbit, i, pose }) => {
const lockPos = lockedSlot(i, starOrbits.length);
const { x, y } = lerpPos(pose, lockPos, blend);
const moons = snapsByOrbit.get(orbit.name) ?? [];
const color = statusColor(orbit.last_status);
const depthScale = pose.scale * blend + (1 - blend);
const starR = (flowing ? STAR_R_FLOW : STAR_R_LOCK) * depthScale;
const dim = 0.55 + ((pose.z + 1) / 2) * 0.45;
const opacity = dim * blend + (1 - blend);
const interactive = !flowing || blend < 0.15;
const label = lockLayout.labels[i];
const flowOff = flowMoonOffsets(moons.length, t, 32);
const lockMoons = lockLayout.moons[i] ?? [];
const moonNodes = moons.map((moon, mi) => {
const off = flowOff[mi];
const lockMp = lockMoons[mi];
const flowMp = { x: x + off.x * depthScale, y: y + off.y * depthScale };
const mp = lockMp ? lerpPos(flowMp, lockMp, blend) : flowMp;
const localZ = off.z;
return { moon, mp, localZ, off };
});
const moonsBehind = moonNodes.filter((m) => m.localZ < 0 || blend < 0.5);
const moonsAhead = blend < 0.5 ? [] : moonNodes.filter((m) => m.localZ >= 0);
const paintMoon = (node: (typeof moonNodes)[number]) => {
const sel = node.moon.id === selectedSnapshotId;
const mr =
((flowing ? 3.8 : sel ? 7.5 : 6) *
(blend < 0.5 ? 1 : node.off.scale) *
depthScale);
return (
<MoonBody
key={node.moon.id}
x={node.mp.x}
y={node.mp.y}
r={mr}
hitR={flowing ? mr : MOON_HIT_LOCK}
rim={color}
selected={sel}
locked={!flowing}
onClick={(e) => {
e.stopPropagation();
if (!flowing) onSelectSnapshot(node.moon.id);
}}
/>
);
};
return (
<g key={orbit.id} opacity={opacity}>
{moonsBehind.map(paintMoon)}
{!flowing && (
<circle
cx={x}
cy={y}
r={HIT_R_LOCK}
fill="transparent"
className="cursor-pointer"
onClick={(e) => {
e.stopPropagation();
onSelectOrbit(orbit.id);
}}
/>
)}
<StarBody
id={orbit.id}
x={x}
y={y}
r={starR}
color={color}
locked={!flowing}
interactive={interactive}
onClick={(e) => {
e.stopPropagation();
if (!flowing) onSelectOrbit(orbit.id);
}}
/>
{moonsAhead.map(paintMoon)}
{showLabels && which === "front" && label && (
<LockLabel
x={label.x}
y={label.y}
text={shortenOrbitName(orbit.name)}
/>
)}
</g>
);
});
}
function StarBody({
id,
x,
@@ -357,6 +435,7 @@ function MoonBody({
rim,
selected,
locked,
opacity = 1,
onClick,
}: {
x: number;
@@ -366,10 +445,15 @@ function MoonBody({
rim: string;
selected: boolean;
locked: boolean;
opacity?: number;
onClick: (e: React.MouseEvent<SVGGElement>) => void;
}) {
return (
<g className={locked ? "cursor-pointer" : "pointer-events-none"} onClick={onClick}>
<g
opacity={opacity}
className={locked ? "cursor-pointer" : "pointer-events-none"}
onClick={onClick}
>
{locked && (
<circle cx={x} cy={y} r={hitR} fill="transparent" />
)}

View File

@@ -3,65 +3,74 @@
export type OrbitPhysics = {
rx: number;
ry: number;
tiltDeg: number;
nodeDeg: number;
inclination: number;
speed: number;
phase: number;
layer: number;
};
export type Vec2 = { x: number; y: number };
const GOLDEN = 2.399963229728653;
export type Pose3 = Vec2 & { z: number; scale: number };
const VIEW_R = 258;
/** Per-star ellipses from inner rim to ~90% of viewport radius. */
/** Inclined shells: each star has its own node, tilt, and radius. */
export function flowOrbitPhysics(index: number, total: number): OrbitPhysics {
const n = Math.max(total, 1);
const layer = index % 3;
const lane = index / Math.max(n - 1, 1);
const band = index % 5;
const rx =
92 +
lane * (VIEW_R - 92) +
band * 7 +
((index * 11) % 5) * 4;
const ry =
68 +
((index * 13) % 9) * 14 +
lane * 72 +
(index % 3) * 9;
const tiltDeg = -42 + ((index * 23 + band * 17) % 84);
const rx = Math.min(108 + lane * (VIEW_R - 108), VIEW_R);
return {
rx: Math.min(rx, VIEW_R),
ry: Math.min(ry, VIEW_R * 0.88),
tiltDeg,
speed: 0.14 + (index % 9) * 0.018 + band * 0.008,
phase: (index / n) * Math.PI * 2 + index * GOLDEN * 0.15,
rx,
ry: rx * (0.96 - layer * 0.03),
nodeDeg: (index * 53) % 360,
inclination: 0.42 + layer * 0.32,
speed: 0.18 + layer * 0.06 + (index % 5) * 0.015,
phase: (index / n) * Math.PI * 2 + index * 0.35,
layer,
};
}
export function positionOnEllipse(
angle: number,
rx: number,
ry: number,
tiltDeg: number,
): Vec2 {
const ex = Math.cos(angle) * rx;
const ey = Math.sin(angle) * ry;
const rad = (tiltDeg * Math.PI) / 180;
/** Orbital plane → screen, with line-of-sight depth for scale and paint order. */
export function positionOnEllipse(angle: number, phys: OrbitPhysics): Pose3 {
const lx = Math.cos(angle) * phys.rx;
const ly = Math.sin(angle) * phys.ry;
const yInc = ly * Math.cos(phys.inclination);
const zRaw = ly * Math.sin(phys.inclination);
const node = (phys.nodeDeg * Math.PI) / 180;
const x = lx * Math.cos(node) - yInc * Math.sin(node);
const y = lx * Math.sin(node) + yInc * Math.cos(node);
const depth = zRaw / Math.max(phys.rx, 1);
return {
x: ex * Math.cos(rad) - ey * Math.sin(rad),
y: ex * Math.sin(rad) + ey * Math.cos(rad),
x,
y,
z: depth,
scale: 0.7 + (depth + 1) * 0.3,
};
}
function slotSeed(index: number, total: number): Vec2 {
const angle = -Math.PI / 2 + index * GOLDEN;
const band = index % 4;
const radius = 118 + band * 46 + Math.floor(index / 4) * 10;
const squash = 0.9 + (index % 3) * 0.05;
return {
x: Math.cos(angle) * Math.min(radius, VIEW_R),
y: Math.sin(angle) * Math.min(radius, VIEW_R) * squash,
};
/** Split an inclined orbit into the arc behind the vault and the arc in front. */
export function orbitArcs(
phys: OrbitPhysics,
samples = 80,
): { back: string; front: string } {
let back = "";
let front = "";
let prev: boolean | null = null;
for (let i = 0; i <= samples; i++) {
const p = positionOnEllipse((i / samples) * Math.PI * 2, phys);
const near = p.z >= 0;
const cmd = `${p.x.toFixed(1)} ${p.y.toFixed(1)}`;
if (near) {
front += prev === true ? `L ${cmd} ` : `M ${cmd} `;
} else {
back += prev === false ? `L ${cmd} ` : `M ${cmd} `;
}
prev = near;
}
return { back, front };
}
function dist(a: Vec2, b: Vec2): number {
@@ -70,45 +79,68 @@ function dist(a: Vec2, b: Vec2): number {
return Math.hypot(dx, dy);
}
const LAYOUT_VER = 2;
const LAYOUT_VER = 3;
const layoutCache = new Map<string, Vec2[]>();
/** Golden-angle seeds relaxed for minimum star separation (lock mode). */
/**
* Lock layout: concentric rings, counts proportional to radius so
* neighbor chord length stays nearly constant on every ring.
*/
export function lockedPositions(total: number): Vec2[] {
const key = `${LAYOUT_VER}:${total}`;
const cached = layoutCache.get(key);
if (cached) return cached;
const pts = Array.from({ length: total }, (_, i) => slotSeed(i, total));
const minSep = total > 12 ? 56 : 62;
const pts: Vec2[] = [];
if (total <= 0) {
layoutCache.set(key, pts);
return pts;
}
for (let iter = 0; iter < 56; iter++) {
const inner = 124;
const outer = 246;
if (total <= 7) {
const radius = (inner + outer) / 2;
for (let i = 0; i < total; i++) {
for (let j = i + 1; j < total; j++) {
const d = dist(pts[i], pts[j]);
if (d < minSep && d > 0.001) {
const push = (minSep - d) * 0.55;
const ux = (pts[i].x - pts[j].x) / d;
const uy = (pts[i].y - pts[j].y) / d;
pts[i].x += ux * push;
pts[i].y += uy * push;
pts[j].x -= ux * push;
pts[j].y -= uy * push;
}
}
const r = dist(pts[i], { x: 0, y: 0 });
const vaultPad = 96;
if (r < vaultPad && r > 0.001) {
const s = vaultPad / r;
pts[i].x *= s;
pts[i].y *= s;
}
const edge = dist(pts[i], { x: 0, y: 0 });
if (edge > VIEW_R) {
const s = VIEW_R / edge;
pts[i].x *= s;
pts[i].y *= s;
}
const a = -Math.PI / 2 + (i / total) * Math.PI * 2;
pts.push({ x: Math.cos(a) * radius, y: Math.sin(a) * radius });
}
layoutCache.set(key, pts);
return pts;
}
const rings = total <= 14 ? 2 : 3;
const radii = Array.from({ length: rings }, (_, i) =>
rings === 1 ? (inner + outer) / 2 : inner + ((outer - inner) * i) / (rings - 1),
);
const wsum = radii.reduce((a, b) => a + b, 0);
const counts = radii.map((r) => Math.max(1, Math.round((r / wsum) * total)));
let sum = counts.reduce((a, b) => a + b, 0);
while (sum > total) {
let idx = 0;
for (let i = 1; i < counts.length; i++) {
if (counts[i] > counts[idx]) idx = i;
}
if (counts[idx] <= 1) break;
counts[idx] -= 1;
sum -= 1;
}
while (sum < total) {
let idx = 0;
for (let i = 1; i < counts.length; i++) {
if (counts[i] < counts[idx]) idx = i;
}
counts[idx] += 1;
sum += 1;
}
for (let ri = 0; ri < rings; ri++) {
const count = counts[ri];
const radius = radii[ri];
const twist = (ri % 2) * (Math.PI / count);
for (let i = 0; i < count; i++) {
const a = -Math.PI / 2 + twist + (i / count) * Math.PI * 2;
pts.push({ x: Math.cos(a) * radius, y: Math.sin(a) * radius });
}
}
@@ -117,7 +149,34 @@ export function lockedPositions(total: number): Vec2[] {
}
export function lockedSlot(index: number, total: number): Vec2 {
return lockedPositions(total)[index] ?? slotSeed(index, total);
const pts = lockedPositions(total);
return pts[index] ?? { x: 0, y: 0 };
}
/** Small inclined moon orbits around a parent, in parent-local space. */
export function flowMoonOffsets(
count: number,
time: number,
spread = 30,
): Pose3[] {
const out: Pose3[] = [];
const inc = 0.85;
for (let mi = 0; mi < count; mi++) {
const ang =
(mi / Math.max(count, 1)) * Math.PI * 2 + time * (1.05 + mi * 0.08);
const rad = spread * (0.82 + (mi % 3) * 0.1);
const lx = Math.cos(ang) * rad;
const ly = Math.sin(ang) * rad;
const y = ly * Math.cos(inc);
const z = Math.sin(ang) * Math.sin(inc);
out.push({
x: lx,
y,
z,
scale: 0.82 + (z + 1) * 0.12,
});
}
return out;
}
export function moonPositions(