@uxfront/scene 0.1.0 → 0.2.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.
package/dist/gl/math.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  //#region src/gl/math.d.ts
2
2
  export type Vec2 = [number, number];
3
3
  export type Vec3 = [number, number, number];
4
+ export type Vec4 = [number, number, number, number];
4
5
  export type Mat4 = Float32Array;
5
6
  export declare const clamp: (value: number, min: number, max: number) => number;
6
7
  export declare const lerp: (a: number, b: number, t: number) => number;
@@ -0,0 +1,53 @@
1
+ import { Vec2, Vec4 } from "./math.js";
2
+ //#region src/gl/shapes.d.ts
3
+ /**
4
+ * Line art drawn in particles, in a formation's own 2D art units. Outlines
5
+ * (`seg`, `rrect`, `circle`) spread their particles along the line, fills
6
+ * (`disc`, `fill`: a filled rounded rectangle) over the area. `bright` scales
7
+ * the shape's light.
8
+ */
9
+ export type Shape = {
10
+ kind: "seg";
11
+ a: Vec2;
12
+ b: Vec2;
13
+ bright: number;
14
+ } | {
15
+ kind: "rrect";
16
+ c: Vec2;
17
+ h: Vec2;
18
+ r: number;
19
+ bright: number;
20
+ } | {
21
+ kind: "circle";
22
+ c: Vec2;
23
+ r: number;
24
+ bright: number;
25
+ } | {
26
+ kind: "disc";
27
+ c: Vec2;
28
+ r: number;
29
+ bright: number;
30
+ } | {
31
+ kind: "fill";
32
+ c: Vec2;
33
+ h: Vec2;
34
+ r: number;
35
+ bright: number;
36
+ };
37
+ export interface PackedShapes {
38
+ /** Geometry: `seg` a.xy, b.xy; `rrect` and `fill` centre, half size; `circle` and `disc` centre, radius. */
39
+ a: Vec4[];
40
+ /** Kind, corner radius, tag, brightness. */
41
+ b: Vec4[];
42
+ /** Cumulative share of the particles, by line length (or area, for fills). The last one is past 1. */
43
+ cdf: number[];
44
+ /** Divide by it to keep the lines' brightness independent of how much art there is. */
45
+ totalWeight: number;
46
+ }
47
+ /**
48
+ * Packs line art into rows for `shapePoint(A, B, b, c)` in the shader.
49
+ * Particles pick a shape by the CDF, so every line gets the same particle
50
+ * density. `tag` fills the free slot, `B.z`, like the plate a shape is on.
51
+ */
52
+ export declare function packShapes<S extends Shape>(shapes: readonly S[], tag?: (shape: S) => number): PackedShapes;
53
+ //#endregion
@@ -0,0 +1,72 @@
1
+ //#region src/gl/shapes.ts
2
+ /** Shape kinds as `shapePoint` reads them, in `B.x`. */
3
+ const KIND = {
4
+ seg: 0,
5
+ rrect: 1,
6
+ circle: 2,
7
+ disc: 3,
8
+ fill: 4
9
+ };
10
+ const shapeWeight = (shape) => {
11
+ if (shape.kind === "seg") return Math.hypot(shape.b[0] - shape.a[0], shape.b[1] - shape.a[1]);
12
+ if (shape.kind === "rrect") {
13
+ const r = Math.min(shape.r, shape.h[0], shape.h[1]);
14
+ return 4 * (shape.h[0] + shape.h[1]) - 8 * r + 2 * Math.PI * r;
15
+ }
16
+ if (shape.kind === "circle") return 2 * Math.PI * shape.r;
17
+ if (shape.kind === "disc") return Math.PI * shape.r * shape.r * 24;
18
+ return 4 * shape.h[0] * shape.h[1] * 24;
19
+ };
20
+ /**
21
+ * Packs line art into rows for `shapePoint(A, B, b, c)` in the shader.
22
+ * Particles pick a shape by the CDF, so every line gets the same particle
23
+ * density. `tag` fills the free slot, `B.z`, like the plate a shape is on.
24
+ */
25
+ function packShapes(shapes, tag = () => 0) {
26
+ if (shapes.length === 0) throw new Error("Line art needs at least one shape.");
27
+ const weights = shapes.map(shapeWeight);
28
+ const totalWeight = weights.reduce((sum, w) => sum + w, 0);
29
+ let running = 0;
30
+ const cdf = weights.map((w) => (running += w) / totalWeight);
31
+ cdf[cdf.length - 1] = 1.0001;
32
+ return {
33
+ a: shapes.map((shape) => {
34
+ if (shape.kind === "seg") return [...shape.a, ...shape.b];
35
+ if (shape.kind === "rrect" || shape.kind === "fill") return [...shape.c, ...shape.h];
36
+ return [
37
+ ...shape.c,
38
+ shape.r,
39
+ 0
40
+ ];
41
+ }),
42
+ b: shapes.map((shape) => [
43
+ KIND[shape.kind],
44
+ shape.kind === "rrect" || shape.kind === "fill" ? shape.r : 0,
45
+ tag(shape),
46
+ shape.bright
47
+ ]),
48
+ cdf,
49
+ totalWeight
50
+ };
51
+ }
52
+ /** The shader side of `packShapes`, in the helpers every formation gets. */
53
+ const SHAPE_POINT = `
54
+ // A point on a shape packed by packShapes() (rows A and B), from two uniform
55
+ // randoms: outlines take b along the line, fills spread b and c over the area.
56
+ vec2 shapePoint(vec4 A, vec4 B, float b, float c) {
57
+ int kind = int(B.x + 0.5);
58
+ if (kind == 0) return mix(A.xy, A.zw, b);
59
+ if (kind == 1) return rrectPoint(A, B.y, b);
60
+ if (kind == 2) return A.xy + A.z * vec2(cos(b * TAU), sin(b * TAU));
61
+ if (kind == 3) return A.xy + A.z * sqrt(c) * vec2(cos(b * TAU), sin(b * TAU));
62
+ // Even over the rectangle, with the corners pulled in onto their arcs.
63
+ vec2 p = (vec2(b, c) * 2.0 - 1.0) * A.zw;
64
+ vec2 inner = A.zw - B.y;
65
+ vec2 d = max(abs(p) - inner, 0.0);
66
+ float len = length(d);
67
+ if (len > B.y) p = sign(p) * (inner + d * (B.y / len));
68
+ return A.xy + p;
69
+ }
70
+ `;
71
+ //#endregion
72
+ export { SHAPE_POINT, packShapes };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Mat4, Vec2, Vec3, clamp, damp, lerp, lerp3, projectToScreen, smoothstep } from "./gl/math.js";
1
+ import { Mat4, Vec2, Vec3, Vec4, clamp, damp, lerp, lerp3, projectToScreen, smoothstep } from "./gl/math.js";
2
2
  import { AnchorDef, Formation, FormationDef, FormationFrame, FormationLook, FormationOptions, FormationProgress, FormationShader, FormationViews, StageView, Uniforms } from "./gl/types.js";
3
+ import { PackedShapes, Shape, packShapes } from "./gl/shapes.js";
3
4
  import { Experience, ExperienceOptions, ExperienceState, GlStatus, createExperience, initialExperienceState } from "./experience.js";
4
5
  import { ShaderPart, composeParticleShader, namespaceGlsl } from "./gl/compose.js";
5
6
  import { BASE_PARTICLES, FLOOR_Y } from "./gl/constants.js";
6
7
  import { defineFormation, mergeViews } from "./gl/formation.js";
7
- import { glslFloat } from "./gl/glsl.js";
8
+ import { glslArray, glslFloat, glslVec } from "./gl/glsl.js";
8
9
  import { SectionSpan, approachOf, localOf, presenceOf, progressOf, stageAt } from "./gl/stage.js";
9
10
  import { STACKED_QUERY } from "./layout.js";
10
- export { type AnchorDef, BASE_PARTICLES, type Experience, type ExperienceOptions, type ExperienceState, FLOOR_Y, type Formation, type FormationDef, type FormationFrame, type FormationLook, type FormationOptions, type FormationProgress, type FormationShader, type FormationViews, type GlStatus, type Mat4, STACKED_QUERY, type SectionSpan, type ShaderPart, type StageView, type Uniforms, type Vec2, type Vec3, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslFloat, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
11
+ export { type AnchorDef, BASE_PARTICLES, type Experience, type ExperienceOptions, type ExperienceState, FLOOR_Y, type Formation, type FormationDef, type FormationFrame, type FormationLook, type FormationOptions, type FormationProgress, type FormationShader, type FormationViews, type GlStatus, type Mat4, type PackedShapes, STACKED_QUERY, type SectionSpan, type ShaderPart, type Shape, type StageView, type Uniforms, type Vec2, type Vec3, type Vec4, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
package/dist/index.js CHANGED
@@ -2,8 +2,9 @@ import { clamp, damp, lerp, lerp3, projectToScreen, smoothstep } from "./gl/math
2
2
  import { approachOf, localOf, presenceOf, progressOf, stageAt } from "./gl/stage.js";
3
3
  import { createExperience, initialExperienceState } from "./experience.js";
4
4
  import { BASE_PARTICLES, FLOOR_Y } from "./gl/constants.js";
5
- import { glslFloat } from "./gl/glsl.js";
5
+ import { packShapes } from "./gl/shapes.js";
6
+ import { glslArray, glslFloat, glslVec } from "./gl/glsl.js";
6
7
  import { composeParticleShader, namespaceGlsl } from "./gl/compose.js";
7
8
  import { defineFormation, mergeViews } from "./gl/formation.js";
8
9
  import { STACKED_QUERY } from "./layout.js";
9
- export { BASE_PARTICLES, FLOOR_Y, STACKED_QUERY, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslFloat, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
10
+ export { BASE_PARTICLES, FLOOR_Y, STACKED_QUERY, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxfront/scene",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The scroll-driven WebGL particle scene behind the uxfront.com family of homepages: a pluggable formation pipeline, a formation catalog and a framework-agnostic scroll controller.",
5
5
  "keywords": [
6
6
  "particles",