faster particles; imgui ini

This commit is contained in:
John Alanbrook 2024-09-30 13:06:31 -05:00
parent 3717e84fe7
commit 6456590492
2 changed files with 30 additions and 39 deletions

View file

@ -1,5 +1,4 @@
var emitter = {}; var emitter = {};
emitter.particles = {};
emitter.life = 10; emitter.life = 10;
emitter.scale = 1; emitter.scale = 1;
emitter.grow_for = 0; emitter.grow_for = 0;
@ -9,16 +8,6 @@ emitter.color = Color.white;
var ssbo; var ssbo;
var drawnum = 0;
emitter.draw = function (arr) {
var pars = Object.values(this.particles);
if (pars.length === 0) return;
// render.make_particle_ssbo(pars, ssbo);
for (var i of pars)
arr.push(i);
// drawnum += pars.length;
};
emitter.kill = function () { emitter.kill = function () {
emitters.remove(this); emitters.remove(this);
}; };
@ -26,10 +15,10 @@ emitter.kill = function () {
var std_step = function (p) { var std_step = function (p) {
if (p.time < this.grow_for) { if (p.time < this.grow_for) {
var s = Math.lerp(0, this.scale, p.time / this.grow_for); var s = Math.lerp(0, this.scale, p.time / this.grow_for);
p.transform.scale = [s, s, s]; p.transform.scale = s;
} else if (p.time > p.life - this.shrink_for) { } else if (p.time > p.life - this.shrink_for) {
var s = Math.lerp(0, this.scale, (p.life - p.time) / this.shrink_for); var s = Math.lerp(0, this.scale, (p.life - p.time) / this.shrink_for);
p.transform.scale = [s, s, s]; p.transform.scale = s;
} else p.transform.scale = [this.scale, this.scale, this.scale]; } else p.transform.scale = [this.scale, this.scale, this.scale];
}; };
@ -41,8 +30,8 @@ emitter.spawn = function (t) {
var par = this.dead.shift(); var par = this.dead.shift();
if (par) { if (par) {
par.body.pos = t.pos; par.body.pos = t.pos;
par.transform.scale = [this.scale, this.scale, this.scale]; par.transform.scale = this.scale;
this.particles[par.id] = par; this.particles.push(par);
par.time = 0; par.time = 0;
this.spawn_hook?.(par); this.spawn_hook?.(par);
par.life = this.life; par.life = this.life;
@ -59,9 +48,8 @@ emitter.spawn = function (t) {
par.body = os.make_body(par.transform); par.body = os.make_body(par.transform);
par.body.pos = t.pos; par.body.pos = t.pos;
par.transform.scale = [this.scale, this.scale, this.scale]; par.transform.scale = this.scale;
par.id = prosperon.guid(); this.particles.push(par);
this.particles[par.id] = par;
this.spawn_hook(par); this.spawn_hook(par);
}; };
@ -78,14 +66,14 @@ emitter.step = function (dt) {
} }
// update all particles // update all particles
for (var p of Object.values(this.particles)) { for (var p of this.particles) {
p.time += dt; p.time += dt;
this.step_hook?.(p); this.step_hook?.(p);
if (this.kill_hook?.(p) || p.time >= p.life) { if (this.kill_hook?.(p) || p.time >= p.life) {
this.die_hook?.(p); this.die_hook?.(p);
this.dead.push(this.particles[p.id]); this.dead.push(p);
delete this.particles[p.id]; this.particles.remove(p);
} }
} }
}; };
@ -101,6 +89,7 @@ var make_emitter = function () {
e.ssbo = render.make_textssbo(); e.ssbo = render.make_textssbo();
e.shape = shape.centered_quad; e.shape = shape.centered_quad;
e.shader = "shaders/baseparticle.cg"; e.shader = "shaders/baseparticle.cg";
e.particles = [];
e.dead = []; e.dead = [];
emitters.push(e); emitters.push(e);
return e; return e;
@ -110,35 +99,33 @@ function update_emitters(dt) {
for (var e of emitters) e.step(dt); for (var e of emitters) e.step(dt);
} }
var arr = [];
function draw_emitters() { function draw_emitters() {
ssbo ??= render.make_textssbo(); ssbo ??= render.make_textssbo();
render.use_shader("shaders/baseparticle.cg"); render.use_shader("shaders/baseparticle.cg");
var buckets = new Map(); var buckets = {};
var base = 0; var base = 0;
for (var e of emitters) { for (var e of emitters) {
var bucket = buckets.get(e.diffuse); var bucket = buckets[e.diffuse.path];
if (!bucket) if (!bucket)
buckets.set(e.diffuse, [e]); buckets[e.diffuse.path] = [e];
else else
bucket.push(e); bucket.push(e);
} }
for (var bucket of buckets.values()) { for (var path in buckets) {
drawnum = 0; arr.length = 0;
var append = []; var bucket = buckets[path];
bucket[0].baseinstance = base; bucket[0].baseinstance = base;
render.use_mat(bucket[0]); render.use_mat(bucket[0]);
for (var e of bucket) for (var e of bucket) {
e.draw(append); if (e.particles.length === 0) continue;
for (var p of e.particles) arr.push(p);
render.make_particle_ssbo(append, ssbo); }
render.draw(bucket[0].shape, ssbo, append.length); render.make_particle_ssbo(arr, ssbo);
base += append.length; render.draw(bucket[0].shape, ssbo, arr.length);
base += arr.length;
} }
// for (var e of emitters) e.draw();
// render.draw(shape.centered_quad, ssbo, drawnum);
} }
function stat_emitters() function stat_emitters()
@ -146,8 +133,8 @@ function stat_emitters()
var stat = {}; var stat = {};
stat.emitters = emitters.length; stat.emitters = emitters.length;
var particles = 0; var particles = 0;
for (var e of emitters) particles += Object.values(e.particles).length; for (var e of emitters) particles += e.particles.length;
stat.particles = particles stat.particles = particles;
return stat; return stat;
} }

View file

@ -954,6 +954,10 @@ JSValue gui_init(JSContext *js)
simgui_desc_t sdesc = {}; simgui_desc_t sdesc = {};
simgui_setup(&sdesc); simgui_setup(&sdesc);
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = ".prosperon/imgui.ini";
ImGui::LoadIniSettingsFromDisk(".prosperon/imgui.ini");
sgimgui_desc_t desc = {0}; sgimgui_desc_t desc = {0};
sgimgui_init(&sgimgui, &desc); sgimgui_init(&sgimgui, &desc);