fix particle render bug

This commit is contained in:
John Alanbrook 2024-08-25 14:23:22 -05:00
parent 76497fb600
commit 94d4d86944
6 changed files with 49 additions and 45 deletions

View file

@ -9,17 +9,12 @@ emitter.color = Color.white;
emitter.draw = function() emitter.draw = function()
{ {
var amt = Object.values(this.particles).length; var pars = Object.values(this.particles);
if (amt === 0) return; if (pars.length === 0) return;
render.use_shader(this.shader); render.use_shader(this.shader);
render.use_mat(this); render.use_mat(this);
render.make_particle_ssbo(Object.values(this.particles), this.ssbo); render.make_particle_ssbo(pars, this.ssbo);
render.draw(this.shape, this.ssbo, amt); render.draw(this.shape, this.ssbo, pars.length);
}
var std_spawn = function(par)
{
} }
var std_step = function(p) var std_step = function(p)
@ -35,13 +30,7 @@ var std_step = function(p)
p.transform.scale = [this.scale,this.scale,this.scale]; p.transform.scale = [this.scale,this.scale,this.scale];
} }
var std_die = function(par)
{
}
emitter.spawn_hook = std_spawn;
emitter.step_hook = std_step; emitter.step_hook = std_step;
emitter.die_hook = std_die;
emitter.spawn = function(t) emitter.spawn = function(t)
{ {
@ -53,7 +42,7 @@ emitter.spawn = function(t)
par.transform.scale = [this.scale,this.scale,this.scale]; par.transform.scale = [this.scale,this.scale,this.scale];
this.particles[par.id] = par; this.particles[par.id] = par;
par.time = 0; par.time = 0;
this.spawn_hook(par); this.spawn_hook?.(par);
return; return;
} }
@ -92,7 +81,7 @@ emitter.step = function(dt)
this.step_hook?.(p); this.step_hook?.(p);
if (p.time >= p.life) { if (p.time >= p.life) {
this.die_hook(p); this.die_hook?.(p);
this.dead.push(this.particles[p.id]); this.dead.push(this.particles[p.id]);
delete this.particles[p.id]; delete this.particles[p.id];
} }
@ -103,6 +92,8 @@ emitter.burst = function(count, t) {
for (var i = 0; i < count; i++) this.spawn(t); for (var i = 0; i < count; i++) this.spawn(t);
} }
var emitters = [];
var make_emitter = function() var make_emitter = function()
{ {
var e = Object.create(emitter); var e = Object.create(emitter);
@ -110,7 +101,19 @@ var make_emitter = function()
e.shape = shape.centered_quad; e.shape = shape.centered_quad;
e.shader = "shaders/baseparticle.cg"; e.shader = "shaders/baseparticle.cg";
e.dead = []; e.dead = [];
emitters.push(e);
return e; return e;
} }
return {make_emitter}; function update_emitters(dt)
{
for (var e of emitters)
e.step(dt);
}
function draw_emitters()
{
for (var e of emitters) e.draw();
}
return {make_emitter, update_emitters, draw_emitters};

View file

@ -295,6 +295,7 @@ window.__proto__.toJSON = function()
return { return {
size: this.size, size: this.size,
fullscreen: this.fullscreen, fullscreen: this.fullscreen,
title: this.title
}; };
} }

View file

@ -390,13 +390,6 @@ function shader_apply_material(shader, material = {}, old = {})
function sg_bind(mesh, ssbo) function sg_bind(mesh, ssbo)
{ {
if (cur.mesh === mesh && cur.bind) {
cur.bind.inst = 1;
cur.bind.images = cur.images;
render.setbind(cur.bind);
return cur.bind;
}
cur.mesh = mesh; cur.mesh = mesh;
var bind = {}; var bind = {};
@ -525,6 +518,11 @@ render.init = function() {
} }
} }
render.draw_sprites = true;
render.draw_particles = true;
render.draw_hud = true;
render.draw_gui = true;
render.sprites = function render_sprites(gridsize = 1) render.sprites = function render_sprites(gridsize = 1)
{ {
// When y sorting, draw layer is firstly important followed by the gameobject position // When y sorting, draw layer is firstly important followed by the gameobject position
@ -827,18 +825,6 @@ render.slice9 = function(tex, pos, bb, scale = [tex.width,tex.height], color = C
render.draw(shape.quad); render.draw(shape.quad);
} }
render.emitter = function(emit)
{
var amt = Object.values(emit.particles).length;
if (amt === 0) return;
render.use_shader(parshader);
render.use_mat(emit);
var ts = [];
for (var p of Object.values(emit.particles)) ts.push([p.transform,p.color]);
render.make_particle_ssbo(ts, emit.ssbo);
render.draw(shape.quad, emit.ssbo, amt);
}
var textssbo; var textssbo;
render.flush_text = function() render.flush_text = function()
@ -984,7 +970,8 @@ prosperon.render = function()
profile.frame("world"); profile.frame("world");
render.set_camera(prosperon.camera); render.set_camera(prosperon.camera);
profile.frame("sprites"); profile.frame("sprites");
render.sprites(); if (render.draw_sprites) render.sprites();
if (render.draw_particles) draw_emitters();
profile.endframe(); profile.endframe();
profile.frame("draws"); profile.frame("draws");
prosperon.draw(); prosperon.draw();
@ -996,7 +983,7 @@ prosperon.render = function()
profile.endframe(); profile.endframe();
profile.frame("hud"); profile.frame("hud");
prosperon.hud(); if (render.draw_hud) prosperon.hud();
render.flush_text(); render.flush_text();
render.end_pass(); render.end_pass();
@ -1029,7 +1016,7 @@ prosperon.render = function()
// Call gui functions // Call gui functions
mum.style = mum.dbg_style; mum.style = mum.dbg_style;
prosperon.gui(); if (render.draw_gui) prosperon.gui();
if (mum.drawinput) mum.drawinput(); if (mum.drawinput) mum.drawinput();
prosperon.gui_dbg(); prosperon.gui_dbg();
render.flush_text(); render.flush_text();
@ -1078,6 +1065,7 @@ prosperon.process = function process() {
if (sim.mode === "play" || sim.mode === "step") { if (sim.mode === "play" || sim.mode === "step") {
profile.frame("update"); profile.frame("update");
prosperon.update(dt * game.timescale); prosperon.update(dt * game.timescale);
update_emitters(dt * game.timescale);
profile.endframe(); profile.endframe();
if (sim.mode === "step") sim.pause(); if (sim.mode === "step") sim.pause();

View file

@ -22,14 +22,15 @@ audio.bus.master = dspsound.master();
audio.dsp = {}; audio.dsp = {};
audio.dsp = dspsound; audio.dsp = dspsound;
audio.dsp.mix().__proto__.imgui = function()
audio.bus.master.__proto__.imgui = function()
{ {
imgui.pushid(this.memid());
this.volume = imgui.slider("Volume", this.volume); this.volume = imgui.slider("Volume", this.volume);
this.off = imgui.checkbox("Mute", this.off); this.off = imgui.checkbox("Mute", this.off);
imgui.popid();
} }
audio.cry = function(file, bus = audio.bus.master) audio.cry = function(file, bus = audio.bus.sfx)
{ {
file = Resources.find_sound(file); file = Resources.find_sound(file);
var player = audio.play(file, bus); var player = audio.play(file, bus);
@ -83,6 +84,9 @@ audio.music = function(file, fade = 0.5) {
audio.bus.music = audio.dsp.mix(); audio.bus.music = audio.dsp.mix();
audio.bus.music.plugin(audio.bus.master); audio.bus.music.plugin(audio.bus.master);
audio.bus.sfx = audio.dsp.mix();
audio.bus.sfx.plugin(audio.bus.master);
audio.dsp.allpass = function(secs, decay) { audio.dsp.allpass = function(secs, decay) {
var composite = {}; var composite = {};
var fwd = audio.dsp.fwd_delay(secs,-decay); var fwd = audio.dsp.fwd_delay(secs,-decay);

View file

@ -7,6 +7,7 @@
#define SOKOL_IMPL #define SOKOL_IMPL
#include "sokol/util/sokol_imgui.h" #include "sokol/util/sokol_imgui.h"
#include "sokol/util/sokol_gfx_imgui.h" #include "sokol/util/sokol_gfx_imgui.h"
#include <stdlib.h>
static sgimgui_t sgimgui; static sgimgui_t sgimgui;
@ -82,7 +83,7 @@ JSC_SCALL(imgui_text,
JSC_SCALL(imgui_button, JSC_SCALL(imgui_button,
if (ImGui::Button(str)) if (ImGui::Button(str))
script_call_sym(argv[1], 0, NULL); script_call_sym(argv[1], 1, argv);
) )
JSC_CCALL(imgui_sokol_gfx, JSC_CCALL(imgui_sokol_gfx,
@ -115,9 +116,17 @@ JSC_SCALL(imgui_checkbox,
ret = boolean2js(val); ret = boolean2js(val);
) )
JSC_CCALL(imgui_pushid,
ImGui::PushID(js2number(argv[0]));
)
JSC_CCALL(imgui_popid, ImGui::PopID(); )
static const JSCFunctionListEntry js_imgui_funcs[] = { static const JSCFunctionListEntry js_imgui_funcs[] = {
MIST_FUNC_DEF(imgui, window, 2), MIST_FUNC_DEF(imgui, window, 2),
MIST_FUNC_DEF(imgui, menu, 2), MIST_FUNC_DEF(imgui, menu, 2),
MIST_FUNC_DEF(imgui, pushid, 1),
MIST_FUNC_DEF(imgui, popid, 0),
MIST_FUNC_DEF(imgui, slider, 4), MIST_FUNC_DEF(imgui, slider, 4),
MIST_FUNC_DEF(imgui, menubar, 1), MIST_FUNC_DEF(imgui, menubar, 1),
MIST_FUNC_DEF(imgui, mainmenubar, 1), MIST_FUNC_DEF(imgui, mainmenubar, 1),

View file

@ -1024,7 +1024,6 @@ JSC_CCALL(render_make_particle_ssbo,
size_t size = js_arrlen(array)*(sizeof(particle_ss)); size_t size = js_arrlen(array)*(sizeof(particle_ss));
sg_buffer *b = js2sg_buffer(argv[1]); sg_buffer *b = js2sg_buffer(argv[1]);
if (!b) return JS_UNDEFINED; if (!b) return JS_UNDEFINED;
particle_ss ms[js_arrlen(array)]; particle_ss ms[js_arrlen(array)];
if (sg_query_buffer_will_overflow(*b, size)) { if (sg_query_buffer_will_overflow(*b, size)) {