@wolsok/loading-wave 0.0.1 → 0.0.3

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.
Files changed (64) hide show
  1. package/README.md +146 -12
  2. package/esm2022/index.js +1 -0
  3. package/esm2022/index.js.map +1 -1
  4. package/esm2022/lib/canvas-wave-renderer.js +34 -12
  5. package/esm2022/lib/canvas-wave-renderer.js.map +1 -1
  6. package/esm2022/lib/choose-wave-renderer.js +8 -3
  7. package/esm2022/lib/choose-wave-renderer.js.map +1 -1
  8. package/esm2022/lib/loading-wave-options.js +16 -2
  9. package/esm2022/lib/loading-wave-options.js.map +1 -1
  10. package/esm2022/lib/loading-wave.js +11 -3
  11. package/esm2022/lib/loading-wave.js.map +1 -1
  12. package/esm2022/lib/renderer.js.map +1 -1
  13. package/esm2022/lib/wave-model.js +33 -9
  14. package/esm2022/lib/wave-model.js.map +1 -1
  15. package/esm2022/lib/webgl/echo.js +5 -0
  16. package/esm2022/lib/webgl/echo.js.map +1 -0
  17. package/esm2022/lib/webgl/loading-wave-shader.js +1 -0
  18. package/esm2022/lib/webgl/loading-wave-shader.js.map +1 -0
  19. package/esm2022/lib/webgl/shader-program.js +235 -0
  20. package/esm2022/lib/webgl/shader-program.js.map +1 -0
  21. package/esm2022/lib/webgl/shaders/ripple.shader.js +110 -0
  22. package/esm2022/lib/webgl/shaders/ripple.shader.js.map +1 -0
  23. package/esm2022/lib/webgl/webgl-wave-renderer.js +102 -92
  24. package/esm2022/lib/webgl/webgl-wave-renderer.js.map +1 -1
  25. package/esm2022/shaders/lib/webgl/loading-wave-shader.js +1 -0
  26. package/esm2022/shaders/lib/webgl/loading-wave-shader.js.map +1 -0
  27. package/esm2022/shaders/lib/webgl/shaders/circuit-traces.shader.js +136 -0
  28. package/esm2022/shaders/lib/webgl/shaders/circuit-traces.shader.js.map +1 -0
  29. package/esm2022/shaders/lib/webgl/shaders/data-bus.shader.js +91 -0
  30. package/esm2022/shaders/lib/webgl/shaders/data-bus.shader.js.map +1 -0
  31. package/esm2022/shaders/lib/webgl/shaders/die-scan.shader.js +128 -0
  32. package/esm2022/shaders/lib/webgl/shaders/die-scan.shader.js.map +1 -0
  33. package/esm2022/shaders/lib/webgl/shaders/signal-trace.shader.js +89 -0
  34. package/esm2022/shaders/lib/webgl/shaders/signal-trace.shader.js.map +1 -0
  35. package/esm2022/shaders/lib/webgl/shaders/spark-front.shader.js +130 -0
  36. package/esm2022/shaders/lib/webgl/shaders/spark-front.shader.js.map +1 -0
  37. package/esm2022/shaders/shaders.js +12 -0
  38. package/esm2022/shaders/shaders.js.map +1 -0
  39. package/esm2022/shaders/wolsok-loading-wave-shaders.js +5 -0
  40. package/esm2022/shaders/wolsok-loading-wave-shaders.js.map +1 -0
  41. package/index.d.ts +3 -1
  42. package/lib/canvas-wave-renderer.d.ts +2 -0
  43. package/lib/choose-wave-renderer.d.ts +3 -0
  44. package/lib/loading-wave-options.d.ts +24 -4
  45. package/lib/loading-wave.d.ts +3 -3
  46. package/lib/renderer.d.ts +5 -0
  47. package/lib/wave-model.d.ts +17 -6
  48. package/lib/webgl/echo.d.ts +4 -0
  49. package/lib/webgl/loading-wave-shader.d.ts +20 -0
  50. package/lib/webgl/shader-program.d.ts +33 -0
  51. package/lib/webgl/shaders/ripple.shader.d.ts +10 -0
  52. package/lib/webgl/webgl-wave-renderer.d.ts +18 -7
  53. package/package.json +5 -1
  54. package/shaders/lib/webgl/loading-wave-shader.d.ts +20 -0
  55. package/shaders/lib/webgl/shaders/circuit-traces.shader.d.ts +12 -0
  56. package/shaders/lib/webgl/shaders/data-bus.shader.d.ts +10 -0
  57. package/shaders/lib/webgl/shaders/die-scan.shader.d.ts +10 -0
  58. package/shaders/lib/webgl/shaders/signal-trace.shader.d.ts +10 -0
  59. package/shaders/lib/webgl/shaders/spark-front.shader.d.ts +8 -0
  60. package/shaders/shaders.d.ts +5 -0
  61. package/shaders/wolsok-loading-wave-shaders.d.ts +5 -0
  62. package/esm2022/lib/webgl/wave-shaders.js +0 -114
  63. package/esm2022/lib/webgl/wave-shaders.js.map +0 -1
  64. package/lib/webgl/wave-shaders.d.ts +0 -14
@@ -1,42 +1,24 @@
1
- import { dotsOpacity, sizeCanvas, } from '../renderer';
2
- import { frontRadius, hasPreviousSweep } from '../wave-model';
3
- import { ECHO_LAG, WAVE_FRAGMENT_SHADER, WAVE_VERTEX_SHADER, } from './wave-shaders';
4
- const UNIFORMS = [
5
- 'u_size',
6
- 'u_bufferSize',
7
- 'u_spacing',
8
- 'u_dotSize',
9
- 'u_color',
10
- 'u_center',
11
- 'u_frontRadius',
12
- 'u_echoRadius',
13
- 'u_travel',
14
- 'u_previousSweep',
15
- 'u_falloff',
16
- 'u_rise',
17
- 'u_maxPush',
18
- 'u_wave',
19
- 'u_dots',
20
- ];
21
- /** the shaders did not compile or link on this GPU */
22
- export class WaveShaderError extends Error {
23
- constructor() {
24
- super(...arguments);
25
- this.name = 'WaveShaderError';
26
- }
27
- }
1
+ import { sizeCanvas, } from '../renderer';
2
+ import { frontRadius, hasPreviousSweep, sweepPhase, } from '../wave-model';
3
+ import { linkWaveProgram, STANDARD_UNIFORMS, WaveShaderError, } from './shader-program';
4
+ import { ECHO_LAG } from './echo';
5
+ export { WaveShaderError };
28
6
  /**
29
- * Draws the wave with a WebGL2 fragment shader: the 2D canvas's look plus a
30
- * glow around lit dots and an echo ripple trailing the front.
7
+ * Draws the wave with a WebGL2 fragment shader (`LoadingWaveShader`) over a
8
+ * full-canvas triangle, setting the contract's `ws_*` uniforms and the
9
+ * shader's own each frame.
31
10
  */
32
11
  export class WebGlWaveRenderer {
33
12
  /** @throws WaveShaderError when the shaders fail on this GPU */
34
- constructor(canvas, gl, look, hooks) {
13
+ constructor(canvas, gl, look, shader, hooks) {
35
14
  this.canvas = canvas;
36
15
  this.gl = gl;
37
16
  this.look = look;
38
17
  this.hooks = hooks;
39
18
  this.kind = 'webgl';
19
+ this.ownLocations = new Map();
20
+ /** uniforms `setShaderUniforms` left out, set to 0 on the next draw */
21
+ this.pendingResets = {};
40
22
  this.lost = false;
41
23
  this.disposed = false;
42
24
  this.handleContextLost = (event) => {
@@ -44,8 +26,12 @@ export class WebGlWaveRenderer {
44
26
  this.lost = true;
45
27
  this.hooks.onContextLost();
46
28
  };
47
- this.program = linkProgram(gl);
48
- this.uniforms = Object.fromEntries(UNIFORMS.map((name) => [name, gl.getUniformLocation(this.program, name)]));
29
+ this.program = linkWaveProgram(gl, shader);
30
+ this.uniforms = Object.fromEntries(STANDARD_UNIFORMS.map((name) => [
31
+ name,
32
+ gl.getUniformLocation(this.program, name),
33
+ ]));
34
+ this.ownUniforms = shader.uniforms ?? {};
49
35
  canvas.addEventListener('webglcontextlost', this.handleContextLost);
50
36
  }
51
37
  /**
@@ -77,7 +63,7 @@ export class WebGlWaveRenderer {
77
63
  }
78
64
  gl.clearColor(0, 0, 0, 0);
79
65
  gl.clear(gl.COLOR_BUFFER_BIT);
80
- if (dotsOpacity(frame, this.look.color) <= 0) {
66
+ if (frame.dots <= 0) {
81
67
  return;
82
68
  }
83
69
  gl.useProgram(this.program);
@@ -87,6 +73,19 @@ export class WebGlWaveRenderer {
87
73
  setLook(look) {
88
74
  this.look = look;
89
75
  }
76
+ /**
77
+ * a uniform the new values leave out goes back to 0, the value GLSL gives
78
+ * every uniform before it is set
79
+ */
80
+ setShaderUniforms(uniforms) {
81
+ const next = uniforms ?? {};
82
+ for (const [name, value] of Object.entries(this.ownUniforms)) {
83
+ if (!(name in next)) {
84
+ this.pendingResets[name] = unsetValue(value);
85
+ }
86
+ }
87
+ this.ownUniforms = next;
88
+ }
90
89
  dispose() {
91
90
  if (this.disposed) {
92
91
  return;
@@ -100,22 +99,76 @@ export class WebGlWaveRenderer {
100
99
  this.hooks.onDispose?.();
101
100
  }
102
101
  setUniforms(geometry, frame) {
103
- const { gl, uniforms, look } = this;
104
- gl.uniform2f(uniforms.u_size, geometry.width, geometry.height);
105
- gl.uniform2f(uniforms.u_bufferSize, gl.drawingBufferWidth, gl.drawingBufferHeight);
106
- gl.uniform1f(uniforms.u_spacing, geometry.dotSpacing);
107
- gl.uniform1f(uniforms.u_dotSize, look.dotSize);
108
- gl.uniform3f(uniforms.u_color, look.color.r / 255, look.color.g / 255, look.color.b / 255);
109
- gl.uniform2f(uniforms.u_center, geometry.centerX, geometry.centerY);
110
- gl.uniform1f(uniforms.u_frontRadius, frontRadius(geometry, frame.elapsedMs));
111
- gl.uniform1f(uniforms.u_echoRadius, echoRadius(geometry, frame.elapsedMs));
112
- gl.uniform1f(uniforms.u_travel, geometry.travel);
113
- gl.uniform1f(uniforms.u_previousSweep, hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0);
114
- gl.uniform1f(uniforms.u_falloff, geometry.falloff);
115
- gl.uniform1f(uniforms.u_rise, geometry.rise);
116
- gl.uniform1f(uniforms.u_maxPush, geometry.maxPush);
117
- gl.uniform1f(uniforms.u_wave, frame.wave);
118
- gl.uniform1f(uniforms.u_dots, dotsOpacity(frame, look.color));
102
+ const { gl, uniforms: locations, look } = this;
103
+ const bufferWidth = gl.drawingBufferWidth;
104
+ const bufferHeight = gl.drawingBufferHeight;
105
+ gl.uniform2f(locations.ws_size, geometry.width, geometry.height);
106
+ gl.uniform2f(locations.ws_bufferSize, bufferWidth, bufferHeight);
107
+ gl.uniform1f(locations.ws_pixel, geometry.width / Math.max(1, bufferWidth));
108
+ gl.uniform1f(locations.ws_spacing, geometry.dotSpacing);
109
+ gl.uniform1f(locations.ws_dotSize, look.dotSize);
110
+ setColor(gl, locations.ws_color, look.color);
111
+ setColor(gl, locations.ws_accent, look.accent ?? look.color);
112
+ gl.uniform2f(locations.ws_center, geometry.centerX, geometry.centerY);
113
+ gl.uniform1f(locations.ws_frontRadius, frontRadius(geometry, frame.elapsedMs));
114
+ gl.uniform1f(locations.ws_echoRadius, echoRadius(geometry, frame.elapsedMs));
115
+ gl.uniform1f(locations.ws_travel, geometry.travel);
116
+ gl.uniform1f(locations.ws_falloff, geometry.falloff);
117
+ gl.uniform1f(locations.ws_rise, geometry.rise);
118
+ gl.uniform1f(locations.ws_tail, geometry.tail);
119
+ gl.uniform1f(locations.ws_maxPush, geometry.maxPush);
120
+ gl.uniform1f(locations.ws_phase, sweepPhase(geometry, frame.elapsedMs));
121
+ gl.uniform1f(locations.ws_time, frame.elapsedMs / 1000);
122
+ gl.uniform1f(locations.ws_previousSweep, hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0);
123
+ gl.uniform1f(locations.ws_wave, frame.wave);
124
+ gl.uniform1f(locations.ws_dots, frame.dots);
125
+ // a dropped uniform is reset once, then left alone
126
+ const own = { ...this.pendingResets, ...this.ownUniforms };
127
+ this.pendingResets = {};
128
+ for (const [name, value] of Object.entries(own)) {
129
+ // the ws_ uniforms are the lib's: a shader's own never override them
130
+ if (!name.startsWith('ws_')) {
131
+ setOwnUniform(gl, this.ownLocation(name), value);
132
+ }
133
+ }
134
+ }
135
+ /** where the shader's own uniform lives, looked up once per name */
136
+ ownLocation(name) {
137
+ if (!this.ownLocations.has(name)) {
138
+ this.ownLocations.set(name, this.gl.getUniformLocation(this.program, name));
139
+ }
140
+ return this.ownLocations.get(name) ?? null;
141
+ }
142
+ }
143
+ /** zeros of the same size as `value` */
144
+ const ZEROS = {
145
+ 1: [0],
146
+ 2: [0, 0],
147
+ 3: [0, 0, 0],
148
+ 4: [0, 0, 0, 0],
149
+ };
150
+ function unsetValue(value) {
151
+ return typeof value === 'number' ? 0 : ZEROS[value.length];
152
+ }
153
+ /** a colour as a straight RGBA `vec4`, each 0 to 1 */
154
+ function setColor(gl, location, { r, g, b, alpha = 1 }) {
155
+ gl.uniform4f(location, r / 255, g / 255, b / 255, alpha);
156
+ }
157
+ /** sets a `float` to `vec4` uniform by how many components the value has */
158
+ function setOwnUniform(gl, location, value) {
159
+ const [x, y = 0, z = 0, w = 0] = typeof value === 'number' ? [value] : value;
160
+ const size = typeof value === 'number' ? 1 : value.length;
161
+ if (size === 1) {
162
+ gl.uniform1f(location, x);
163
+ }
164
+ else if (size === 2) {
165
+ gl.uniform2f(location, x, y);
166
+ }
167
+ else if (size === 3) {
168
+ gl.uniform3f(location, x, y, z);
169
+ }
170
+ else {
171
+ gl.uniform4f(location, x, y, z, w);
119
172
  }
120
173
  }
121
174
  /**
@@ -126,47 +179,4 @@ export function echoRadius(geometry, elapsedMs) {
126
179
  const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;
127
180
  return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);
128
181
  }
129
- function compileShader(gl, type, source) {
130
- const shader = gl.createShader(type);
131
- if (shader == null) {
132
- throw new WaveShaderError('could not create a shader');
133
- }
134
- gl.shaderSource(shader, source);
135
- gl.compileShader(shader);
136
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
137
- const log = gl.getShaderInfoLog(shader);
138
- gl.deleteShader(shader);
139
- throw new WaveShaderError(`shader did not compile: ${log}`);
140
- }
141
- return shader;
142
- }
143
- function linkProgram(gl) {
144
- const vertex = compileShader(gl, gl.VERTEX_SHADER, WAVE_VERTEX_SHADER);
145
- let fragment;
146
- try {
147
- fragment = compileShader(gl, gl.FRAGMENT_SHADER, WAVE_FRAGMENT_SHADER);
148
- }
149
- catch (error) {
150
- gl.deleteShader(vertex);
151
- throw error;
152
- }
153
- const program = gl.createProgram();
154
- if (program == null) {
155
- gl.deleteShader(vertex);
156
- gl.deleteShader(fragment);
157
- throw new WaveShaderError('could not create a program');
158
- }
159
- gl.attachShader(program, vertex);
160
- gl.attachShader(program, fragment);
161
- gl.linkProgram(program);
162
- // the program keeps what it needs
163
- gl.deleteShader(vertex);
164
- gl.deleteShader(fragment);
165
- if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
166
- const log = gl.getProgramInfoLog(program);
167
- gl.deleteProgram(program);
168
- throw new WaveShaderError(`shaders did not link: ${log}`);
169
- }
170
- return program;
171
- }
172
182
  //# sourceMappingURL=webgl-wave-renderer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"webgl-wave-renderer.js","sourceRoot":"","sources":["../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/webgl-wave-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,GAIX,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AAC5E,OAAO,EACL,QAAQ,EACR,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAExB,MAAM,QAAQ,GAAG;IACf,QAAQ;IACR,cAAc;IACd,WAAW;IACX,WAAW;IACX,SAAS;IACT,UAAU;IACV,eAAe;IACf,cAAc;IACd,UAAU;IACV,iBAAiB;IACjB,WAAW;IACX,QAAQ;IACR,WAAW;IACX,QAAQ;IACR,QAAQ;CACA,CAAC;AAIX,sDAAsD;AACtD,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAA1C;;QAC2B,SAAI,GAAG,iBAAiB,CAAC;IACpD,CAAC;CAAA;AASD;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAc5B,gEAAgE;IAChE,YACkB,MAAyB,EACxB,EAA0B,EACnC,IAAc,EACL,KAA6B;QAH9B,WAAM,GAAN,MAAM,CAAmB;QACxB,OAAE,GAAF,EAAE,CAAwB;QACnC,SAAI,GAAJ,IAAI,CAAU;QACL,UAAK,GAAL,KAAK,CAAwB;QAlBhC,SAAI,GAAG,OAAO,CAAC;QAIvB,SAAI,GAAG,KAAK,CAAC;QACb,aAAQ,GAAG,KAAK,CAAC;QAER,sBAAiB,GAAG,CAAC,KAAY,EAAQ,EAAE;YAC1D,KAAK,CAAC,cAAc,EAAE,CAAC;YACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC;QAC7B,CAAC,CAAC;QASA,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;QAC/B,IAAI,CAAC,Q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AAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import {\n dotsOpacity,\n sizeCanvas,\n WaveFrame,\n WaveLook,\n WaveRenderer,\n} from '../renderer';\nimport { frontRadius, hasPreviousSweep, WaveGeometry } from '../wave-model';\nimport {\n ECHO_LAG,\n WAVE_FRAGMENT_SHADER,\n WAVE_VERTEX_SHADER,\n} from './wave-shaders';\n\nconst UNIFORMS = [\n 'u_size',\n 'u_bufferSize',\n 'u_spacing',\n 'u_dotSize',\n 'u_color',\n 'u_center',\n 'u_frontRadius',\n 'u_echoRadius',\n 'u_travel',\n 'u_previousSweep',\n 'u_falloff',\n 'u_rise',\n 'u_maxPush',\n 'u_wave',\n 'u_dots',\n] as const;\n\ntype UniformName = (typeof UNIFORMS)[number];\n\n/** the shaders did not compile or link on this GPU */\nexport class WaveShaderError extends Error {\n public override readonly name = 'WaveShaderError';\n}\n\nexport interface WebGlWaveRendererHooks {\n /** the browser took the GPU context away; the renderer draws no more */\n readonly onContextLost: () => void;\n /** called once by dispose(), after the context was freed */\n readonly onDispose?: () => void;\n}\n\n/**\n * Draws the wave with a WebGL2 fragment shader: the 2D canvas's look plus a\n * glow around lit dots and an echo ripple trailing the front.\n */\nexport class WebGlWaveRenderer implements WaveRenderer {\n public readonly kind = 'webgl';\n private readonly program: WebGLProgram;\n private readonly uniforms: Record<UniformName, WebGLUniformLocation | null>;\n private geometry?: WaveGeometry;\n private lost = false;\n private disposed = false;\n\n private readonly handleContextLost = (event: Event): void => {\n event.preventDefault();\n this.lost = true;\n this.hooks.onContextLost();\n };\n\n /** @throws WaveShaderError when the shaders fail on this GPU */\n public constructor(\n public readonly canvas: HTMLCanvasElement,\n private readonly gl: WebGL2RenderingContext,\n private look: WaveLook,\n private readonly hooks: WebGlWaveRendererHooks\n ) {\n this.program = linkProgram(gl);\n this.uniforms = Object.fromEntries(\n UNIFORMS.map((name) => [name, gl.getUniformLocation(this.program, name)])\n ) as Record<UniformName, WebGLUniformLocation | null>;\n canvas.addEventListener('webglcontextlost', this.handleContextLost);\n }\n\n /**\n * A WebGL2 context, or `null` where the browser offers none. With\n * `hardwareOnly` also `null` where it would render on the CPU (no GPU, a\n * blocked driver, a software renderer).\n */\n public static createContext(\n canvas: HTMLCanvasElement,\n hardwareOnly = true\n ): WebGL2RenderingContext | null {\n return canvas.getContext('webgl2', {\n alpha: true,\n premultipliedAlpha: true,\n antialias: false,\n depth: false,\n stencil: false,\n powerPreference: 'low-power',\n failIfMajorPerformanceCaveat: hardwareOnly,\n });\n }\n\n public resize(geometry: WaveGeometry, pixelRatio: number): void {\n this.geometry = geometry;\n sizeCanvas(this.canvas, geometry, pixelRatio);\n // a GPU may give a smaller drawing buffer than the canvas asks for\n this.gl.viewport(\n 0,\n 0,\n this.gl.drawingBufferWidth,\n this.gl.drawingBufferHeight\n );\n }\n\n public draw(frame: WaveFrame): void {\n const { gl, geometry } = this;\n if (this.lost || this.disposed || geometry == null) {\n return;\n }\n gl.clearColor(0, 0, 0, 0);\n gl.clear(gl.COLOR_BUFFER_BIT);\n if (dotsOpacity(frame, this.look.color) <= 0) {\n return;\n }\n gl.useProgram(this.program);\n this.setUniforms(geometry, frame);\n gl.drawArrays(gl.TRIANGLES, 0, 3);\n }\n\n public setLook(look: WaveLook): void {\n this.look = look;\n }\n\n public dispose(): void {\n if (this.disposed) {\n return;\n }\n this.disposed = true;\n this.canvas.removeEventListener('webglcontextlost', this.handleContextLost);\n if (!this.gl.isContextLost()) {\n this.gl.deleteProgram(this.program);\n this.gl.getExtension('WEBGL_lose_context')?.loseContext();\n }\n this.hooks.onDispose?.();\n }\n\n private setUniforms(geometry: WaveGeometry, frame: WaveFrame): void {\n const { gl, uniforms, look } = this;\n gl.uniform2f(uniforms.u_size, geometry.width, geometry.height);\n gl.uniform2f(\n uniforms.u_bufferSize,\n gl.drawingBufferWidth,\n gl.drawingBufferHeight\n );\n gl.uniform1f(uniforms.u_spacing, geometry.dotSpacing);\n gl.uniform1f(uniforms.u_dotSize, look.dotSize);\n gl.uniform3f(\n uniforms.u_color,\n look.color.r / 255,\n look.color.g / 255,\n look.color.b / 255\n );\n gl.uniform2f(uniforms.u_center, geometry.centerX, geometry.centerY);\n gl.uniform1f(\n uniforms.u_frontRadius,\n frontRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(uniforms.u_echoRadius, echoRadius(geometry, frame.elapsedMs));\n gl.uniform1f(uniforms.u_travel, geometry.travel);\n gl.uniform1f(\n uniforms.u_previousSweep,\n hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0\n );\n gl.uniform1f(uniforms.u_falloff, geometry.falloff);\n gl.uniform1f(uniforms.u_rise, geometry.rise);\n gl.uniform1f(uniforms.u_maxPush, geometry.maxPush);\n gl.uniform1f(uniforms.u_wave, frame.wave);\n gl.uniform1f(uniforms.u_dots, dotsOpacity(frame, look.color));\n }\n}\n\n/**\n * The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,\n * so no echo shows, until the front has run that far.\n */\nexport function echoRadius(geometry: WaveGeometry, elapsedMs: number): number {\n const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;\n return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);\n}\n\nfunction compileShader(\n gl: WebGL2RenderingContext,\n type: number,\n source: string\n): WebGLShader {\n const shader = gl.createShader(type);\n if (shader == null) {\n throw new WaveShaderError('could not create a shader');\n }\n gl.shaderSource(shader, source);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const log = gl.getShaderInfoLog(shader);\n gl.deleteShader(shader);\n throw new WaveShaderError(`shader did not compile: ${log}`);\n }\n return shader;\n}\n\nfunction linkProgram(gl: WebGL2RenderingContext): WebGLProgram {\n const vertex = compileShader(gl, gl.VERTEX_SHADER, WAVE_VERTEX_SHADER);\n let fragment: WebGLShader;\n try {\n fragment = compileShader(gl, gl.FRAGMENT_SHADER, WAVE_FRAGMENT_SHADER);\n } catch (error) {\n gl.deleteShader(vertex);\n throw error;\n }\n const program = gl.createProgram();\n if (program == null) {\n gl.deleteShader(vertex);\n gl.deleteShader(fragment);\n throw new WaveShaderError('could not create a program');\n }\n gl.attachShader(program, vertex);\n gl.attachShader(program, fragment);\n gl.linkProgram(program);\n // the program keeps what it needs\n gl.deleteShader(vertex);\n gl.deleteShader(fragment);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const log = gl.getProgramInfoLog(program);\n gl.deleteProgram(program);\n throw new WaveShaderError(`shaders did not link: ${log}`);\n }\n return program;\n}\n"]}
1
+ {"version":3,"file":"webgl-wave-renderer.js","sourceRoot":"","sources":["../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/webgl-wave-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,GAKX,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,UAAU,GAEX,MAAM,eAAe,CAAC;AAMvB,OAAO,EACL,eAAe,EACf,iBAAiB,EAEjB,eAAe,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,eAAe,EAAE,CAAC;AAS3B;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IAwB5B,gEAAgE;IAChE,YACkB,MAAyB,EACxB,EAA0B,EACnC,IAAc,EACtB,MAAyB,EACR,KAA6B;QAJ9B,WAAM,GAAN,MAAM,CAAmB;QACxB,OAAE,GAAF,EAAE,CAAwB;QACnC,SAAI,GAAJ,IAAI,CAAU;QAEL,UAAK,GAAL,KAAK,CAAwB;QA7BhC,SAAI,GAAG,OAAO,CAAC;QAMd,iBAAY,GAAG,IAAI,GAAG,EAGpC,CAAC;QAEJ,uEAAuE;QAC/D,kBAAa,GAAuC,EAAE,CAAC;QAEvD,SAAI,GAAG,KAAK,CAAC;QACb,aAAQ,GAAG,KAAK,CAAC;QAER,sBAAiB,GAAG,CAAC,KAAY,EAAQ,EAAE;YAC1D,KAAK,CAAC,c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,SAAS,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAClC,CAAC;SAAM,CAAC;QACN,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACrC,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,QAAsB,EAAE,SAAiB;IAClE,MAAM,WAAW,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC;IAC5D,OAAO,WAAW,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import {\n sizeCanvas,\n WaveFrame,\n WaveLook,\n WaveRenderer,\n type Rgb,\n} from '../renderer';\nimport {\n frontRadius,\n hasPreviousSweep,\n sweepPhase,\n WaveGeometry,\n} from '../wave-model';\nimport type {\n LoadingWaveShader,\n ShaderUniforms,\n ShaderUniformValue,\n} from './loading-wave-shader';\nimport {\n linkWaveProgram,\n STANDARD_UNIFORMS,\n StandardUniform,\n WaveShaderError,\n} from './shader-program';\nimport { ECHO_LAG } from './echo';\n\nexport { WaveShaderError };\n\nexport interface WebGlWaveRendererHooks {\n /** the browser took the GPU context away; the renderer draws no more */\n readonly onContextLost: () => void;\n /** called once by dispose(), after the context was freed */\n readonly onDispose?: () => void;\n}\n\n/**\n * Draws the wave with a WebGL2 fragment shader (`LoadingWaveShader`) over a\n * full-canvas triangle, setting the contract's `ws_*` uniforms and the\n * shader's own each frame.\n */\nexport class WebGlWaveRenderer implements WaveRenderer {\n public readonly kind = 'webgl';\n private readonly program: WebGLProgram;\n private readonly uniforms: Record<\n StandardUniform,\n WebGLUniformLocation | null\n >;\n private readonly ownLocations = new Map<\n string,\n WebGLUniformLocation | null\n >();\n private ownUniforms: ShaderUniforms;\n /** uniforms `setShaderUniforms` left out, set to 0 on the next draw */\n private pendingResets: Record<string, ShaderUniformValue> = {};\n private geometry?: WaveGeometry;\n private lost = false;\n private disposed = false;\n\n private readonly handleContextLost = (event: Event): void => {\n event.preventDefault();\n this.lost = true;\n this.hooks.onContextLost();\n };\n\n /** @throws WaveShaderError when the shaders fail on this GPU */\n public constructor(\n public readonly canvas: HTMLCanvasElement,\n private readonly gl: WebGL2RenderingContext,\n private look: WaveLook,\n shader: LoadingWaveShader,\n private readonly hooks: WebGlWaveRendererHooks\n ) {\n this.program = linkWaveProgram(gl, shader);\n this.uniforms = Object.fromEntries(\n STANDARD_UNIFORMS.map((name) => [\n name,\n gl.getUniformLocation(this.program, name),\n ])\n ) as Record<StandardUniform, WebGLUniformLocation | null>;\n this.ownUniforms = shader.uniforms ?? {};\n canvas.addEventListener('webglcontextlost', this.handleContextLost);\n }\n\n /**\n * A WebGL2 context, or `null` where the browser offers none. With\n * `hardwareOnly` also `null` where it would render on the CPU (no GPU, a\n * blocked driver, a software renderer).\n */\n public static createContext(\n canvas: HTMLCanvasElement,\n hardwareOnly = true\n ): WebGL2RenderingContext | null {\n return canvas.getContext('webgl2', {\n alpha: true,\n premultipliedAlpha: true,\n antialias: false,\n depth: false,\n stencil: false,\n powerPreference: 'low-power',\n failIfMajorPerformanceCaveat: hardwareOnly,\n });\n }\n\n public resize(geometry: WaveGeometry, pixelRatio: number): void {\n this.geometry = geometry;\n sizeCanvas(this.canvas, geometry, pixelRatio);\n // a GPU may give a smaller drawing buffer than the canvas asks for\n this.gl.viewport(\n 0,\n 0,\n this.gl.drawingBufferWidth,\n this.gl.drawingBufferHeight\n );\n }\n\n public draw(frame: WaveFrame): void {\n const { gl, geometry } = this;\n if (this.lost || this.disposed || geometry == null) {\n return;\n }\n gl.clearColor(0, 0, 0, 0);\n gl.clear(gl.COLOR_BUFFER_BIT);\n if (frame.dots <= 0) {\n return;\n }\n gl.useProgram(this.program);\n this.setUniforms(geometry, frame);\n gl.drawArrays(gl.TRIANGLES, 0, 3);\n }\n\n public setLook(look: WaveLook): void {\n this.look = look;\n }\n\n /**\n * a uniform the new values leave out goes back to 0, the value GLSL gives\n * every uniform before it is set\n */\n public setShaderUniforms(uniforms: ShaderUniforms | undefined): void {\n const next = uniforms ?? {};\n for (const [name, value] of Object.entries(this.ownUniforms)) {\n if (!(name in next)) {\n this.pendingResets[name] = unsetValue(value);\n }\n }\n this.ownUniforms = next;\n }\n\n public dispose(): void {\n if (this.disposed) {\n return;\n }\n this.disposed = true;\n this.canvas.removeEventListener('webglcontextlost', this.handleContextLost);\n if (!this.gl.isContextLost()) {\n this.gl.deleteProgram(this.program);\n this.gl.getExtension('WEBGL_lose_context')?.loseContext();\n }\n this.hooks.onDispose?.();\n }\n\n private setUniforms(geometry: WaveGeometry, frame: WaveFrame): void {\n const { gl, uniforms: locations, look } = this;\n const bufferWidth = gl.drawingBufferWidth;\n const bufferHeight = gl.drawingBufferHeight;\n gl.uniform2f(locations.ws_size, geometry.width, geometry.height);\n gl.uniform2f(locations.ws_bufferSize, bufferWidth, bufferHeight);\n gl.uniform1f(locations.ws_pixel, geometry.width / Math.max(1, bufferWidth));\n gl.uniform1f(locations.ws_spacing, geometry.dotSpacing);\n gl.uniform1f(locations.ws_dotSize, look.dotSize);\n setColor(gl, locations.ws_color, look.color);\n setColor(gl, locations.ws_accent, look.accent ?? look.color);\n gl.uniform2f(locations.ws_center, geometry.centerX, geometry.centerY);\n gl.uniform1f(\n locations.ws_frontRadius,\n frontRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(\n locations.ws_echoRadius,\n echoRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(locations.ws_travel, geometry.travel);\n gl.uniform1f(locations.ws_falloff, geometry.falloff);\n gl.uniform1f(locations.ws_rise, geometry.rise);\n gl.uniform1f(locations.ws_tail, geometry.tail);\n gl.uniform1f(locations.ws_maxPush, geometry.maxPush);\n gl.uniform1f(locations.ws_phase, sweepPhase(geometry, frame.elapsedMs));\n gl.uniform1f(locations.ws_time, frame.elapsedMs / 1000);\n gl.uniform1f(\n locations.ws_previousSweep,\n hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0\n );\n gl.uniform1f(locations.ws_wave, frame.wave);\n gl.uniform1f(locations.ws_dots, frame.dots);\n // a dropped uniform is reset once, then left alone\n const own = { ...this.pendingResets, ...this.ownUniforms };\n this.pendingResets = {};\n for (const [name, value] of Object.entries(own)) {\n // the ws_ uniforms are the lib's: a shader's own never override them\n if (!name.startsWith('ws_')) {\n setOwnUniform(gl, this.ownLocation(name), value);\n }\n }\n }\n\n /** where the shader's own uniform lives, looked up once per name */\n private ownLocation(name: string): WebGLUniformLocation | null {\n if (!this.ownLocations.has(name)) {\n this.ownLocations.set(\n name,\n this.gl.getUniformLocation(this.program, name)\n );\n }\n return this.ownLocations.get(name) ?? null;\n }\n}\n\n/** zeros of the same size as `value` */\nconst ZEROS: Record<1 | 2 | 3 | 4, ShaderUniformValue> = {\n 1: [0],\n 2: [0, 0],\n 3: [0, 0, 0],\n 4: [0, 0, 0, 0],\n};\n\nfunction unsetValue(value: ShaderUniformValue): ShaderUniformValue {\n return typeof value === 'number' ? 0 : ZEROS[value.length];\n}\n\n/** a colour as a straight RGBA `vec4`, each 0 to 1 */\nfunction setColor(\n gl: WebGL2RenderingContext,\n location: WebGLUniformLocation | null,\n { r, g, b, alpha = 1 }: Rgb\n): void {\n gl.uniform4f(location, r / 255, g / 255, b / 255, alpha);\n}\n\n/** sets a `float` to `vec4` uniform by how many components the value has */\nfunction setOwnUniform(\n gl: WebGL2RenderingContext,\n location: WebGLUniformLocation | null,\n value: ShaderUniformValue\n): void {\n const [x, y = 0, z = 0, w = 0] = typeof value === 'number' ? [value] : value;\n const size = typeof value === 'number' ? 1 : value.length;\n if (size === 1) {\n gl.uniform1f(location, x);\n } else if (size === 2) {\n gl.uniform2f(location, x, y);\n } else if (size === 3) {\n gl.uniform3f(location, x, y, z);\n } else {\n gl.uniform4f(location, x, y, z, w);\n }\n}\n\n/**\n * The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,\n * so no echo shows, until the front has run that far.\n */\nexport function echoRadius(geometry: WaveGeometry, elapsedMs: number): number {\n const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;\n return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);\n}\n"]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=loading-wave-shader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loading-wave-shader.js","sourceRoot":"","sources":["../../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/loading-wave-shader.ts"],"names":[],"mappings":"","sourcesContent":["/** a uniform's value: a `float`, or the components of a `vec2` to `vec4` */\nexport type ShaderUniformValue =\n | number\n | readonly [number]\n | readonly [number, number]\n | readonly [number, number, number]\n | readonly [number, number, number, number];\n\n/** values of the uniforms a fragment declares itself, by their GLSL name */\nexport type ShaderUniforms = Readonly<Record<string, ShaderUniformValue>>;\n\n/**\n * A fragment shader for the loading wave's WebGL renderer.\n *\n * `fragment` is GLSL ES 3.00 that defines `vec4 wsShade(vec2 point)`: the\n * straight (not premultiplied) colour and opacity at `point`, in CSS pixels\n * from the host's top left corner. The lib puts the `ws_*` uniforms and\n * helpers before it and a `main` after it (see the README), so the fragment\n * holds neither `#version` nor `main`. At `ws_wave == 0` it draws its resting\n * state, without `ws_time`: that is what `afterLoading: 'keep'` leaves and\n * what `prefers-reduced-motion` shows.\n */\nexport interface LoadingWaveShader {\n readonly fragment: string;\n /** values of uniforms the fragment declares itself; never `ws_*` ones */\n readonly uniforms?: ShaderUniforms;\n}\n"]}
@@ -0,0 +1,136 @@
1
+ /**
2
+ * A printed circuit board: each grid cell leads a copper trace on to the
3
+ * right, now and then jogging a row up or down at 45° within a pair of rows,
4
+ * so the cells join into traces running left to right, with the dots as
5
+ * solder points and some of them pads. Ahead of the front the board lies
6
+ * dark; behind it the traces light up in the accent colour and current
7
+ * pulses, bright packets with comet tails, run on along them at a speed per
8
+ * pair of rows (a differential pair, so a pulse runs on through a jog),
9
+ * flashing the pads they pass, then die out in the afterglow.
10
+ */
11
+ export const circuitTracesShader = {
12
+ fragment: `
13
+ const float TRACE_WIDTH = 0.07;
14
+ const float TRACE_REST_ALPHA = 0.12;
15
+ const float TRACE_LIT_ALPHA = 0.6;
16
+ const float PAD_RADIUS = 0.17;
17
+ const float PAD_HOLE = 0.45;
18
+ const float PAD_CHANCE = 0.35;
19
+ const float PAD_RING_ALPHA = 0.7;
20
+ const float PAD_FLASH = 1.5;
21
+ const float PULSE_GLOW_WIDTH = 0.2;
22
+ const float PULSE_ALPHA = 0.9;
23
+ const float PULSE_HEAD_GLOW = 0.5;
24
+ const float PULSE_HEAD_EDGE = 0.15;
25
+ const float PULSE_PERIOD_SPACINGS = 7.0;
26
+ const float PULSE_TAIL_SPACINGS = 1.6;
27
+ const float PULSE_SPEED_SPACINGS = 9.0;
28
+ const float PULSE_SPEED_SPREAD = 0.6;
29
+ const float PULSE_WHITENING = 0.4;
30
+ const float JOG_CHANCE = 0.4;
31
+ const float GAP_CHANCE = 0.1;
32
+
33
+ /* the pair of rows a row belongs to; traces jog only within a pair */
34
+ float rowPair(float row) {
35
+ return floor(row * 0.5);
36
+ }
37
+
38
+ /*
39
+ * Where the trace leaving a cell goes: 0 a gap, 1 straight on to the right,
40
+ * 2 a jog a row down, -2 a jog a row up. In a pair only the top row jogs
41
+ * down or the bottom row jogs up at a column, so no traces cross.
42
+ */
43
+ float traceKind(vec2 cell) {
44
+ if (ws_hash(cell + vec2(17.0, 3.0)) < GAP_CHANCE) {
45
+ return 0.0;
46
+ }
47
+ float pair = rowPair(cell.y);
48
+ bool top = cell.y - 2.0 * pair < 0.5;
49
+ float jog = ws_hash(vec2(cell.x, pair) + vec2(5.0, 41.0));
50
+ if (jog < 0.5 * JOG_CHANCE) {
51
+ return top ? 2.0 : 1.0;
52
+ }
53
+ if (jog < JOG_CHANCE) {
54
+ return top ? 1.0 : -2.0;
55
+ }
56
+ return 1.0;
57
+ }
58
+
59
+ /* where the trace leaving a cell ends: the next column's centre, maybe a row off */
60
+ vec2 traceEnd(vec2 cell, float kind) {
61
+ float rows = kind > 1.5 ? 1.0 : (kind < -1.5 ? -1.0 : 0.0);
62
+ return ws_cellCenter(cell + vec2(1.0, rows));
63
+ }
64
+
65
+ /* how far x lies behind the head of a pair's current pulse, 0 up to one period */
66
+ float behindPulse(float x, float pair) {
67
+ float speed = PULSE_SPEED_SPACINGS * ws_spacing
68
+ * (1.0 - 0.5 * PULSE_SPEED_SPREAD + PULSE_SPEED_SPREAD * ws_hash(vec2(pair, 7.0)));
69
+ float period = PULSE_PERIOD_SPACINGS * ws_spacing;
70
+ return mod(ws_time * speed - x + ws_hash(vec2(pair, 13.0)) * period, period);
71
+ }
72
+
73
+ /* a pulse's brightness: 1 at its head, a comet tail behind it */
74
+ float comet(float behind) {
75
+ float period = PULSE_PERIOD_SPACINGS * ws_spacing;
76
+ return exp(-behind / (PULSE_TAIL_SPACINGS * ws_spacing))
77
+ * smoothstep(0.0, PULSE_HEAD_EDGE * ws_spacing, period - behind);
78
+ }
79
+
80
+ vec4 wsShade(vec2 point) {
81
+ vec2 cell = ws_cell(point);
82
+ float traceWidth = max(TRACE_WIDTH * ws_spacing, 1.5 * ws_pixel);
83
+ float glowWidth = PULSE_GLOW_WIDTH * ws_spacing;
84
+ float nearest = 1.0e6;
85
+ float tracePair = rowPair(cell.y);
86
+ for (int row = -1; row <= 1; row++) {
87
+ for (int column = -1; column <= 0; column++) {
88
+ vec2 from = cell + vec2(float(column), float(row));
89
+ float kind = traceKind(from);
90
+ if (kind == 0.0) {
91
+ continue;
92
+ }
93
+ float away = ws_segmentDistance(point, ws_cellCenter(from), traceEnd(from, kind));
94
+ if (away < nearest) {
95
+ nearest = away;
96
+ tracePair = rowPair(from.y);
97
+ }
98
+ }
99
+ }
100
+ float trace = ws_band(nearest, traceWidth);
101
+ float lit = ws_waveIntensity(point);
102
+ float behind = behindPulse(point.x, tracePair);
103
+ float current = lit * comet(behind);
104
+ // the head glows round, off the trace too
105
+ float toHead = min(behind, PULSE_PERIOD_SPACINGS * ws_spacing - behind);
106
+ float headGlow = lit * exp(-(toHead * toHead + nearest * nearest) / (2.0 * glowWidth * glowWidth));
107
+ vec4 ink = ws_ink(lit);
108
+ float copper = trace * ink.a * mix(TRACE_REST_ALPHA, TRACE_LIT_ALPHA, lit);
109
+ float pulse = ws_accent.a * PULSE_ALPHA * max(current * trace, PULSE_HEAD_GLOW * headGlow);
110
+
111
+ // every dot of the grid is a solder point; some are pads, a copper ring around it
112
+ vec2 padCenter = ws_cellCenter(cell);
113
+ bool onGrid = all(lessThan(padCenter, ws_size));
114
+ float padLit = ws_waveIntensity(padCenter);
115
+ // a passing pulse makes the pad flash brighter than the wave alone
116
+ float flash = clamp(padLit * (1.0 + PAD_FLASH * comet(behindPulse(padCenter.x, rowPair(cell.y)))), 0.0, 1.0);
117
+ float padRadius = max(PAD_RADIUS * ws_spacing, 0.75 * ws_dotSize + ws_pixel);
118
+ float ring = ws_hash(cell + vec2(29.0, 11.0)) < PAD_CHANCE
119
+ ? ws_dotDisk(point, padCenter, padRadius)
120
+ * (1.0 - ws_dotDisk(point, padCenter, PAD_HOLE * padRadius))
121
+ : 0.0;
122
+ float pad = onGrid ? max(PAD_RING_ALPHA * ring, ws_dotDisk(point, padCenter, 0.5 * ws_dotSize)) : 0.0;
123
+ vec4 padInk = ws_ink(flash);
124
+ float padAlpha = pad * padInk.a * mix(WS_REST_ALPHA, 1.0, flash);
125
+
126
+ // the pad sits on top of the trace, the pulse on top of both
127
+ float alpha = padAlpha + (1.0 - padAlpha) * copper;
128
+ vec3 color = padInk.rgb * padAlpha + ink.rgb * (1.0 - padAlpha) * copper;
129
+ vec3 hot = mix(ws_accent.rgb, vec3(1.0), PULSE_WHITENING * current);
130
+ color = color * (1.0 - pulse) + hot * pulse;
131
+ alpha = alpha * (1.0 - pulse) + pulse;
132
+ return alpha <= 0.0 ? vec4(0.0) : vec4(color / alpha, alpha);
133
+ }
134
+ `,
135
+ };
136
+ //# sourceMappingURL=circuit-traces.shader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"circuit-traces.shader.js","sourceRoot":"","sources":["../../../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/shaders/circuit-traces.shader.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAsB;IACpD,QAAQ,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0HX;CACA,CAAC","sourcesContent":["import type { LoadingWaveShader } from '../loading-wave-shader';\n\n/**\n * A printed circuit board: each grid cell leads a copper trace on to the\n * right, now and then jogging a row up or down at 45° within a pair of rows,\n * so the cells join into traces running left to right, with the dots as\n * solder points and some of them pads. Ahead of the front the board lies\n * dark; behind it the traces light up in the accent colour and current\n * pulses, bright packets with comet tails, run on along them at a speed per\n * pair of rows (a differential pair, so a pulse runs on through a jog),\n * flashing the pads they pass, then die out in the afterglow.\n */\nexport const circuitTracesShader: LoadingWaveShader = {\n fragment: `\nconst float TRACE_WIDTH = 0.07;\nconst float TRACE_REST_ALPHA = 0.12;\nconst float TRACE_LIT_ALPHA = 0.6;\nconst float PAD_RADIUS = 0.17;\nconst float PAD_HOLE = 0.45;\nconst float PAD_CHANCE = 0.35;\nconst float PAD_RING_ALPHA = 0.7;\nconst float PAD_FLASH = 1.5;\nconst float PULSE_GLOW_WIDTH = 0.2;\nconst float PULSE_ALPHA = 0.9;\nconst float PULSE_HEAD_GLOW = 0.5;\nconst float PULSE_HEAD_EDGE = 0.15;\nconst float PULSE_PERIOD_SPACINGS = 7.0;\nconst float PULSE_TAIL_SPACINGS = 1.6;\nconst float PULSE_SPEED_SPACINGS = 9.0;\nconst float PULSE_SPEED_SPREAD = 0.6;\nconst float PULSE_WHITENING = 0.4;\nconst float JOG_CHANCE = 0.4;\nconst float GAP_CHANCE = 0.1;\n\n/* the pair of rows a row belongs to; traces jog only within a pair */\nfloat rowPair(float row) {\n return floor(row * 0.5);\n}\n\n/*\n * Where the trace leaving a cell goes: 0 a gap, 1 straight on to the right,\n * 2 a jog a row down, -2 a jog a row up. In a pair only the top row jogs\n * down or the bottom row jogs up at a column, so no traces cross.\n */\nfloat traceKind(vec2 cell) {\n if (ws_hash(cell + vec2(17.0, 3.0)) < GAP_CHANCE) {\n return 0.0;\n }\n float pair = rowPair(cell.y);\n bool top = cell.y - 2.0 * pair < 0.5;\n float jog = ws_hash(vec2(cell.x, pair) + vec2(5.0, 41.0));\n if (jog < 0.5 * JOG_CHANCE) {\n return top ? 2.0 : 1.0;\n }\n if (jog < JOG_CHANCE) {\n return top ? 1.0 : -2.0;\n }\n return 1.0;\n}\n\n/* where the trace leaving a cell ends: the next column's centre, maybe a row off */\nvec2 traceEnd(vec2 cell, float kind) {\n float rows = kind > 1.5 ? 1.0 : (kind < -1.5 ? -1.0 : 0.0);\n return ws_cellCenter(cell + vec2(1.0, rows));\n}\n\n/* how far x lies behind the head of a pair's current pulse, 0 up to one period */\nfloat behindPulse(float x, float pair) {\n float speed = PULSE_SPEED_SPACINGS * ws_spacing\n * (1.0 - 0.5 * PULSE_SPEED_SPREAD + PULSE_SPEED_SPREAD * ws_hash(vec2(pair, 7.0)));\n float period = PULSE_PERIOD_SPACINGS * ws_spacing;\n return mod(ws_time * speed - x + ws_hash(vec2(pair, 13.0)) * period, period);\n}\n\n/* a pulse's brightness: 1 at its head, a comet tail behind it */\nfloat comet(float behind) {\n float period = PULSE_PERIOD_SPACINGS * ws_spacing;\n return exp(-behind / (PULSE_TAIL_SPACINGS * ws_spacing))\n * smoothstep(0.0, PULSE_HEAD_EDGE * ws_spacing, period - behind);\n}\n\nvec4 wsShade(vec2 point) {\n vec2 cell = ws_cell(point);\n float traceWidth = max(TRACE_WIDTH * ws_spacing, 1.5 * ws_pixel);\n float glowWidth = PULSE_GLOW_WIDTH * ws_spacing;\n float nearest = 1.0e6;\n float tracePair = rowPair(cell.y);\n for (int row = -1; row <= 1; row++) {\n for (int column = -1; column <= 0; column++) {\n vec2 from = cell + vec2(float(column), float(row));\n float kind = traceKind(from);\n if (kind == 0.0) {\n continue;\n }\n float away = ws_segmentDistance(point, ws_cellCenter(from), traceEnd(from, kind));\n if (away < nearest) {\n nearest = away;\n tracePair = rowPair(from.y);\n }\n }\n }\n float trace = ws_band(nearest, traceWidth);\n float lit = ws_waveIntensity(point);\n float behind = behindPulse(point.x, tracePair);\n float current = lit * comet(behind);\n // the head glows round, off the trace too\n float toHead = min(behind, PULSE_PERIOD_SPACINGS * ws_spacing - behind);\n float headGlow = lit * exp(-(toHead * toHead + nearest * nearest) / (2.0 * glowWidth * glowWidth));\n vec4 ink = ws_ink(lit);\n float copper = trace * ink.a * mix(TRACE_REST_ALPHA, TRACE_LIT_ALPHA, lit);\n float pulse = ws_accent.a * PULSE_ALPHA * max(current * trace, PULSE_HEAD_GLOW * headGlow);\n\n // every dot of the grid is a solder point; some are pads, a copper ring around it\n vec2 padCenter = ws_cellCenter(cell);\n bool onGrid = all(lessThan(padCenter, ws_size));\n float padLit = ws_waveIntensity(padCenter);\n // a passing pulse makes the pad flash brighter than the wave alone\n float flash = clamp(padLit * (1.0 + PAD_FLASH * comet(behindPulse(padCenter.x, rowPair(cell.y)))), 0.0, 1.0);\n float padRadius = max(PAD_RADIUS * ws_spacing, 0.75 * ws_dotSize + ws_pixel);\n float ring = ws_hash(cell + vec2(29.0, 11.0)) < PAD_CHANCE\n ? ws_dotDisk(point, padCenter, padRadius)\n * (1.0 - ws_dotDisk(point, padCenter, PAD_HOLE * padRadius))\n : 0.0;\n float pad = onGrid ? max(PAD_RING_ALPHA * ring, ws_dotDisk(point, padCenter, 0.5 * ws_dotSize)) : 0.0;\n vec4 padInk = ws_ink(flash);\n float padAlpha = pad * padInk.a * mix(WS_REST_ALPHA, 1.0, flash);\n\n // the pad sits on top of the trace, the pulse on top of both\n float alpha = padAlpha + (1.0 - padAlpha) * copper;\n vec3 color = padInk.rgb * padAlpha + ink.rgb * (1.0 - padAlpha) * copper;\n vec3 hot = mix(ws_accent.rgb, vec3(1.0), PULSE_WHITENING * current);\n color = color * (1.0 - pulse) + hot * pulse;\n alpha = alpha * (1.0 - pulse) + pulse;\n return alpha <= 0.0 ? vec4(0.0) : vec4(color / alpha, alpha);\n}\n`,\n};\n"]}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * A logic analyser: the grid's rows are the lanes of buses, four to six
3
+ * lanes each with a gap between buses, now and then a whole bus bending a
4
+ * lane down and back. Each lane carries bits, a short dash for a zero and a
5
+ * long one for a one. Ahead of the front they idle dim and still; the front
6
+ * is a clock edge: behind it the bits flip, brighten and stream on to the
7
+ * right at a speed per bus.
8
+ */
9
+ export const dataBusShader = {
10
+ fragment: `
11
+ // a bus and its bend take at most BLOCK_ROWS - 1 rows: one row stays a gap
12
+ const float BLOCK_ROWS = 8.0;
13
+ const float MIN_LANES = 4.0;
14
+ const float MAX_LANES = BLOCK_ROWS - 2.0;
15
+ const float STRETCH_COLUMNS = 6.0;
16
+ const float BEND_CHANCE = 0.35;
17
+ const float LANE_ALPHA = 0.07;
18
+ const float LANE_LIT_ALPHA = 0.3;
19
+ const float LANE_WIDTH_DOTS = 0.5;
20
+ const float IDLE_ALPHA = 0.5;
21
+ const float CLOCKED_THRESHOLD = 0.02;
22
+ const float BIT_GROWTH = 0.6;
23
+ const float SPEED_SPACINGS = 3.0;
24
+ const float SPEED_SPREAD = 0.8;
25
+ const float ZERO_LENGTH = 0.25;
26
+ const float ONE_LENGTH = 0.7;
27
+
28
+ /* 1 where a bus is bent a lane down at the start of a stretch of columns, else 0 */
29
+ float bentAt(float block, float stretch) {
30
+ return ws_hash(vec2(block, stretch) + vec2(3.0, 71.0)) < BEND_CHANCE ? 1.0 : 0.0;
31
+ }
32
+
33
+ /*
34
+ * How many rows a bus is shifted down at x, 0 to 1. A bend runs at 45° over
35
+ * the first column of a stretch, from the last stretch's shift to this one's.
36
+ */
37
+ float busShift(float block, float x) {
38
+ float stretchLength = STRETCH_COLUMNS * ws_spacing;
39
+ float stretch = floor(x / stretchLength);
40
+ float into = (x - stretch * stretchLength) / ws_spacing;
41
+ float before = bentAt(block, stretch - 1.0);
42
+ float now = bentAt(block, stretch);
43
+ return mix(before, now, clamp(into - 0.5, 0.0, 1.0));
44
+ }
45
+
46
+ /* the coverage of a bit dash of the given length, centred in its slot, round-capped */
47
+ float bitDash(float along, float across, float dashLength, float radius) {
48
+ float halfLength = 0.5 * dashLength * ws_spacing;
49
+ float fromMiddle = abs(along - 0.5 * ws_spacing);
50
+ float away = length(vec2(max(fromMiddle - halfLength, 0.0), across));
51
+ return ws_band(away, 2.0 * radius);
52
+ }
53
+
54
+ vec4 wsShade(vec2 point) {
55
+ float blockHeight = BLOCK_ROWS * ws_spacing;
56
+ float block = floor(point.y / blockHeight);
57
+ float lanes = MIN_LANES + floor(ws_hash(vec2(block, 5.0)) * (MAX_LANES - MIN_LANES + 1.0));
58
+ float shift = busShift(block, point.x);
59
+ float local = point.y - block * blockHeight - shift * ws_spacing;
60
+ float lane = floor(local / ws_spacing);
61
+ float laneCenter = point.y - local + (lane + 0.5) * ws_spacing;
62
+ // no lane outside the lanes of the bus or cut by the host's bottom edge
63
+ if (lane < 0.0 || lane >= lanes || laneCenter >= ws_size.y) {
64
+ return vec4(0.0);
65
+ }
66
+ // a bent stretch runs at 45°: measure across the lane, not straight down
67
+ float slope = abs(busShift(block, point.x + 0.5 * ws_pixel) - busShift(block, point.x - 0.5 * ws_pixel))
68
+ * ws_spacing / ws_pixel;
69
+ float across = abs(local - (lane + 0.5) * ws_spacing) / sqrt(1.0 + slope * slope);
70
+ float laneId = block * BLOCK_ROWS + lane;
71
+
72
+ float lit = ws_waveIntensity(point);
73
+ bool clocked = lit > CLOCKED_THRESHOLD;
74
+ float speed = clocked ? SPEED_SPACINGS * ws_spacing * (1.0 - 0.5 * SPEED_SPREAD + SPEED_SPREAD * ws_hash(vec2(block, 9.0))) : 0.0;
75
+ float streamed = point.x - (clocked ? ws_time * speed : 0.0);
76
+ float slot = floor(streamed / ws_spacing);
77
+ float along = streamed - slot * ws_spacing;
78
+ float one = step(0.5, ws_hash(vec2(laneId, slot) + (clocked ? vec2(113.0, 17.0) : vec2(0.0))));
79
+ float radius = 0.5 * ws_dotSize * (1.0 + BIT_GROWTH * lit);
80
+ float bit = bitDash(along, across, mix(ZERO_LENGTH, ONE_LENGTH, one), radius);
81
+ float wire = ws_band(across, LANE_WIDTH_DOTS * ws_dotSize);
82
+
83
+ vec4 ink = ws_ink(lit);
84
+ float bitAlpha = bit * ink.a * mix(WS_REST_ALPHA * IDLE_ALPHA, WS_LIT_ALPHA, lit);
85
+ float wireAlpha = wire * ink.a * mix(LANE_ALPHA, LANE_LIT_ALPHA, lit);
86
+ float alpha = bitAlpha + (1.0 - bitAlpha) * wireAlpha;
87
+ return alpha <= 0.0 ? vec4(0.0) : vec4(ink.rgb, alpha);
88
+ }
89
+ `,
90
+ };
91
+ //# sourceMappingURL=data-bus.shader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-bus.shader.js","sourceRoot":"","sources":["../../../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/shaders/data-bus.shader.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GAAsB;IAC9C,QAAQ,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+EX;CACA,CAAC","sourcesContent":["import type { LoadingWaveShader } from '../loading-wave-shader';\n\n/**\n * A logic analyser: the grid's rows are the lanes of buses, four to six\n * lanes each with a gap between buses, now and then a whole bus bending a\n * lane down and back. Each lane carries bits, a short dash for a zero and a\n * long one for a one. Ahead of the front they idle dim and still; the front\n * is a clock edge: behind it the bits flip, brighten and stream on to the\n * right at a speed per bus.\n */\nexport const dataBusShader: LoadingWaveShader = {\n fragment: `\n// a bus and its bend take at most BLOCK_ROWS - 1 rows: one row stays a gap\nconst float BLOCK_ROWS = 8.0;\nconst float MIN_LANES = 4.0;\nconst float MAX_LANES = BLOCK_ROWS - 2.0;\nconst float STRETCH_COLUMNS = 6.0;\nconst float BEND_CHANCE = 0.35;\nconst float LANE_ALPHA = 0.07;\nconst float LANE_LIT_ALPHA = 0.3;\nconst float LANE_WIDTH_DOTS = 0.5;\nconst float IDLE_ALPHA = 0.5;\nconst float CLOCKED_THRESHOLD = 0.02;\nconst float BIT_GROWTH = 0.6;\nconst float SPEED_SPACINGS = 3.0;\nconst float SPEED_SPREAD = 0.8;\nconst float ZERO_LENGTH = 0.25;\nconst float ONE_LENGTH = 0.7;\n\n/* 1 where a bus is bent a lane down at the start of a stretch of columns, else 0 */\nfloat bentAt(float block, float stretch) {\n return ws_hash(vec2(block, stretch) + vec2(3.0, 71.0)) < BEND_CHANCE ? 1.0 : 0.0;\n}\n\n/*\n * How many rows a bus is shifted down at x, 0 to 1. A bend runs at 45° over\n * the first column of a stretch, from the last stretch's shift to this one's.\n */\nfloat busShift(float block, float x) {\n float stretchLength = STRETCH_COLUMNS * ws_spacing;\n float stretch = floor(x / stretchLength);\n float into = (x - stretch * stretchLength) / ws_spacing;\n float before = bentAt(block, stretch - 1.0);\n float now = bentAt(block, stretch);\n return mix(before, now, clamp(into - 0.5, 0.0, 1.0));\n}\n\n/* the coverage of a bit dash of the given length, centred in its slot, round-capped */\nfloat bitDash(float along, float across, float dashLength, float radius) {\n float halfLength = 0.5 * dashLength * ws_spacing;\n float fromMiddle = abs(along - 0.5 * ws_spacing);\n float away = length(vec2(max(fromMiddle - halfLength, 0.0), across));\n return ws_band(away, 2.0 * radius);\n}\n\nvec4 wsShade(vec2 point) {\n float blockHeight = BLOCK_ROWS * ws_spacing;\n float block = floor(point.y / blockHeight);\n float lanes = MIN_LANES + floor(ws_hash(vec2(block, 5.0)) * (MAX_LANES - MIN_LANES + 1.0));\n float shift = busShift(block, point.x);\n float local = point.y - block * blockHeight - shift * ws_spacing;\n float lane = floor(local / ws_spacing);\n float laneCenter = point.y - local + (lane + 0.5) * ws_spacing;\n // no lane outside the lanes of the bus or cut by the host's bottom edge\n if (lane < 0.0 || lane >= lanes || laneCenter >= ws_size.y) {\n return vec4(0.0);\n }\n // a bent stretch runs at 45°: measure across the lane, not straight down\n float slope = abs(busShift(block, point.x + 0.5 * ws_pixel) - busShift(block, point.x - 0.5 * ws_pixel))\n * ws_spacing / ws_pixel;\n float across = abs(local - (lane + 0.5) * ws_spacing) / sqrt(1.0 + slope * slope);\n float laneId = block * BLOCK_ROWS + lane;\n\n float lit = ws_waveIntensity(point);\n bool clocked = lit > CLOCKED_THRESHOLD;\n float speed = clocked ? SPEED_SPACINGS * ws_spacing * (1.0 - 0.5 * SPEED_SPREAD + SPEED_SPREAD * ws_hash(vec2(block, 9.0))) : 0.0;\n float streamed = point.x - (clocked ? ws_time * speed : 0.0);\n float slot = floor(streamed / ws_spacing);\n float along = streamed - slot * ws_spacing;\n float one = step(0.5, ws_hash(vec2(laneId, slot) + (clocked ? vec2(113.0, 17.0) : vec2(0.0))));\n float radius = 0.5 * ws_dotSize * (1.0 + BIT_GROWTH * lit);\n float bit = bitDash(along, across, mix(ZERO_LENGTH, ONE_LENGTH, one), radius);\n float wire = ws_band(across, LANE_WIDTH_DOTS * ws_dotSize);\n\n vec4 ink = ws_ink(lit);\n float bitAlpha = bit * ink.a * mix(WS_REST_ALPHA * IDLE_ALPHA, WS_LIT_ALPHA, lit);\n float wireAlpha = wire * ink.a * mix(LANE_ALPHA, LANE_LIT_ALPHA, lit);\n float alpha = bitAlpha + (1.0 - bitAlpha) * wireAlpha;\n return alpha <= 0.0 ? vec4(0.0) : vec4(ink.rgb, alpha);\n}\n`,\n};\n"]}