Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

fix(effects): let CC Particle World's Textured Square use a Texture Layer (fixes #1465)

Marco Arnulfo0c35a7a889de
diff --git a/crates/effects/src/catalog.rs b/crates/effects/src/catalog.rs
index 85be739ed..bac2bb065 100644
--- a/crates/effects/src/catalog.rs
+++ b/crates/effects/src/catalog.rs
@@ -89,6 +89,7 @@ const CPU_ONLY_CONTROLS: &[(&str, &[&str])] = &[
("ec.distort.ccpowerpin", &["unstretch"]),
("ec.matte.refinesoft", &["reduceChatter", "useMotionBlur", "invert"]),
("ec.matte.refinehard", &["reduceChatter", "useMotionBlur", "invert"]),
+ ("ec.sim.ccparticleworld", &["particle/texture/textureLayer"]),
];
/// Whether the GPU kernel of effect `id` covers the instance's current settings (see
diff --git a/crates/effects/src/sim/particle_fx.rs b/crates/effects/src/sim/particle_fx.rs
index 440951c83..ca5c3c279 100644
--- a/crates/effects/src/sim/particle_fx.rs
+++ b/crates/effects/src/sim/particle_fx.rs
@@ -1,5 +1,5 @@
use super::engine::{MERCURY, PS2, PState, PW_EFFECT_CAMERA, Phys, norm3, particle_look, pw_state, type_sprite};
-use super::sprites::{Acc, Post, Sprite, SpritePlan, raster, splat};
+use super::sprites::{Acc, Post, Sprite, SpritePlan, aa, raster, splat, sprite_bbox};
use super::{Buf, EffectCtx, Image, IntoParallelRefIterator, ParallelIterator, SimCache, Value, unpremul};
// ------------------------------------------------------------------ CC Particle World
@@ -35,7 +35,18 @@ pub(super) fn pw_phys(ctx: &EffectCtx) -> Phys {
}
}
-pub(super) fn particle_world(ctx: &EffectCtx, b: Buf) -> Buf {
+/// CC Particle World's Particle Type ▸ Textured Square.
+const PW_TEXTURED_SQUARE: u32 = 8;
+/// CC Particle World's Particle ▸ Texture ▸ Texture Layer.
+pub(super) const PW_TEXTURE_LAYER: &str = "particle/texture/textureLayer";
+
+/// Whether CC Particle World draws its Texture Layer on the particles (Textured Square with a
+/// layer chosen). Only the CPU does: the Texture Layer is a CPU-only control in `catalog`.
+fn particle_world_textured(ctx: &EffectCtx) -> bool {
+ ctx.params.e("particle/particleType") == PW_TEXTURED_SQUARE && ctx.params.get(PW_TEXTURE_LAYER).and_then(Value::as_layer).is_some()
+}
+
+pub(super) fn particle_world(ctx: &EffectCtx, mut b: Buf) -> Buf {
// #1195: with the comp's motion-blur shutter open, particles draw as motion trails: the
// sprite fields of samples across the shutter are averaged, as Transform averages its
// matrix samples. (The GPU compositor runs this case on the CPU: catalog::gpu_supported.)
@@ -59,12 +70,43 @@ pub(super) fn particle_world(ctx: &EffectCtx, b: Buf) -> Buf {
None => b,
};
}
- match particle_world_plan(ctx, &b) {
- Some(plan) => plan.finish(b),
- None => b,
+ let Some(plan) = particle_world_plan(ctx, &b) else { return b };
+ // The texture at the current comp time. A layer the host can't give (the effect's own
+ // layer with its effects, a reference cycle) leaves the squares in the particle colour.
+ match particle_world_textured(ctx).then(|| ctx.layer_param(PW_TEXTURE_LAYER, true)).flatten() {
+ // The particles replace the layer ([`particle_sprites`]).
+ Some(tex) => {
+ b.img = splat_textured(b.img.width, b.img.height, &plan.sprites, plan.acc, &tex);
+ b
+ }
+ None => plan.finish(b),
}
}
+/// Draw square sprites into a new transparent image of `w`×`h`, each showing the layer `tex`
+/// stretched over the square (turned with it) at the sprite's opacity.
+fn splat_textured(w: u32, hgt: u32, sprites: &[Sprite], acc: Acc, tex: &crate::LayerPixels) -> Image {
+ let mut img = Image::new(w, hgt);
+ let (tw, th) = (tex.size[0], tex.size[1]);
+ let shade = |s: &Sprite, x: f32, y: f32| {
+ // As [`sprite_shade`]'s Square, sub-pixel sprites included.
+ let (r, fade) = if s.r < 0.5 { (0.5, s.r / 0.5) } else { (s.r, 1.0) };
+ let (sn, cs) = s.rot.sin_cos();
+ let (dx, dy) = (x - s.x, y - s.y);
+ let (ux, uy) = (dx * cs + dy * sn, -dx * sn + dy * cs);
+ let e = r * 0.75;
+ let a = aa(e - ux.abs()) * aa(e - uy.abs()) * s.c[3] * fade;
+ if a <= 1e-6 {
+ return None;
+ }
+ let (u, v) = (((ux / e + 1.0) * 0.5).clamp(0.0, 1.0) as f64, ((uy / e + 1.0) * 0.5).clamp(0.0, 1.0) as f64);
+ let (px, py) = tex.buf.to_px([u * tw, v * th]);
+ Some(tex.buf.img.sample_bilinear_clamped(px, py).map(|c| c * a))
+ };
+ raster(&mut img, sprites, sprite_bbox, shade, acc);
+ img
+}
+
/// Effect Camera ▸ FOV's default (degrees).
pub(super) const PW_FOV: f64 = 50.0;
diff --git a/crates/effects/src/sim/registry.rs b/crates/effects/src/sim/registry.rs
index f29eb2653..f55b99e27 100644
--- a/crates/effects/src/sim/registry.rs
+++ b/crates/effects/src/sim/registry.rs
@@ -1,6 +1,6 @@
use super::common::{pct, pt, spec};
use super::engine::{PARTICLE_TYPES, PW_ANIMS};
-use super::particle_fx::{PW_FOV, mr_mercury, particle_systems2, particle_systems2_plan, particle_world, particle_world_plan};
+use super::particle_fx::{PW_FOV, PW_TEXTURE_LAYER, mr_mercury, particle_systems2, particle_systems2_plan, particle_world, particle_world_plan};
use super::sprites::SpritePlan;
use super::weather::{bubbles, drizzle, rainfall, rainfall_plan, snowfall, snowfall_plan, star_burst, star_burst_plan};
use super::{Buf, EffectCtx, EffectSpec, ParamUi, Value, col, num, p, popup, slider};
@@ -151,6 +151,7 @@ pub fn specs() -> Vec<EffectSpec> {
p("physics/gravityVector/gravityY", "Gravity Y", num(1.0), slider(-1.0, 1.0, -1.0, 1.0, 2)),
p("physics/gravityVector/gravityZ", "Gravity Z", num(0.0), slider(-1.0, 1.0, -1.0, 1.0, 2)),
p("particle/particleType", "Particle Type", Value::Enum(0), popup(PARTICLE_TYPES)),
+ p(PW_TEXTURE_LAYER, "Texture Layer", Value::Layer(None), ParamUi::Layer),
p("particle/birthSize", "Birth Size", num(0.2), slider(0.0, 10.0, 0.0, 1.0, 3)),
p("particle/deathSize", "Death Size", num(0.2), slider(0.0, 10.0, 0.0, 1.0, 3)),
p("particle/sizeVariation", "Size Variation", num(0.0), pct()),
diff --git a/crates/effects/src/sim/sprites.rs b/crates/effects/src/sim/sprites.rs
index 78c405abd..55ba594b4 100644
--- a/crates/effects/src/sim/sprites.rs
+++ b/crates/effects/src/sim/sprites.rs
@@ -225,7 +225,7 @@ impl Post {
}
}
-fn sprite_bbox(s: &Sprite) -> Option<[f32; 4]> {
+pub(super) fn sprite_bbox(s: &Sprite) -> Option<[f32; 4]> {
if s.c[3] <= 1e-5 || s.r <= 0.0 {
return None;
}
diff --git a/crates/effects/src/sim/tests.rs b/crates/effects/src/sim/tests.rs
index e368c7bbe..a1d2f14e2 100644
--- a/crates/effects/src/sim/tests.rs
+++ b/crates/effects/src/sim/tests.rs
@@ -329,6 +329,53 @@ fn particle_world_effect_camera() {
assert_ne!(key(&params), key(&moved));
}
+/// A host whose every layer is `img`.
+struct TexHost(Image);
+impl crate::EffectHost for TexHost {
+ fn layer(&self, _: u64, _: bool) -> Option<crate::LayerPixels> {
+ let s = [self.0.width as f64, self.0.height as f64];
+ Some(crate::LayerPixels { buf: Buf { img: self.0.clone(), offset: [0.0; 2], scale: 1.0 }, size: s })
+ }
+ fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option<Vec<f32>> {
+ None
+ }
+}
+
+/// #1465: Textured Square draws the Texture Layer on each particle, not the particle colour.
+#[test]
+fn particle_world_textured_square_draws_the_texture_layer() {
+ // Left half green, right half blue; the particles themselves are red.
+ let mut tex = Image::new(8, 8);
+ for y in 0..8 {
+ for x in 0..8 {
+ tex.set(x, y, if x < 4 { [0.0, 1.0, 0.0, 1.0] } else { [0.0, 0.0, 1.0, 1.0] });
+ }
+ }
+ let host = TexHost(tex);
+ let vals = |layer: Option<u64>| {
+ vec![
+ ("birthRate", num(1.0)),
+ ("extras/randomSeed", num(4.0)),
+ ("particle/particleType", Value::Enum(8)),
+ ("particle/texture/textureLayer", Value::Layer(layer)),
+ ("particle/birthSize", num(1.0)),
+ ("particle/deathSize", num(1.0)),
+ ("particle/birthColor", col(1.0, 0.0, 0.0)),
+ ("particle/deathColor", col(1.0, 0.0, 0.0)),
+ ]
+ };
+ let env = EffectEnv { host: Some(&host), ..Default::default() };
+ let out = run_fx("ec.sim.ccparticleworld", &vals(Some(1)), Image::new(48, 48), 1.0, env).img;
+ let seen: Vec<_> = out.data.iter().filter(|p| p[3] > 0.05).collect();
+ assert!(!seen.is_empty(), "particles drawn");
+ assert!(seen.iter().all(|p| p[0] < 0.02 * p[3]), "no particle colour: {:?}", seen.iter().find(|p| p[0] >= 0.02 * p[3]));
+ // Both halves of the texture show: it is mapped across the square, not sampled once.
+ assert!(seen.iter().any(|p| p[1] > 2.0 * p[2]) && seen.iter().any(|p| p[2] > 2.0 * p[1]), "both texture halves");
+ // Without a texture layer the squares keep the particle colour.
+ let plain = run_fx("ec.sim.ccparticleworld", &vals(None), Image::new(48, 48), 1.0, env).img;
+ assert!(plain.data.iter().any(|p| p[3] > 0.05 && p[0] > 0.9 * p[3]));
+}
+
#[test]
fn particle_systems2_seek_and_direction() {
let v = vec![
diff --git a/docs/effects.md b/docs/effects.md
index c6fb40922..b3adb46ec 100644
--- a/docs/effects.md
+++ b/docs/effects.md
@@ -3,7 +3,7 @@
<!-- Generated from the effect registry (crates/effects/src/catalog.rs); do not edit by hand.
Regenerate: UPDATE_DOCS=1 cargo test -p effectcraft-effects --lib effects_doc_is_current -->
-EffectCraft ships 308 effects with 3033 parameters in total, grouped into the same categories as the Effects & Presets panel. Every effect is our own implementation, written from public behaviour descriptions and standard image-processing literature. Parameter names, order, popup options, units, defaults and ranges follow the reference application so that projects, expressions and muscle memory carry over.
+EffectCraft ships 308 effects with 3034 parameters in total, grouped into the same categories as the Effects & Presets panel. Every effect is our own implementation, written from public behaviour descriptions and standard image-processing literature. Parameter names, order, popup options, units, defaults and ranges follow the reference application so that projects, expressions and muscle memory carry over.
- **GPU**: 280 effects also run on the GPU compositor with identical results.
- **32**: 308 effects process 32-bit float (HDR, overbright) pixels without clamping.
@@ -325,7 +325,7 @@ Effects are addressed by id (`ec.<category>.<name>`) or display name in commands
| CC Hair | `ec.sim.cchair` | 20 | GPU | 32 | Implemented |
| CC Mr. Mercury | `ec.sim.ccmrmercury` | 23 | GPU | 32 | Implemented |
| CC Particle Systems II | `ec.sim.ccparticlesystems2` | 21 | GPU | 32 | Implemented |
-| CC Particle World | `ec.sim.ccparticleworld` | 35 | GPU | 32 | Implemented |
+| CC Particle World | `ec.sim.ccparticleworld` | 36 | GPU | 32 | Implemented |
| CC Pixel Polly | `ec.sim.ccpixelpolly` | 10 | GPU | 32 | Implemented |
| CC Rainfall | `ec.sim.ccrainfall` | 14 | GPU | 32 | Implemented |
| CC Scatterize | `ec.sim.ccscatterize` | 4 | GPU | 32 | Implemented |