mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-16 06:42:14 +00:00
hero: cards size to their neighbors — packed, collision-free, offscreen death
The galaxy's sizing moves from fixed birth-to-full scaling to the real ask: every frame each card takes a tunable density fraction of the exact axis-aligned gap to its nearest neighbor (O(N²) over ~20 cards), so the field packs as tight as the Density knob allows and can never overlap — growth is eased so freed space fills organically, shrinking is instant so the guarantee never breaks. Wobble is now collision-safe (sizing measures wobbled positions) but defaults to 0. Cards travel past the viewport corner before wrapping, so the death is never on screen; the exit fade is gone (birth fade stays) and the arm linework extends past the exit. Corners get a tunable radius via a rounded-rect SDF in the card shader, with the hairline frame drawn as a band riding the same edge (replacing the rectangular EdgesGeometry frames). Clip playback start points are staggered across each clip's duration so loop resets never line up across the wall. Claude-Session: https://claude.ai/code/session_01YBoaBEt3gynzTwogBFtn4B
This commit is contained in:
1 parent
9644886ed7
commit
0d32304be7
2 files changed
+129
-84
No files matched your search
+27
-19
@@ -27,12 +27,12 @@ export const galaxyLayoutTuner = defineTunables(
|
||||
info: "How many revolutions each arm makes from origin to outer end — lower is straighter rays, higher is a tighter swirl.",
|
||||
},
|
||||
rMaxFrac: {
|
||||
label: "Outer radius",
|
||||
label: "Track pitch",
|
||||
min: 0.4,
|
||||
max: 1.2,
|
||||
step: 0.02,
|
||||
default: 0.72,
|
||||
info: "Radius where cards exit, as a fraction of the viewport half-diagonal — above ~0.7 the outermost cards leave through the corners.",
|
||||
info: "Sets the spiral's tightness: the radius the drawn windings span, as a fraction of the viewport half-diagonal. Larger = wider winding gaps = bigger cards. Cards travel past it until fully offscreen.",
|
||||
},
|
||||
birthFrac: {
|
||||
label: "Birth radius",
|
||||
@@ -51,20 +51,12 @@ export const galaxyLayoutTuner = defineTunables(
|
||||
info: "Total cards riding the arms at once.",
|
||||
},
|
||||
cardHFrac: {
|
||||
label: "Card h × vh",
|
||||
label: "Card h cap",
|
||||
min: 0.08,
|
||||
max: 0.3,
|
||||
max: 0.35,
|
||||
step: 0.005,
|
||||
default: 0.16,
|
||||
info: "Full-grown card height as a fraction of the viewport height (cards are 16:9).",
|
||||
},
|
||||
minScale: {
|
||||
label: "Birth scale",
|
||||
min: 0.15,
|
||||
max: 1,
|
||||
step: 0.01,
|
||||
default: 0.32,
|
||||
info: "Card scale at birth near the center; cards grow to full size as they swirl outward.",
|
||||
default: 0.2,
|
||||
info: "Absolute card height ceiling as a fraction of the viewport height — the track-gap math sizes cards below this so they never collide.",
|
||||
},
|
||||
armJitter: {
|
||||
label: "Arm desync",
|
||||
@@ -127,8 +119,8 @@ export const galaxyMotionTuner = defineTunables(
|
||||
min: 0,
|
||||
max: 40,
|
||||
step: 1,
|
||||
default: 10,
|
||||
info: "How far cards drift off their exact arm position — the floating-in-space noise.",
|
||||
default: 0,
|
||||
info: "How far cards drift off their exact arm position. Off by default — the layout owes its collision guarantee to cards staying on track; a fallback flavor knob only.",
|
||||
},
|
||||
wobbleFreq: {
|
||||
label: "Wobble Hz",
|
||||
@@ -167,6 +159,22 @@ export const galaxyMotionTuner = defineTunables(
|
||||
|
||||
// Ink and lens knobs — read per frame.
|
||||
export const galaxyLookTuner = defineTunables("galaxyLook", "Galaxy look", {
|
||||
density: {
|
||||
label: "Density",
|
||||
min: 0.3,
|
||||
max: 1,
|
||||
step: 0.02,
|
||||
default: 0.82,
|
||||
info: "Fraction of the gap to its nearest neighbor each card may fill — higher packs the screen tighter, lower gives more air. Collision-free across the whole range (at 1.0 cards may kiss edges).",
|
||||
},
|
||||
cornerPx: {
|
||||
label: "Corner px",
|
||||
min: 0,
|
||||
max: 24,
|
||||
step: 1,
|
||||
default: 10,
|
||||
info: "Corner radius of each card (and its hairline frame), in px — the one deliberate exception to the site's zero-radius rule.",
|
||||
},
|
||||
blurMax: {
|
||||
label: "Blur max",
|
||||
min: 0,
|
||||
@@ -200,12 +208,12 @@ export const galaxyLookTuner = defineTunables("galaxyLook", "Galaxy look", {
|
||||
info: "Card presence at birth — newborn cards sit faint near the origin and solidify as they emerge.",
|
||||
},
|
||||
fadeBand: {
|
||||
label: "Fade band",
|
||||
label: "Birth fade",
|
||||
min: 0.02,
|
||||
max: 0.3,
|
||||
step: 0.01,
|
||||
default: 0.09,
|
||||
info: "Fraction of the journey over which cards fade in at birth and out at the edge — also hides the conveyor wrap.",
|
||||
info: "Fraction of the journey over which a newborn card fades in at the center. There is no exit fade — cards die fully offscreen.",
|
||||
},
|
||||
dim: {
|
||||
label: "Footage dim",
|
||||
@@ -237,7 +245,7 @@ export const galaxyLookTuner = defineTunables("galaxyLook", "Galaxy look", {
|
||||
max: 1,
|
||||
step: 0.02,
|
||||
default: 0.35,
|
||||
info: "Opacity of the hairline frames around each card.",
|
||||
info: "Strength of the hairline frame drawn along each card's (rounded) edge.",
|
||||
},
|
||||
lineAlpha: {
|
||||
label: "Line alpha",
|
||||
|
||||
@@ -72,6 +72,10 @@ const CARD_FRAG = /* glsl */ `
|
||||
uniform float uDim;
|
||||
uniform float uAlpha;
|
||||
uniform float uAber;
|
||||
uniform vec2 uSize;
|
||||
uniform float uRadius;
|
||||
uniform vec3 uFrameCol;
|
||||
uniform float uFrameA;
|
||||
in vec2 vUv;
|
||||
out vec4 outColor;
|
||||
|
||||
@@ -110,7 +114,16 @@ const CARD_FRAG = /* glsl */ `
|
||||
);
|
||||
}
|
||||
col = mix(uBg, col, uTexA) * uDim;
|
||||
outColor = vec4(col, uAlpha);
|
||||
|
||||
// Rounded-rect SDF in card px: antialiased corner cut, plus the
|
||||
// hairline frame drawn as a ~1px band riding the same edge (so it
|
||||
// follows the radius exactly).
|
||||
vec2 q = abs((vUv - 0.5) * uSize) - (0.5 * uSize - vec2(uRadius));
|
||||
float dEdge = length(max(q, vec2(0.0))) + min(max(q.x, q.y), 0.0) - uRadius;
|
||||
float frameBand = 1.0 - smoothstep(0.5, 1.5, abs(dEdge + 1.0));
|
||||
col = mix(col, uFrameCol, frameBand * uFrameA);
|
||||
float aa = 1.0 - smoothstep(-0.75, 0.75, dEdge);
|
||||
outColor = vec4(col, uAlpha * aa);
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -272,13 +285,16 @@ function GalaxyScene({
|
||||
v.load();
|
||||
}
|
||||
});
|
||||
// Prime a frozen first frame per clip, staggered so page load never
|
||||
// fights a burst of range requests. A paused seek decodes one frame —
|
||||
// exactly what the inner blurred cards need, no decoder kept.
|
||||
// Prime a frozen frame per clip, staggered so page load never fights a
|
||||
// burst of range requests. The seek target is a per-clip offset spread
|
||||
// across each clip's duration, so playback phases — and therefore loop
|
||||
// resets — never line up across the wall (most clips share a length).
|
||||
const timers = videos.map((v, i) =>
|
||||
setTimeout(() => {
|
||||
const seek = () => {
|
||||
if (v.paused && v.currentTime === 0) v.currentTime = 0.1;
|
||||
if (!v.paused || v.currentTime !== 0) return;
|
||||
const usable = Math.max(0.2, (v.duration || 8) - 0.5);
|
||||
v.currentTime = 0.1 + ((i * 2.618) % usable);
|
||||
};
|
||||
if (v.readyState >= 1) seek();
|
||||
else v.addEventListener("loadedmetadata", seek, { once: true });
|
||||
@@ -331,21 +347,25 @@ function GalaxyScene({
|
||||
const t = galaxyLayoutTuner.read();
|
||||
const arms = Math.max(1, Math.round(t.arms));
|
||||
const cardN = Math.round(t.cardN);
|
||||
const rMax = (t.rMaxFrac * Math.hypot(size.width, size.height)) / 2;
|
||||
const halfDiag = Math.hypot(size.width, size.height) / 2;
|
||||
const rMax = t.rMaxFrac * halfDiag;
|
||||
const thetaMax = t.turns * Math.PI * 2;
|
||||
const b = rMax / thetaMax;
|
||||
const cardH = Math.min(320, Math.max(70, size.height * t.cardHFrac));
|
||||
const cardHCap = Math.min(340, Math.max(70, size.height * t.cardHFrac));
|
||||
const slotsPerArm = Math.max(1, Math.ceil(cardN / arms));
|
||||
|
||||
// Arm guide curves.
|
||||
const thetaBirth = t.birthFrac * thetaMax;
|
||||
// Cards travel until fully past the viewport corner (plus a card of
|
||||
// margin) before wrapping — the death is never on screen.
|
||||
const thetaExit = (halfDiag + cardHCap * 1.5) / b;
|
||||
// Arm guide curves, drawn out past the exit so cards never outrun the
|
||||
// linework.
|
||||
const armGeoms: THREE.BufferGeometry[] = [];
|
||||
for (let j = 0; j < arms; j++) {
|
||||
const phase = (j * Math.PI * 2) / arms;
|
||||
const pts: number[] = [];
|
||||
const steps = 140;
|
||||
const steps = 160;
|
||||
for (let k = 0; k <= steps; k++) {
|
||||
const theta = t.birthFrac * thetaMax * 0.3 + (k / steps) * thetaMax;
|
||||
if (theta > thetaMax) break;
|
||||
const theta = thetaBirth * 0.3 + (k / steps) * (thetaExit - thetaBirth * 0.3);
|
||||
const r = b * theta;
|
||||
pts.push(r * Math.cos(theta + phase), r * Math.sin(theta + phase), -2);
|
||||
}
|
||||
@@ -401,11 +421,10 @@ function GalaxyScene({
|
||||
cardN,
|
||||
rMax,
|
||||
thetaMax,
|
||||
thetaBirth: t.birthFrac * thetaMax,
|
||||
thetaBirth,
|
||||
thetaExit,
|
||||
b,
|
||||
cardH,
|
||||
cardW: (cardH * 16) / 9,
|
||||
minScale: t.minScale,
|
||||
cardHCap,
|
||||
armJitter: t.armJitter,
|
||||
slotsPerArm,
|
||||
armGeoms,
|
||||
@@ -456,29 +475,16 @@ function GalaxyScene({
|
||||
uAlpha: { value: 0 },
|
||||
uAber: { value: 0 },
|
||||
uCurve: { value: 0 },
|
||||
uSize: { value: new THREE.Vector2(160, 90) },
|
||||
uRadius: { value: 0 },
|
||||
uFrameCol: { value: new THREE.Vector3(0.5, 0.5, 0.5) },
|
||||
uFrameA: { value: 0 },
|
||||
},
|
||||
}),
|
||||
),
|
||||
[cards, textures, clipFit],
|
||||
);
|
||||
const frameMaterials = useMemo(
|
||||
() =>
|
||||
cards.map(
|
||||
() =>
|
||||
new THREE.LineBasicMaterial({
|
||||
transparent: true,
|
||||
depthTest: false,
|
||||
depthWrite: false,
|
||||
opacity: 0,
|
||||
}),
|
||||
),
|
||||
[cards],
|
||||
);
|
||||
useEffect(() => () => materials.forEach((m) => m.dispose()), [materials]);
|
||||
useEffect(
|
||||
() => () => frameMaterials.forEach((m) => m.dispose()),
|
||||
[frameMaterials],
|
||||
);
|
||||
|
||||
// Underlay materials (shared across arms/ticks/circle instances).
|
||||
const lineMat = useMemo(
|
||||
@@ -511,28 +517,18 @@ function GalaxyScene({
|
||||
// would churn every mesh for nothing.
|
||||
useEffect(() => {
|
||||
const bg = hexToVec3(colors.bg);
|
||||
const frame = hexToVec3(colors.lineStrong);
|
||||
materials.forEach((m) => {
|
||||
(m.uniforms.uBg.value as THREE.Vector3).copy(bg);
|
||||
(m.uniforms.uFrameCol.value as THREE.Vector3).copy(frame);
|
||||
});
|
||||
frameMaterials.forEach((m) => m.color.set(colors.lineStrong));
|
||||
lineMat.color.set(colors.lineStrong);
|
||||
tickMat.color.set(colors.lineStrong);
|
||||
}, [colors, materials, frameMaterials, lineMat, tickMat]);
|
||||
}, [colors, materials, lineMat, tickMat]);
|
||||
|
||||
// Shared unit geometries: the card plane (segmented for the curve warp)
|
||||
// and its hairline frame, both scaled per mesh.
|
||||
// Shared unit card plane, segmented for the curve warp and scaled per mesh.
|
||||
const unitPlane = useMemo(() => new THREE.PlaneGeometry(1, 1, 12, 12), []);
|
||||
const unitFrame = useMemo(
|
||||
() => new THREE.EdgesGeometry(new THREE.PlaneGeometry(1, 1)),
|
||||
[],
|
||||
);
|
||||
useEffect(
|
||||
() => () => {
|
||||
unitPlane.dispose();
|
||||
unitFrame.dispose();
|
||||
},
|
||||
[unitPlane, unitFrame],
|
||||
);
|
||||
useEffect(() => () => unitPlane.dispose(), [unitPlane]);
|
||||
|
||||
// Underlay objects: arm curves as THREE.Line (r3f has no line intrinsic —
|
||||
// it collides with the SVG element), ticks and circle as segments.
|
||||
@@ -545,6 +541,13 @@ function GalaxyScene({
|
||||
[layout, lineMat, tickMat],
|
||||
);
|
||||
|
||||
// Per-card scratch state reused every frame so the loop allocates
|
||||
// nothing: positions and cycle for the two-pass sizing, plus the smoothed
|
||||
// heights (cards grow in from 0, shrink instantly when space demands).
|
||||
const cardPos = useMemo(() => new Float32Array(cards.length * 2), [cards]);
|
||||
const cardCyc = useMemo(() => new Float32Array(cards.length), [cards]);
|
||||
const cardH = useMemo(() => new Float32Array(cards.length), [cards]);
|
||||
|
||||
// Per-card readiness easing and per-clip decode bookkeeping, reused every
|
||||
// frame so the loop allocates nothing.
|
||||
const videoAlpha = useMemo(() => new Float32Array(cards.length), [cards]);
|
||||
@@ -573,33 +576,68 @@ function GalaxyScene({
|
||||
|
||||
clipBest.fill(-1);
|
||||
|
||||
// Pass 1: place every card along its arm. All positions must be known
|
||||
// before any card can size itself against its neighbors.
|
||||
for (let i = 0; i < cards.length; i++) {
|
||||
const mesh = cardRefs.current[i];
|
||||
if (!mesh) continue;
|
||||
const card = cards[i];
|
||||
|
||||
const c = cycle(
|
||||
(card.slot + 0.5) / L.slotsPerArm + P + card.arm * L.armJitter,
|
||||
);
|
||||
const theta = L.thetaBirth + c * (L.thetaMax - L.thetaBirth);
|
||||
cardCyc[i] = c;
|
||||
const theta = L.thetaBirth + c * (L.thetaExit - L.thetaBirth);
|
||||
const r = L.b * theta;
|
||||
const a = theta + (card.arm * Math.PI * 2) / L.arms;
|
||||
const ux = Math.cos(a);
|
||||
const uy = Math.sin(a);
|
||||
// Floating noise: tangential + a lighter radial component, phased per
|
||||
// card so the cloud never moves in lockstep.
|
||||
// card. Off by default; safe to enable — sizing measures the real
|
||||
// wobbled positions.
|
||||
const wobT = Math.sin(t * mv.wobbleFreq * Math.PI * 2 + i * 2.399);
|
||||
const wobR = Math.cos(t * mv.wobbleFreq * Math.PI * 2 * 0.7 + i * 1.713);
|
||||
const wx = mv.wobbleAmp * (wobT * -uy + 0.6 * wobR * ux);
|
||||
const wy = mv.wobbleAmp * (wobT * ux + 0.6 * wobR * uy);
|
||||
mesh.position.set(r * ux + wx, r * uy + wy, 0);
|
||||
cardPos[i * 2] = r * ux + mv.wobbleAmp * (wobT * -uy + 0.6 * wobR * ux);
|
||||
cardPos[i * 2 + 1] =
|
||||
r * uy + mv.wobbleAmp * (wobT * ux + 0.6 * wobR * uy);
|
||||
}
|
||||
|
||||
for (let i = 0; i < cards.length; i++) {
|
||||
const mesh = cardRefs.current[i];
|
||||
if (!mesh) continue;
|
||||
const card = cards[i];
|
||||
const c = cardCyc[i];
|
||||
const x = cardPos[i * 2];
|
||||
const y = cardPos[i * 2 + 1];
|
||||
|
||||
// Pass 2: exact collision-free sizing against the actual nearest
|
||||
// neighbor. For upright 16:9 rectangles, two cards clear each other
|
||||
// iff their center gap beats their half-extents on either axis; when
|
||||
// every card takes density × its nearest such separation, no pair
|
||||
// can ever overlap (each contributes at most half the gap). Growth
|
||||
// is eased so freed space fills organically; shrinking is instant so
|
||||
// the guarantee never breaks mid-frame.
|
||||
let sep = Infinity;
|
||||
for (let j = 0; j < cards.length; j++) {
|
||||
if (j === i) continue;
|
||||
const s = Math.max(
|
||||
(Math.abs(cardPos[j * 2] - x) * 9) / 16,
|
||||
Math.abs(cardPos[j * 2 + 1] - y),
|
||||
);
|
||||
if (s < sep) sep = s;
|
||||
}
|
||||
const target = Math.min(L.cardHCap, lk.density * sep);
|
||||
cardH[i] =
|
||||
target < cardH[i]
|
||||
? target
|
||||
: cardH[i] + (target - cardH[i]) * (1 - Math.exp(-3 * dt));
|
||||
const H = Math.max(0.001, cardH[i]);
|
||||
const W = (H * 16) / 9;
|
||||
|
||||
mesh.position.set(x, y, 0);
|
||||
mesh.rotation.z = -st.spin; // cards stay upright while the system spins
|
||||
const s = L.minScale + (1 - L.minScale) * c;
|
||||
mesh.scale.set(L.cardW * s, L.cardH * s, 1);
|
||||
mesh.scale.set(W, H, 1);
|
||||
mesh.renderOrder = 10 + Math.round(c * 100); // outer paints over inner
|
||||
|
||||
const lifecycle =
|
||||
clamp01(c / lk.fadeBand) * clamp01((1 - c) / lk.fadeBand);
|
||||
// Birth fade only: the death happens fully offscreen past thetaExit.
|
||||
const lifecycle = clamp01(c / lk.fadeBand);
|
||||
const solid = lk.washInner + (1 - lk.washInner) * c;
|
||||
const intro = clamp01(
|
||||
(t - mv.introDelay - i * mv.introStagger) / Math.max(0.05, mv.introDur),
|
||||
@@ -619,6 +657,9 @@ function GalaxyScene({
|
||||
u.uTexA.value = va;
|
||||
u.uAber.value = lk.aberration;
|
||||
u.uCurve.value = lk.curvePx;
|
||||
(u.uSize.value as THREE.Vector2).set(W, H);
|
||||
u.uRadius.value = Math.min(lk.cornerPx, H * 0.49);
|
||||
u.uFrameA.value = lk.frameAlpha;
|
||||
if (dark) {
|
||||
u.uDim.value = lk.dim * solid;
|
||||
u.uAlpha.value = lifecycle * intro;
|
||||
@@ -626,7 +667,6 @@ function GalaxyScene({
|
||||
u.uDim.value = 1;
|
||||
u.uAlpha.value = lifecycle * solid * intro * Math.sqrt(lk.dim);
|
||||
}
|
||||
frameMaterials[i].opacity = lk.frameAlpha * lifecycle * intro;
|
||||
|
||||
if (u.uAlpha.value > 0.04 && c > clipBest[card.clip]) {
|
||||
clipBest[card.clip] = c;
|
||||
@@ -666,10 +706,7 @@ function GalaxyScene({
|
||||
geometry={unitPlane}
|
||||
material={materials[i]}
|
||||
scale={[1, 1, 1]}
|
||||
>
|
||||
{/* Hairline frame, matching the site's bordered viewport idiom. */}
|
||||
<lineSegments geometry={unitFrame} material={frameMaterials[i]} />
|
||||
</mesh>
|
||||
/>
|
||||
))}
|
||||
</group>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user