index.vert
#version 300 es
void main() {
float x = float((gl_VertexID & 1) << 2);
float y = float((gl_VertexID & 2) << 1);
gl_Position = vec4(x - 1.0, y - 1.0, 0, 1);
}
index.frag
#version 300 es
precision highp float;
out vec4 fragColor;
uniform vec2 uResolution;
uniform float uTime;
const uint UINT_MAX = 0xffffffffu;
uvec3 k = uvec3(0x456789abu, 0x6789ab45u, 0x89ab4567u);
uvec3 u = uvec3(1, 2, 3);
uvec2 uhash22(uvec2 n){
n ^= (n.yx << u.xy);
n ^= (n.yx >> u.xy);
n *= k.xy;
n ^= (n.yx << u.xy);
return n * k.xy;
}
vec2 hash22(vec2 b) {
uvec2 n = floatBitsToUint(b);
return vec2(uhash22(n)) / vec2(UINT_MAX);
}
vec2 voronoi2(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
float distMin = 1.0;
vec2 iMin;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 offset = vec2(float(x), float(y));
vec2 neighbor = hash22(i + offset);
float dist = distance(neighbor + offset, f);
if (distMin > dist) {
iMin = neighbor;
distMin = dist;
}
}
}
return iMin;
}
void main() {
vec2 pos = gl_FragCoord.xy / min(uResolution.x, uResolution.y);
pos *= 10.0;
pos += uTime;
vec2 voronoi = voronoi2(pos);
vec2 noise = hash22(voronoi);
fragColor = vec4(noise, 1.0, 1.0);
}
render.ts
import { SketchFrg, type FragmentSketchConfig, type FragmentSketchFn } from "sketchgl"
import { Uniforms } from "sketchgl/program"
import { Timer } from "sketchgl/interactive"
import frag from "./index.frag?raw"
const sketch: FragmentSketchFn = ({ gl, canvas, program, renderToCanvas }) => {
const uniforms = new Uniforms(gl, ["uResolution", "uTime"])
uniforms.init(program)
const timer = new Timer()
timer.start()
gl.clearColor(0.0, 0.0, 0.0, 1.0)
gl.clearDepth(1.0)
return {
drawOnFrame() {
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
uniforms.float("uTime", timer.elapsed * 0.001)
uniforms.fvector2("uResolution", [canvas.width, canvas.height])
renderToCanvas()
}
}
}
export const onload = () => {
const config: FragmentSketchConfig = {
frag,
canvas: {
el: "gl-canvas",
fit: "screen",
autoResize: true
}
}
SketchFrg.init(config, sketch)
}