update.vert
#version 300 es
layout (location = 0) in vec2 aPosition;
layout (location = 1) in float aAge;
layout (location = 2) in float aLife;
layout (location = 3) in vec2 aVelocity;
out vec2 vPosition;
out vec2 vVelocity;
out float vAge;
out float vLife;
uniform float uTimeDelta;
uniform float uMinTheta;
uniform float uMaxTheta;
uniform float uMinSpeed;
uniform float uMaxSpeed;
uniform vec2 uGravity;
uniform vec2 uOrigin;
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);
}
void main(){
if (aAge >= aLife) {
vec2 noiseCoord = vec2(gl_VertexID % 512, gl_VertexID / 512);
vec2 rand = hash22(noiseCoord);
float theta = uMinTheta + rand.x * (uMaxTheta - uMinTheta);
float x = cos(theta);
float y = sin(theta);
vPosition = uOrigin;
vAge = 0.0;
vLife = aLife;
vVelocity = vec2(x, y) * (uMinSpeed + rand.y * (uMaxSpeed - uMinSpeed));
} else {
vPosition = aPosition + aVelocity * uTimeDelta;
vAge = aAge + uTimeDelta;
vLife = aLife;
vVelocity = aVelocity + uGravity * uTimeDelta;
}
}
render.frag
#version 300 es
precision highp float;
in float vAge;
in float vLife;
out vec4 fragColor;
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b * cos(6.28318 * (c * t + d));
}
void main(){
float t = vAge / vLife;
vec3 outColor = palette(
t,
vec3(0.5, 0.5, 0.5),
vec3(0.5, 0.5, 0.5),
vec3(1.0, 1.0, 1.0),
vec3(0.0, 0.33, 0.67)
);
fragColor = vec4(outColor, 1.0 - t);
}
render.ts
import { SketchGl, type SketchConfig, type SketchFn } from "sketchgl"
import { InterleavedInitialData } from "sketchgl/utility"
import { Timer } from "sketchgl/interactive"
import { SwapTFRenderer } from "sketchgl/renderer"
import { Uniforms } from "sketchgl/program"
import { AliveParticlesSystem } from "@/lib/feature/particle/alive-particle"
import vertForUpdate from "./update.vert?raw"
import fragForUpdate from "./update.frag?raw"
import vertForRender from "./render.vert?raw"
import fragForRender from "./render.frag?raw"
const sketch: SketchFn = ({ gl, canvas }) => {
const particlesCount = 10000
const particleMinAge = 1.01
const particleMaxAge = 1.15
const initialData = new InterleavedInitialData(() => {
const life = particleMinAge + Math.random() * (particleMaxAge - particleMinAge)
return {
vPosition: [0, 0],
vAge: life + 1,
vLife: life,
vVelocity: [0, 0]
}
})
const interleaved = initialData.generate({ count: particlesCount })
const particles = new AliveParticlesSystem(canvas, interleaved.length / 6)
const uniformsFor = {
update: new Uniforms(gl, ["uTimeDelta", "uGravity", "uOrigin", "uMinTheta", "uMaxTheta", "uMinSpeed", "uMaxSpeed"])
}
const renderer = new SwapTFRenderer(gl, initialData.keys)
renderer.attachUpdateProgram(vertForUpdate, fragForUpdate)
renderer.attachRenderProgram(vertForRender, fragForRender)
renderer.registUpdateAttrib("vPosition", { location: 0, components: 2 })
renderer.registUpdateAttrib("vAge", { location: 1, components: 1 })
renderer.registUpdateAttrib("vLife", { location: 2, components: 1 })
renderer.registUpdateAttrib("vVelocity", { location: 3, components: 2 })
renderer.registRenderAttrib("aPosition", { location: 0, components: 2 })
renderer.registRenderAttrib("aAge", { location: 1, components: 1 })
renderer.registRenderAttrib("aLife", { location: 2, components: 1 })
renderer.setup(new Float32Array(interleaved))
uniformsFor.update.init(renderer.glProgramForUpdate)
const timer = new Timer()
timer.start()
gl.enable(gl.BLEND)
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.clearColor(0.0, 0.0, 0.0, 1.0)
return {
drawOnFrame() {
const deltaTime = timer.elapsed
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
renderer.startUpdate()
uniformsFor.update.float("uTimeDelta", deltaTime * 0.001)
uniformsFor.update.fvector2("uGravity", particles.gravity)
uniformsFor.update.fvector2("uOrigin", particles.origin)
uniformsFor.update.float("uMinTheta", particles.minTheta)
uniformsFor.update.float("uMaxTheta", particles.maxTheta)
uniformsFor.update.float("uMinSpeed", particles.minSpeed)
uniformsFor.update.float("uMaxSpeed", particles.maxSpeed)
gl.drawArrays(gl.POINTS, 0, particles.alives)
renderer.endUpdate()
renderer.startRender()
gl.drawArrays(gl.POINTS, 0, particles.alives)
renderer.endRender()
timer.update()
particles.updateForNext(deltaTime)
}
}
}
export const onload = () => {
const config: SketchConfig = {
canvas: {
el: "gl-canvas",
fit: "square",
autoResize: true
}
}
SketchGl.init(config, sketch)
}