| ... | ... | @@ -0,0 +1,432 @@ |
| 1 | //! Native ports of Clover's custom Fuses: `SwitchElse`, `CheckerPieceGenerator`, |
| 2 | //! `AlphaWeightedNoise`, and `Crystallize`. |
| 3 | |
| 4 | use super::*; |
| 5 | |
| 6 | impl Engine { |
| 7 | pub(super) fn eval_switch_else( |
| 8 | &mut self, |
| 9 | comp: &Comp, |
| 10 | node: &Node, |
| 11 | time: f64, |
| 12 | enc: &mut wgpu::CommandEncoder, |
| 13 | visiting: &mut HashSet<(NodeId, i64)>, |
| 14 | stats: &mut EvalStats, |
| 15 | ) -> CachedTexture { |
| 16 | let index = |name: &str, default: f64| comp.eval_number(node, name, time, default, &self.expr).round().max(1.0) as usize; |
| 17 | let picked = [index("Switch", 1.0), index("Else", 2.0)].into_iter().find_map(|i| { |
| 18 | node.connected_node(&format!("Input{i}")).and_then(|s| comp.find_node(s)) |
| 19 | }); |
| 20 | match picked { |
| 21 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), |
| 22 | None => self.dummy.clone(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub(super) fn eval_checker_piece( |
| 27 | &mut self, |
| 28 | comp: &Comp, |
| 29 | node: &Node, |
| 30 | time: f64, |
| 31 | frame_i: i64, |
| 32 | enc: &mut wgpu::CommandEncoder, |
| 33 | stats: &mut EvalStats, |
| 34 | ) -> CachedTexture { |
| 35 | let (full_size, _) = comp.checker_piece_metrics(node, time, &self.expr); |
| 36 | let edge = (full_size / self.proxy_scale.max(1)).max(1); |
| 37 | let flip = comp.eval_number(node, "Flip", time, 0.0, &self.expr) > 0.5; |
| 38 | let color = |suffix: &str, default: [f64; 3]| { |
| 39 | [ |
| 40 | comp.eval_number(node, &format!("R{suffix}"), time, default[0], &self.expr) as f32, |
| 41 | comp.eval_number(node, &format!("G{suffix}"), time, default[1], &self.expr) as f32, |
| 42 | comp.eval_number(node, &format!("B{suffix}"), time, default[2], &self.expr) as f32, |
| 43 | comp.eval_number(node, &format!("A{suffix}"), time, 1.0, &self.expr) as f32, |
| 44 | ] |
| 45 | }; |
| 46 | let (a, b) = (color("1", [0.0; 3]), color("2", [1.0; 3])); |
| 47 | |
| 48 | let mut u = UniformParams::default(); |
| 49 | (u.p[0], u.p[1]) = if flip { (b, a) } else { (a, b) }; |
| 50 | let depth = self.node_depth(comp, node, time, None); |
| 51 | let size = [edge, edge]; |
| 52 | let key = self.make_key(node, "CheckerPiece", size, &[depth as u64], bytemuck::cast_slice(&u.p[..2])); |
| 53 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 54 | stats.cache_hits += 1; |
| 55 | return hit; |
| 56 | } |
| 57 | stats.cache_misses += 1; |
| 58 | stats.nodes_evaluated += 1; |
| 59 | let dst = self.alloc_tex(size, depth, key); |
| 60 | self.dispatch_op(enc, ShaderOp::CheckerPiece, &self.dummy.view, &self.dummy.view, &self.dummy.view, &dst, &u); |
| 61 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 62 | dst |
| 63 | } |
| 64 | |
| 65 | pub(super) fn eval_alpha_weighted_noise( |
| 66 | &mut self, |
| 67 | comp: &Comp, |
| 68 | node: &Node, |
| 69 | time: f64, |
| 70 | frame_i: i64, |
| 71 | enc: &mut wgpu::CommandEncoder, |
| 72 | visiting: &mut HashSet<(NodeId, i64)>, |
| 73 | stats: &mut EvalStats, |
| 74 | ) -> CachedTexture { |
| 75 | let Some(src_id) = node |
| 76 | .connected_node("Image") |
| 77 | .or_else(|| node.connected_node("Input")) |
| 78 | .and_then(|s| comp.find_node(s)) |
| 79 | else { |
| 80 | return self.dummy.clone(); |
| 81 | }; |
| 82 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 83 | let mut u = UniformParams::default(); |
| 84 | u.p[0] = [ |
| 85 | comp.eval_number(node, "RandomSeed", time, 0.0, &self.expr).round().max(0.0) as f32, |
| 86 | comp.eval_number(node, "Gain", time, 1.0, &self.expr) as f32, |
| 87 | comp.eval_number(node, "Lift", time, 0.0, &self.expr) as f32, |
| 88 | 0.0, |
| 89 | ]; |
| 90 | let key = self.make_key(node, "AlphaWeightedNoise", src.size, &[src.content_hash], bytemuck::cast_slice(&u.p[..1])); |
| 91 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 92 | stats.cache_hits += 1; |
| 93 | return hit; |
| 94 | } |
| 95 | stats.cache_misses += 1; |
| 96 | stats.nodes_evaluated += 1; |
| 97 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 98 | self.dispatch_op(enc, ShaderOp::AlphaWeightedNoise, &src.view, &self.dummy.view, &self.dummy.view, &dst, &u); |
| 99 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 100 | dst |
| 101 | } |
| 102 | |
| 103 | pub(super) fn eval_crystallize( |
| 104 | &mut self, |
| 105 | comp: &Comp, |
| 106 | node: &Node, |
| 107 | time: f64, |
| 108 | frame_i: i64, |
| 109 | enc: &mut wgpu::CommandEncoder, |
| 110 | visiting: &mut HashSet<(NodeId, i64)>, |
| 111 | stats: &mut EvalStats, |
| 112 | ) -> CachedTexture { |
| 113 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 114 | return self.dummy.clone(); |
| 115 | }; |
| 116 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 117 | let num = |name: &str, default: f64| comp.eval_number(node, name, time, default, &self.expr); |
| 118 | let size = num("Size", 0.025).max(0.001); |
| 119 | let smooth = num("Smooth", 1.0) >= 0.5; |
| 120 | let width = src.canvas_size[0] as f32; |
| 121 | |
| 122 | let mut u = UniformParams::default(); |
| 123 | u.p[0] = [ |
| 124 | size as f32 * width, |
| 125 | (num("Feathering", 0.0).max(0.0) * 0.2).max(1e-4) as f32, |
| 126 | (num("Seethe", 0.0) + num("SeetheRate", 0.02) * time) as f32, |
| 127 | num("RandomSeed", 0.0) as f32, |
| 128 | ]; |
| 129 | u.p[1] = [ |
| 130 | num("Jitter", 1.0).clamp(0.0, 1.0) as f32, |
| 131 | num("Blades", 2.0).round().clamp(1.0, 3.0) as f32, |
| 132 | src.origin[0] as f32, |
| 133 | src.origin[1] as f32, |
| 134 | ]; |
| 135 | u.p[2] = [src.canvas_size[1] as f32, if smooth { 1.0 } else { 0.0 }, 0.0, 0.0]; |
| 136 | |
| 137 | let key = self.make_key(node, "Crystallize", src.size, &[src.content_hash], bytemuck::cast_slice(&u.p[..3])); |
| 138 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 139 | stats.cache_hits += 1; |
| 140 | return hit; |
| 141 | } |
| 142 | stats.cache_misses += 1; |
| 143 | stats.nodes_evaluated += 1; |
| 144 | |
| 145 | let blurred; |
| 146 | let facet_src = if smooth { |
| 147 | // Box about one facet wide, standing in for the facet's average. |
| 148 | let radius = 0.35 * u.p[0][0]; |
| 149 | let tmp = self.alloc_tex_like(&src, src.depth, 0); |
| 150 | blurred = self.alloc_tex_like(&src, src.depth, 0); |
| 151 | for (dir, from, to) in [([1.0, 0.0], &src, &tmp), ([0.0, 1.0], &tmp, &blurred)] { |
| 152 | let mut ub = UniformParams::default(); |
| 153 | ub.p[0] = [dir[0], dir[1], radius, 1.0]; |
| 154 | ub.p[1] = [1.0, 0.0, 0.0, 1.0]; |
| 155 | self.dispatch_op(enc, ShaderOp::Blur, &from.view, &self.dummy.view, &self.dummy.view, to, &ub); |
| 156 | } |
| 157 | &blurred |
| 158 | } else { |
| 159 | &src |
| 160 | }; |
| 161 | |
| 162 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 163 | self.dispatch_op(enc, ShaderOp::Crystallize, &facet_src.view, &self.dummy.view, &self.dummy.view, &dst, &u); |
| 164 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 165 | dst |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[cfg(test)] |
| 170 | mod tests { |
| 171 | use super::*; |
| 172 | |
| 173 | fn comp(tools: &str) -> Comp { |
| 174 | let src = format!("Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ {tools} }} }}"); |
| 175 | Comp::from_fusion_str(&src).expect("parse comp") |
| 176 | } |
| 177 | |
| 178 | fn bg(name: &str, w: u32, inputs: &str) -> String { |
| 179 | format!("{name} = Background {{ Inputs = {{ Width = Input {{ Value = {w}, }}, Height = Input {{ Value = {w}, }}, {inputs} }}, }},") |
| 180 | } |
| 181 | |
| 182 | /// Renders `name` and returns `(size, rgba8 rows top-down)`; `None` without a GPU adapter. |
| 183 | fn render(comp: &Comp, name: &str, time: f64) -> Option<([u32; 2], Vec<u8>)> { |
| 184 | let mut eng = Engine::new_headless().ok()?; |
| 185 | let (tex, _) = eng.evaluate(comp, comp.find_node(name).unwrap(), time); |
| 186 | Some((tex.size, eng.readback_rgba(&tex).unwrap())) |
| 187 | } |
| 188 | |
| 189 | fn px(img: &(Vec<u8>, u32), x: u32, y: u32) -> [u8; 4] { |
| 190 | let i = ((y * img.1 + x) * 4) as usize; |
| 191 | img.0[i..i + 4].try_into().unwrap() |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn switch_else_prefers_switch_input_then_else_then_dummy() { |
| 196 | let tools = [ |
| 197 | bg("Red", 16, "TopLeftRed = Input { Value = 1, },"), |
| 198 | bg("Green", 16, "TopLeftGreen = Input { Value = 1, },"), |
| 199 | r#"Both = Fuse.SwitchElse { Inputs = { |
| 200 | Input1 = Input { SourceOp = "Red", Source = "Output", }, |
| 201 | Input2 = Input { SourceOp = "Green", Source = "Output", }, |
| 202 | }, }, |
| 203 | ElseOnly = Fuse.SwitchElse { Inputs = { |
| 204 | Input2 = Input { SourceOp = "Green", Source = "Output", }, |
| 205 | }, }, |
| 206 | Swapped = Fuse.SwitchElse { Inputs = { |
| 207 | Switch = Input { Value = 2, }, |
| 208 | Else = Input { Value = 1, }, |
| 209 | Input1 = Input { SourceOp = "Red", Source = "Output", }, |
| 210 | Input2 = Input { SourceOp = "Green", Source = "Output", }, |
| 211 | }, }, |
| 212 | SwappedElse = Fuse.SwitchElse { Inputs = { |
| 213 | Switch = Input { Value = 2, }, |
| 214 | Else = Input { Value = 1, }, |
| 215 | Input1 = Input { SourceOp = "Red", Source = "Output", }, |
| 216 | }, }, |
| 217 | Dangling = Fuse.SwitchElse { Inputs = { |
| 218 | Input1 = Input { SourceOp = "Missing", Source = "Output", }, |
| 219 | Input2 = Input { SourceOp = "Green", Source = "Output", }, |
| 220 | }, }, |
| 221 | Empty = Fuse.SwitchElse { },"# |
| 222 | .to_string(), |
| 223 | ] |
| 224 | .concat(); |
| 225 | let c = comp(&tools); |
| 226 | let color = |name| render(&c, name, 0.0).map(|(size, p)| (size, [p[0], p[1], p[2]])); |
| 227 | let Some(both) = color("Both") else { return }; |
| 228 | assert_eq!(both, ([16, 16], [255, 0, 0])); |
| 229 | assert_eq!(color("ElseOnly").unwrap(), ([16, 16], [0, 255, 0])); |
| 230 | assert_eq!(color("Swapped").unwrap(), ([16, 16], [0, 255, 0])); |
| 231 | assert_eq!(color("SwappedElse").unwrap(), ([16, 16], [255, 0, 0])); |
| 232 | assert_eq!(color("Dangling").unwrap(), ([16, 16], [0, 255, 0])); |
| 233 | assert_eq!(color("Empty").unwrap().0, [1, 1]); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn checker_piece_is_y_up_nearest_2x2_with_flip_and_colors() { |
| 238 | let c = comp( |
| 239 | r#"Plain = Fuse.CheckerPieceGenerator { Inputs = { |
| 240 | Width = Input { Value = 40, }, |
| 241 | Repeat = Input { Value = 5, }, |
| 242 | }, }, |
| 243 | Flipped = Fuse.CheckerPieceGenerator { Inputs = { |
| 244 | Width = Input { Value = 40, }, |
| 245 | Repeat = Input { Value = 5, }, |
| 246 | Flip = Input { Value = 1, }, |
| 247 | R1 = Input { Value = 1, }, G1 = Input { Value = 0, }, B1 = Input { Value = 0, }, A1 = Input { Value = 1, }, |
| 248 | R2 = Input { Value = 0, }, G2 = Input { Value = 0, }, B2 = Input { Value = 1, }, A2 = Input { Value = 0.5, }, |
| 249 | }, }, |
| 250 | Odd = Fuse.CheckerPieceGenerator { Inputs = { |
| 251 | Width = Input { Value = 100, }, |
| 252 | Repeat = Input { Value = 3, }, |
| 253 | }, }, |
| 254 | Floor = Fuse.CheckerPieceGenerator { Inputs = { |
| 255 | Width = Input { Value = 4, }, |
| 256 | Repeat = Input { Value = 10, }, |
| 257 | }, },"#, |
| 258 | ); |
| 259 | let Some((size, rgba)) = render(&c, "Plain", 0.0) else { return }; |
| 260 | // 40 / 5 = 8 px tiles; A = black bottom-left & top-right, B = white top-left & bottom-right. |
| 261 | assert_eq!(size, [8, 8]); |
| 262 | let img = (rgba, 8); |
| 263 | let [top_left, top_right, bottom_left, bottom_right] = |
| 264 | [(0, 0), (7, 0), (0, 7), (7, 7)].map(|(x, y)| px(&img, x, y)); |
| 265 | assert_eq!([top_left, bottom_right], [[255; 4]; 2]); |
| 266 | assert_eq!([top_right, bottom_left], [[0, 0, 0, 255]; 2]); |
| 267 | // Nearest-neighbor: the quadrant seams sit exactly at the midlines. |
| 268 | assert_eq!([px(&img, 3, 3), px(&img, 4, 3), px(&img, 3, 4), px(&img, 4, 4)], [top_left, top_right, bottom_left, bottom_right]); |
| 269 | |
| 270 | let (_, rgba) = render(&c, "Flipped", 0.0).unwrap(); |
| 271 | let img = (rgba, 8); |
| 272 | // Flip swaps A and B: B (blue, half alpha) now bottom-left & top-right, A (red) top-left & bottom-right. |
| 273 | assert_eq!(px(&img, 0, 0), [255, 0, 0, 255]); |
| 274 | assert_eq!(px(&img, 7, 7), [255, 0, 0, 255]); |
| 275 | assert_eq!(px(&img, 7, 0), [0, 0, 255, 128]); |
| 276 | assert_eq!(px(&img, 0, 7), [0, 0, 255, 128]); |
| 277 | |
| 278 | assert_eq!(render(&c, "Odd", 0.0).unwrap().0, [32, 32]); |
| 279 | assert_eq!(render(&c, "Floor", 0.0).unwrap().0, [2, 2]); |
| 280 | } |
| 281 | |
| 282 | #[test] |
| 283 | fn alpha_weighted_noise_thresholds_alpha_and_unpremultiplies_without_nan() { |
| 284 | let tools = [ |
| 285 | bg("Clear", 64, "TopLeftRed = Input { Value = 0.5, }, TopLeftAlpha = Input { Value = 0, },"), |
| 286 | bg("Solid", 64, "TopLeftRed = Input { Value = 0.5, }, TopLeftAlpha = Input { Value = 1, },"), |
| 287 | bg("Half", 64, "TopLeftRed = Input { Value = 0.25, }, TopLeftAlpha = Input { Value = 0.5, },"), |
| 288 | r#"ClearNoise = Fuse.AlphaWeightedNoise { Inputs = { |
| 289 | Image = Input { SourceOp = "Clear", Source = "Output", }, |
| 290 | Lift = Input { Value = 1, }, |
| 291 | }, }, |
| 292 | SolidNoise = Fuse.AlphaWeightedNoise { Inputs = { Image = Input { SourceOp = "Solid", Source = "Output", }, }, }, |
| 293 | SolidGone = Fuse.AlphaWeightedNoise { Inputs = { |
| 294 | Image = Input { SourceOp = "Solid", Source = "Output", }, |
| 295 | Gain = Input { Value = 0, }, |
| 296 | }, }, |
| 297 | HalfA = Fuse.AlphaWeightedNoise { Inputs = { Image = Input { SourceOp = "Half", Source = "Output", }, }, }, |
| 298 | HalfAgain = Fuse.AlphaWeightedNoise { Inputs = { Input = Input { SourceOp = "Half", Source = "Output", }, }, }, |
| 299 | HalfB = Fuse.AlphaWeightedNoise { Inputs = { |
| 300 | Image = Input { SourceOp = "Half", Source = "Output", }, |
| 301 | RandomSeed = Input { Value = 7, }, |
| 302 | }, }, |
| 303 | HalfBright = Fuse.AlphaWeightedNoise { Inputs = { |
| 304 | Image = Input { SourceOp = "Half", Source = "Output", }, |
| 305 | Lift = Input { Value = 0.25, }, |
| 306 | }, }, |
| 307 | NoInput = Fuse.AlphaWeightedNoise { },"# |
| 308 | .to_string(), |
| 309 | ] |
| 310 | .concat(); |
| 311 | let c = comp(&tools); |
| 312 | let Some((_, clear)) = render(&c, "ClearNoise", 0.0) else { return }; |
| 313 | assert!(clear.chunks(4).all(|p| p == [0, 0, 0, 255]), "alpha 0 -> 1 has no color to recover, so rgb is 0 rather than NaN"); |
| 314 | assert!(render(&c, "SolidNoise", 0.0).unwrap().1.chunks(4).all(|p| p == [128, 0, 0, 255])); |
| 315 | assert!(render(&c, "SolidGone", 0.0).unwrap().1.chunks(4).all(|p| p == [0, 0, 0, 0])); |
| 316 | |
| 317 | let coverage = |name| { |
| 318 | let (_, px) = render(&c, name, 0.0).unwrap(); |
| 319 | let opaque = px.chunks(4).filter(|p| p[3] == 255).count(); |
| 320 | assert!(px.chunks(4).all(|p| p == [0, 0, 0, 0] || p[3] == 255), "{name}: alpha is binary"); |
| 321 | assert!(px.chunks(4).filter(|p| p[3] == 255).all(|p| p[0] == 128), "{name}: rgb / alpha restored"); |
| 322 | opaque as f32 / 4096.0 |
| 323 | }; |
| 324 | assert!((coverage("HalfA") - 0.5).abs() < 0.05); |
| 325 | assert!((coverage("HalfBright") - 0.75).abs() < 0.05); |
| 326 | let a = render(&c, "HalfA", 0.0).unwrap().1; |
| 327 | assert_eq!(a, render(&c, "HalfA", 1.0).unwrap().1, "deterministic"); |
| 328 | assert_eq!(a, render(&c, "HalfAgain", 0.0).unwrap().1, "`Input` is the fallback main port"); |
| 329 | assert_ne!(a, render(&c, "HalfB", 0.0).unwrap().1, "seed changes the pattern"); |
| 330 | assert_eq!(render(&c, "NoInput", 0.0).unwrap().0, [1, 1]); |
| 331 | } |
| 332 | |
| 333 | fn crystallize_comp(input: &str, crystal: &str) -> Comp { |
| 334 | comp(&format!( |
| 335 | r#"{} Crystal = Fuse.Crystallize {{ Inputs = {{ |
| 336 | Input = Input {{ SourceOp = "Src", Source = "Output", }}, |
| 337 | Size = Input {{ Value = 0.125, }}, |
| 338 | Smooth = Input {{ Value = 0, }}, |
| 339 | Feathering = Input {{ Value = 0, }}, |
| 340 | Jitter = Input {{ Value = 0, }}, |
| 341 | Blades = Input {{ Value = 1, }}, |
| 342 | {crystal} |
| 343 | }}, }},"#, |
| 344 | bg("Src", 64, input) |
| 345 | )) |
| 346 | } |
| 347 | |
| 348 | const H_RAMP: &str = "Type = Input { Value = \"Horizontal\", }, TopRightRed = Input { Value = 1, }, TopRightAlpha = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, },"; |
| 349 | const V_RAMP: &str = "Type = Input { Value = \"Vertical\", }, TopLeftRed = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, }, BottomLeftAlpha = Input { Value = 1, },"; |
| 350 | |
| 351 | #[test] |
| 352 | fn crystallize_samples_facet_homes_in_fusion_y_up_pixels() { |
| 353 | // 8 px cells, jitter 0: every pixel takes the ramp value at the center of a nearby cell. |
| 354 | for (ramp, horizontal) in [(H_RAMP, true), (V_RAMP, false)] { |
| 355 | let c = crystallize_comp(ramp, ""); |
| 356 | let Some((size, rgba)) = render(&c, "Crystal", 0.0) else { return }; |
| 357 | assert_eq!(size, [64, 64]); |
| 358 | let img = (rgba, 64); |
| 359 | let mut input = std::collections::HashSet::new(); |
| 360 | for y in 12..52 { |
| 361 | for x in 12..52 { |
| 362 | let r = f32::from(px(&img, x, y)[0]) / 255.0; |
| 363 | // Fusion y is up: the vertical ramp is 1 at the top row. |
| 364 | let ramp_at = if horizontal { (x as f32 + 0.5) / 64.0 } else { 1.0 - (y as f32 + 0.5) / 64.0 }; |
| 365 | assert!((r - ramp_at).abs() < 0.2, "({x},{y}) {r} vs {ramp_at}"); |
| 366 | let cell_center = r * 8.0 - 0.5; |
| 367 | assert!((cell_center - cell_center.round()).abs() < 0.05, "({x},{y}) {r} is not a cell center"); |
| 368 | input.insert((r * 255.0).round() as u8); |
| 369 | } |
| 370 | } |
| 371 | assert!(input.len() > 3 && input.len() <= 8, "{} distinct facets", input.len()); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | #[test] |
| 376 | fn crystallize_seed_seethe_and_blades_change_the_facets() { |
| 377 | let render_with = |extra: &str, time: f64| { |
| 378 | let c = crystallize_comp(H_RAMP, &format!("Jitter = Input {{ Value = 1, }}, SeetheRate = Input {{ Value = 0, }}, {extra}")); |
| 379 | render(&c, "Crystal", time).map(|r| r.1) |
| 380 | }; |
| 381 | let Some(base) = render_with("", 0.0) else { return }; |
| 382 | assert_eq!(base, render_with("", 5.0).unwrap(), "SeetheRate 0 ignores time"); |
| 383 | assert_ne!(base, render_with("RandomSeed = Input { Value = 3, },", 0.0).unwrap()); |
| 384 | assert_ne!(base, render_with("Seethe = Input { Value = 0.3, },", 0.0).unwrap()); |
| 385 | assert_ne!(base, render_with("Blades = Input { Value = 3, },", 0.0).unwrap()); |
| 386 | |
| 387 | let c = crystallize_comp(H_RAMP, "SeetheRate = Input { Value = 0.1, },"); |
| 388 | assert_ne!(render(&c, "Crystal", 0.0).unwrap().1, render(&c, "Crystal", 5.0).unwrap().1, "phase = seethe + rate * time"); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn crystallize_feathering_blends_neighbouring_facets_and_smooth_averages_the_source() { |
| 393 | let c = crystallize_comp(H_RAMP, ""); |
| 394 | let hard = render(&c, "Crystal", 0.0).unwrap().1; |
| 395 | let c = crystallize_comp(H_RAMP, "Feathering = Input { Value = 1, },"); |
| 396 | let feathered = render(&c, "Crystal", 0.0).unwrap().1; |
| 397 | let distinct = |p: &[u8]| p.chunks(4).map(|p| p[0]).collect::<std::collections::HashSet<_>>().len(); |
| 398 | assert!(distinct(&feathered) > distinct(&hard) * 2, "feathering mixes facet colors"); |
| 399 | |
| 400 | // Per-pixel alpha noise: facets sampling one texel stay binary-ish, Smooth averages a facet's worth. |
| 401 | let noisy = |smooth| { |
| 402 | comp(&format!( |
| 403 | r#"{} |
| 404 | Noise = Fuse.AlphaWeightedNoise {{ Inputs = {{ Image = Input {{ SourceOp = "Half", Source = "Output", }}, }}, }}, |
| 405 | Crystal = Fuse.Crystallize {{ Inputs = {{ |
| 406 | Input = Input {{ SourceOp = "Noise", Source = "Output", }}, |
| 407 | Size = Input {{ Value = 0.125, }}, |
| 408 | Smooth = Input {{ Value = {smooth}, }}, |
| 409 | }}, }},"#, |
| 410 | bg("Half", 64, "TopLeftAlpha = Input { Value = 0.5, },") |
| 411 | )) |
| 412 | }; |
| 413 | let spread = |smooth| { |
| 414 | let a: Vec<f32> = render(&noisy(smooth), "Crystal", 0.0).unwrap().1.chunks(4).map(|p| f32::from(p[3]) / 255.0).collect(); |
| 415 | let mean = a.iter().sum::<f32>() / a.len() as f32; |
| 416 | a.iter().map(|v| (v - mean).powi(2)).sum::<f32>() / a.len() as f32 |
| 417 | }; |
| 418 | assert!(spread(1) * 3.0 < spread(0), "smooth {} vs raw {}", spread(1), spread(0)); |
| 419 | } |
| 420 | |
| 421 | #[test] |
| 422 | fn crystallize_preserves_flat_images_and_requires_an_input() { |
| 423 | let c = comp(&format!( |
| 424 | r#"{} Crystal = Fuse.Crystallize {{ Inputs = {{ Input = Input {{ SourceOp = "Src", Source = "Output", }}, }}, }}, Bare = Fuse.Crystallize {{ }},"#, |
| 425 | bg("Src", 32, "TopLeftRed = Input { Value = 0.5, }, TopLeftGreen = Input { Value = 0.25, }, TopLeftAlpha = Input { Value = 1, },") |
| 426 | )); |
| 427 | let Some((size, rgba)) = render(&c, "Crystal", 2.0) else { return }; |
| 428 | assert_eq!(size, [32, 32]); |
| 429 | assert!(rgba.chunks(4).all(|p| p == [128, 64, 0, 255]), "default Smooth/Feathering keep a flat field flat"); |
| 430 | assert_eq!(render(&c, "Bare", 0.0).unwrap().0, [1, 1]); |
| 431 | } |
| 432 | } |