@cesarvenzac/figures 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,65 @@
1
+ import { type Face, type Figure, type Glyph, type Look } from "./figure.ts";
2
+ import type { Pose } from "./figure-pose.ts";
3
+ import { type Ball, bodyTurn, type CosinePalette, type EyeFrame, type FigureScene, type Vec3 } from "./figure-space.ts";
4
+ /**
5
+ * Everything about a figure at one moment, put together as the shader takes it: its shape turned,
6
+ * lifted, squashed and swollen, the eyes on the face that's in front (or trailing the sphere's
7
+ * turn), their glyph and lids, the shape's own parts as its pose has them (the cube's cubelets,
8
+ * the block's dials, the pyramid's pieces, the sphere's moons, the cloud's puffs and raindrops),
9
+ * the bits in front of it, and its Aura. Pure, so a still figure, a live one and the tests build
10
+ * the same scene.
11
+ */
12
+ /** How much of its own colour shows in the middle, through Aura's milk (the experiment had 0.42; a little more tells the colourways apart). */
13
+ export declare const MILK = 0.55;
14
+ /** How far the halo reaches, in world units. */
15
+ export declare const REACH = 0.8;
16
+ /** How much colour a figure keeps once it's drained all it can (`drain` 1). */
17
+ export declare const DRAINED = 0.45;
18
+ export type SceneInput = {
19
+ figure: Figure;
20
+ /** Its expression's eyes and head as they are now, the gaze, sway and glances already in. */
21
+ face: Face;
22
+ glyph: Glyph;
23
+ glyphTo: Glyph;
24
+ glyphMix: number;
25
+ /** How open a blink leaves the eyes, 0–1. */
26
+ open: number;
27
+ pose: Pose;
28
+ /** How far round its turns have taken it, in degrees, and the turn of the face the eyes are on (the same, once they've moved to it). */
29
+ baseYaw: number;
30
+ eyeYaw: number;
31
+ /** A sphere's eyes trail its turns: where they are, as the body's own angles would have them. Null, they're painted on. */
32
+ trailing: {
33
+ yaw: number;
34
+ pitch: number;
35
+ roll: number;
36
+ } | null;
37
+ look: Look;
38
+ /** The Aura's phases: how far the colour has flowed, the hue turned, the rim light travelled, the rings spread; and the clock, for a blob's wobble. */
39
+ flow: number;
40
+ hue: number;
41
+ arcAngle: number;
42
+ rings: number;
43
+ time: number;
44
+ swell: number;
45
+ palette: CosinePalette;
46
+ /** The cube's scramble as it thinks: how far it has split, each cell's cubelet's turn, and a layer mid-turn. */
47
+ split: number;
48
+ cells: Float32Array | null;
49
+ turning: {
50
+ axis: number;
51
+ layer: number;
52
+ angle: number;
53
+ } | null;
54
+ ink: Vec3;
55
+ /** How much of its colour has drained away, 0–1 (an agent's budget running out, say). */
56
+ drain?: number;
57
+ };
58
+ /** The cloud's puffs as a pose has moved and swollen them. */
59
+ export declare function puffsOf(pose: Pick<Pose, "puffs">): Ball[];
60
+ /** The eyes' frame in body space: on the face in front, carried toward a morph's face, trailing a sphere's turn, riding the cloud's face puff. */
61
+ export declare function eyeFrame(input: Pick<SceneInput, "figure" | "pose" | "eyeYaw" | "trailing" | "split">, body: ReturnType<typeof bodyTurn>): EyeFrame;
62
+ /** An empty scene, to fill. */
63
+ export declare function emptyScene(palette: CosinePalette): FigureScene;
64
+ /** The scene for one moment of one figure; `into` is reused when given, so a figure drawn every frame makes no garbage. */
65
+ export declare function sceneFor(input: SceneInput, into?: FigureScene): FigureScene;
@@ -0,0 +1,210 @@
1
+ import { GLYPHS, SHAPES } from "./figure.js";
2
+ import { REST_YAW } from "./figure-motion.js";
3
+ import { apply, applyInverse, BIT_KINDS, bodyTurn, BOTTOM, CUBELET_GAP, EYES, FLOOR, frameFacing, HOVER, IDENTITY_CELLS, MORPHS, MOST, PUFFS, turnAbout, } from "./figure-space.js";
4
+ /**
5
+ * Everything about a figure at one moment, put together as the shader takes it: its shape turned,
6
+ * lifted, squashed and swollen, the eyes on the face that's in front (or trailing the sphere's
7
+ * turn), their glyph and lids, the shape's own parts as its pose has them (the cube's cubelets,
8
+ * the block's dials, the pyramid's pieces, the sphere's moons, the cloud's puffs and raindrops),
9
+ * the bits in front of it, and its Aura. Pure, so a still figure, a live one and the tests build
10
+ * the same scene.
11
+ */
12
+ /** How much of its own colour shows in the middle, through Aura's milk (the experiment had 0.42; a little more tells the colourways apart). */
13
+ export const MILK = 0.55;
14
+ /** How far the halo reaches, in world units. */
15
+ export const REACH = 0.8;
16
+ /** How much colour a figure keeps once it's drained all it can (`drain` 1). */
17
+ export const DRAINED = 0.45;
18
+ const MORPH_EYES = {
19
+ blob: { origin: [0, 0.1, 0.78], forward: [0, 0.12, 1] },
20
+ star: { origin: [0, 0.03, 0.27], forward: [0, 0, 1] },
21
+ heart: { origin: [0, 0.06, 0.29], forward: [0, 0, 1] },
22
+ };
23
+ const lerp = (a, b, t) => a + (b - a) * t;
24
+ const lerp3 = (a, b, t) => [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)];
25
+ const RADIANS = Math.PI / 180;
26
+ /** The cloud's puffs as a pose has moved and swollen them. */
27
+ export function puffsOf(pose) {
28
+ return PUFFS.map((puff, i) => {
29
+ const moved = pose.puffs?.[i];
30
+ return moved ? [puff[0] + moved[0], puff[1] + moved[1], puff[2] + moved[2], puff[3] * moved[3]] : [...puff];
31
+ });
32
+ }
33
+ /** The eyes' frame in body space: on the face in front, carried toward a morph's face, trailing a sphere's turn, riding the cloud's face puff. */
34
+ export function eyeFrame(input, body) {
35
+ const shape = input.figure.shape;
36
+ const rest = EYES[shape];
37
+ let origin = rest.origin;
38
+ let forward = rest.forward;
39
+ const { morph, morphTo } = input.pose;
40
+ // The cloud's face puff carries its eyes as it bobs.
41
+ const face = shape === "cloud" ? input.pose.puffs?.[0] : null;
42
+ if (face)
43
+ origin = [origin[0] + face[0], origin[1] + face[1], origin[2] + face[2] + PUFFS[0][3] * (face[3] - 1)];
44
+ if (morphTo !== "none" && morph > 0) {
45
+ const target = MORPH_EYES[morphTo];
46
+ origin = lerp3(origin, target.origin, morph);
47
+ forward = lerp3(forward, target.forward, morph);
48
+ }
49
+ // On the face the body's turns have brought round.
50
+ if (input.eyeYaw) {
51
+ const around = turnAbout(1, -input.eyeYaw * RADIANS);
52
+ origin = apply(around, origin);
53
+ forward = apply(around, forward);
54
+ }
55
+ // A sphere's eyes where they trail to: placed by the trailing angles, read back in the body.
56
+ if (input.trailing) {
57
+ const trail = bodyTurn(input.trailing.yaw, input.trailing.pitch, input.trailing.roll);
58
+ origin = applyInverse(body, apply(trail, origin));
59
+ forward = applyInverse(body, apply(trail, forward));
60
+ }
61
+ const split = Math.max(input.split, shape === "cube" ? input.pose.split : 0);
62
+ const gap = shape === "cube" ? lerp(rest.gap, CUBELET_GAP, split) : rest.gap;
63
+ return frameFacing(forward, origin, rest.width, rest.height, gap);
64
+ }
65
+ /** An empty scene, to fill. */
66
+ export function emptyScene(palette) {
67
+ return {
68
+ shape: 0,
69
+ morphTo: 0,
70
+ morph: 0,
71
+ time: 0,
72
+ body: { rotation: new Float32Array(9), position: new Float32Array(3), scale: new Float32Array(3) },
73
+ split: 0,
74
+ spread: 0,
75
+ cells: IDENTITY_CELLS(),
76
+ layers: new Float32Array([-1, 0, 0, 0]),
77
+ pieces: new Float32Array(4),
78
+ shear: 0,
79
+ core: 1,
80
+ beads: new Float32Array(MOST.beads * 4),
81
+ beadCount: 0,
82
+ puffs: new Float32Array(MOST.puffs * 4),
83
+ surface: new Float32Array(4),
84
+ roll: 0,
85
+ bits: new Float32Array(MOST.bits * 4),
86
+ bitKinds: new Float32Array(MOST.bits),
87
+ rainbow: new Float32Array(2),
88
+ bolt: new Float32Array(2),
89
+ eyes: {
90
+ origin: new Float32Array(3),
91
+ right: new Float32Array(3),
92
+ up: new Float32Array(3),
93
+ forward: new Float32Array(3),
94
+ width: 0,
95
+ height: 0,
96
+ gap: 0,
97
+ size: 1,
98
+ dx: 0,
99
+ dy: 0,
100
+ left: new Float32Array(3),
101
+ right3: new Float32Array(3),
102
+ glyph: 0,
103
+ glyphTo: 0,
104
+ glyphMix: 0,
105
+ open: 1,
106
+ },
107
+ aura: { palette, hue: 0, flow: 0, swirl: 0, energy: 0, saturation: 1, halo: 1, ripple: 0, rings: 0, arc: 0, arcAngle: 0, milk: MILK },
108
+ ink: new Float32Array(3),
109
+ floor: FLOOR,
110
+ bottom: 0,
111
+ reach: REACH,
112
+ };
113
+ }
114
+ /** The scene for one moment of one figure; `into` is reused when given, so a figure drawn every frame makes no garbage. */
115
+ export function sceneFor(input, into) {
116
+ const { figure, face, pose, look } = input;
117
+ const shape = figure.shape;
118
+ const yaw = REST_YAW[shape] + face.yaw + pose.yaw + input.baseYaw;
119
+ const pitch = face.pitch + pose.pitch;
120
+ const roll = face.roll + pose.roll;
121
+ const turn = bodyTurn(yaw, pitch, roll);
122
+ const size = input.swell * pose.pulse;
123
+ const sy = pose.squash * size;
124
+ const sx = size / Math.sqrt(pose.squash);
125
+ // A sphere shrunk (deflated, splashed) still stands on the ground.
126
+ const bottom = shape === "sphere" ? BOTTOM.sphere * pose.core : BOTTOM[shape];
127
+ const y = FLOOR - bottom * sy + pose.lift + HOVER[shape];
128
+ const frame = eyeFrame(input, turn);
129
+ const scene = into ?? emptyScene(input.palette);
130
+ const cube = shape === "cube";
131
+ const parted = cube || shape === "block";
132
+ scene.shape = SHAPES.indexOf(shape);
133
+ scene.morphTo = MORPHS.indexOf(pose.morphTo);
134
+ scene.morph = pose.morphTo === "none" ? 0 : pose.morph;
135
+ scene.time = input.time;
136
+ scene.body.rotation.set(turn);
137
+ scene.body.position.set([pose.x, y, 0]);
138
+ scene.body.scale.set([sx, sy, sx]);
139
+ // Each shape's own parts.
140
+ scene.split = parted ? Math.max(cube ? input.split : 0, pose.split) : 0;
141
+ scene.spread = cube ? pose.spread : 0;
142
+ if (input.cells && cube)
143
+ scene.cells.set(input.cells);
144
+ else
145
+ scene.cells.set(IDENTITY_CELLS());
146
+ const layers = cube && input.turning ? null : parted ? pose.layers : null;
147
+ if (cube && input.turning) {
148
+ const angles = [0, 0, 0];
149
+ angles[input.turning.layer + 1] = input.turning.angle;
150
+ scene.layers.set([input.turning.axis, ...angles]);
151
+ }
152
+ else
153
+ scene.layers.set(layers ? [layers.axis, ...layers.angles] : [-1, 0, 0, 0]);
154
+ scene.pieces.set(shape === "pyramid" ? pose.pieces : [0, 0, 0, 0]);
155
+ scene.shear = shape === "pyramid" ? pose.shear : 0;
156
+ scene.core = shape === "sphere" ? pose.core : 1;
157
+ scene.surface.set(shape === "sphere" ? [pose.wave, pose.wavePhase, pose.dent, pose.wrinkle] : [0, 0, 0, 0]);
158
+ scene.beads.fill(0);
159
+ const beads = pose.beads.slice(0, MOST.beads);
160
+ beads.forEach((bead, i) => scene.beads.set(bead, i * 4));
161
+ scene.beadCount = beads.length;
162
+ scene.puffs.fill(0);
163
+ if (shape === "cloud")
164
+ puffsOf(pose).forEach((puff, i) => scene.puffs.set(puff, i * 4));
165
+ scene.roll = pose.colourRoll;
166
+ scene.bits.fill(0);
167
+ scene.bitKinds.fill(0);
168
+ pose.bits.slice(0, MOST.bits).forEach((bit, i) => {
169
+ scene.bits.set([bit.x, bit.y, bit.size, bit.alpha], i * 4);
170
+ scene.bitKinds[i] = BIT_KINDS.indexOf(bit.kind);
171
+ });
172
+ scene.rainbow.set(shape === "cloud" ? [pose.rainbowSweep, pose.rainbow] : [0, 0]);
173
+ scene.bolt.set(shape === "cloud" ? [pose.bolt, pose.boltSeed] : [0, 0]);
174
+ const eyes = scene.eyes;
175
+ eyes.origin.set(frame.origin);
176
+ eyes.right.set(frame.right);
177
+ eyes.up.set(frame.up);
178
+ eyes.forward.set(frame.forward);
179
+ eyes.width = frame.width;
180
+ eyes.height = frame.height;
181
+ eyes.gap = frame.gap;
182
+ eyes.size = face.size * lerp(1, 0.92, scene.split) * pose.eyes;
183
+ eyes.dx = face.dx;
184
+ eyes.dy = face.dy;
185
+ eyes.left.set([face.li, face.lo, face.smile]);
186
+ eyes.right3.set([face.li, face.lo, face.smile]);
187
+ // A pose's glyph (a star's starry eyes) wins over the expression's while it lasts.
188
+ const posed = pose.glyph && pose.glyphMix > 0;
189
+ eyes.glyph = GLYPHS.indexOf(posed ? (input.glyphMix > 0.5 ? input.glyphTo : input.glyph) : input.glyph);
190
+ eyes.glyphTo = GLYPHS.indexOf(posed ? pose.glyph : input.glyphTo);
191
+ eyes.glyphMix = posed ? pose.glyphMix : input.glyphMix;
192
+ eyes.open = Math.min(input.open, 1 - pose.shut * 0.95);
193
+ const aura = scene.aura;
194
+ const drained = 1 - (1 - DRAINED) * Math.min(1, Math.max(0, input.drain ?? 0));
195
+ aura.palette = input.palette;
196
+ aura.hue = input.hue;
197
+ aura.flow = input.flow;
198
+ aura.swirl = look.swirl;
199
+ aura.energy = look.energy * lerp(1, pose.dim, 0.6);
200
+ aura.saturation = look.saturation * pose.dim * drained;
201
+ aura.halo = look.halo;
202
+ aura.ripple = look.ripple;
203
+ aura.rings = input.rings;
204
+ aura.arc = look.arc;
205
+ aura.arcAngle = input.arcAngle;
206
+ aura.milk = MILK;
207
+ scene.ink.set(input.ink);
208
+ scene.bottom = y + bottom * sy;
209
+ return scene;
210
+ }
@@ -0,0 +1,233 @@
1
+ /**
2
+ * The figures in 3D: the camera, each shape's size, where its eyes sit, and the scene
3
+ * the shader draws (components/figures/figure-gl.ts). Pure: numbers and small maths, so what
4
+ * the figure does can be worked out, tested, and drawn anywhere.
5
+ *
6
+ * World space: y up, the camera in front (+z) and a little above, looking at the middle. Body
7
+ * space: the shape's own, its front facing +z, turned and lifted into the world by the body's
8
+ * transform. Distances are in units where the figure's box is 2 wide.
9
+ */
10
+ /** The camera: how far it is, how far it looks down (radians), and half the picture's height at the middle (world units). */
11
+ export declare const CAMERA: {
12
+ readonly distance: 7;
13
+ readonly pitch: 0.25;
14
+ readonly half: 1.5;
15
+ };
16
+ /** The ground, where every shape stands. */
17
+ export declare const FLOOR = -0.86;
18
+ /**
19
+ * Each shape's size, in body space. The cube and block are boxes with rounded edges (half their
20
+ * width each way, and the rounding); the block turns as three dials round its length, parted
21
+ * `dial` either side of its middle. The pyramid stands on a square base `half` wide each way at
22
+ * `base`, its apex `height` above, and comes apart in three at its `cuts` (a slab, then the
23
+ * capstone). The sphere is a sphere; the cloud is its puffs (`PUFFS`), flat underneath at `base`.
24
+ */
25
+ export declare const BODY: {
26
+ readonly cube: {
27
+ readonly half: 0.6;
28
+ readonly round: 0.12;
29
+ };
30
+ readonly sphere: {
31
+ readonly radius: 0.8;
32
+ };
33
+ readonly pyramid: {
34
+ readonly half: 0.82;
35
+ readonly height: 1.34;
36
+ readonly base: -0.52;
37
+ readonly round: 0.06;
38
+ readonly cuts: [number, number];
39
+ };
40
+ readonly block: {
41
+ readonly half: [number, number, number];
42
+ readonly round: 0.12;
43
+ readonly dial: 0.35;
44
+ };
45
+ readonly cloud: {
46
+ readonly base: -0.48;
47
+ readonly blend: 0.15;
48
+ };
49
+ };
50
+ export type Vec3 = [number, number, number];
51
+ /** A ball: where it is in body space, and its radius. */
52
+ export type Ball = [number, number, number, number];
53
+ /** The cloud's puffs as it rests, in body space: the first, the largest, carries the face. */
54
+ export declare const PUFFS: readonly Ball[];
55
+ /** How far below its middle each shape reaches, so it stands on the ground. */
56
+ export declare const BOTTOM: {
57
+ readonly cube: number;
58
+ readonly sphere: number;
59
+ readonly pyramid: number;
60
+ readonly block: number;
61
+ readonly cloud: -0.48;
62
+ };
63
+ /** How high above its middle each shape reaches. */
64
+ export declare const TOP: {
65
+ readonly cube: 0.6;
66
+ readonly sphere: 0.8;
67
+ readonly pyramid: number;
68
+ readonly block: number;
69
+ readonly cloud: 0.68;
70
+ };
71
+ /** How far above the ground each shape floats at rest: a cloud hovers. */
72
+ export declare const HOVER: {
73
+ readonly cube: 0;
74
+ readonly sphere: 0;
75
+ readonly pyramid: 0;
76
+ readonly block: 0;
77
+ readonly cloud: 0.26;
78
+ };
79
+ /** The 3D shapes, in the order the shader numbers them. */
80
+ export declare const SHAPES_3D: readonly ["cube", "sphere", "pyramid", "block", "cloud"];
81
+ export type Shape3D = (typeof SHAPES_3D)[number];
82
+ /** What a shape can turn into for a moment, in the order the shader numbers them (0 is none). */
83
+ export declare const MORPHS: readonly ["none", "blob", "star", "heart"];
84
+ export type Morph = (typeof MORPHS)[number];
85
+ /** Where the eyes sit on a shape: the face's middle (`origin`), its right, up and outward directions, the eyes' width and height, and how far each is from the middle. */
86
+ export type EyeFrame = {
87
+ origin: Vec3;
88
+ right: Vec3;
89
+ up: Vec3;
90
+ forward: Vec3;
91
+ width: number;
92
+ height: number;
93
+ gap: number;
94
+ };
95
+ declare const norm: (v: Vec3) => Vec3;
96
+ declare const cross: (a: Vec3, b: Vec3) => Vec3;
97
+ declare const dot: (a: Vec3, b: Vec3) => number;
98
+ declare const scale: (v: Vec3, s: number) => Vec3;
99
+ declare const add: (a: Vec3, b: Vec3) => Vec3;
100
+ /** A frame facing `forward`, upright as far as it can be. */
101
+ export declare function frameFacing(forward: Vec3, origin: Vec3, width: number, height: number, gap: number): EyeFrame;
102
+ /** The eyes on each shape, as it rests. */
103
+ export declare const EYES: Record<Shape3D, EyeFrame>;
104
+ /** The picture's height of a point at height `y` and depth `z` in the world, as the camera sees it (world units at the middle). */
105
+ export declare function pictureY(y: number, z?: number): number;
106
+ /** Where a cube's eyes sit once it splits into cubelets: on the middle row's outer two, so no gap runs through them. */
107
+ export declare const CUBELET_GAP: number;
108
+ /** A 3×3 rotation, column-major (as WebGL takes it). */
109
+ export type Mat3 = [number, number, number, number, number, number, number, number, number];
110
+ export declare const IDENTITY: Mat3;
111
+ /** a·b, both column-major. */
112
+ export declare function multiply(a: Mat3, b: Mat3): Mat3;
113
+ /** A turn about x, y or z by `angle` radians (right-handed: a positive turn about z takes x toward y). */
114
+ export declare function turnAbout(axis: 0 | 1 | 2, angle: number): Mat3;
115
+ /** m·v, m column-major. */
116
+ export declare const apply: (m: Mat3, v: Vec3) => Vec3;
117
+ /** mᵀ·v: a world direction in body space. */
118
+ export declare const applyInverse: (m: Mat3, v: Vec3) => Vec3;
119
+ /**
120
+ * The body's turn from how it holds itself, in degrees: turned toward the picture's right (`yaw`),
121
+ * tipped up (`pitch`), leaning to the picture's right (`roll`). Rolled, then tipped, then turned.
122
+ */
123
+ export declare function bodyTurn(yaw: number, pitch: number, roll: number): Mat3;
124
+ /** A cosine palette, Inigo Quilez's `a + b·cos(2π(t + d))` per channel, in linear RGB. */
125
+ export type CosinePalette = {
126
+ a: Vec3;
127
+ b: Vec3;
128
+ d: Vec3;
129
+ };
130
+ /** A cosine palette at `t`, as the shader computes it. */
131
+ export declare const paletteAt: (palette: CosinePalette, t: number) => Vec3;
132
+ /**
133
+ * The cosine palette that passes through three colours (linear RGB) a third of the way round
134
+ * each: a colourway's own, lighter and deeper tokens, so the figure's rainbow keeps to its family
135
+ * and its tokens stay the one place its colours are said. Per channel, c(t) = a + C·cos 2πt +
136
+ * S·sin 2πt, solved from the three samples.
137
+ */
138
+ export declare function paletteThrough(first: Vec3, second: Vec3, third: Vec3): CosinePalette;
139
+ /** Everything the shader needs for one frame of one figure. */
140
+ export type FigureScene = {
141
+ shape: number;
142
+ morphTo: number;
143
+ morph: number;
144
+ /** Seconds, for a blob's wobble. */
145
+ time: number;
146
+ body: {
147
+ rotation: Float32Array;
148
+ position: Float32Array;
149
+ scale: Float32Array;
150
+ };
151
+ /** 0 a solid cube (or block), 1 split into its 27 cubelets (or three dials). */
152
+ split: number;
153
+ /** How far the cube's cubelets have flown apart, as a share of their size. */
154
+ spread: number;
155
+ /** Each cell's cubelet's turn from where it started, 27 column-major 3×3s (rubik.ts). */
156
+ cells: Float32Array;
157
+ /** Layers turning: the axis (−1 for none), then how far each layer (−1, 0, 1) has turned, in radians. The cube's layers, the block's dials. */
158
+ layers: Float32Array;
159
+ /** The pyramid apart: its middle slab's slide, its capstone's slide, lift and spin (radians). */
160
+ pieces: Float32Array;
161
+ /** How far the pyramid's top leans, jelly-like. */
162
+ shear: number;
163
+ /** The sphere's own size, as a share. */
164
+ core: number;
165
+ /** Small balls beside the body (a sphere's moons and droplets, a cloud's raindrops), each x, y, z and radius in body space; a radius of 0 is none. */
166
+ beads: Float32Array;
167
+ beadCount: number;
168
+ /** The cloud's puffs, each x, y, z and radius in body space. */
169
+ puffs: Float32Array;
170
+ /** The surface moved: a ripple's height and how far it has run, a poke's dent, wrinkles. */
171
+ surface: Float32Array;
172
+ /** How far the colour has rolled round, in radians: the sphere at work. */
173
+ roll: number;
174
+ /** Bits drawn in front: hearts, stars, z's and sparks, each x and y in the picture from its middle, size and opacity; and each one's kind (`BIT_KINDS`). */
175
+ bits: Float32Array;
176
+ bitKinds: Float32Array;
177
+ /** The cloud's rainbow: how far it has swept across, and how strongly it shows. */
178
+ rainbow: Float32Array;
179
+ /** A lightning flash under the cloud: how bright, and which zigzag. */
180
+ bolt: Float32Array;
181
+ eyes: {
182
+ origin: Float32Array;
183
+ right: Float32Array;
184
+ up: Float32Array;
185
+ forward: Float32Array;
186
+ width: number;
187
+ height: number;
188
+ gap: number;
189
+ size: number;
190
+ dx: number;
191
+ dy: number;
192
+ /** Each eye's lids: inner, outer, smile. */
193
+ left: Float32Array;
194
+ right3: Float32Array;
195
+ glyph: number;
196
+ glyphTo: number;
197
+ glyphMix: number;
198
+ /** How open a blink leaves them, 0–1. */
199
+ open: number;
200
+ };
201
+ aura: {
202
+ palette: CosinePalette;
203
+ hue: number;
204
+ flow: number;
205
+ swirl: number;
206
+ energy: number;
207
+ saturation: number;
208
+ halo: number;
209
+ ripple: number;
210
+ rings: number;
211
+ arc: number;
212
+ arcAngle: number;
213
+ /** How much of its own colour shows in the middle, through the milk (Aura's 0.42). */
214
+ milk: number;
215
+ };
216
+ ink: Float32Array;
217
+ floor: number;
218
+ /** The body's lowest point, for its shadow. */
219
+ bottom: number;
220
+ /** How far the halo reaches, in world units. */
221
+ reach: number;
222
+ };
223
+ /** What a bit in front of the figure can be, in the order the shader numbers them (0 is none). */
224
+ export declare const BIT_KINDS: readonly ["none", "heart", "star", "z", "spark"];
225
+ export type BitKind = (typeof BIT_KINDS)[number];
226
+ /** How many beads, puffs and bits the shader draws at most. */
227
+ export declare const MOST: {
228
+ readonly beads: 6;
229
+ readonly puffs: 6;
230
+ readonly bits: 4;
231
+ };
232
+ export declare const IDENTITY_CELLS: () => Float32Array<ArrayBuffer>;
233
+ export { add, cross, dot, norm, scale };
@@ -0,0 +1,151 @@
1
+ /**
2
+ * The figures in 3D: the camera, each shape's size, where its eyes sit, and the scene
3
+ * the shader draws (components/figures/figure-gl.ts). Pure: numbers and small maths, so what
4
+ * the figure does can be worked out, tested, and drawn anywhere.
5
+ *
6
+ * World space: y up, the camera in front (+z) and a little above, looking at the middle. Body
7
+ * space: the shape's own, its front facing +z, turned and lifted into the world by the body's
8
+ * transform. Distances are in units where the figure's box is 2 wide.
9
+ */
10
+ /** The camera: how far it is, how far it looks down (radians), and half the picture's height at the middle (world units). */
11
+ export const CAMERA = { distance: 7, pitch: 0.25, half: 1.5 };
12
+ /** The ground, where every shape stands. */
13
+ export const FLOOR = -0.86;
14
+ /**
15
+ * Each shape's size, in body space. The cube and block are boxes with rounded edges (half their
16
+ * width each way, and the rounding); the block turns as three dials round its length, parted
17
+ * `dial` either side of its middle. The pyramid stands on a square base `half` wide each way at
18
+ * `base`, its apex `height` above, and comes apart in three at its `cuts` (a slab, then the
19
+ * capstone). The sphere is a sphere; the cloud is its puffs (`PUFFS`), flat underneath at `base`.
20
+ */
21
+ export const BODY = {
22
+ cube: { half: 0.6, round: 0.12 },
23
+ sphere: { radius: 0.8 },
24
+ pyramid: { half: 0.82, height: 1.34, base: -0.52, round: 0.06, cuts: [0.06, 0.38] },
25
+ block: { half: [0.86, 0.5, 0.5], round: 0.12, dial: 0.35 },
26
+ cloud: { base: -0.48, blend: 0.15 },
27
+ };
28
+ /** The cloud's puffs as it rests, in body space: the first, the largest, carries the face. */
29
+ export const PUFFS = [
30
+ [0, 0.12, 0.02, 0.56],
31
+ [-0.56, -0.12, 0.02, 0.38],
32
+ [0.58, -0.1, 0, 0.4],
33
+ [-0.3, 0.28, -0.04, 0.35],
34
+ [0.02, 0.04, -0.28, 0.46],
35
+ [0.04, -0.22, 0.06, 0.42],
36
+ ];
37
+ /** How far below its middle each shape reaches, so it stands on the ground. */
38
+ export const BOTTOM = {
39
+ cube: -BODY.cube.half,
40
+ sphere: -BODY.sphere.radius,
41
+ pyramid: BODY.pyramid.base - BODY.pyramid.round,
42
+ block: -BODY.block.half[1],
43
+ cloud: BODY.cloud.base,
44
+ };
45
+ /** How high above its middle each shape reaches. */
46
+ export const TOP = { cube: BODY.cube.half, sphere: BODY.sphere.radius, pyramid: BODY.pyramid.base + BODY.pyramid.height + BODY.pyramid.round, block: BODY.block.half[1], cloud: 0.68 };
47
+ /** How far above the ground each shape floats at rest: a cloud hovers. */
48
+ export const HOVER = { cube: 0, sphere: 0, pyramid: 0, block: 0, cloud: 0.26 };
49
+ /** The 3D shapes, in the order the shader numbers them. */
50
+ export const SHAPES_3D = ["cube", "sphere", "pyramid", "block", "cloud"];
51
+ /** What a shape can turn into for a moment, in the order the shader numbers them (0 is none). */
52
+ export const MORPHS = ["none", "blob", "star", "heart"];
53
+ const norm = (v) => {
54
+ const length = Math.hypot(...v) || 1;
55
+ return [v[0] / length, v[1] / length, v[2] / length];
56
+ };
57
+ const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
58
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
59
+ const scale = (v, s) => [v[0] * s, v[1] * s, v[2] * s];
60
+ const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
61
+ /** A frame facing `forward`, upright as far as it can be. */
62
+ export function frameFacing(forward, origin, width, height, gap) {
63
+ const f = norm(forward);
64
+ const up = norm(add([0, 1, 0], scale(f, -f[1])));
65
+ return { origin, forward: f, up, right: cross(up, f), width, height, gap };
66
+ }
67
+ const pyramidFace = (() => {
68
+ const { half, height, base, round } = BODY.pyramid;
69
+ const normal = norm([0, half, height]);
70
+ const along = 0.3;
71
+ const point = [0, base + height * along, half * (1 - along)];
72
+ return { normal, origin: add(point, scale(normal, round)) };
73
+ })();
74
+ /** The eyes on each shape, as it rests. */
75
+ export const EYES = {
76
+ cube: frameFacing([0, 0, 1], [0, 0.02, BODY.cube.half], 0.15, 0.22, 0.2),
77
+ sphere: frameFacing([0, 0.12, 1], scale(norm([0, 0.12, 1]), BODY.sphere.radius), 0.15, 0.22, 0.2),
78
+ pyramid: frameFacing(pyramidFace.normal, pyramidFace.origin, 0.145, 0.21, 0.185),
79
+ block: frameFacing([0, 0, 1], [0, 0.02, BODY.block.half[2]], 0.15, 0.22, 0.23),
80
+ cloud: frameFacing([0, 0.04, 1], [0, 0.09, 0.58], 0.15, 0.22, 0.2),
81
+ };
82
+ /** The picture's height of a point at height `y` and depth `z` in the world, as the camera sees it (world units at the middle). */
83
+ export function pictureY(y, z = 0) {
84
+ const { distance, pitch } = CAMERA;
85
+ return ((y * Math.cos(pitch) - z * Math.sin(pitch)) * distance) / (distance - y * Math.sin(pitch) - z * Math.cos(pitch));
86
+ }
87
+ /** Where a cube's eyes sit once it splits into cubelets: on the middle row's outer two, so no gap runs through them. */
88
+ export const CUBELET_GAP = (2 * BODY.cube.half) / 3;
89
+ export const IDENTITY = [1, 0, 0, 0, 1, 0, 0, 0, 1];
90
+ /** a·b, both column-major. */
91
+ export function multiply(a, b) {
92
+ const out = [0, 0, 0, 0, 0, 0, 0, 0, 0];
93
+ for (let column = 0; column < 3; column++)
94
+ for (let row = 0; row < 3; row++)
95
+ out[column * 3 + row] = a[row] * b[column * 3] + a[3 + row] * b[column * 3 + 1] + a[6 + row] * b[column * 3 + 2];
96
+ return out;
97
+ }
98
+ /** A turn about x, y or z by `angle` radians (right-handed: a positive turn about z takes x toward y). */
99
+ export function turnAbout(axis, angle) {
100
+ const c = Math.cos(angle);
101
+ const s = Math.sin(angle);
102
+ if (axis === 0)
103
+ return [1, 0, 0, 0, c, s, 0, -s, c];
104
+ if (axis === 1)
105
+ return [c, 0, -s, 0, 1, 0, s, 0, c];
106
+ return [c, s, 0, -s, c, 0, 0, 0, 1];
107
+ }
108
+ /** m·v, m column-major. */
109
+ export const apply = (m, v) => [m[0] * v[0] + m[3] * v[1] + m[6] * v[2], m[1] * v[0] + m[4] * v[1] + m[7] * v[2], m[2] * v[0] + m[5] * v[1] + m[8] * v[2]];
110
+ /** mᵀ·v: a world direction in body space. */
111
+ export const applyInverse = (m, v) => [m[0] * v[0] + m[1] * v[1] + m[2] * v[2], m[3] * v[0] + m[4] * v[1] + m[5] * v[2], m[6] * v[0] + m[7] * v[1] + m[8] * v[2]];
112
+ const RADIANS = Math.PI / 180;
113
+ /**
114
+ * The body's turn from how it holds itself, in degrees: turned toward the picture's right (`yaw`),
115
+ * tipped up (`pitch`), leaning to the picture's right (`roll`). Rolled, then tipped, then turned.
116
+ */
117
+ export function bodyTurn(yaw, pitch, roll) {
118
+ return multiply(turnAbout(1, yaw * RADIANS), multiply(turnAbout(0, -pitch * RADIANS), turnAbout(2, -roll * RADIANS)));
119
+ }
120
+ /** A cosine palette at `t`, as the shader computes it. */
121
+ export const paletteAt = (palette, t) => [0, 1, 2].map((i) => palette.a[i] + palette.b[i] * Math.cos(2 * Math.PI * (t + palette.d[i])));
122
+ /**
123
+ * The cosine palette that passes through three colours (linear RGB) a third of the way round
124
+ * each: a colourway's own, lighter and deeper tokens, so the figure's rainbow keeps to its family
125
+ * and its tokens stay the one place its colours are said. Per channel, c(t) = a + C·cos 2πt +
126
+ * S·sin 2πt, solved from the three samples.
127
+ */
128
+ export function paletteThrough(first, second, third) {
129
+ const a = [0, 0, 0];
130
+ const b = [0, 0, 0];
131
+ const d = [0, 0, 0];
132
+ for (let i = 0; i < 3; i++) {
133
+ a[i] = (first[i] + second[i] + third[i]) / 3;
134
+ const c = first[i] - a[i];
135
+ const s = (second[i] - third[i]) / Math.sqrt(3);
136
+ b[i] = Math.hypot(c, s);
137
+ d[i] = Math.atan2(-s, c) / (2 * Math.PI);
138
+ }
139
+ return { a, b, d };
140
+ }
141
+ /** What a bit in front of the figure can be, in the order the shader numbers them (0 is none). */
142
+ export const BIT_KINDS = ["none", "heart", "star", "z", "spark"];
143
+ /** How many beads, puffs and bits the shader draws at most. */
144
+ export const MOST = { beads: 6, puffs: 6, bits: 4 };
145
+ export const IDENTITY_CELLS = () => {
146
+ const cells = new Float32Array(27 * 9);
147
+ for (let i = 0; i < 27; i++)
148
+ cells.set(IDENTITY, i * 9);
149
+ return cells;
150
+ };
151
+ export { add, cross, dot, norm, scale };