index.frag
#version 300 es
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
const uint UINT_MAX = 0xffffffffu;
uvec3 k = uvec3(0x456789abu, 0x6789ab45u, 0x89ab4567u);
uvec3 u = uvec3(1, 2, 3);
uvec3 uhash33(uvec3 n){
n ^= (n.yzx << u);
n ^= (n.yzx >> u);
n *= k;
n ^= (n.yzx << u);
return n * k;
}
vec3 hash33(vec3 p){
uvec3 n = floatBitsToUint(p);
return vec3(uhash33(n)) / vec3(UINT_MAX);
}
vec3 hermite5(vec3 x) {
return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
}
float gnoise31(vec3 p) {
vec3 p0 = floor(p);
vec3 p1 = p0 + vec3(1.0, 0.0, 0.0);
vec3 p2 = p0 + vec3(0.0, 1.0, 0.0);
vec3 p3 = p0 + vec3(1.0, 1.0, 0.0);
vec3 p4 = p0 + vec3(0.0, 0.0, 1.0);
vec3 p5 = p4 + vec3(1.0, 0.0, 0.0);
vec3 p6 = p4 + vec3(0.0, 1.0, 0.0);
vec3 p7 = p4 + vec3(1.0, 1.0, 0.0);
vec3 h0 = normalize(hash33(p0) - vec3(0.5));
vec3 h1 = normalize(hash33(p1) - vec3(0.5));
vec3 h2 = normalize(hash33(p2) - vec3(0.5));
vec3 h3 = normalize(hash33(p3) - vec3(0.5));
vec3 h4 = normalize(hash33(p4) - vec3(0.5));
vec3 h5 = normalize(hash33(p5) - vec3(0.5));
vec3 h6 = normalize(hash33(p6) - vec3(0.5));
vec3 h7 = normalize(hash33(p7) - vec3(0.5));
vec3 f = fract(p);
vec3 d0 = f;
vec3 d1 = f - vec3(1.0, 0.0, 0.0);
vec3 d2 = f - vec3(0.0, 1.0, 0.0);
vec3 d3 = f - vec3(1.0, 1.0, 0.0);
vec3 d4 = f - vec3(0.0, 0.0, 1.0);
vec3 d5 = f - vec3(1.0, 0.0, 1.0);
vec3 d6 = f - vec3(0.0, 1.0, 1.0);
vec3 d7 = f - vec3(1.0, 1.0, 1.0);
float dh0 = dot(d0, h0);
float dh1 = dot(d1, h1);
float dh2 = dot(d2, h2);
float dh3 = dot(d3, h3);
float dh4 = dot(d4, h4);
float dh5 = dot(d5, h5);
float dh6 = dot(d6, h6);
float dh7 = dot(d7, h7);
vec3 w = hermite5(f);
float z0 = mix(mix(dh0, dh1, w.x), mix(dh2, dh3, w.x), w.y);
float z1 = mix(mix(dh4, dh5, w.x), mix(dh6, dh7, w.x), w.y);
float i = mix(z0, z1, w.z);
return i * 0.5 + 0.5;
}
void main() {
vec2 pos = gl_FragCoord.xy / min(uResolution.x, uResolution.y);
pos *= 10.0;
pos += uTime;
float noise = gnoise31(vec3(pos, uTime));
fragColor = vec4(vec3(noise), 1.0);
}
render.ts
import { SketchFrg, type FragmentSketchConfig, type FragmentSketchFn } from "sketchgl"
import { Timer } from "sketchgl/interactive"
import { Uniforms } from "sketchgl/program"
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)
}