Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

A particle system shown in place starts without motion vectors (Bugbot)

Evan (Yifeng) Wang39ec804f5649
diff --git a/renderer/shaders/particles.wgsl b/renderer/shaders/particles.wgsl
index e2d888d..5855010 100644
--- a/renderer/shaders/particles.wgsl
+++ b/renderer/shaders/particles.wgsl
@@ -44,8 +44,8 @@ struct TriAttr { n0: vec4<f32>, n1: vec4<f32>, n2: vec4<f32>, uv: vec4<f32>, t:
// a: (first triangle in the GPU order, count, first object, function); b: (seed, first dynamic triangle, translucent
// (1: its triangles are the translucency pass's, `tblas` from the first translucent triangle), first translucent
-// triangle); c: the node's pivot, w > 0: collapse every particle (the system was just hidden: gpu.rs
-// `set_particles_hidden`); p: parameters; d: the node's customData.
+// triangle); c: the node's pivot, w: 1 collapse every particle (the system was just hidden), 2 its first run since it
+// was shown (no last frame: motion vectors 0) (gpu.rs `set_particles_hidden`); p: parameters; d: the node's customData.
struct System { a: vec4<u32>, b: vec4<u32>, c: vec4<f32>, p: array<vec4<f32>, 8>, d: vec4<f32> };
@group(0) @binding(0) var<uniform> PPS: PP;
@@ -78,10 +78,9 @@ fn particle_function(f: u32, i: PIn, p: array<vec4<f32>, 8>) -> POut { return PO
// scene.rs tri_attr), BLAS input.
// Translucent triangles (`trans` = first translucent triangle) have no motion vectors (the pass draws them over the
// image each frame) and their own BLAS input.
-fn put_tri(t: u32, dyn_first: u32, trans: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>, lod_k: f32) {
+fn put_tri(t: u32, dyn_first: u32, trans: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>, lod_k: f32, first: bool) {
let flags = verts[t * 3u].w;
let now = array<vec4<f32>, 3>(vec4<f32>(a, flags), vec4<f32>(b - a, 0.0), vec4<f32>(c - a, 0.0));
- let first = PPS.env.z > 0.5;
for (var k = 0u; k < 3u; k++) {
verts[t * 3u + k] = now[k];
if (trans == 0xffffffffu) {
@@ -133,7 +132,7 @@ fn particles(@builtin(global_invocation_id) gid: vec3<u32>) {
i.pivot = s.c.xyz;
i.custom = s.d;
var o = particle_function(s.a.w, i, s.p);
- if (!o.visible || s.c.w > 0.5) {
+ if (!o.visible || (s.c.w > 0.5 && s.c.w < 1.5)) {
o.u = vec3<f32>(0.0);
o.v = vec3<f32>(0.0);
}
@@ -150,8 +149,9 @@ fn particles(@builtin(global_invocation_id) gid: vec3<u32>) {
let lod_k = select(0.0, 0.5 * log2(1.0 / (4.0 * area)), area > 1e-12);
let t = s.a.x + 2u * k;
let trans = select(0xffffffffu, s.b.w, s.b.z == 1u);
- put_tri(t, s.b.y, trans, c00, c10, c11, n, tg, lod_k);
- put_tri(t + 1u, s.b.y, trans, c00, c11, c01, n, tg, lod_k);
+ let first = PPS.env.z > 0.5 || s.c.w > 1.5;
+ put_tri(t, s.b.y, trans, c00, c10, c11, n, tg, lod_k, first);
+ put_tri(t + 1u, s.b.y, trans, c00, c11, c01, n, tg, lod_k, first);
let oi = (s.a.z + k) * OBJECT_STRIDE;
objects[oi] = vec4<f32>(o.center, objects[oi].w);
objects[oi + 1u] = vec4<f32>(2.0 * (abs(o.u) + abs(o.v)), objects[oi + 1u].w);
diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rs
index 00ea0b4..eb59f3e 100644
--- a/renderer/src/gpu.rs
+++ b/renderer/src/gpu.rs
@@ -232,9 +232,10 @@ pub struct ParticleGpu {
fresh: std::cell::Cell<bool>,
/// Some systems are opaque (in the scene's dynamic BLASes, rebuilt every frame).
opaque: bool,
- /// Per system: its particle count, whether the translucency pass draws it, and its state when hidden in place
- /// (`set_particles_hidden`): `Shown`, `Collapse` (this frame's pass collapses its quads), `Collapsed` (zero its
- /// count next frame), `Idle`.
+ /// Per system: its particle count, whether the translucency pass draws it, and its state when hidden or shown in
+ /// place (`set_particles_hidden`): `Shown`; `Collapse` (this frame's pass collapses its quads), `Collapsed` (zero
+ /// its count next frame), `Idle`; `Showing` (this frame's pass is its first: no motion vectors), `ShowRan` (clear
+ /// that next frame).
counts: Vec<u32>,
translucent: Vec<bool>,
hidden: std::cell::RefCell<Vec<Hidden>>,
@@ -247,6 +248,8 @@ enum Hidden {
Collapse,
Collapsed,
Idle,
+ Showing,
+ ShowRan,
}
/// Volume training points per frame at most (translucent.wgsl `volume_train`).
@@ -1869,9 +1872,14 @@ impl Gpu {
match *h {
Hidden::Collapse => *h = Hidden::Collapsed,
Hidden::Collapsed => {
- self.write_particle_count(p, k, 0, false);
+ self.write_particle_count(p, k, 0, 0.0);
*h = Hidden::Idle;
}
+ Hidden::Showing => *h = Hidden::ShowRan,
+ Hidden::ShowRan => {
+ self.write_particle_count(p, k, p.counts[k], 0.0);
+ *h = Hidden::Shown;
+ }
_ => {}
}
}
@@ -2073,11 +2081,11 @@ impl Gpu {
}
}
- /// System `k`'s particle count and collapse flag in the table (a.y, c.w).
- fn write_particle_count(&self, p: &ParticleGpu, k: usize, count: u32, collapse: bool) {
+ /// System `k`'s particle count and flag in the table (a.y; c.w: 1 collapse, 2 first run, particles.wgsl).
+ fn write_particle_count(&self, p: &ParticleGpu, k: usize, count: u32, flag: f32) {
let at = (k * std::mem::size_of::<ParticleSystemGpu>()) as u64;
self.queue.write_buffer(&p.table, at + 4, bytemuck::bytes_of(&count));
- self.queue.write_buffer(&p.table, at + 44, bytemuck::bytes_of(&if collapse { 1.0f32 } else { 0.0 }));
+ self.queue.write_buffer(&p.table, at + 44, bytemuck::bytes_of(&flag));
}
/// A particle node shown or hidden by its own `visible`, in place: hidden, its systems' next pass collapses their
@@ -2089,13 +2097,14 @@ impl Gpu {
let mut found = false;
for (k, _) in p.nodes.iter().enumerate().filter(|(_, n)| **n == node) {
found = true;
- let shown = hidden[k] == Hidden::Shown;
+ let shown = matches!(hidden[k], Hidden::Shown | Hidden::Showing | Hidden::ShowRan);
if hide && shown {
- self.write_particle_count(p, k, p.counts[k], true);
+ self.write_particle_count(p, k, p.counts[k], 1.0);
hidden[k] = Hidden::Collapse;
} else if !hide && !shown {
- self.write_particle_count(p, k, p.counts[k], false);
- hidden[k] = Hidden::Shown;
+ // (Its first run since: no last frame for its motion vectors.)
+ self.write_particle_count(p, k, p.counts[k], 2.0);
+ hidden[k] = Hidden::Showing;
}
}
found