@cesarvenzac/figures 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -2
- package/dist/figure-acts.d.ts +55 -0
- package/dist/figure-acts.js +616 -0
- package/dist/figure-curves.d.ts +30 -0
- package/dist/figure-curves.js +44 -0
- package/dist/figure-gl.d.ts +70 -0
- package/dist/figure-gl.js +960 -0
- package/dist/figure-loop.d.ts +3 -0
- package/dist/figure-loop.js +21 -0
- package/dist/figure-motion.d.ts +76 -0
- package/dist/figure-motion.js +260 -0
- package/dist/figure-peers.d.ts +36 -0
- package/dist/figure-peers.js +37 -0
- package/dist/figure-pose.d.ts +77 -0
- package/dist/figure-pose.js +108 -0
- package/dist/figure-scene.d.ts +65 -0
- package/dist/figure-scene.js +210 -0
- package/dist/figure-space.d.ts +233 -0
- package/dist/figure-space.js +151 -0
- package/dist/figure.d.ts +204 -0
- package/dist/figure.js +277 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/react.d.ts +71 -0
- package/dist/react.js +796 -0
- package/dist/rubik.d.ts +34 -0
- package/dist/rubik.js +54 -0
- package/package.json +73 -4
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The curves the figures move by (figure-motion.ts and figure-acts.ts): easing,
|
|
3
|
+
* keyframes, bumps and wobbles, each a pure function of how far through something is.
|
|
4
|
+
*/
|
|
5
|
+
export const clamp01 = (x) => Math.min(1, Math.max(0, x));
|
|
6
|
+
/** Where `p` is between `from` and `to`, 0 to 1. */
|
|
7
|
+
export const span = (p, from, to) => clamp01((p - from) / (to - from));
|
|
8
|
+
export const easeOut = (x) => 1 - (1 - x) ** 3;
|
|
9
|
+
export const easeIn = (x) => x ** 3;
|
|
10
|
+
export const easeInOut = (x) => (x < 0.5 ? 4 * x ** 3 : 1 - (-2 * x + 2) ** 3 / 2);
|
|
11
|
+
/** Past the end and back: an overshoot that settles. */
|
|
12
|
+
export const easeOutBack = (x, s = 1.7) => 1 + (s + 1) * (x - 1) ** 3 + s * (x - 1) ** 2;
|
|
13
|
+
/** Up and down again, 0 at both ends and 1 in the middle. */
|
|
14
|
+
export const bump = (x) => Math.sin(Math.PI * clamp01(x));
|
|
15
|
+
/** A ball's arc, 0 at both ends and 1 in the middle. */
|
|
16
|
+
export const arc = (x) => 4 * clamp01(x) * (1 - clamp01(x));
|
|
17
|
+
/** A wobble that dies away: `cycles` swings over 0–1. */
|
|
18
|
+
export const wobble = (x, cycles) => Math.sin(2 * Math.PI * cycles * clamp01(x)) * (1 - clamp01(x)) ** 1.4;
|
|
19
|
+
/** A value through keyframes, eased in and out between each: [p, value] pairs, p rising. */
|
|
20
|
+
export function keys(p, frames) {
|
|
21
|
+
if (p <= frames[0][0])
|
|
22
|
+
return frames[0][1];
|
|
23
|
+
for (let i = 1; i < frames.length; i++) {
|
|
24
|
+
const [p1, v1] = frames[i];
|
|
25
|
+
const [p0, v0] = frames[i - 1];
|
|
26
|
+
if (p <= p1)
|
|
27
|
+
return v0 + (v1 - v0) * easeInOut(span(p, p0, p1));
|
|
28
|
+
}
|
|
29
|
+
return frames[frames.length - 1][1];
|
|
30
|
+
}
|
|
31
|
+
/** In and out again, held between: 0 to 1 from `start` to `full`, back to 0 from `leave` to `end`, eased both ways. */
|
|
32
|
+
export const hold = (p, start, full, leave, end) => Math.min(easeInOut(span(p, start, full)), 1 - easeInOut(span(p, leave, end)));
|
|
33
|
+
/** Into a morph and out again, held at the top: 0 to 1 by `full`, back to 0 from `leave` to `end`, overshooting on the way in. */
|
|
34
|
+
export const into = (p, start, full, leave, end = 1) => Math.min(easeOutBack(span(p, start, full), 1.2), 1 - easeInOut(span(p, leave, end)));
|
|
35
|
+
/** A number from a seed and an index, the same every time, 0 to 1. */
|
|
36
|
+
export const noise = (seed, index) => {
|
|
37
|
+
const x = Math.sin(seed * 12.9898 + index * 78.233) * 43758.5453;
|
|
38
|
+
return x - Math.floor(x);
|
|
39
|
+
};
|
|
40
|
+
/** Where `t` is in a cycle of `period`, 0 to 1. */
|
|
41
|
+
export const phaseOf = (t, period) => ((t % period) + period) % period / period;
|
|
42
|
+
const TAU = Math.PI * 2;
|
|
43
|
+
/** A whole turn in radians. */
|
|
44
|
+
export const TURN = TAU;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type FigureScene } from "./figure-space.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The figures, drawn: real 3D shapes (a cube, a sphere, a pyramid, a block, a cloud) found
|
|
4
|
+
* by raymarching their distance fields in one fragment shader, in the original Aura's material, the
|
|
5
|
+
* eyes painted on their surface. No geometry and no textures: every pixel asks the shapes how far
|
|
6
|
+
* it is from them.
|
|
7
|
+
*
|
|
8
|
+
* Aura's material, as its experiment drew it on a sphere: colour as noise warped by noise
|
|
9
|
+
* (MaterialX's Perlin noise, as Three's TSL gave it), flowing, through a cosine palette; a rainbow
|
|
10
|
+
* running round the rim, turned from the direction itself so it has no seam; milky towards the
|
|
11
|
+
* middle like a ball of thick glass lit from inside; a gloss where the light lands; and a halo of
|
|
12
|
+
* the rim's own colour outside, with rings while it listens and a light running round while it
|
|
13
|
+
* thinks. Here the rim follows each shape's surface (how far it faces away), so a cube's front is
|
|
14
|
+
* milky and its edges and faces turned away run with colour.
|
|
15
|
+
*
|
|
16
|
+
* Everything that moves arrives as numbers (`FigureScene`, figure-space.ts): how the
|
|
17
|
+
* body is turned, lifted and squashed, a morph into a blob, a star or a heart, where the eyes are
|
|
18
|
+
* and how their lids sit; and each shape's own parts: the cube's cubelets (turning a layer, as a
|
|
19
|
+
* Rubik's cube does, or flying apart), the block's three dials, the pyramid's slabs and capstone,
|
|
20
|
+
* the sphere's size, ripples and moons, the cloud's puffs and raindrops. In front of it, hearts,
|
|
21
|
+
* stars, z's and sparks; behind and under a cloud, its rainbow and its lightning. One WebGL 2 context for every figure on the
|
|
22
|
+
* page (a browser keeps only a few): each figure is drawn into it and copied onto its own canvas
|
|
23
|
+
* straight away. The shader is made ready without holding up the page (`prepare`), and until it
|
|
24
|
+
* is, or where WebGL 2 is missing or lost, the figures show their flat drawings (`shaderState`).
|
|
25
|
+
*/
|
|
26
|
+
/** The largest figure drawn, in device pixels; a bigger one is drawn at this and scaled. */
|
|
27
|
+
export declare const MOST_PIXELS = 512;
|
|
28
|
+
type Uniform = "uSize" | "uShape" | "uMorphTo" | "uMorph" | "uTime" | "uBody" | "uBodyPos" | "uBodyScale" | "uSplit" | "uSpread" | "uCells" | "uLayers" | "uPieces" | "uShear" | "uCore" | "uBeads" | "uBeadCount" | "uPuffs" | "uSurface" | "uRoll" | "uBits" | "uBitKinds" | "uRainbow" | "uBolt" | "uEyeO" | "uEyeR" | "uEyeU" | "uEyeF" | "uEyeSize" | "uLook" | "uLidL" | "uLidR" | "uGlyph" | "uInk" | "uPalA" | "uPalB" | "uPalD" | "uHue" | "uFlow" | "uSwirl" | "uEnergy" | "uSaturation" | "uHalo" | "uRipple" | "uRings" | "uArc" | "uArcAngle" | "uFloorY" | "uBottom" | "uReach" | "uMilk";
|
|
29
|
+
type Renderer = {
|
|
30
|
+
gl: WebGL2RenderingContext;
|
|
31
|
+
at: Record<Uniform, WebGLUniformLocation | null>;
|
|
32
|
+
};
|
|
33
|
+
/** What `prepare` waits for between its steps: a moment the page is idle, or the next frame. */
|
|
34
|
+
export type Wait = "idle" | "frame";
|
|
35
|
+
/**
|
|
36
|
+
* The shader made ready without holding up the page, a step at a time: `prepare` yields what to
|
|
37
|
+
* wait for before its next step, and never asks the context anything it would have to wait for.
|
|
38
|
+
*
|
|
39
|
+
* A browser hands its WebGL calls to a GPU process and answers a question about them (did it
|
|
40
|
+
* compile? what does it draw?) only once the work is done, holding the page's main thread
|
|
41
|
+
* meanwhile. This shader is big: a slow or software driver takes seconds over it (SwiftShader, as
|
|
42
|
+
* headless Chromium has it, compiles it in 30 ms and then 2 s more over the first draw, making the
|
|
43
|
+
* pipeline). So nothing is asked until it's done. With `KHR_parallel_shader_compile` the program
|
|
44
|
+
* is compiled and linked at once and its completion asked each frame, which never waits. Without
|
|
45
|
+
* it, the compile starts once the page is idle, so it isn't inside the first render, with a fence
|
|
46
|
+
* behind it, which says each frame whether the work before it is done, again without waiting.
|
|
47
|
+
* Then a first draw, on one pixel, behind a fence of its own: the pipeline is made there. Only
|
|
48
|
+
* then is the program used, and drawing a figure costs what drawing it costs.
|
|
49
|
+
*
|
|
50
|
+
* Returns the renderer, or null when the shader doesn't compile here (it says why on the console).
|
|
51
|
+
*/
|
|
52
|
+
export declare function prepare(gl: WebGL2RenderingContext): Generator<Wait, Renderer | null, void>;
|
|
53
|
+
/**
|
|
54
|
+
* Where the page's shader stands: being made ready (`compiling`: the figures show their flat
|
|
55
|
+
* drawings meanwhile), `ready`, or `unavailable` (no WebGL 2, or it didn't compile). `generation`
|
|
56
|
+
* counts the times it became ready: a lost context comes back as a new one, drawn into afresh.
|
|
57
|
+
*/
|
|
58
|
+
export type ShaderState = {
|
|
59
|
+
readiness: "compiling" | "ready" | "unavailable";
|
|
60
|
+
generation: number;
|
|
61
|
+
};
|
|
62
|
+
/** How things stand before anything's tried, as the server draws a figure. */
|
|
63
|
+
export declare const NOT_YET: ShaderState;
|
|
64
|
+
/** Where the page's shader stands now (`ShaderState`). */
|
|
65
|
+
export declare const shaderState: () => ShaderState;
|
|
66
|
+
/** Calls `change` whenever the shader's state changes, and starts making it ready if nothing has yet. Returns a function to stop. */
|
|
67
|
+
export declare function watchShader(change: () => void): () => void;
|
|
68
|
+
/** Draws a figure's scene onto `target` (its own canvas, in device pixels). False when the shader isn't ready (`shaderState`), or the canvas can't be drawn on. */
|
|
69
|
+
export declare function drawFigure(target: HTMLCanvasElement, scene: FigureScene): boolean;
|
|
70
|
+
export {};
|