index.frag
#version 300 es
precision highp float;
#pragma glslify: bilerp_colors4 = require('sketchgl/glsl/bilerp-colors4');
uniform vec2 uResolution;
out vec4 outColor;
void main() {
vec2 pos = gl_FragCoord.xy / uResolution.xy;
vec3 RED = vec3(1.0, 0.0, 0.0);
vec3 GREEN = vec3(0.0, 1.0, 0.0);
vec3 BLUE = vec3(0.0, 0.0, 1.0);
vec3 YELLOW = vec3(1.0, 1.0, 0.0);
vec3[4] colors4 = vec3[](RED, BLUE, GREEN, YELLOW);
vec3 bilerpColor = bilerp_colors4(colors4, pos);
outColor = vec4(bilerpColor, 1.0);
}
render.ts
import { SketchFrg, type FragmentSketchConfig, type FragmentSketchFn } from "sketchgl"
import { Uniforms } from "sketchgl/program"
import frag from "./index.frag"
const sketch: FragmentSketchFn = ({ gl, canvas, program, renderToCanvas }) => {
const uniforms = new Uniforms(gl, ["uResolution"])
uniforms.init(program)
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.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)
}