diff --git a/scripts/particle.js b/scripts/particle.js index 6919129..8c7e41e 100644 --- a/scripts/particle.js +++ b/scripts/particle.js @@ -7,13 +7,16 @@ emitter.spawn_timer = 0; emitter.pps = 0; emitter.color = Color.white; -emitter.draw = function () { +var ssbo; + +var drawnum = 0; +emitter.draw = function (arr) { var pars = Object.values(this.particles); if (pars.length === 0) return; - render.use_shader(this.shader); - render.use_mat(this); - render.make_particle_ssbo(pars, this.ssbo); - render.draw(this.shape, this.ssbo, pars.length); +// render.make_particle_ssbo(pars, ssbo); + for (var i of pars) + arr.push(i); +// drawnum += pars.length; }; emitter.kill = function () { @@ -108,7 +111,44 @@ function update_emitters(dt) { } function draw_emitters() { - for (var e of emitters) e.draw(); + ssbo ??= render.make_textssbo(); + render.use_shader("shaders/baseparticle.cg"); + var buckets = new Map(); + var base = 0; + for (var e of emitters) { + var bucket = buckets.get(e.diffuse); + if (!bucket) + buckets.set(e.diffuse, [e]); + else + bucket.push(e); + } + + for (var bucket of buckets.values()) { + drawnum = 0; + var append = []; + bucket[0].baseinstance = base; + render.use_mat(bucket[0]); + for (var e of bucket) + e.draw(append); + + render.make_particle_ssbo(append, ssbo); + render.draw(bucket[0].shape, ssbo, append.length); + base += append.length; + } + +// for (var e of emitters) e.draw(); + +// render.draw(shape.centered_quad, ssbo, drawnum); } -return { make_emitter, update_emitters, draw_emitters }; +function stat_emitters() +{ + var stat = {}; + stat.emitters = emitters.length; + var particles = 0; + for (var e of emitters) particles += Object.values(e.particles).length; + stat.particles = particles + return stat; +} + +return { make_emitter, update_emitters, draw_emitters, stat_emitters }; diff --git a/scripts/profile.js b/scripts/profile.js index fd5688c..c2745e7 100644 --- a/scripts/profile.js +++ b/scripts/profile.js @@ -352,6 +352,9 @@ var get_snapshot = function() snap.mallinfo ??= {}; Object.assign(snap.mallinfo, os.mallinfo()); + snap.particles ??= {}; + Object.assign(snap.particles, stat_emitters()); + snap.obj ??= {}; for (var i of classes) { var proto = globalThis[`${i}_proto`]; diff --git a/shaders/baseparticle.cg b/shaders/baseparticle.cg index 1e8da56..01ea057 100644 --- a/shaders/baseparticle.cg +++ b/shaders/baseparticle.cg @@ -20,10 +20,11 @@ out vec2 uv; out vec4 color0; uniform mat4 vp; +uniform float baseinstance; void main() { - particle p = par[gl_InstanceIndex]; + particle p = par[int(baseinstance)+gl_InstanceIndex]; gl_Position = vp * p.model * vec4(a_pos, 0.0, 1.0); uv = a_uv; color0 = p.color;