@uxfront/scene 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.
- package/README.md +128 -0
- package/dist/experience.d.ts +40 -0
- package/dist/experience.js +306 -0
- package/dist/formations/corridor/index.d.ts +8 -0
- package/dist/formations/corridor/index.js +52 -0
- package/dist/formations/corridor/shader.js +37 -0
- package/dist/formations/index.d.ts +11 -0
- package/dist/formations/index.js +11 -0
- package/dist/formations/ink/index.d.ts +10 -0
- package/dist/formations/ink/index.js +53 -0
- package/dist/formations/ink/shader.js +177 -0
- package/dist/formations/monolith/index.d.ts +5 -0
- package/dist/formations/monolith/index.js +49 -0
- package/dist/formations/monolith/mark.js +89 -0
- package/dist/formations/monolith/shader.js +120 -0
- package/dist/formations/plates/art.d.ts +46 -0
- package/dist/formations/plates/art.js +238 -0
- package/dist/formations/plates/geometry.d.ts +5 -0
- package/dist/formations/plates/geometry.js +8 -0
- package/dist/formations/plates/index.d.ts +13 -0
- package/dist/formations/plates/index.js +67 -0
- package/dist/formations/plates/shader.js +97 -0
- package/dist/formations/prism/geometry.d.ts +4 -0
- package/dist/formations/prism/geometry.js +15 -0
- package/dist/formations/prism/index.d.ts +9 -0
- package/dist/formations/prism/index.js +64 -0
- package/dist/formations/prism/shader.js +140 -0
- package/dist/formations/tokens/geometry.d.ts +4 -0
- package/dist/formations/tokens/geometry.js +22 -0
- package/dist/formations/tokens/index.d.ts +18 -0
- package/dist/formations/tokens/index.js +79 -0
- package/dist/formations/tokens/shader.js +125 -0
- package/dist/gl/compose.d.ts +14 -0
- package/dist/gl/compose.js +52 -0
- package/dist/gl/constants.d.ts +6 -0
- package/dist/gl/constants.js +7 -0
- package/dist/gl/engine.js +559 -0
- package/dist/gl/formation.d.ts +7 -0
- package/dist/gl/formation.js +39 -0
- package/dist/gl/glsl.d.ts +9 -0
- package/dist/gl/glsl.js +419 -0
- package/dist/gl/math.d.ts +16 -0
- package/dist/gl/math.js +102 -0
- package/dist/gl/stage.d.ts +28 -0
- package/dist/gl/stage.js +55 -0
- package/dist/gl/types.d.ts +103 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +9 -0
- package/dist/layout.d.ts +9 -0
- package/dist/layout.js +10 -0
- package/package.json +51 -0
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
import { clamp, damp, lerp, lerp3, lookAt, mat4, multiply, perspective, smoothstep } from "./math.js";
|
|
2
|
+
import { approachOf, localOf, presenceOf } from "./stage.js";
|
|
3
|
+
import { BASE_PARTICLES } from "./constants.js";
|
|
4
|
+
import { BLOOM_DOWN_FRAG, BLOOM_PREFILTER_FRAG, BLOOM_UP_FRAG, COMPOSITE_FRAG, PARTICLE_FRAG, QUAD_VERT, TRAIL_FRAG } from "./glsl.js";
|
|
5
|
+
import { composeParticleShader } from "./compose.js";
|
|
6
|
+
import { STACKED_QUERY } from "../layout.js";
|
|
7
|
+
//#region src/gl/engine.ts
|
|
8
|
+
const PARTICLES = {
|
|
9
|
+
high: BASE_PARTICLES,
|
|
10
|
+
medium: 147456,
|
|
11
|
+
low: 65536
|
|
12
|
+
};
|
|
13
|
+
const PIXEL_RATIO = {
|
|
14
|
+
high: 2,
|
|
15
|
+
medium: 1.5,
|
|
16
|
+
low: 1.25
|
|
17
|
+
};
|
|
18
|
+
const BLOOM_LEVELS = 5;
|
|
19
|
+
const INTRO_SECONDS = 2.8;
|
|
20
|
+
const FADE_SECONDS = 1.1;
|
|
21
|
+
const MAX_BUFFER = 4096;
|
|
22
|
+
const SOFTWARE_RENDERER = /swiftshader|llvmpipe|softpipe|software|basic render/i;
|
|
23
|
+
const toLinear = (channel) => (channel / 255) ** 2.2;
|
|
24
|
+
const BACKGROUND = [
|
|
25
|
+
toLinear(5),
|
|
26
|
+
toLinear(5),
|
|
27
|
+
toLinear(7)
|
|
28
|
+
];
|
|
29
|
+
function createProgram(gl, vertex, fragment) {
|
|
30
|
+
const program = gl.createProgram();
|
|
31
|
+
const compile = (type, source) => {
|
|
32
|
+
const shader = gl.createShader(type);
|
|
33
|
+
gl.shaderSource(shader, source);
|
|
34
|
+
gl.compileShader(shader);
|
|
35
|
+
gl.attachShader(program, shader);
|
|
36
|
+
return shader;
|
|
37
|
+
};
|
|
38
|
+
const shaders = [compile(gl.VERTEX_SHADER, vertex), compile(gl.FRAGMENT_SHADER, fragment)];
|
|
39
|
+
gl.linkProgram(program);
|
|
40
|
+
return {
|
|
41
|
+
program,
|
|
42
|
+
shaders,
|
|
43
|
+
uniforms: /* @__PURE__ */ new Map()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const uniform = (p, name) => p.uniforms.get(name) ?? null;
|
|
47
|
+
/** Pulls the camera back until `span` fits in the frame beside the lens shift. */
|
|
48
|
+
function fitEye(stageView, aspect, out) {
|
|
49
|
+
const dx = stageView.eye[0] - stageView.target[0];
|
|
50
|
+
const dy = stageView.eye[1] - stageView.target[1];
|
|
51
|
+
const dz = stageView.eye[2] - stageView.target[2];
|
|
52
|
+
const dist = Math.hypot(dx, dy, dz);
|
|
53
|
+
const tanY = Math.tan(stageView.fov * Math.PI / 360);
|
|
54
|
+
const tanX = tanY * aspect;
|
|
55
|
+
let need = dist;
|
|
56
|
+
if (stageView.span[0] > 0) need = Math.max(need, stageView.span[0] / (tanX * Math.max(.2, 1 - Math.abs(stageView.shift[0]))));
|
|
57
|
+
if (stageView.span[1] > 0) need = Math.max(need, stageView.span[1] / (tanY * Math.max(.2, 1 - Math.abs(stageView.shift[1]))));
|
|
58
|
+
const k = need / dist;
|
|
59
|
+
out[0] = stageView.target[0] + dx * k;
|
|
60
|
+
out[1] = stageView.target[1] + dy * k;
|
|
61
|
+
out[2] = stageView.target[2] + dz * k;
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
|
|
65
|
+
/**
|
|
66
|
+
* Waits for the driver to finish compiling without ever blocking the main
|
|
67
|
+
* thread on a status query (KHR_parallel_shader_compile).
|
|
68
|
+
*/
|
|
69
|
+
async function finishPrograms(gl, programs, parallel) {
|
|
70
|
+
if (parallel) while (!programs.every((p) => gl.getProgramParameter(p.program, parallel.COMPLETION_STATUS_KHR))) await nextFrame();
|
|
71
|
+
for (const p of programs) {
|
|
72
|
+
if (!gl.getProgramParameter(p.program, gl.LINK_STATUS)) {
|
|
73
|
+
for (const shader of p.shaders) {
|
|
74
|
+
const log = gl.getShaderInfoLog(shader);
|
|
75
|
+
if (log) console.warn(log);
|
|
76
|
+
}
|
|
77
|
+
console.warn(gl.getProgramInfoLog(p.program));
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const count = Number(gl.getProgramParameter(p.program, gl.ACTIVE_UNIFORMS));
|
|
81
|
+
for (let i = 0; i < count; i++) {
|
|
82
|
+
const info = gl.getActiveUniform(p.program, i);
|
|
83
|
+
if (!info) continue;
|
|
84
|
+
const location = gl.getUniformLocation(p.program, info.name);
|
|
85
|
+
if (location) p.uniforms.set(info.name.replace(/\[0\]$/, ""), location);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Renders a scene of formations as one GPU particle system (WebGL2, no
|
|
92
|
+
* textures or models), then trails, bloom and a filmic composite. Resolves to
|
|
93
|
+
* null without WebGL2, on software rasterizers, when a shader fails to compile
|
|
94
|
+
* or when the context is lost meanwhile, so the caller can show a fallback.
|
|
95
|
+
*/
|
|
96
|
+
async function createEngine(canvas, options) {
|
|
97
|
+
const { formations, shaders: formationShaders } = options;
|
|
98
|
+
const stageCount = formations.length;
|
|
99
|
+
const gl = canvas.getContext("webgl2", {
|
|
100
|
+
alpha: false,
|
|
101
|
+
antialias: false,
|
|
102
|
+
depth: false,
|
|
103
|
+
stencil: false,
|
|
104
|
+
premultipliedAlpha: false,
|
|
105
|
+
preserveDrawingBuffer: false,
|
|
106
|
+
powerPreference: "high-performance",
|
|
107
|
+
failIfMajorPerformanceCaveat: true
|
|
108
|
+
});
|
|
109
|
+
if (!gl) return null;
|
|
110
|
+
const debug = gl.getExtension("WEBGL_debug_renderer_info");
|
|
111
|
+
const renderer = String(gl.getParameter(debug ? debug.UNMASKED_RENDERER_WEBGL : gl.RENDERER));
|
|
112
|
+
if (SOFTWARE_RENDERER.test(renderer)) {
|
|
113
|
+
gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
let hdr = !!gl.getExtension("EXT_color_buffer_float");
|
|
117
|
+
const parallel = gl.getExtension("KHR_parallel_shader_compile");
|
|
118
|
+
const particleProgram = createProgram(gl, composeParticleShader(formations.map((formation, i) => ({
|
|
119
|
+
key: formation.key,
|
|
120
|
+
glsl: formationShaders[i].glsl
|
|
121
|
+
}))), PARTICLE_FRAG);
|
|
122
|
+
const trailProgram = createProgram(gl, QUAD_VERT, TRAIL_FRAG);
|
|
123
|
+
const prefilterProgram = createProgram(gl, QUAD_VERT, BLOOM_PREFILTER_FRAG);
|
|
124
|
+
const downProgram = createProgram(gl, QUAD_VERT, BLOOM_DOWN_FRAG);
|
|
125
|
+
const upProgram = createProgram(gl, QUAD_VERT, BLOOM_UP_FRAG);
|
|
126
|
+
const compositeProgram = createProgram(gl, QUAD_VERT, COMPOSITE_FRAG);
|
|
127
|
+
const programs = [
|
|
128
|
+
particleProgram,
|
|
129
|
+
trailProgram,
|
|
130
|
+
prefilterProgram,
|
|
131
|
+
downProgram,
|
|
132
|
+
upProgram,
|
|
133
|
+
compositeProgram
|
|
134
|
+
];
|
|
135
|
+
if (!await finishPrograms(gl, programs, parallel) || gl.isContextLost()) return null;
|
|
136
|
+
const formationUniforms = formations.map((formation) => ({
|
|
137
|
+
gl,
|
|
138
|
+
get: (name) => uniform(particleProgram, `${formation.key}_${name}`)
|
|
139
|
+
}));
|
|
140
|
+
gl.useProgram(particleProgram.program);
|
|
141
|
+
formationShaders.forEach((shader, i) => shader.setup?.(formationUniforms[i]));
|
|
142
|
+
const vao = gl.createVertexArray();
|
|
143
|
+
gl.bindVertexArray(vao);
|
|
144
|
+
gl.disable(gl.DEPTH_TEST);
|
|
145
|
+
gl.disable(gl.CULL_FACE);
|
|
146
|
+
const maxBuffer = Math.min(MAX_BUFFER, Number(gl.getParameter(gl.MAX_TEXTURE_SIZE)), Number(gl.getParameter(gl.MAX_RENDERBUFFER_SIZE)));
|
|
147
|
+
const createTarget = (width, height) => {
|
|
148
|
+
const tex = gl.createTexture();
|
|
149
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
150
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, hdr ? gl.RGBA16F : gl.RGBA8, width, height, 0, gl.RGBA, hdr ? gl.HALF_FLOAT : gl.UNSIGNED_BYTE, null);
|
|
151
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
152
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
153
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
154
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
155
|
+
const fbo = gl.createFramebuffer();
|
|
156
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
157
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
|
|
158
|
+
gl.clearColor(0, 0, 0, 1);
|
|
159
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
160
|
+
return {
|
|
161
|
+
fbo,
|
|
162
|
+
tex,
|
|
163
|
+
width,
|
|
164
|
+
height
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
const deleteTarget = (target) => {
|
|
168
|
+
gl.deleteFramebuffer(target.fbo);
|
|
169
|
+
gl.deleteTexture(target.tex);
|
|
170
|
+
};
|
|
171
|
+
if (hdr) {
|
|
172
|
+
const probe = createTarget(4, 4);
|
|
173
|
+
hdr = gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE;
|
|
174
|
+
deleteTarget(probe);
|
|
175
|
+
}
|
|
176
|
+
let quality = options.quality;
|
|
177
|
+
let particleCount = PARTICLES[quality];
|
|
178
|
+
let renderScale = 1;
|
|
179
|
+
let scene = null;
|
|
180
|
+
let trails = null;
|
|
181
|
+
let bloom = [];
|
|
182
|
+
const resize = () => {
|
|
183
|
+
const dpr = Math.min(window.devicePixelRatio || 1, PIXEL_RATIO[quality]) * renderScale;
|
|
184
|
+
const cssWidth = Math.max(1, canvas.clientWidth);
|
|
185
|
+
const cssHeight = Math.max(1, canvas.clientHeight);
|
|
186
|
+
const fit = Math.min(1, maxBuffer / Math.max(cssWidth * dpr, cssHeight * dpr));
|
|
187
|
+
const width = Math.max(1, Math.round(cssWidth * dpr * fit));
|
|
188
|
+
const height = Math.max(1, Math.round(cssHeight * dpr * fit));
|
|
189
|
+
if (scene && canvas.width === width && canvas.height === height) return;
|
|
190
|
+
canvas.width = width;
|
|
191
|
+
canvas.height = height;
|
|
192
|
+
if (scene) deleteTarget(scene);
|
|
193
|
+
trails?.forEach(deleteTarget);
|
|
194
|
+
bloom.forEach(deleteTarget);
|
|
195
|
+
scene = createTarget(width, height);
|
|
196
|
+
trails = [createTarget(width, height), createTarget(width, height)];
|
|
197
|
+
bloom = [];
|
|
198
|
+
let w = width;
|
|
199
|
+
let h = height;
|
|
200
|
+
for (let i = 0; i < BLOOM_LEVELS; i++) {
|
|
201
|
+
w = Math.max(1, w >> 1);
|
|
202
|
+
h = Math.max(1, h >> 1);
|
|
203
|
+
bloom.push(createTarget(w, h));
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
resize();
|
|
207
|
+
const projection = mat4();
|
|
208
|
+
const view = mat4();
|
|
209
|
+
const viewProjection = mat4();
|
|
210
|
+
const eye = [
|
|
211
|
+
0,
|
|
212
|
+
0,
|
|
213
|
+
8
|
|
214
|
+
];
|
|
215
|
+
const target = [
|
|
216
|
+
0,
|
|
217
|
+
0,
|
|
218
|
+
0
|
|
219
|
+
];
|
|
220
|
+
const eyeA = [
|
|
221
|
+
0,
|
|
222
|
+
0,
|
|
223
|
+
0
|
|
224
|
+
];
|
|
225
|
+
const eyeB = [
|
|
226
|
+
0,
|
|
227
|
+
0,
|
|
228
|
+
0
|
|
229
|
+
];
|
|
230
|
+
const rayDir = [
|
|
231
|
+
0,
|
|
232
|
+
0,
|
|
233
|
+
-1
|
|
234
|
+
];
|
|
235
|
+
const up = [
|
|
236
|
+
0,
|
|
237
|
+
1,
|
|
238
|
+
0
|
|
239
|
+
];
|
|
240
|
+
const shockAt = [
|
|
241
|
+
0,
|
|
242
|
+
0,
|
|
243
|
+
0
|
|
244
|
+
];
|
|
245
|
+
const basis = {
|
|
246
|
+
right: [
|
|
247
|
+
1,
|
|
248
|
+
0,
|
|
249
|
+
0
|
|
250
|
+
],
|
|
251
|
+
up: [
|
|
252
|
+
0,
|
|
253
|
+
1,
|
|
254
|
+
0
|
|
255
|
+
],
|
|
256
|
+
forward: [
|
|
257
|
+
0,
|
|
258
|
+
0,
|
|
259
|
+
-1
|
|
260
|
+
]
|
|
261
|
+
};
|
|
262
|
+
const shift = [0, 0];
|
|
263
|
+
const stackedQuery = matchMedia(STACKED_QUERY);
|
|
264
|
+
let fov = 30;
|
|
265
|
+
let time = 0;
|
|
266
|
+
let intro = options.reducedMotion ? 1 : 0;
|
|
267
|
+
let fade = 0;
|
|
268
|
+
let shockAge = -1;
|
|
269
|
+
let lastStage = 0;
|
|
270
|
+
let trailBoost = 0;
|
|
271
|
+
let pointerX = 0;
|
|
272
|
+
let pointerY = 0;
|
|
273
|
+
let lensX = 0;
|
|
274
|
+
let lensY = 0;
|
|
275
|
+
let lensAmount = 0;
|
|
276
|
+
let current = 0;
|
|
277
|
+
let lost = false;
|
|
278
|
+
let fps = 60;
|
|
279
|
+
let frames = 0;
|
|
280
|
+
let fpsClock = 0;
|
|
281
|
+
let warmup = 2.5;
|
|
282
|
+
let slowFor = 0;
|
|
283
|
+
let degradeLevel = 0;
|
|
284
|
+
/** A per-formation setting, blended across the morph between two formations. */
|
|
285
|
+
const blendLook = (stage, key) => {
|
|
286
|
+
const a = Math.min(Math.floor(stage), stageCount - 1);
|
|
287
|
+
const b = Math.min(a + 1, stageCount - 1);
|
|
288
|
+
return lerp(formations[a].look[key], formations[b].look[key], smoothstep(0, 1, stage - a));
|
|
289
|
+
};
|
|
290
|
+
const updateCamera = (stage, motion) => {
|
|
291
|
+
const aspect = canvas.width / canvas.height;
|
|
292
|
+
const stacked = stackedQuery.matches;
|
|
293
|
+
const a = Math.min(Math.floor(stage), stageCount - 1);
|
|
294
|
+
const b = Math.min(a + 1, stageCount - 1);
|
|
295
|
+
const e = smoothstep(0, 1, stage - a);
|
|
296
|
+
const va = stacked ? formations[a].views.mobile : formations[a].views.desktop;
|
|
297
|
+
const vb = stacked ? formations[b].views.mobile : formations[b].views.desktop;
|
|
298
|
+
fitEye(va, aspect, eyeA);
|
|
299
|
+
fitEye(vb, aspect, eyeB);
|
|
300
|
+
lerp3(eye, eyeA, eyeB, e);
|
|
301
|
+
lerp3(target, va.target, vb.target, e);
|
|
302
|
+
fov = lerp(va.fov, vb.fov, e);
|
|
303
|
+
shift[0] = lerp(va.shift[0], vb.shift[0], e);
|
|
304
|
+
shift[1] = lerp(va.shift[1], vb.shift[1], e);
|
|
305
|
+
const yaw = (motion ? Math.sin(time * .11) * .06 : 0) + pointerX * .1;
|
|
306
|
+
const pitch = (motion ? Math.sin(time * .07) * .02 : 0) + pointerY * .05;
|
|
307
|
+
let ox = eye[0] - target[0];
|
|
308
|
+
let oy = eye[1] - target[1];
|
|
309
|
+
let oz = eye[2] - target[2];
|
|
310
|
+
const cy = Math.cos(yaw);
|
|
311
|
+
const sy = Math.sin(yaw);
|
|
312
|
+
[ox, oz] = [ox * cy + oz * sy, -ox * sy + oz * cy];
|
|
313
|
+
const r = Math.hypot(ox, oy, oz);
|
|
314
|
+
const elevation = clamp(Math.asin(oy / r) + pitch, -1.2, 1.2);
|
|
315
|
+
const horizontal = Math.hypot(ox, oz) || 1;
|
|
316
|
+
const scale = r * Math.cos(elevation) / horizontal;
|
|
317
|
+
eye[0] = target[0] + ox * scale;
|
|
318
|
+
eye[1] = target[1] + r * Math.sin(elevation);
|
|
319
|
+
eye[2] = target[2] + oz * scale;
|
|
320
|
+
perspective(projection, fov * Math.PI / 180, aspect, .1, 80, shift[0], shift[1]);
|
|
321
|
+
lookAt(view, eye, target, up);
|
|
322
|
+
multiply(viewProjection, projection, view);
|
|
323
|
+
basis.right = [
|
|
324
|
+
view[0],
|
|
325
|
+
view[4],
|
|
326
|
+
view[8]
|
|
327
|
+
];
|
|
328
|
+
basis.up = [
|
|
329
|
+
view[1],
|
|
330
|
+
view[5],
|
|
331
|
+
view[9]
|
|
332
|
+
];
|
|
333
|
+
basis.forward = [
|
|
334
|
+
-view[2],
|
|
335
|
+
-view[6],
|
|
336
|
+
-view[10]
|
|
337
|
+
];
|
|
338
|
+
};
|
|
339
|
+
const rayThrough = (ndcX, ndcY, out) => {
|
|
340
|
+
const aspect = canvas.width / canvas.height;
|
|
341
|
+
const tanY = Math.tan(fov * Math.PI / 360);
|
|
342
|
+
const x = (ndcX - shift[0]) * tanY * aspect;
|
|
343
|
+
const y = (ndcY - shift[1]) * tanY;
|
|
344
|
+
const { right, up: camUp, forward } = basis;
|
|
345
|
+
out[0] = forward[0] + right[0] * x + camUp[0] * y;
|
|
346
|
+
out[1] = forward[1] + right[1] * x + camUp[1] * y;
|
|
347
|
+
out[2] = forward[2] + right[2] * x + camUp[2] * y;
|
|
348
|
+
const len = Math.hypot(out[0], out[1], out[2]) || 1;
|
|
349
|
+
out[0] /= len;
|
|
350
|
+
out[1] /= len;
|
|
351
|
+
out[2] /= len;
|
|
352
|
+
return out;
|
|
353
|
+
};
|
|
354
|
+
const drawQuad = () => gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
355
|
+
const bindTarget = (t) => {
|
|
356
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, t ? t.fbo : null);
|
|
357
|
+
gl.viewport(0, 0, t ? t.width : canvas.width, t ? t.height : canvas.height);
|
|
358
|
+
};
|
|
359
|
+
const bindTexture = (unit, tex) => {
|
|
360
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
361
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
362
|
+
};
|
|
363
|
+
const degrade = () => {
|
|
364
|
+
degradeLevel++;
|
|
365
|
+
if (degradeLevel === 1) renderScale = .8;
|
|
366
|
+
else if (degradeLevel === 2) {
|
|
367
|
+
renderScale = .65;
|
|
368
|
+
particleCount = Math.max(PARTICLES.low, particleCount >> 1);
|
|
369
|
+
} else if (degradeLevel === 3) {
|
|
370
|
+
quality = "low";
|
|
371
|
+
particleCount = PARTICLES.low >> 1;
|
|
372
|
+
}
|
|
373
|
+
resize();
|
|
374
|
+
};
|
|
375
|
+
const render = (input, dt) => {
|
|
376
|
+
if (lost || !scene || !trails) return;
|
|
377
|
+
const motion = input.motion;
|
|
378
|
+
if (motion) time += dt;
|
|
379
|
+
intro = motion ? Math.min(1, intro + dt / INTRO_SECONDS) : 1;
|
|
380
|
+
fade = Math.min(1, fade + dt / FADE_SECONDS);
|
|
381
|
+
if (shockAge >= 0) shockAge = shockAge + dt > 3 ? -1 : shockAge + dt;
|
|
382
|
+
pointerX = damp(pointerX, input.pointerActive && motion ? input.pointerX : 0, 2.5, dt);
|
|
383
|
+
pointerY = damp(pointerY, input.pointerActive && motion ? input.pointerY : 0, 2.5, dt);
|
|
384
|
+
lensX = damp(lensX, input.pointerX, 14, dt);
|
|
385
|
+
lensY = damp(lensY, input.pointerY, 14, dt);
|
|
386
|
+
lensAmount = damp(lensAmount, input.pointerActive && motion ? 1 : 0, 4, dt);
|
|
387
|
+
const stage = clamp(input.stage, 0, stageCount - 1);
|
|
388
|
+
const local = clamp(input.local, 0, 1);
|
|
389
|
+
const stageSpeed = Math.abs(stage - lastStage) / Math.max(dt, .001);
|
|
390
|
+
lastStage = stage;
|
|
391
|
+
trailBoost = damp(trailBoost, clamp(stageSpeed * .9, 0, .8), 6, dt);
|
|
392
|
+
updateCamera(stage, motion);
|
|
393
|
+
rayThrough(lensX, lensY, rayDir);
|
|
394
|
+
const intensity = BASE_PARTICLES / particleCount * (hdr ? 1 : .6);
|
|
395
|
+
const reflect = blendLook(stage, "reflect");
|
|
396
|
+
bindTarget(scene);
|
|
397
|
+
gl.clearColor(0, 0, 0, 1);
|
|
398
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
399
|
+
gl.enable(gl.BLEND);
|
|
400
|
+
gl.blendFunc(gl.ONE, gl.ONE);
|
|
401
|
+
const p = particleProgram;
|
|
402
|
+
gl.useProgram(p.program);
|
|
403
|
+
gl.uniformMatrix4fv(uniform(p, "uProjection"), false, projection);
|
|
404
|
+
gl.uniformMatrix4fv(uniform(p, "uView"), false, view);
|
|
405
|
+
gl.uniform1f(uniform(p, "uTime"), time);
|
|
406
|
+
gl.uniform1f(uniform(p, "uStage"), stage);
|
|
407
|
+
gl.uniform1f(uniform(p, "uLocal"), local);
|
|
408
|
+
gl.uniform1f(uniform(p, "uIntro"), intro);
|
|
409
|
+
gl.uniform1f(uniform(p, "uCount"), particleCount);
|
|
410
|
+
gl.uniform1f(uniform(p, "uPointScale"), scene.height / (2 * Math.tan(fov * Math.PI / 360)));
|
|
411
|
+
gl.uniform1f(uniform(p, "uIntensity"), intensity);
|
|
412
|
+
gl.uniform3fv(uniform(p, "uRayOrigin"), eye);
|
|
413
|
+
gl.uniform3fv(uniform(p, "uRayDir"), rayDir);
|
|
414
|
+
gl.uniform1f(uniform(p, "uPointer"), lensAmount);
|
|
415
|
+
gl.uniform4f(uniform(p, "uShock"), shockAt[0], shockAt[1], shockAt[2], shockAge);
|
|
416
|
+
gl.uniform1f(uniform(p, "uReflect"), reflect);
|
|
417
|
+
for (let k = 0; k < stageCount; k++) {
|
|
418
|
+
const shader = formationShaders[k];
|
|
419
|
+
if (!shader.update) continue;
|
|
420
|
+
const frame = {
|
|
421
|
+
stage,
|
|
422
|
+
local: localOf(k, stage, local),
|
|
423
|
+
approach: approachOf(k, stage),
|
|
424
|
+
presence: presenceOf(k, stage),
|
|
425
|
+
time,
|
|
426
|
+
dt,
|
|
427
|
+
motion,
|
|
428
|
+
scrolled: input.scrolled
|
|
429
|
+
};
|
|
430
|
+
shader.update(formationUniforms[k], frame);
|
|
431
|
+
}
|
|
432
|
+
gl.uniform1f(uniform(p, "uMirror"), 0);
|
|
433
|
+
gl.drawArrays(gl.POINTS, 0, particleCount);
|
|
434
|
+
if (reflect > .01) {
|
|
435
|
+
gl.uniform1f(uniform(p, "uMirror"), 1);
|
|
436
|
+
gl.drawArrays(gl.POINTS, 0, particleCount);
|
|
437
|
+
}
|
|
438
|
+
gl.disable(gl.BLEND);
|
|
439
|
+
const decay = motion ? Math.max(blendLook(stage, "trail"), trailBoost, (1 - intro) * .75) : 0;
|
|
440
|
+
const previous = trails[current];
|
|
441
|
+
current = 1 - current;
|
|
442
|
+
const trail = trails[current];
|
|
443
|
+
bindTarget(trail);
|
|
444
|
+
gl.useProgram(trailProgram.program);
|
|
445
|
+
bindTexture(0, scene.tex);
|
|
446
|
+
bindTexture(1, previous.tex);
|
|
447
|
+
gl.uniform1i(uniform(trailProgram, "uScene"), 0);
|
|
448
|
+
gl.uniform1i(uniform(trailProgram, "uPrevious"), 1);
|
|
449
|
+
gl.uniform1f(uniform(trailProgram, "uDecay"), decay);
|
|
450
|
+
drawQuad();
|
|
451
|
+
const threshold = .55;
|
|
452
|
+
const knee = .45;
|
|
453
|
+
bindTarget(bloom[0]);
|
|
454
|
+
gl.useProgram(prefilterProgram.program);
|
|
455
|
+
bindTexture(0, trail.tex);
|
|
456
|
+
gl.uniform1i(uniform(prefilterProgram, "uSource"), 0);
|
|
457
|
+
gl.uniform2f(uniform(prefilterProgram, "uTexel"), 1 / trail.width, 1 / trail.height);
|
|
458
|
+
gl.uniform4f(uniform(prefilterProgram, "uThreshold"), threshold, .10000000000000003, 2 * knee, .25 / knee);
|
|
459
|
+
drawQuad();
|
|
460
|
+
gl.useProgram(downProgram.program);
|
|
461
|
+
gl.uniform1i(uniform(downProgram, "uSource"), 0);
|
|
462
|
+
for (let i = 1; i < bloom.length; i++) {
|
|
463
|
+
const src = bloom[i - 1];
|
|
464
|
+
bindTarget(bloom[i]);
|
|
465
|
+
bindTexture(0, src.tex);
|
|
466
|
+
gl.uniform2f(uniform(downProgram, "uTexel"), .5 / src.width, .5 / src.height);
|
|
467
|
+
drawQuad();
|
|
468
|
+
}
|
|
469
|
+
gl.useProgram(upProgram.program);
|
|
470
|
+
gl.uniform1i(uniform(upProgram, "uSource"), 0);
|
|
471
|
+
gl.enable(gl.BLEND);
|
|
472
|
+
gl.blendFunc(gl.ONE, gl.ONE);
|
|
473
|
+
for (let i = bloom.length - 1; i > 0; i--) {
|
|
474
|
+
const src = bloom[i];
|
|
475
|
+
bindTarget(bloom[i - 1]);
|
|
476
|
+
bindTexture(0, src.tex);
|
|
477
|
+
gl.uniform2f(uniform(upProgram, "uTexel"), .5 / src.width, .5 / src.height);
|
|
478
|
+
drawQuad();
|
|
479
|
+
}
|
|
480
|
+
gl.disable(gl.BLEND);
|
|
481
|
+
bindTarget(null);
|
|
482
|
+
gl.useProgram(compositeProgram.program);
|
|
483
|
+
bindTexture(0, trail.tex);
|
|
484
|
+
bindTexture(1, bloom[0].tex);
|
|
485
|
+
gl.uniform1i(uniform(compositeProgram, "uScene"), 0);
|
|
486
|
+
gl.uniform1i(uniform(compositeProgram, "uBloom"), 1);
|
|
487
|
+
gl.uniform1f(uniform(compositeProgram, "uBloomStrength"), .55 * blendLook(stage, "bloom"));
|
|
488
|
+
gl.uniform1f(uniform(compositeProgram, "uExposure"), blendLook(stage, "exposure"));
|
|
489
|
+
gl.uniform1f(uniform(compositeProgram, "uTime"), time);
|
|
490
|
+
gl.uniform1f(uniform(compositeProgram, "uAberration"), .012 + trailBoost * .03);
|
|
491
|
+
gl.uniform1f(uniform(compositeProgram, "uFade"), smoothstep(0, 1, fade));
|
|
492
|
+
gl.uniform3fv(uniform(compositeProgram, "uBackground"), BACKGROUND);
|
|
493
|
+
drawQuad();
|
|
494
|
+
frames++;
|
|
495
|
+
fpsClock += dt;
|
|
496
|
+
if (fpsClock >= .5) {
|
|
497
|
+
fps = Math.round(frames / fpsClock);
|
|
498
|
+
frames = 0;
|
|
499
|
+
fpsClock = 0;
|
|
500
|
+
}
|
|
501
|
+
if (warmup > 0) warmup -= dt;
|
|
502
|
+
else if (degradeLevel < 3) {
|
|
503
|
+
slowFor = dt > .028 ? slowFor + dt : Math.max(0, slowFor - dt * .5);
|
|
504
|
+
if (slowFor > 1.5) {
|
|
505
|
+
slowFor = 0;
|
|
506
|
+
warmup = 1.5;
|
|
507
|
+
degrade();
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
const onLost = (event) => {
|
|
512
|
+
event.preventDefault();
|
|
513
|
+
lost = true;
|
|
514
|
+
options.onContextLost();
|
|
515
|
+
};
|
|
516
|
+
canvas.addEventListener("webglcontextlost", onLost);
|
|
517
|
+
return {
|
|
518
|
+
get particles() {
|
|
519
|
+
return particleCount;
|
|
520
|
+
},
|
|
521
|
+
get fps() {
|
|
522
|
+
return fps;
|
|
523
|
+
},
|
|
524
|
+
get time() {
|
|
525
|
+
return time;
|
|
526
|
+
},
|
|
527
|
+
viewProjection,
|
|
528
|
+
render,
|
|
529
|
+
resize,
|
|
530
|
+
shock(ndcX, ndcY) {
|
|
531
|
+
if (lost) return;
|
|
532
|
+
const dir = rayThrough(ndcX, ndcY, [
|
|
533
|
+
0,
|
|
534
|
+
0,
|
|
535
|
+
0
|
|
536
|
+
]);
|
|
537
|
+
const { forward } = basis;
|
|
538
|
+
const along = ((target[0] - eye[0]) * forward[0] + (target[1] - eye[1]) * forward[1] + (target[2] - eye[2]) * forward[2]) / Math.max(.05, dir[0] * forward[0] + dir[1] * forward[1] + dir[2] * forward[2]);
|
|
539
|
+
shockAt[0] = eye[0] + dir[0] * along;
|
|
540
|
+
shockAt[1] = eye[1] + dir[1] * along;
|
|
541
|
+
shockAt[2] = eye[2] + dir[2] * along;
|
|
542
|
+
shockAge = 0;
|
|
543
|
+
},
|
|
544
|
+
destroy() {
|
|
545
|
+
canvas.removeEventListener("webglcontextlost", onLost);
|
|
546
|
+
if (gl.isContextLost()) return;
|
|
547
|
+
for (const p of programs) {
|
|
548
|
+
p.shaders.forEach((s) => gl.deleteShader(s));
|
|
549
|
+
gl.deleteProgram(p.program);
|
|
550
|
+
}
|
|
551
|
+
if (scene) deleteTarget(scene);
|
|
552
|
+
trails?.forEach(deleteTarget);
|
|
553
|
+
bloom.forEach(deleteTarget);
|
|
554
|
+
gl.deleteVertexArray(vao);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
//#endregion
|
|
559
|
+
export { createEngine, fitEye };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Formation, FormationDef, FormationOptions, FormationViews } from "./types.js";
|
|
2
|
+
//#region src/gl/formation.d.ts
|
|
3
|
+
/** Checks a formation and fills in its defaults. Anchor ids become `key:id`. */
|
|
4
|
+
export declare function defineFormation(def: FormationDef): Formation;
|
|
5
|
+
/** Applies a catalog formation's `views` option over its default framing. */
|
|
6
|
+
export declare function mergeViews(base: FormationViews, override: FormationOptions["views"]): FormationViews;
|
|
7
|
+
//#endregion
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/gl/formation.ts
|
|
2
|
+
const KEY = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
3
|
+
const DEFAULT_LOOK = {
|
|
4
|
+
bloom: 1,
|
|
5
|
+
trail: 0,
|
|
6
|
+
exposure: 1,
|
|
7
|
+
reflect: 0
|
|
8
|
+
};
|
|
9
|
+
/** Checks a formation and fills in its defaults. Anchor ids become `key:id`. */
|
|
10
|
+
function defineFormation(def) {
|
|
11
|
+
if (!KEY.test(def.key)) throw new Error(`Formation key "${def.key}" must start with a letter and hold only letters and digits.`);
|
|
12
|
+
return {
|
|
13
|
+
...def,
|
|
14
|
+
look: {
|
|
15
|
+
...DEFAULT_LOOK,
|
|
16
|
+
...def.look
|
|
17
|
+
},
|
|
18
|
+
anchors: (def.anchors ?? []).map((anchor) => ({
|
|
19
|
+
...anchor,
|
|
20
|
+
id: `${def.key}:${anchor.id}`
|
|
21
|
+
}))
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const mergeView = (base, override) => ({
|
|
25
|
+
eye: [...override?.eye ?? base.eye],
|
|
26
|
+
target: [...override?.target ?? base.target],
|
|
27
|
+
fov: override?.fov ?? base.fov,
|
|
28
|
+
span: [...override?.span ?? base.span],
|
|
29
|
+
shift: [...override?.shift ?? base.shift]
|
|
30
|
+
});
|
|
31
|
+
/** Applies a catalog formation's `views` option over its default framing. */
|
|
32
|
+
function mergeViews(base, override) {
|
|
33
|
+
return {
|
|
34
|
+
desktop: mergeView(base.desktop, override?.desktop),
|
|
35
|
+
mobile: mergeView(base.mobile, override?.mobile)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { defineFormation, mergeViews };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/gl/glsl.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The particle pipeline shared by every formation, and the post-processing
|
|
4
|
+
* passes. Formations only supply `Particle $main(uint id, float sel, Frame f)`
|
|
5
|
+
* (see `compose.ts`); everything here runs for all of them.
|
|
6
|
+
*/
|
|
7
|
+
/** Formats a number as a GLSL float literal, for constants interpolated into shaders. */
|
|
8
|
+
export declare const glslFloat: (n: number) => string;
|
|
9
|
+
//#endregion
|