@paper-design/shaders 0.0.31 → 0.0.32
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/index.d.ts +23 -25
- package/dist/shaders/dithering.d.ts +8 -8
- package/dist/shaders/dot-grid.d.ts +1 -1
- package/dist/shaders/dot-orbit.d.ts +3 -6
- package/dist/shaders/god-rays.d.ts +6 -9
- package/dist/shaders/grain-gradient.d.ts +3 -5
- package/dist/shaders/liquid-metal.d.ts +12 -27
- package/dist/shaders/mesh-gradient.d.ts +3 -3
- package/dist/shaders/metaballs.d.ts +3 -6
- package/dist/shaders/neuro-noise.d.ts +6 -12
- package/dist/shaders/perlin-noise.d.ts +10 -10
- package/dist/shaders/pulsing-border.d.ts +3 -3
- package/dist/shaders/simplex-noise.d.ts +3 -3
- package/dist/shaders/smoke-ring.d.ts +3 -3
- package/dist/shaders/spiral.d.ts +8 -8
- package/dist/shaders/swirl.d.ts +3 -5
- package/dist/shaders/voronoi.d.ts +8 -8
- package/dist/shaders/warp.d.ts +3 -3
- package/dist/shaders/waves.d.ts +7 -7
- package/package.json +2 -2
- package/dist/get-shader-color-from-string.js +0 -93
- package/dist/get-shader-color-from-string.js.map +0 -7
- package/dist/get-shader-noise-texture.js +0 -23
- package/dist/get-shader-noise-texture.js.map +0 -7
- package/dist/index.js +0 -118
- package/dist/index.js.map +0 -7
- package/dist/shader-color-spaces.js +0 -96
- package/dist/shader-color-spaces.js.map +0 -7
- package/dist/shader-mount.js +0 -582
- package/dist/shader-mount.js.map +0 -7
- package/dist/shader-sizing.js +0 -193
- package/dist/shader-sizing.js.map +0 -7
- package/dist/shader-utils.js +0 -59
- package/dist/shader-utils.js.map +0 -7
- package/dist/shaders/color-panels.d.ts +0 -48
- package/dist/shaders/color-panels.js +0 -213
- package/dist/shaders/color-panels.js.map +0 -7
- package/dist/shaders/dithering.js +0 -221
- package/dist/shaders/dithering.js.map +0 -7
- package/dist/shaders/dot-grid.js +0 -107
- package/dist/shaders/dot-grid.js.map +0 -7
- package/dist/shaders/dot-orbit.js +0 -124
- package/dist/shaders/dot-orbit.js.map +0 -7
- package/dist/shaders/god-rays.js +0 -133
- package/dist/shaders/god-rays.js.map +0 -7
- package/dist/shaders/grain-gradient.js +0 -243
- package/dist/shaders/grain-gradient.js.map +0 -7
- package/dist/shaders/liquid-metal.js +0 -221
- package/dist/shaders/liquid-metal.js.map +0 -7
- package/dist/shaders/mesh-gradient.js +0 -97
- package/dist/shaders/mesh-gradient.js.map +0 -7
- package/dist/shaders/metaballs.js +0 -110
- package/dist/shaders/metaballs.js.map +0 -7
- package/dist/shaders/neuro-noise.js +0 -81
- package/dist/shaders/neuro-noise.js.map +0 -7
- package/dist/shaders/perlin-noise.js +0 -201
- package/dist/shaders/perlin-noise.js.map +0 -7
- package/dist/shaders/pulsing-border.js +0 -218
- package/dist/shaders/pulsing-border.js.map +0 -7
- package/dist/shaders/simplex-noise.js +0 -103
- package/dist/shaders/simplex-noise.js.map +0 -7
- package/dist/shaders/smoke-ring.js +0 -141
- package/dist/shaders/smoke-ring.js.map +0 -7
- package/dist/shaders/spiral.js +0 -83
- package/dist/shaders/spiral.js.map +0 -7
- package/dist/shaders/swirl.js +0 -91
- package/dist/shaders/swirl.js.map +0 -7
- package/dist/shaders/voronoi.js +0 -143
- package/dist/shaders/voronoi.js.map +0 -7
- package/dist/shaders/warp.js +0 -128
- package/dist/shaders/warp.js.map +0 -7
- package/dist/shaders/waves.js +0 -68
- package/dist/shaders/waves.js.map +0 -7
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -7
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/shader-color-spaces.ts"],
|
|
4
|
-
"sourcesContent": ["export const ShaderColorSpaces = {\n rgb: 0,\n oklch: 1,\n} as const;\n\nexport type ShaderColorSpace = keyof typeof ShaderColorSpaces;\n\nexport const declareOklchTransforms = `\n\n// magic numbers (and magic could be better tbh)\n#define OKLCH_CHROMA_THRESHOLD .001\n#define OKLCH_HUE_NEUTRALIZER - 2.\n\nvec3 srgbToLinear(vec3 srgb) {\n return pow(srgb, vec3(2.2));\n}\n\nvec3 linearToSrgb(vec3 linear) {\n return pow(linear, vec3(1.0 / 2.2));\n}\n\nvec3 LrgbToOklab(vec3 rgb) {\n float L = pow(0.4122214708 * rgb.r + 0.5363325363 * rgb.g + 0.0514459929 * rgb.b, 1.0 / 3.0);\n float M = pow(0.2119034982 * rgb.r + 0.6806995451 * rgb.g + 0.1073969566 * rgb.b, 1.0 / 3.0);\n float S = pow(0.0883024619 * rgb.r + 0.2817188376 * rgb.g + 0.6299787005 * rgb.b, 1.0 / 3.0);\n return vec3(\n 0.2104542553 * L + 0.793617785 * M - 0.0040720468 * S,\n 1.9779984951 * L - 2.428592205 * M + 0.4505937099 * S,\n 0.0259040371 * L + 0.7827717662 * M - 0.808675766 * S\n );\n}\n\nvec3 OklabToLrgb(vec3 oklab) {\n float L = oklab.x;\n float a = oklab.y;\n float b = oklab.z;\n\n float l_ = L + 0.3963377774 * a + 0.2158037573 * b;\n float m_ = L - 0.1055613458 * a - 0.0638541728 * b;\n float s_ = L - 0.0894841775 * a - 1.291485548 * b;\n\n float l = l_ * l_ * l_;\n float m = m_ * m_ * m_;\n float s = s_ * s_ * s_;\n\n return vec3(\n 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,\n -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,\n -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s\n );\n}\n\nvec3 oklabToOklch(vec3 oklab) {\n float C = length(oklab.yz);\n float H = atan(oklab.z, oklab.y);\n if (C < OKLCH_CHROMA_THRESHOLD) {\n H = OKLCH_HUE_NEUTRALIZER;\n }\n return vec3(oklab.x, C, H);\n}\n\nvec3 oklchToOklab(vec3 oklch) {\n float a = oklch.y * cos(oklch.z);\n float b = oklch.y * sin(oklch.z);\n return vec3(oklch.x, a, b);\n}\n\nfloat mixHue(float h1, float h2, float mixer) {\n float delta = mod(h2 - h1 + PI, TWO_PI) - PI;\n return h1 + mixer * delta;\n}\n\nvec3 srgbToOklab(vec3 rgb) {\n return oklabToOklch(LrgbToOklab(srgbToLinear(rgb)));\n}\n\nvec3 oklabToSrgb(vec3 oklab) {\n return linearToSrgb(OklabToLrgb(oklchToOklab(oklab)));\n}\n\nvec3 mixOklabVector(vec3 color1, vec3 color2, float mixer) {\n color1.x = mix(color1.x, color2.x, mixer);\n color1.y = mix(color1.y, color2.y, mixer);\n if (color1.y > OKLCH_CHROMA_THRESHOLD && color2.y > OKLCH_CHROMA_THRESHOLD) {\n color1.z = mixHue(color1.z, color2.z, mixer);\n }\n return color1;\n}\n`;\n"],
|
|
5
|
-
"mappings": ";;;;;AAAO,MAAM,oBAAoB;AAAA,EAC/B,KAAK;AAAA,EACL,OAAO;AACT;AAIO,MAAM,yBAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/dist/shader-mount.js
DELETED
|
@@ -1,582 +0,0 @@
|
|
|
1
|
-
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
2
|
-
* Paper Shaders *
|
|
3
|
-
* https://github.com/paper-design/shaders *
|
|
4
|
-
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
5
|
-
|
|
6
|
-
const DEFAULT_MAX_PIXEL_COUNT = 1920 * 1080 * 4;
|
|
7
|
-
class ShaderMount {
|
|
8
|
-
parentElement;
|
|
9
|
-
canvasElement;
|
|
10
|
-
gl;
|
|
11
|
-
program = null;
|
|
12
|
-
uniformLocations = {};
|
|
13
|
-
/** The fragment shader that we are using */
|
|
14
|
-
fragmentShader;
|
|
15
|
-
/** Stores the RAF for the render loop */
|
|
16
|
-
rafId = null;
|
|
17
|
-
/** Time of the last rendered frame */
|
|
18
|
-
lastRenderTime = 0;
|
|
19
|
-
/** Total time that we have played any animation, passed as a uniform to the shader for time-based VFX */
|
|
20
|
-
totalFrameTime = 0;
|
|
21
|
-
/** The current speed that we progress through animation time (multiplies by delta time every update). Allows negatives to play in reverse. If set to 0, rAF will stop entirely so static shaders have no recurring performance costs */
|
|
22
|
-
speed = 0;
|
|
23
|
-
/** Uniforms that are provided by the user for the specific shader being mounted (not including uniforms that this Mount adds, like time and resolution) */
|
|
24
|
-
providedUniforms;
|
|
25
|
-
/** Just a sanity check to make sure frames don't run after we're disposed */
|
|
26
|
-
hasBeenDisposed = false;
|
|
27
|
-
/** If the resolution of the canvas has changed since the last render */
|
|
28
|
-
resolutionChanged = true;
|
|
29
|
-
/** Store textures that are provided by the user */
|
|
30
|
-
textures = /* @__PURE__ */ new Map();
|
|
31
|
-
minPixelRatio;
|
|
32
|
-
maxPixelCount;
|
|
33
|
-
isSafari = isSafari();
|
|
34
|
-
constructor(parentElement, fragmentShader, uniforms, webGlContextAttributes, speed = 0, frame = 0, minPixelRatio = 2, maxPixelCount = DEFAULT_MAX_PIXEL_COUNT) {
|
|
35
|
-
if (parentElement instanceof HTMLElement) {
|
|
36
|
-
this.parentElement = parentElement;
|
|
37
|
-
} else {
|
|
38
|
-
throw new Error("Paper Shaders: parent element must be an HTMLElement");
|
|
39
|
-
}
|
|
40
|
-
if (!document.querySelector("style[data-paper-shaders]")) {
|
|
41
|
-
const styleElement = document.createElement("style");
|
|
42
|
-
styleElement.innerHTML = defaultStyle;
|
|
43
|
-
styleElement.setAttribute("data-paper-shaders", "");
|
|
44
|
-
document.head.prepend(styleElement);
|
|
45
|
-
}
|
|
46
|
-
const canvasElement = document.createElement("canvas");
|
|
47
|
-
this.canvasElement = canvasElement;
|
|
48
|
-
this.parentElement.prepend(canvasElement);
|
|
49
|
-
this.fragmentShader = fragmentShader;
|
|
50
|
-
this.providedUniforms = uniforms;
|
|
51
|
-
this.totalFrameTime = frame;
|
|
52
|
-
this.minPixelRatio = minPixelRatio;
|
|
53
|
-
this.maxPixelCount = maxPixelCount;
|
|
54
|
-
const gl = canvasElement.getContext("webgl2", webGlContextAttributes);
|
|
55
|
-
if (!gl) {
|
|
56
|
-
throw new Error("Paper Shaders: WebGL is not supported in this browser");
|
|
57
|
-
}
|
|
58
|
-
this.gl = gl;
|
|
59
|
-
this.initProgram();
|
|
60
|
-
this.setupPositionAttribute();
|
|
61
|
-
this.setupUniforms();
|
|
62
|
-
this.setUniformValues(this.providedUniforms);
|
|
63
|
-
this.setupResizeObserver();
|
|
64
|
-
this.setSpeed(speed);
|
|
65
|
-
this.parentElement.setAttribute("data-paper-shaders", "");
|
|
66
|
-
this.parentElement.paperShaderMount = this;
|
|
67
|
-
}
|
|
68
|
-
initProgram = () => {
|
|
69
|
-
const program = createProgram(this.gl, vertexShaderSource, this.fragmentShader);
|
|
70
|
-
if (!program) return;
|
|
71
|
-
this.program = program;
|
|
72
|
-
};
|
|
73
|
-
setupPositionAttribute = () => {
|
|
74
|
-
const positionAttributeLocation = this.gl.getAttribLocation(this.program, "a_position");
|
|
75
|
-
const positionBuffer = this.gl.createBuffer();
|
|
76
|
-
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, positionBuffer);
|
|
77
|
-
const positions = [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1];
|
|
78
|
-
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW);
|
|
79
|
-
this.gl.enableVertexAttribArray(positionAttributeLocation);
|
|
80
|
-
this.gl.vertexAttribPointer(positionAttributeLocation, 2, this.gl.FLOAT, false, 0, 0);
|
|
81
|
-
};
|
|
82
|
-
setupUniforms = () => {
|
|
83
|
-
const uniformLocations = {
|
|
84
|
-
u_time: this.gl.getUniformLocation(this.program, "u_time"),
|
|
85
|
-
u_pixelRatio: this.gl.getUniformLocation(this.program, "u_pixelRatio"),
|
|
86
|
-
u_resolution: this.gl.getUniformLocation(this.program, "u_resolution")
|
|
87
|
-
};
|
|
88
|
-
Object.entries(this.providedUniforms).forEach(([key, value]) => {
|
|
89
|
-
uniformLocations[key] = this.gl.getUniformLocation(this.program, key);
|
|
90
|
-
if (value instanceof HTMLImageElement) {
|
|
91
|
-
const aspectRatioUniformName = `${key}_aspect_ratio`;
|
|
92
|
-
uniformLocations[aspectRatioUniformName] = this.gl.getUniformLocation(this.program, aspectRatioUniformName);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
this.uniformLocations = uniformLocations;
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* The scale that we should render at.
|
|
99
|
-
* - Used to target 2x rendering even on 1x screens for better antialiasing
|
|
100
|
-
* - Prevents the virtual resolution from going beyond the maximum resolution
|
|
101
|
-
* - Accounts for the page zoom level so we render in physical device pixels rather than CSS pixels
|
|
102
|
-
*/
|
|
103
|
-
renderScale = 1;
|
|
104
|
-
parentWidth = 0;
|
|
105
|
-
parentHeight = 0;
|
|
106
|
-
resizeObserver = null;
|
|
107
|
-
setupResizeObserver = () => {
|
|
108
|
-
this.resizeObserver = new ResizeObserver(([entry]) => {
|
|
109
|
-
if (entry?.borderBoxSize[0]) {
|
|
110
|
-
this.parentWidth = entry.borderBoxSize[0].inlineSize;
|
|
111
|
-
this.parentHeight = entry.borderBoxSize[0].blockSize;
|
|
112
|
-
}
|
|
113
|
-
this.handleResize();
|
|
114
|
-
});
|
|
115
|
-
this.resizeObserver.observe(this.parentElement);
|
|
116
|
-
visualViewport?.addEventListener("resize", this.handleVisualViewportChange);
|
|
117
|
-
const rect = this.parentElement.getBoundingClientRect();
|
|
118
|
-
this.parentWidth = rect.width;
|
|
119
|
-
this.parentHeight = rect.height;
|
|
120
|
-
this.handleResize();
|
|
121
|
-
};
|
|
122
|
-
// Visual viewport resize handler, mainly used to react to browser zoom changes.
|
|
123
|
-
// Wait 2 frames to align with when the resize observer callback is done (in case it might follow):
|
|
124
|
-
// - Frame 1: a paint after the visual viewport resize
|
|
125
|
-
// - Frame 2: a paint after the resize observer has been handled, if it was ever triggered
|
|
126
|
-
//
|
|
127
|
-
// Both resize observer and visual viewport will react to classic browser zoom changes,
|
|
128
|
-
// so we dedupe the callbacks, but pinch zoom only triggers the visual viewport handler.
|
|
129
|
-
resizeRafId = null;
|
|
130
|
-
handleVisualViewportChange = () => {
|
|
131
|
-
if (this.resizeRafId !== null) {
|
|
132
|
-
cancelAnimationFrame(this.resizeRafId);
|
|
133
|
-
}
|
|
134
|
-
this.resizeRafId = requestAnimationFrame(() => {
|
|
135
|
-
this.resizeRafId = requestAnimationFrame(() => {
|
|
136
|
-
this.handleResize();
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
};
|
|
140
|
-
/** Resize handler for when the container div changes size or the max pixel count changes and we want to resize our canvas to match */
|
|
141
|
-
handleResize = () => {
|
|
142
|
-
if (this.resizeRafId !== null) {
|
|
143
|
-
cancelAnimationFrame(this.resizeRafId);
|
|
144
|
-
}
|
|
145
|
-
const pinchZoom = visualViewport?.scale ?? 1;
|
|
146
|
-
const innerWidth = visualViewport ? visualViewport.width * visualViewport.scale : window.innerWidth;
|
|
147
|
-
const classicZoom = Math.round(1e4 * window.outerWidth / innerWidth) / 1e4;
|
|
148
|
-
const realPixelRatio = this.isSafari ? devicePixelRatio : devicePixelRatio / classicZoom;
|
|
149
|
-
const targetPixelRatio = Math.max(realPixelRatio, this.minPixelRatio);
|
|
150
|
-
const targetRenderScale = targetPixelRatio * classicZoom * pinchZoom;
|
|
151
|
-
const targetPixelWidth = this.parentWidth * targetRenderScale;
|
|
152
|
-
const targetPixelHeight = this.parentHeight * targetRenderScale;
|
|
153
|
-
const maxPixelCountHeadroom = Math.sqrt(this.maxPixelCount) / Math.sqrt(targetPixelWidth * targetPixelHeight);
|
|
154
|
-
const newRenderScale = targetRenderScale * Math.min(1, maxPixelCountHeadroom);
|
|
155
|
-
const newWidth = Math.round(this.parentWidth * newRenderScale);
|
|
156
|
-
const newHeight = Math.round(this.parentHeight * newRenderScale);
|
|
157
|
-
if (this.canvasElement.width !== newWidth || this.canvasElement.height !== newHeight || this.renderScale !== newRenderScale) {
|
|
158
|
-
this.renderScale = newRenderScale;
|
|
159
|
-
this.canvasElement.width = newWidth;
|
|
160
|
-
this.canvasElement.height = newHeight;
|
|
161
|
-
this.resolutionChanged = true;
|
|
162
|
-
this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);
|
|
163
|
-
this.render(performance.now());
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
render = (currentTime) => {
|
|
167
|
-
if (this.hasBeenDisposed) return;
|
|
168
|
-
if (this.program === null) {
|
|
169
|
-
console.warn("Tried to render before program or gl was initialized");
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
const dt = currentTime - this.lastRenderTime;
|
|
173
|
-
this.lastRenderTime = currentTime;
|
|
174
|
-
if (this.speed !== 0) {
|
|
175
|
-
this.totalFrameTime += dt * this.speed;
|
|
176
|
-
}
|
|
177
|
-
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
|
178
|
-
this.gl.useProgram(this.program);
|
|
179
|
-
this.gl.uniform1f(this.uniformLocations.u_time, this.totalFrameTime * 1e-3);
|
|
180
|
-
if (this.resolutionChanged) {
|
|
181
|
-
this.gl.uniform2f(this.uniformLocations.u_resolution, this.gl.canvas.width, this.gl.canvas.height);
|
|
182
|
-
this.gl.uniform1f(this.uniformLocations.u_pixelRatio, this.renderScale);
|
|
183
|
-
this.resolutionChanged = false;
|
|
184
|
-
}
|
|
185
|
-
this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
|
|
186
|
-
if (this.speed !== 0) {
|
|
187
|
-
this.requestRender();
|
|
188
|
-
} else {
|
|
189
|
-
this.rafId = null;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
requestRender = () => {
|
|
193
|
-
if (this.rafId !== null) {
|
|
194
|
-
cancelAnimationFrame(this.rafId);
|
|
195
|
-
}
|
|
196
|
-
this.rafId = requestAnimationFrame(this.render);
|
|
197
|
-
};
|
|
198
|
-
/** Creates a texture from an image and sets it into a uniform value */
|
|
199
|
-
setTextureUniform = (uniformName, image) => {
|
|
200
|
-
if (!image.complete || image.naturalWidth === 0) {
|
|
201
|
-
throw new Error(`Paper Shaders: image for uniform ${uniformName} must be fully loaded`);
|
|
202
|
-
}
|
|
203
|
-
const existingTexture = this.textures.get(uniformName);
|
|
204
|
-
if (existingTexture) {
|
|
205
|
-
this.gl.deleteTexture(existingTexture);
|
|
206
|
-
}
|
|
207
|
-
const texture = this.gl.createTexture();
|
|
208
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
209
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.REPEAT);
|
|
210
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.REPEAT);
|
|
211
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
|
|
212
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
|
|
213
|
-
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image);
|
|
214
|
-
const error = this.gl.getError();
|
|
215
|
-
if (error !== this.gl.NO_ERROR || texture === null) {
|
|
216
|
-
console.error("Paper Shaders: WebGL error when uploading texture:", error);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
this.textures.set(uniformName, texture);
|
|
220
|
-
const location = this.uniformLocations[uniformName];
|
|
221
|
-
if (location) {
|
|
222
|
-
const textureUnit = this.textures.size - 1;
|
|
223
|
-
this.gl.useProgram(this.program);
|
|
224
|
-
this.gl.activeTexture(this.gl.TEXTURE0 + textureUnit);
|
|
225
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
226
|
-
this.gl.uniform1i(location, textureUnit);
|
|
227
|
-
const aspectRatioUniformName = `${uniformName}_aspect_ratio`;
|
|
228
|
-
const aspectRatioLocation = this.uniformLocations[aspectRatioUniformName];
|
|
229
|
-
if (aspectRatioLocation) {
|
|
230
|
-
const aspectRatio = image.naturalWidth / image.naturalHeight;
|
|
231
|
-
this.gl.uniform1f(aspectRatioLocation, aspectRatio);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
/** Sets the provided uniform values into the WebGL program, can be a partial list of uniforms that have changed */
|
|
236
|
-
setUniformValues = (updatedUniforms) => {
|
|
237
|
-
this.gl.useProgram(this.program);
|
|
238
|
-
Object.entries(updatedUniforms).forEach(([key, value]) => {
|
|
239
|
-
const location = this.uniformLocations[key];
|
|
240
|
-
if (!location) {
|
|
241
|
-
console.warn(`Uniform location for ${key} not found`);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
if (value instanceof HTMLImageElement) {
|
|
245
|
-
this.setTextureUniform(key, value);
|
|
246
|
-
} else if (Array.isArray(value)) {
|
|
247
|
-
let flatArray = null;
|
|
248
|
-
let valueLength = null;
|
|
249
|
-
if (value[0] !== void 0 && Array.isArray(value[0])) {
|
|
250
|
-
const firstChildLength = value[0].length;
|
|
251
|
-
if (value.every((arr) => arr.length === firstChildLength)) {
|
|
252
|
-
flatArray = value.flat();
|
|
253
|
-
valueLength = firstChildLength;
|
|
254
|
-
} else {
|
|
255
|
-
console.warn(`All child arrays must be the same length for ${key}`);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
} else {
|
|
259
|
-
flatArray = value;
|
|
260
|
-
valueLength = flatArray.length;
|
|
261
|
-
}
|
|
262
|
-
switch (valueLength) {
|
|
263
|
-
case 2:
|
|
264
|
-
this.gl.uniform2fv(location, flatArray);
|
|
265
|
-
break;
|
|
266
|
-
case 3:
|
|
267
|
-
this.gl.uniform3fv(location, flatArray);
|
|
268
|
-
break;
|
|
269
|
-
case 4:
|
|
270
|
-
this.gl.uniform4fv(location, flatArray);
|
|
271
|
-
break;
|
|
272
|
-
case 9:
|
|
273
|
-
this.gl.uniformMatrix3fv(location, false, flatArray);
|
|
274
|
-
break;
|
|
275
|
-
case 16:
|
|
276
|
-
this.gl.uniformMatrix4fv(location, false, flatArray);
|
|
277
|
-
break;
|
|
278
|
-
default:
|
|
279
|
-
console.warn(`Unsupported uniform array length: ${valueLength}`);
|
|
280
|
-
}
|
|
281
|
-
} else if (typeof value === "number") {
|
|
282
|
-
this.gl.uniform1f(location, value);
|
|
283
|
-
} else if (typeof value === "boolean") {
|
|
284
|
-
this.gl.uniform1i(location, value ? 1 : 0);
|
|
285
|
-
} else {
|
|
286
|
-
console.warn(`Unsupported uniform type for ${key}: ${typeof value}`);
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
};
|
|
290
|
-
/** Gets the current total animation time from 0ms */
|
|
291
|
-
getCurrentFrameTime = () => {
|
|
292
|
-
return this.totalFrameTime;
|
|
293
|
-
};
|
|
294
|
-
/** Set a frame to get a deterministic result, frames are literally just milliseconds from zero since the animation started */
|
|
295
|
-
setFrame = (newFrame) => {
|
|
296
|
-
this.totalFrameTime = newFrame;
|
|
297
|
-
this.lastRenderTime = performance.now();
|
|
298
|
-
this.render(performance.now());
|
|
299
|
-
};
|
|
300
|
-
/** Set an animation speed (or 0 to stop animation) */
|
|
301
|
-
setSpeed = (newSpeed = 1) => {
|
|
302
|
-
this.speed = newSpeed;
|
|
303
|
-
if (this.rafId === null && newSpeed !== 0) {
|
|
304
|
-
this.lastRenderTime = performance.now();
|
|
305
|
-
this.rafId = requestAnimationFrame(this.render);
|
|
306
|
-
}
|
|
307
|
-
if (this.rafId !== null && newSpeed === 0) {
|
|
308
|
-
cancelAnimationFrame(this.rafId);
|
|
309
|
-
this.rafId = null;
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
/** Set the maximum pixel count for the shader, this will limit the number of pixels that will be rendered */
|
|
313
|
-
setMaxPixelCount = (newMaxPixelCount = DEFAULT_MAX_PIXEL_COUNT) => {
|
|
314
|
-
this.maxPixelCount = newMaxPixelCount;
|
|
315
|
-
this.handleResize();
|
|
316
|
-
};
|
|
317
|
-
/** Set the minimum pixel ratio for the shader */
|
|
318
|
-
setMinPixelRatio = (newMinPixelRatio = 2) => {
|
|
319
|
-
this.minPixelRatio = newMinPixelRatio;
|
|
320
|
-
this.handleResize();
|
|
321
|
-
};
|
|
322
|
-
/** Update the uniforms that are provided by the outside shader, can be a partial set with only the uniforms that have changed */
|
|
323
|
-
setUniforms = (newUniforms) => {
|
|
324
|
-
this.providedUniforms = { ...this.providedUniforms, ...newUniforms };
|
|
325
|
-
this.setUniformValues(newUniforms);
|
|
326
|
-
this.render(performance.now());
|
|
327
|
-
};
|
|
328
|
-
/** Dispose of the shader mount, cleaning up all of the WebGL resources */
|
|
329
|
-
dispose = () => {
|
|
330
|
-
this.hasBeenDisposed = true;
|
|
331
|
-
if (this.rafId !== null) {
|
|
332
|
-
cancelAnimationFrame(this.rafId);
|
|
333
|
-
this.rafId = null;
|
|
334
|
-
}
|
|
335
|
-
if (this.gl && this.program) {
|
|
336
|
-
this.textures.forEach((texture) => {
|
|
337
|
-
this.gl.deleteTexture(texture);
|
|
338
|
-
});
|
|
339
|
-
this.textures.clear();
|
|
340
|
-
this.gl.deleteProgram(this.program);
|
|
341
|
-
this.program = null;
|
|
342
|
-
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, null);
|
|
343
|
-
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, null);
|
|
344
|
-
this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, null);
|
|
345
|
-
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
|
|
346
|
-
this.gl.getError();
|
|
347
|
-
}
|
|
348
|
-
if (this.resizeObserver) {
|
|
349
|
-
this.resizeObserver.disconnect();
|
|
350
|
-
this.resizeObserver = null;
|
|
351
|
-
}
|
|
352
|
-
visualViewport?.removeEventListener("resize", this.handleVisualViewportChange);
|
|
353
|
-
this.uniformLocations = {};
|
|
354
|
-
this.parentElement.paperShaderMount = void 0;
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
const vertexShaderSource = `#version 300 es
|
|
358
|
-
precision mediump float;
|
|
359
|
-
|
|
360
|
-
layout(location = 0) in vec4 a_position;
|
|
361
|
-
|
|
362
|
-
uniform vec2 u_resolution;
|
|
363
|
-
uniform float u_pixelRatio;
|
|
364
|
-
|
|
365
|
-
uniform float u_originX;
|
|
366
|
-
uniform float u_originY;
|
|
367
|
-
uniform float u_worldWidth;
|
|
368
|
-
uniform float u_worldHeight;
|
|
369
|
-
uniform float u_fit;
|
|
370
|
-
|
|
371
|
-
uniform float u_scale;
|
|
372
|
-
uniform float u_rotation;
|
|
373
|
-
uniform float u_offsetX;
|
|
374
|
-
uniform float u_offsetY;
|
|
375
|
-
|
|
376
|
-
uniform float u_pxSize;
|
|
377
|
-
|
|
378
|
-
out vec2 v_objectUV;
|
|
379
|
-
out vec2 v_objectBoxSize;
|
|
380
|
-
out vec2 v_objectHelperBox;
|
|
381
|
-
|
|
382
|
-
out vec2 v_responsiveUV;
|
|
383
|
-
out vec2 v_responsiveBoxSize;
|
|
384
|
-
out vec2 v_responsiveHelperBox;
|
|
385
|
-
out vec2 v_responsiveBoxGivenSize;
|
|
386
|
-
|
|
387
|
-
out vec2 v_patternUV;
|
|
388
|
-
out vec2 v_patternBoxSize;
|
|
389
|
-
out vec2 v_patternHelperBox;
|
|
390
|
-
|
|
391
|
-
// #define ADD_HELPERS
|
|
392
|
-
|
|
393
|
-
vec3 getBoxSize(float boxRatio, vec2 givenBoxSize, vec2 maxBoxSize) {
|
|
394
|
-
vec2 box = vec2(0.);
|
|
395
|
-
// fit = none
|
|
396
|
-
box.x = boxRatio * min(givenBoxSize.x / boxRatio, givenBoxSize.y);
|
|
397
|
-
float noFitBoxWidth = box.x;
|
|
398
|
-
if (u_fit == 1.) { // fit = contain
|
|
399
|
-
box.x = boxRatio * min(maxBoxSize[0] / boxRatio, maxBoxSize[1]);
|
|
400
|
-
} else if (u_fit == 2.) { // fit = cover
|
|
401
|
-
box.x = boxRatio * max(maxBoxSize[0] / boxRatio, maxBoxSize[1]);
|
|
402
|
-
}
|
|
403
|
-
box.y = box.x / boxRatio;
|
|
404
|
-
return vec3(box, noFitBoxWidth);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
void main() {
|
|
408
|
-
gl_Position = a_position;
|
|
409
|
-
|
|
410
|
-
vec2 uv = gl_Position.xy * .5;
|
|
411
|
-
vec2 boxOrigin = vec2(.5 - u_originX, u_originY - .5);
|
|
412
|
-
vec2 givenBoxSize = vec2(u_worldWidth, u_worldHeight);
|
|
413
|
-
givenBoxSize = max(givenBoxSize, vec2(1.)) * u_pixelRatio;
|
|
414
|
-
vec2 maxBoxSize = vec2(max(u_resolution.x, givenBoxSize.x), max(u_resolution.y, givenBoxSize.y));
|
|
415
|
-
float r = u_rotation * 3.14159265358979323846 / 180.;
|
|
416
|
-
mat2 graphicRotation = mat2(cos(r), sin(r), -sin(r), cos(r));
|
|
417
|
-
vec2 graphicOffset = vec2(-u_offsetX, u_offsetY);
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
// ===================================================
|
|
421
|
-
// Sizing api for graphic objects with fixed ratio
|
|
422
|
-
// (currently supports only ratio = 1)
|
|
423
|
-
|
|
424
|
-
float fixedRatio = 1.;
|
|
425
|
-
vec2 fixedRatioBoxGivenSize = vec2(
|
|
426
|
-
(u_worldWidth == 0.) ? u_resolution.x : givenBoxSize.x,
|
|
427
|
-
(u_worldHeight == 0.) ? u_resolution.y : givenBoxSize.y
|
|
428
|
-
);
|
|
429
|
-
|
|
430
|
-
v_objectBoxSize = getBoxSize(fixedRatio, fixedRatioBoxGivenSize, maxBoxSize).xy;
|
|
431
|
-
vec2 objectWorldScale = u_resolution.xy / v_objectBoxSize;
|
|
432
|
-
|
|
433
|
-
#ifdef ADD_HELPERS
|
|
434
|
-
v_objectHelperBox = uv;
|
|
435
|
-
v_objectHelperBox *= objectWorldScale;
|
|
436
|
-
v_objectHelperBox += boxOrigin * (objectWorldScale - 1.);
|
|
437
|
-
#endif
|
|
438
|
-
|
|
439
|
-
v_objectUV = uv;
|
|
440
|
-
v_objectUV *= objectWorldScale;
|
|
441
|
-
v_objectUV += boxOrigin * (objectWorldScale - 1.);
|
|
442
|
-
v_objectUV += graphicOffset;
|
|
443
|
-
v_objectUV /= u_scale;
|
|
444
|
-
v_objectUV = graphicRotation * v_objectUV;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
// ===================================================
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
// ===================================================
|
|
451
|
-
// Sizing api for graphic objects with either givenBoxSize ratio or canvas ratio.
|
|
452
|
-
// Full-screen mode available with u_worldWidth = u_worldHeight = 0
|
|
453
|
-
|
|
454
|
-
v_responsiveBoxGivenSize = vec2(
|
|
455
|
-
(u_worldWidth == 0.) ? u_resolution.x : givenBoxSize.x,
|
|
456
|
-
(u_worldHeight == 0.) ? u_resolution.y : givenBoxSize.y
|
|
457
|
-
);
|
|
458
|
-
float responsiveRatio = v_responsiveBoxGivenSize.x / v_responsiveBoxGivenSize.y;
|
|
459
|
-
v_responsiveBoxSize = getBoxSize(responsiveRatio, v_responsiveBoxGivenSize, maxBoxSize).xy;
|
|
460
|
-
vec2 responsiveBoxScale = u_resolution.xy / v_responsiveBoxSize;
|
|
461
|
-
|
|
462
|
-
#ifdef ADD_HELPERS
|
|
463
|
-
v_responsiveHelperBox = uv;
|
|
464
|
-
v_responsiveHelperBox *= responsiveBoxScale;
|
|
465
|
-
v_responsiveHelperBox += boxOrigin * (responsiveBoxScale - 1.);
|
|
466
|
-
#endif
|
|
467
|
-
|
|
468
|
-
v_responsiveUV = uv;
|
|
469
|
-
v_responsiveUV *= responsiveBoxScale;
|
|
470
|
-
v_responsiveUV += boxOrigin * (responsiveBoxScale - 1.);
|
|
471
|
-
v_responsiveUV += graphicOffset;
|
|
472
|
-
v_responsiveUV /= u_scale;
|
|
473
|
-
v_responsiveUV.x *= responsiveRatio;
|
|
474
|
-
v_responsiveUV = graphicRotation * v_responsiveUV;
|
|
475
|
-
v_responsiveUV.x /= responsiveRatio;
|
|
476
|
-
|
|
477
|
-
// ===================================================
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
// ===================================================
|
|
481
|
-
// Sizing api for patterns
|
|
482
|
-
// (treating graphics as a image u_worldWidth x u_worldHeight size)
|
|
483
|
-
|
|
484
|
-
float patternBoxRatio = givenBoxSize.x / givenBoxSize.y;
|
|
485
|
-
vec2 patternBoxGivenSize = vec2(
|
|
486
|
-
(u_worldWidth == 0.) ? u_resolution.x : givenBoxSize.x,
|
|
487
|
-
(u_worldHeight == 0.) ? u_resolution.y : givenBoxSize.y
|
|
488
|
-
);
|
|
489
|
-
patternBoxRatio = patternBoxGivenSize.x / patternBoxGivenSize.y;
|
|
490
|
-
|
|
491
|
-
vec3 boxSizeData = getBoxSize(patternBoxRatio, patternBoxGivenSize, maxBoxSize);
|
|
492
|
-
v_patternBoxSize = boxSizeData.xy;
|
|
493
|
-
float patternBoxNoFitBoxWidth = boxSizeData.z;
|
|
494
|
-
vec2 patternBoxScale = u_resolution.xy / v_patternBoxSize;
|
|
495
|
-
|
|
496
|
-
#ifdef ADD_HELPERS
|
|
497
|
-
v_patternHelperBox = uv;
|
|
498
|
-
v_patternHelperBox *= patternBoxScale;
|
|
499
|
-
v_patternHelperBox += boxOrigin * (patternBoxScale - 1.);
|
|
500
|
-
#endif
|
|
501
|
-
|
|
502
|
-
v_patternUV = uv;
|
|
503
|
-
v_patternUV += graphicOffset / patternBoxScale;
|
|
504
|
-
v_patternUV += boxOrigin;
|
|
505
|
-
v_patternUV -= boxOrigin / patternBoxScale;
|
|
506
|
-
v_patternUV *= u_resolution.xy;
|
|
507
|
-
v_patternUV /= u_pixelRatio;
|
|
508
|
-
if (u_fit > 0.) {
|
|
509
|
-
v_patternUV *= (patternBoxNoFitBoxWidth / v_patternBoxSize.x);
|
|
510
|
-
}
|
|
511
|
-
v_patternUV /= u_scale;
|
|
512
|
-
v_patternUV = graphicRotation * v_patternUV;
|
|
513
|
-
v_patternUV += boxOrigin / patternBoxScale;
|
|
514
|
-
v_patternUV -= boxOrigin;
|
|
515
|
-
v_patternUV += .5;
|
|
516
|
-
|
|
517
|
-
// ===================================================
|
|
518
|
-
|
|
519
|
-
}`;
|
|
520
|
-
function createShader(gl, type, source) {
|
|
521
|
-
const shader = gl.createShader(type);
|
|
522
|
-
if (!shader) return null;
|
|
523
|
-
gl.shaderSource(shader, source);
|
|
524
|
-
gl.compileShader(shader);
|
|
525
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
526
|
-
console.error("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));
|
|
527
|
-
gl.deleteShader(shader);
|
|
528
|
-
return null;
|
|
529
|
-
}
|
|
530
|
-
return shader;
|
|
531
|
-
}
|
|
532
|
-
function createProgram(gl, vertexShaderSource2, fragmentShaderSource) {
|
|
533
|
-
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource2);
|
|
534
|
-
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
535
|
-
if (!vertexShader || !fragmentShader) return null;
|
|
536
|
-
const program = gl.createProgram();
|
|
537
|
-
if (!program) return null;
|
|
538
|
-
gl.attachShader(program, vertexShader);
|
|
539
|
-
gl.attachShader(program, fragmentShader);
|
|
540
|
-
gl.linkProgram(program);
|
|
541
|
-
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
542
|
-
console.error("Unable to initialize the shader program: " + gl.getProgramInfoLog(program));
|
|
543
|
-
gl.deleteProgram(program);
|
|
544
|
-
gl.deleteShader(vertexShader);
|
|
545
|
-
gl.deleteShader(fragmentShader);
|
|
546
|
-
return null;
|
|
547
|
-
}
|
|
548
|
-
gl.detachShader(program, vertexShader);
|
|
549
|
-
gl.detachShader(program, fragmentShader);
|
|
550
|
-
gl.deleteShader(vertexShader);
|
|
551
|
-
gl.deleteShader(fragmentShader);
|
|
552
|
-
return program;
|
|
553
|
-
}
|
|
554
|
-
const defaultStyle = `@layer paper-shaders {
|
|
555
|
-
:where([data-paper-shaders]) {
|
|
556
|
-
isolation: isolate;
|
|
557
|
-
position: relative;
|
|
558
|
-
|
|
559
|
-
& canvas {
|
|
560
|
-
contain: strict;
|
|
561
|
-
display: block;
|
|
562
|
-
position: absolute;
|
|
563
|
-
inset: 0;
|
|
564
|
-
z-index: -1;
|
|
565
|
-
width: 100%;
|
|
566
|
-
height: 100%;
|
|
567
|
-
border-radius: inherit;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}`;
|
|
571
|
-
function isPaperShaderElement(element) {
|
|
572
|
-
return "paperShaderMount" in element;
|
|
573
|
-
}
|
|
574
|
-
function isSafari() {
|
|
575
|
-
const ua = navigator.userAgent.toLowerCase();
|
|
576
|
-
return ua.includes("safari") && !ua.includes("chrome") && !ua.includes("android");
|
|
577
|
-
}
|
|
578
|
-
export {
|
|
579
|
-
ShaderMount,
|
|
580
|
-
isPaperShaderElement
|
|
581
|
-
};
|
|
582
|
-
//# sourceMappingURL=shader-mount.js.map
|