| 1 | //! Unit & end-to-end GPU tests for `pAvoid`, `pCustom`, `pCustomForce`, `pFollow`, |
| 2 | //! `pImageEmitter`, and `pTangentForce`. |
| 3 | |
| 4 | use super::plan::build_particle_plan; |
| 5 | use super::sim::step_simulation; |
| 6 | use super::*; |
| 7 | |
| 8 | #[test] |
| 9 | fn test_p_avoid_repels_from_obstacle_region() { |
| 10 | let comp_src = r#"Composition { |
| 11 | GlobalRange = { 0, 10 }, |
| 12 | RenderRange = { 0, 10 }, |
| 13 | Tools = ordered() { |
| 14 | EN = BezierSpline { |
| 15 | KeyFrames = { |
| 16 | [0] = { 1, Flags = { Linear = true } }, |
| 17 | [1] = { 0, Flags = { Linear = true } }, |
| 18 | }, |
| 19 | }, |
| 20 | ENear = pEmitter { |
| 21 | Inputs = { |
| 22 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 23 | Lifespan = Input { Value = 50, }, |
| 24 | Region = Input { Value = FuID { "Point" }, }, |
| 25 | ["Point.Translate.X"] = Input { Value = 0.05, }, |
| 26 | }, |
| 27 | }, |
| 28 | EFar = pEmitter { |
| 29 | Inputs = { |
| 30 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 31 | Lifespan = Input { Value = 50, }, |
| 32 | Region = Input { Value = FuID { "Point" }, }, |
| 33 | ["Point.Translate.X"] = Input { Value = 0.40, }, |
| 34 | }, |
| 35 | }, |
| 36 | M = pMerge { |
| 37 | Inputs = { |
| 38 | Input1 = Input { SourceOp = "ENear", Source = "Output", }, |
| 39 | Input2 = Input { SourceOp = "EFar", Source = "Output", }, |
| 40 | }, |
| 41 | }, |
| 42 | Av = pAvoid { |
| 43 | Inputs = { |
| 44 | Input = Input { SourceOp = "M", Source = "Output", }, |
| 45 | Region = Input { Value = FuID { "SphereRgn" }, }, |
| 46 | ["SphereRgn.Translate.X"] = Input { Value = 0.0, }, |
| 47 | ["SphereRgn.Size"] = Input { Value = 0.02, }, |
| 48 | Distance = Input { Value = 0.15, }, |
| 49 | Strength = Input { Value = 2.0, }, |
| 50 | }, |
| 51 | }, |
| 52 | R = pRender { Inputs = { Input = Input { SourceOp = "Av", Source = "Output", }, }, }, |
| 53 | }, |
| 54 | }"#; |
| 55 | let comp = Comp::from_fusion_str(comp_src).unwrap(); |
| 56 | let render_id = comp.node_by_name["R"]; |
| 57 | let plan = build_particle_plan(&comp, render_id); |
| 58 | let expr = ExprEngine::new(); |
| 59 | let empty_fields = HashMap::new(); |
| 60 | let no_sprites = HashMap::new(); |
| 61 | let mut state = SimState::default(); |
| 62 | for f in 0..=4 { |
| 63 | step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr); |
| 64 | } |
| 65 | assert_eq!(state.particles.len(), 2); |
| 66 | let near = &state.particles[0]; |
| 67 | let far = &state.particles[1]; |
| 68 | assert!(near.vel[0] > 0.01 && near.pos[0] > 0.07, "near particle should be repelled +X: {:?}", near); |
| 69 | assert!(far.vel[0].abs() < 1e-9 && (far.pos[0] - 0.40).abs() < 1e-9, "far particle outside Distance must remain unaffected: {:?}", far); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn test_p_tangent_force_orbital_velocity() { |
| 74 | let comp_src = r#"Composition { |
| 75 | GlobalRange = { 0, 10 }, |
| 76 | RenderRange = { 0, 10 }, |
| 77 | Tools = ordered() { |
| 78 | EN = BezierSpline { |
| 79 | KeyFrames = { |
| 80 | [0] = { 1, Flags = { Linear = true } }, |
| 81 | [1] = { 0, Flags = { Linear = true } }, |
| 82 | }, |
| 83 | }, |
| 84 | E = pEmitter { |
| 85 | Inputs = { |
| 86 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 87 | Lifespan = Input { Value = 50, }, |
| 88 | Region = Input { Value = FuID { "Point" }, }, |
| 89 | ["Point.Translate.X"] = Input { Value = 0.25, }, |
| 90 | ["Point.Translate.Y"] = Input { Value = 0.0, }, |
| 91 | }, |
| 92 | }, |
| 93 | TF = pTangentForce { |
| 94 | Inputs = { |
| 95 | Input = Input { SourceOp = "E", Source = "Output", }, |
| 96 | ["Translate.X"] = Input { Value = 0.0, }, |
| 97 | ["Translate.Y"] = Input { Value = 0.0, }, |
| 98 | ZStrength = Input { Value = 1.0, }, |
| 99 | }, |
| 100 | }, |
| 101 | R = pRender { Inputs = { Input = Input { SourceOp = "TF", Source = "Output", }, }, }, |
| 102 | }, |
| 103 | }"#; |
| 104 | let comp = Comp::from_fusion_str(comp_src).unwrap(); |
| 105 | let render_id = comp.node_by_name["R"]; |
| 106 | let plan = build_particle_plan(&comp, render_id); |
| 107 | let expr = ExprEngine::new(); |
| 108 | let empty_fields = HashMap::new(); |
| 109 | let no_sprites = HashMap::new(); |
| 110 | let mut state = SimState::default(); |
| 111 | for f in 0..=1 { |
| 112 | step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr); |
| 113 | } |
| 114 | let p1 = state.particles[0]; |
| 115 | assert!(p1.vel[1] > 1e-4, "ZStrength > 0 at (+X,0) should produce +Y orbital velocity, got {:?}", p1.vel); |
| 116 | assert!(p1.vel[0].abs() < 1e-9, "radial X force should be 0 on first tangent impulse, got {}", p1.vel[0]); |
| 117 | step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, 2, 0, &expr); |
| 118 | let p2 = state.particles[0]; |
| 119 | assert!(p2.vel[0] < 0.0, "second step after moving +Y should deflect velocity inward toward -X, got {:?}", p2.vel); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn test_p_follow_spring_target_and_leader_sets() { |
| 124 | // 1. Native Fusion spring-damper follow toward Position. |
| 125 | let spring_src = r#"Composition { |
| 126 | GlobalRange = { 0, 10 }, |
| 127 | RenderRange = { 0, 10 }, |
| 128 | Tools = ordered() { |
| 129 | EN = BezierSpline { KeyFrames = { [0] = { 1, Flags = { Linear = true } }, [1] = { 0, Flags = { Linear = true } } } }, |
| 130 | E = pEmitter { |
| 131 | Inputs = { |
| 132 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 133 | Lifespan = Input { Value = 50, }, |
| 134 | Region = Input { Value = FuID { "Point" }, }, |
| 135 | ["Point.Translate.X"] = Input { Value = -0.3, }, |
| 136 | }, |
| 137 | }, |
| 138 | Fol = pFollow { |
| 139 | Inputs = { |
| 140 | Input = Input { SourceOp = "E", Source = "Output", }, |
| 141 | ["Position.X"] = Input { Value = 0.3, }, |
| 142 | ["Position.Y"] = Input { Value = 0.2, }, |
| 143 | Spring = Input { Value = 2.0, }, |
| 144 | Damping = Input { Value = 0.2, }, |
| 145 | }, |
| 146 | }, |
| 147 | R = pRender { Inputs = { Input = Input { SourceOp = "Fol", Source = "Output", }, }, }, |
| 148 | }, |
| 149 | }"#; |
| 150 | let comp = Comp::from_fusion_str(spring_src).unwrap(); |
| 151 | let plan = build_particle_plan(&comp, comp.node_by_name["R"]); |
| 152 | let expr = ExprEngine::new(); |
| 153 | let empty_fields = HashMap::new(); |
| 154 | let no_sprites = HashMap::new(); |
| 155 | let mut state = SimState::default(); |
| 156 | for f in 0..=4 { |
| 157 | step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr); |
| 158 | } |
| 159 | let p = state.particles[0]; |
| 160 | assert!(p.vel[0] > 0.01 && p.vel[1] > 0.005, "spring target must pull particle toward (0.3, 0.2), got {:?}", p.vel); |
| 161 | |
| 162 | // 2. Boids leader-follower mode using FollowSet / AffectSet / Strength / Distance. |
| 163 | let boids_src = r#"Composition { |
| 164 | GlobalRange = { 0, 10 }, |
| 165 | RenderRange = { 0, 10 }, |
| 166 | Tools = ordered() { |
| 167 | EN = BezierSpline { KeyFrames = { [0] = { 1, Flags = { Linear = true } }, [1] = { 0, Flags = { Linear = true } } } }, |
| 168 | Leader = pEmitter { |
| 169 | Inputs = { |
| 170 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 171 | Lifespan = Input { Value = 50, }, |
| 172 | Set1 = Input { Value = 1, }, |
| 173 | Region = Input { Value = FuID { "Point" }, }, |
| 174 | ["Point.Translate.X"] = Input { Value = 0.2, }, |
| 175 | Velocity = Input { Value = 0.0, }, |
| 176 | }, |
| 177 | }, |
| 178 | Follower = pEmitter { |
| 179 | Inputs = { |
| 180 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 181 | Lifespan = Input { Value = 50, }, |
| 182 | Set2 = Input { Value = 1, }, |
| 183 | Region = Input { Value = FuID { "Point" }, }, |
| 184 | ["Point.Translate.X"] = Input { Value = -0.1, }, |
| 185 | Velocity = Input { Value = 0.0, }, |
| 186 | }, |
| 187 | }, |
| 188 | M = pMerge { |
| 189 | Inputs = { |
| 190 | Input1 = Input { SourceOp = "Leader", Source = "Output", }, |
| 191 | Input2 = Input { SourceOp = "Follower", Source = "Output", }, |
| 192 | }, |
| 193 | }, |
| 194 | Fol = pFollow { |
| 195 | Inputs = { |
| 196 | Input = Input { SourceOp = "M", Source = "Output", }, |
| 197 | FollowSet = Input { Value = FuID { "Set1" }, }, |
| 198 | AffectSet = Input { Value = FuID { "Set2" }, }, |
| 199 | Strength = Input { Value = 3.0, }, |
| 200 | Distance = Input { Value = 0.5, }, |
| 201 | }, |
| 202 | }, |
| 203 | R = pRender { Inputs = { Input = Input { SourceOp = "Fol", Source = "Output", }, }, }, |
| 204 | }, |
| 205 | }"#; |
| 206 | let bcomp = Comp::from_fusion_str(boids_src).unwrap(); |
| 207 | let bplan = build_particle_plan(&bcomp, bcomp.node_by_name["R"]); |
| 208 | let mut bstate = SimState::default(); |
| 209 | for f in 0..=4 { |
| 210 | step_simulation(&mut bstate, &bcomp, &bplan, &empty_fields, &no_sprites, f, 0, &expr); |
| 211 | } |
| 212 | let leader = bstate.particles.iter().find(|x| (x.sets & 1) != 0).unwrap(); |
| 213 | let follower = bstate.particles.iter().find(|x| (x.sets & 2) != 0).unwrap(); |
| 214 | assert!(leader.vel[0].abs() < 1e-9, "leader in Set1 not in AffectSet must stay still"); |
| 215 | assert!(follower.vel[0] > 0.01 && follower.pos[0] > -0.1, "follower in Set2 must steer toward leader in Set1"); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn test_p_custom_and_p_custom_force_expressions_and_gpu() { |
| 220 | let comp_src = r#"Composition { |
| 221 | GlobalRange = { 0, 10 }, |
| 222 | RenderRange = { 0, 10 }, |
| 223 | Tools = ordered() { |
| 224 | EN = BezierSpline { KeyFrames = { [0] = { 2, Flags = { Linear = true } }, [1] = { 0, Flags = { Linear = true } } } }, |
| 225 | E = pEmitter { |
| 226 | Inputs = { |
| 227 | Number = Input { SourceOp = "EN", Source = "Value", }, |
| 228 | Lifespan = Input { Value = 50, }, |
| 229 | Style = Input { Value = FuID { "ParticleStyleBlob" }, }, |
| 230 | ["ParticleStyle.Size"] = Input { Value = 0.4, }, |
| 231 | }, |
| 232 | }, |
| 233 | Cust = pCustom { |
| 234 | Inputs = { |
| 235 | Input = Input { SourceOp = "E", Source = "Output", }, |
| 236 | NumberIn1 = Input { Value = 0.25, }, |
| 237 | ["Position1.X"] = Input { Value = -0.1, }, |
| 238 | ["Position1.Y"] = Input { Value = 0.05, }, |
| 239 | Setup1 = Input { Value = "sin(90)", }, |
| 240 | Intermediate1 = Input { Value = "s1 * n1", }, |
| 241 | PositionXExpression = Input { Value = "if(id > 1, i1, p1x)", }, |
| 242 | PositionYExpression = Input { Value = "p1y", }, |
| 243 | RedExpression = Input { Value = "clamp(n1 * 4, 0, 1)", }, |
| 244 | GreenExpression = Input { Value = "0", }, |
| 245 | BlueExpression = Input { Value = "0", }, |
| 246 | KillExpressionKillIf10 = Input { Value = "if(id == 1 && age >= 0.04, 1, 0)", }, |
| 247 | }, |
| 248 | }, |
| 249 | CF = pCustomForce { |
| 250 | Inputs = { |
| 251 | Input = Input { SourceOp = "Cust", Source = "Output", }, |
| 252 | ForceYExpression = Input { Value = "2.0", }, |
| 253 | TorqueZExpression = Input { Value = "1.5", }, |
| 254 | }, |
| 255 | }, |
| 256 | R = pRender { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, Input = Input { SourceOp = "CF", Source = "Output", }, }, }, |
| 257 | }, |
| 258 | }"#; |
| 259 | let comp = Comp::from_fusion_str(comp_src).unwrap(); |
| 260 | let render_id = comp.node_by_name["R"]; |
| 261 | let plan = build_particle_plan(&comp, render_id); |
| 262 | let expr = ExprEngine::new(); |
| 263 | let empty_fields = HashMap::new(); |
| 264 | let no_sprites = HashMap::new(); |
| 265 | let mut state = SimState::default(); |
| 266 | for f in 0..=4 { |
| 267 | step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr); |
| 268 | } |
| 269 | // Particle id=1 was killed at age>=2; particle id=2 survived at x=0.25 with +Y acceleration from pCustomForce. |
| 270 | assert_eq!(state.particles.len(), 1, "id=1 must be killed leaving id=2, got {:?}", state.particles); |
| 271 | let p = state.particles[0]; |
| 272 | assert_eq!(p.id, 2); |
| 273 | assert!((p.pos[0] - 0.25).abs() < 1e-6); |
| 274 | assert!(p.vel[1] > 0.02, "ForceYExpression='2.0' must accelerate +Y: {}", p.vel[1]); |
| 275 | assert!(p.rot_z > 1.0, "TorqueZExpression='1.5' must integrate spin_z into rot_z: {}", p.rot_z); |
| 276 | assert!(p.color[0] > 0.99 && p.color[1] < 0.01); |
| 277 | |
| 278 | let Ok(mut eng) = crate::Engine::new_headless() else { return }; |
| 279 | let (tex, stats) = eng.evaluate(&comp, render_id, 4.0); |
| 280 | assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes); |
| 281 | let px = eng.readback_rgba(&tex).unwrap(); |
| 282 | let nonzero = px.chunks_exact(4).filter(|c| c[3] > 0).count(); |
| 283 | assert!(nonzero > 5); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn test_p_image_emitter_samples_rgba_and_alpha_threshold_and_gpu() { |
| 288 | // 32x32 image whose left half is opaque red and right half is fully transparent, |
| 289 | // driving pImageEmitter -> pRender on GPU. |
| 290 | let comp_src = r#"Composition { |
| 291 | GlobalRange = { 0, 5 }, |
| 292 | RenderRange = { 0, 5 }, |
| 293 | Tools = ordered() { |
| 294 | MaskLeft = RectangleMask { |
| 295 | Inputs = { |
| 296 | MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 32, }, |
| 297 | Center = Input { Value = { 0.25, 0.5 }, }, |
| 298 | Width = Input { Value = 0.5, }, Height = Input { Value = 1.0, }, |
| 299 | }, |
| 300 | }, |
| 301 | RedHalf = Background { |
| 302 | Inputs = { |
| 303 | Width = Input { Value = 32, }, Height = Input { Value = 32, }, |
| 304 | TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 0, }, TopLeftBlue = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, |
| 305 | EffectMask = Input { SourceOp = "MaskLeft", Source = "Mask", }, |
| 306 | }, |
| 307 | }, |
| 308 | IE = pImageEmitter { |
| 309 | Inputs = { |
| 310 | Input = Input { SourceOp = "RedHalf", Source = "Output", }, |
| 311 | XDensity = Input { Value = 0.5, }, |
| 312 | YDensity = Input { Value = 0.5, }, |
| 313 | AlphaThreshold = Input { Value = 0.5, }, |
| 314 | Style = Input { Value = FuID { "ParticleStyleBlob" }, }, |
| 315 | ["ParticleStyle.Size"] = Input { Value = 0.25, }, |
| 316 | }, |
| 317 | }, |
| 318 | R = pRender { |
| 319 | Inputs = { |
| 320 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, |
| 321 | Input = Input { SourceOp = "IE", Source = "Output", }, |
| 322 | }, |
| 323 | }, |
| 324 | }, |
| 325 | }"#; |
| 326 | let comp = Comp::from_fusion_str(comp_src).unwrap(); |
| 327 | let render_id = comp.node_by_name["R"]; |
| 328 | let Ok(mut eng) = crate::Engine::new_headless() else { return }; |
| 329 | let (tex, stats) = eng.evaluate(&comp, render_id, 2.0); |
| 330 | assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes); |
| 331 | let px = eng.readback_rgba(&tex).unwrap(); |
| 332 | let px_ref = px.as_slice(); |
| 333 | |
| 334 | let left_red: u64 = (0..64) |
| 335 | .flat_map(|y| (0..28).map(move |x| px_ref[(y * 64 + x) * 4] as u64)) |
| 336 | .sum(); |
| 337 | let right_alpha: u64 = (0..64) |
| 338 | .flat_map(|y| (42..64).map(move |x| px_ref[(y * 64 + x) * 4 + 3] as u64)) |
| 339 | .sum(); |
| 340 | assert!(left_red > 1000, "pImageEmitter must emit bright red particles over left half (left_red={left_red})"); |
| 341 | assert_eq!(right_alpha, 0, "right transparent half above AlphaThreshold must emit no particles (right_alpha={right_alpha})"); |
| 342 | } |