index.vert
#version 300 es
layout (location = 0) in vec3 aVertexPosition;
layout (location = 1) in vec2 aVertexTextureCoords;
out vec2 vTextureCoords;
void main() {
vTextureCoords = aVertexTextureCoords;
gl_Position = vec4(aVertexPosition, 1.0);
}
index.frag
#version 300 es
precision highp float;
const float R_LUMINANCE = 0.298912;
const float G_LUMINANCE = 0.586611;
const float B_LUMINANCE = 0.114478;
float toMonochrome(vec3 color) {
return dot(color, vec3(R_LUMINANCE, G_LUMINANCE, B_LUMINANCE));
}
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;
}
float hash21(vec2 b) {
uvec2 n = floatBitsToUint(b);
return float(uhash22(n).x) / float(UINT_MAX);
}
uniform sampler2D uTexture0;
in vec2 vTextureCoords;
out vec4 fragColor;
void main() {
vec2 texCoord = vec2(vTextureCoords.x, 1.0 - vTextureCoords.y);
vec3 inputColor = texture(uTexture0, texCoord).rgb;
float gray = toMonochrome(inputColor);
float threshold = hash21(texCoord);
vec3 outColor = gray < threshold ? vec3(0.5) : vec3(1.0);
fragColor = vec4(outColor, 1.0);
}
render.ts
import { SketchFilter, type FilterSketchConfig, type FilterSketchFn } from "sketchgl"
import { ImageTexture } from "sketchgl/texture"
import { Program } from "sketchgl/program"
import { CanvasCoverPolygon } from "sketchgl/geometry"
import mainVertSrc from "./index.vert?raw"
import mainFragSrc from "./index.frag?raw"
import imageGeometry from "@/assets/original/pastel-tomixy.png"
import imageAutumnLeaves from "@/assets/original/autumn-leaves_00037.jpg"
import imageWater from "@/assets/original/cat.jpg"
import imageGoldFish from "@/assets/original/fireworks_00018.jpg"
const sketch: FilterSketchFn = ({ gl, fitImage }) => {
const images = [
{ name: "花火", src: imageGoldFish },
{ name: "立方体", src: imageGeometry },
{ name: "紅葉", src: imageAutumnLeaves },
{ name: "猫", src: imageWater }
]
const imageNames = images.map((obj) => obj.name)
const textures = images.map((img) => new ImageTexture(gl, img.src))
let activeImage = 1
const program = new Program(gl)
program.attach(mainVertSrc, mainFragSrc)
program.activate()
const plane = new CanvasCoverPolygon(gl)
plane.setLocations({ vertices: 0, uv: 1 })
gl.clearColor(1.0, 0.0, 0.0, 1.0)
gl.clearDepth(1.0)
return {
preload: [...textures.map((tex) => tex.load())],
preloaded: [() => fitImage(textures[activeImage].img)],
drawOnFrame() {
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
plane.bind()
textures[activeImage].activate(program.glProgram!, "uTexture0")
plane.draw({ primitive: "TRIANGLES" })
},
control(ui) {
ui.select("Image", images[activeImage].name, imageNames, (name) => {
const idx = imageNames.indexOf(name)
if (idx < 0) return
activeImage = idx
fitImage(textures[activeImage].img)
})
}
}
}
export const onload = () => {
const config: FilterSketchConfig = {
canvas: {
el: "gl-canvas",
autoResize: true
}
}
SketchFilter.init(config, sketch)
}