Files
AniCraft/crates/render/src/layer_effects_tests.rs
T

70 lines
3.6 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Effects on a layer and on a keyframe (ADR 0141) in the CPU oracle and the exports that
//! draw evaluated frames: the layer's artwork is drawn together, then tinted, blended and
//! filtered as one.
use crate::{View, render};
use anicraft_doc::{BlendMode, ColorEffect, Document, Effects, Element, ElementId, ElementKind, Filter, Keyframe, Layer, LayerId, Paint, Rgba, Shape, Stage, evaluate};
use anicraft_geom::{Affine, Path};
use std::sync::Arc;
fn rect(id: u64, x: f32, w: f32, colour: Rgba) -> Arc<Element> {
Arc::new(Element { id: ElementId(id), name: String::new(), transform: Affine::translate(x, 0.0), color: ColorEffect::default(), kind: ElementKind::Shape(Shape::filled(Path::rect(0.0, 0.0, w, 8.0), Paint::Solid(colour))) })
}
/// A 24 × 8 white stage. Below: red over the whole stage. Above: a grey block over x 8..16,
/// on a layer with `effects`.
fn scene(effects: Effects) -> Document {
let mut d = Document { stage: Stage { width: 24, height: 8, background: Rgba::WHITE, ..Stage::default() }, ..Document::default() };
let layer = |id: u64, el: Arc<Element>| {
let mut l = Layer::new(LayerId(id), "l");
l.keyframes = vec![Arc::new(Keyframe { elements: vec![el], ..Keyframe::new(0, 1) })];
l
};
let mut top = layer(1, rect(11, 8.0, 8.0, Rgba::new(0.5, 0.5, 0.5, 1.0)));
top.effects = effects;
d.timeline_mut(0).layers = vec![Arc::new(top), Arc::new(layer(2, rect(12, 0.0, 24.0, Rgba::new(1.0, 0.0, 0.0, 1.0))))];
d
}
fn pixels(d: &Document) -> impl Fn(u32) -> [f32; 4] + use<> {
let (pm, _) = render(&evaluate(d, 0), &View::stage(d, 1.0)).unwrap();
move |x| pm.pixel(x, 4).unwrap()
}
fn near(p: [f32; 4], q: [f32; 4]) -> bool {
p.iter().zip(q).all(|(a, b)| (a - b).abs() < 2e-3)
}
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
#[test]
fn a_layers_colour_effect_and_blend_mode_reach_the_pixels() {
let plain = pixels(&scene(Effects::default()));
assert!(near(plain(12), [0.5, 0.5, 0.5, 1.0]) && near(plain(2), RED));
// Tinted fully blue: the layer's block is blue, the layer below is as it was.
let tinted = pixels(&scene(Effects { color: ColorEffect::Tint { color: Rgba::new(0.0, 0.0, 1.0, 1.0), amount: 1.0 }, ..Effects::default() }));
assert!(near(tinted(12), [0.0, 0.0, 1.0, 1.0]), "{:?}", tinted(12));
assert!(near(tinted(2), RED));
// Multiplied: half grey over red is half red.
let multiplied = pixels(&scene(Effects { blend: BlendMode::Multiply, ..Effects::default() }));
assert!(near(multiplied(12), [0.5, 0.0, 0.0, 1.0]), "{:?}", multiplied(12));
// Half transparent by its colour effect: halfway between the grey and the red.
let faded = pixels(&scene(Effects { color: ColorEffect::Alpha(0.5), ..Effects::default() }));
assert!(near(faded(12), [0.75, 0.25, 0.25, 1.0]), "{:?}", faded(12));
}
#[test]
fn a_layers_filter_runs_on_its_artwork_alone() {
let blur = Effects { filters: vec![Filter::Blur { blur_x: 6.0, blur_y: 0.0, quality: 1 }.into()], ..Effects::default() };
let px = pixels(&scene(blur.clone()));
// The block's edge spreads over the red beside it, and the red far from it is untouched:
// the layer below was not blurred with it.
let beside = px(6);
assert!(beside[1] > 0.02 && beside[1] < 0.5, "grey bleeds past the edge: {beside:?}");
assert!(near(px(0), RED), "{:?}", px(0));
// The SVG export carries the same group: a filter, and a blend mode when there is one.
let d = scene(Effects { blend: BlendMode::Screen, ..blur });
let svg = crate::svg::to_svg(&evaluate(&d, 0), &d.stage);
assert!(svg.contains("<filter") && svg.contains("mix-blend-mode:screen"), "{svg}");
}