import { UPDATE_VERT, UPDATE_FRAG } from "./shader.js"
import { RENDER_VERT, RENDER_FRAG } from "./shader.js"
import Olon from "https://cdn.jsdelivr.net/npm/olon@0.2.4/dist/Olon.min.js"
import { random, min } from "./tools.js"
const MAX_AMOUNT = COLS * ROWS
const ol = Olon(innerWidth, innerHeight, true)
ol.blend({ sfactor: ol.SRC_ALPHA, dfactor: ol.ONE })
const TFV = ["vPosition", "vAge", "vLife", "vVel"]
const updateProgram = ol.createProgram(UPDATE_VERT, UPDATE_FRAG, TFV)
const renderProgram = ol.createProgram(RENDER_VERT, RENDER_FRAG)
const aPosition = { name: "aPosition", unit: "f32", size: 2 }
const aAge = { name: "aAge", unit: "f32", size: 1 }
const aLife = { name: "aLife", unit: "f32", size: 1 }
const aVel = { name: "aVel", unit: "f32", size: 2 }
const aCoord = { name: "aCoord", unit: "f32", size: 2 }
const aTexCoord = { name: "aTexCoord", unit: "f32", size: 2 }
const updateAttribs = [aPosition, aAge, aLife, aVel]
const renderAttribs = [aPosition, aAge, aLife]
const quadAttribs = [aCoord, aTexCoord]
for (var i = 0; i < MAX_AMOUNT; i++) {
const LIFE = random(MIN_AGE, MAX_AGE)
particleData.push(0, 0, LIFE + 1, LIFE, 0, 0)
const initData = ol.Data(particleData)
const quadData = ol.quadData()
const buffer0 = ol.createBuffer(initData, ol.STREAM_DRAW)
const buffer1 = ol.createBuffer(initData, ol.STREAM_DRAW)
const quadBuffer = ol.createBuffer(quadData, ol.STATIC_DRAW)
const VAOConfig = (buffer, stride, attributes, divisor) => ({ buffer, stride, attributes, divisor })
const updateVAO0 = ol.createVAO(updateProgram, [
VAOConfig(buffer0, 4 * 6, updateAttribs),
const updateVAO1 = ol.createVAO(updateProgram, [
VAOConfig(buffer1, 4 * 6, updateAttribs),
const renderVAO0 = ol.createVAO(renderProgram, [
VAOConfig(buffer0, 4 * 6, renderAttribs, 1),
VAOConfig(quadBuffer, 4 * 4, quadAttribs),
const renderVAO1 = ol.createVAO(renderProgram, [
VAOConfig(buffer1, 4 * 6, renderAttribs, 1),
VAOConfig(quadBuffer, 4 * 4, quadAttribs),
const buffers = [buffer0, buffer1]
const updateVAOs = [updateVAO0, updateVAO1]
const renderVAOs = [renderVAO0, renderVAO1]
let [read, write] = [0, 1]
BORN_AMOUNT = min(MAX_AMOUNT, BORN_AMOUNT + 10)
ol.clearColor(0, 0, 0, 1)
ol.transformFeedback(updateVAOs[read], buffers[write], ol.POINTS, () => {
ol.uniform("uMouse", ol.mouse)
ol.uniform("uSpeed", SPEED)
ol.uniform("uTime", ol.frame / 60)
ol.uniform("uScale", ol.width / ol.height)
ol.points(0, BORN_AMOUNT)
ol.uniform("uScale", ol.width / ol.height)
ol.trianglesInstanced(0, 6, BORN_AMOUNT)
;[read, write] = [write, read]