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);
}
uvec3 uhash33(uvec3 n){
n ^= (n.yzx << u);
n ^= (n.yzx >> u);
n *= k;
n ^= (n.yzx << u);
return n * k;
}
float gtable3(vec3 lattice, vec3 p){
uvec3 n = floatBitsToUint(lattice);
uint ind = uhash33(n).x >> 28;
float u = ind < 8u ? p.x : p.y;
float v = ind < 4u ? p.y : ind == 12u || ind == 14u ? p.x : p.z;
return ((ind & 1u) == 0u? u: -u) + ((ind & 2u) == 0u? v : -v);
}
vec3 hermite5(vec3 x) {
return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
}
const float apx_1inSqrt2 = 0.70710678;
float pnoise31(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 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 = gtable3(p0, d0) * apx_1inSqrt2;
float dh1 = gtable3(p1, d1) * apx_1inSqrt2;
float dh2 = gtable3(p2, d2) * apx_1inSqrt2;
float dh3 = gtable3(p3, d3) * apx_1inSqrt2;
float dh4 = gtable3(p4, d4) * apx_1inSqrt2;
float dh5 = gtable3(p5, d5) * apx_1inSqrt2;
float dh6 = gtable3(p6, d6) * apx_1inSqrt2;
float dh7 = gtable3(p7, d7) * apx_1inSqrt2;
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;
}
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.ts
import { SketchGl, type SketchConfig, type SketchFn } from "sketchgl"
import { InterleavedInitialData } from "sketchgl/utility"
import { AliveParticlesSystem } from "@/lib/feature/particle/alive-particle"
import { Timer } from "sketchgl/interactive"
import { SwapSpriteTFRenderer } from "sketchgl/renderer"
import { Uniforms } from "sketchgl/program"
import { ImageTexture } from "sketchgl/texture"
import vertForUpdate from "./update.vert?raw"
import fragForUpdate from "./update.frag?raw"
import vertForRender from "./render.vert?raw"
import fragForRender from "./render.frag?raw"
import sprite from "@/assets/datauri/fireball.txt?raw"
const sketch: SketchFn = ({ gl, canvas }) => {
const particlesCount = 800
const particleMinAge = 0.8
const particleMaxAge = 0.9
const interleave = new InterleavedInitialData(() => {
const life = particleMinAge + Math.random() * (particleMaxAge - particleMinAge)
return {
vPosition: [0, 0],
vAge: life + 1,
vLife: life,
vVelocity: [0, 0]
}
})
const initialData = interleave.generate({ count: particlesCount })
const spriteData = [1, 1, 1, 1, -1, 1, 0, 1, -1, -1, 0, 0, 1, 1, 1, 1, -1, -1, 0, 0, 1, -1, 1, 0]
const particles = new AliveParticlesSystem(canvas, initialData.length / 6)
particles.gravity = [0, 0]
particles.minSpeed = 0.1
particles.maxSpeed = 0.5
const uniformsFor = {
update: new Uniforms(gl, ["uTimeDelta", "uGravity", "uOrigin", "uMinTheta", "uMaxTheta", "uMinSpeed", "uMaxSpeed"])
}
const renderer = new SwapSpriteTFRenderer(gl, interleave.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, divisor: 1 })
renderer.registRenderAttrib("aAge", { location: 1, components: 1, divisor: 1 })
renderer.registRenderAttrib("aLife", { location: 2, components: 1, divisor: 1 })
renderer.registSpriteAttrib("aInstanceCoord", { location: 3, components: 2 })
renderer.registSpriteAttrib("aInstanceTexCoord", { location: 4, components: 2 })
renderer.setup(new Float32Array(initialData), new Float32Array(spriteData))
uniformsFor.update.init(renderer.glProgramForUpdate)
const spriteTexture = new ImageTexture(gl, sprite)
spriteTexture.MAG_FILTER = "LINEAR"
spriteTexture.MIN_FILTER = "LINEAR"
const timer = new Timer()
timer.start()
gl.enable(gl.BLEND)
gl.blendFunc(gl.SRC_ALPHA, gl.ONE)
gl.clearColor(0.0, 0.0, 0.0, 1.0)
return {
preload: [spriteTexture.load()],
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()
spriteTexture.activate(renderer.glProgramForRender!, "uSprite")
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, particles.alives)
renderer.endRender()
timer.update()
particles.updateForNext(deltaTime)
}
}
}
export const onload = () => {
const config: SketchConfig = {
canvas: {
el: "gl-canvas",
fit: "screen",
autoResize: true
}
}
SketchGl.init(config, sketch)
}