mirror of
https://github.com/storytold/AniCraft.git
synced 2026-12-01 02:24:23 +01:00
feat(render): layer and keyframe effects in the HTML export, checked against the evaluator (M7)
This commit is contained in:
1 parent
db56d0485a
commit
8e85ebc364
5 files changed
+134
-15
No files matched your search
@@ -189,7 +189,11 @@ fn exact_color(c: Rgba) -> Value {
|
||||
/// An item's filters, on a symbol instance or on text (ADR 0099): `fx` the list, `fm` how far
|
||||
/// they reach together (stage pixels); filters switched off are left out (ADR 0091).
|
||||
fn filters_json(filters: &[anicraft_doc::FilterEntry], item: &mut Map<String, Value>) {
|
||||
let on = anicraft_doc::active_filters(filters);
|
||||
active_filters_json(&anicraft_doc::active_filters(filters), item);
|
||||
}
|
||||
|
||||
/// [`filters_json`] for filters already picked out as the ones that draw (a layer's, ADR 0141).
|
||||
fn active_filters_json(on: &[anicraft_doc::Filter], item: &mut Map<String, Value>) {
|
||||
if !on.is_empty() {
|
||||
item.insert("fx".into(), Value::Array(on.iter().map(filter_json).collect()));
|
||||
item.insert("fm".into(), exact(on.iter().map(anicraft_doc::Filter::margin).sum::<f32>()));
|
||||
@@ -849,11 +853,22 @@ impl<'a> Bake<'a> {
|
||||
if layer.sound_gain() != 1.0 {
|
||||
entry.insert("sg".into(), num(layer.sound_gain(), 10_000.0));
|
||||
}
|
||||
// The layer's opacity (its own times its folders'; ADR 0111): the page fades
|
||||
// the layer as one group. Mask artwork ignores it.
|
||||
let fade = tl.layer_opacity(index);
|
||||
if fade < 1.0 - 1e-4 && layer.kind != LayerKind::Mask {
|
||||
entry.insert("o".into(), num(fade, 10_000.0));
|
||||
// The layer's opacity (its own times its folders'; ADR 0111) and its effects
|
||||
// with the keyframe's (ADR 0141), as the evaluator puts them together: the
|
||||
// page draws the layer as one group with this fade (`o`), blend mode (`bl`)
|
||||
// and filters (`fx`, `fm`), and colours its items by `lc`. Mask artwork
|
||||
// ignores them all.
|
||||
let look = if layer.kind == LayerKind::Mask { anicraft_doc::LayerLook::PLAIN } else { tl.layer_look(index, f) };
|
||||
let grouped = look.grouped();
|
||||
if look.alpha < 1.0 - 1e-4 {
|
||||
entry.insert("o".into(), exact(look.alpha));
|
||||
}
|
||||
if !look.blend.is_normal() {
|
||||
entry.insert("bl".into(), json!(look.blend.key()));
|
||||
}
|
||||
active_filters_json(&look.filters, &mut entry);
|
||||
if let Some(c) = effect(&look.color) {
|
||||
entry.insert("lc".into(), c);
|
||||
}
|
||||
if layer.kind == LayerKind::Mask {
|
||||
entry.insert("mk".into(), json!(slots.get(&layer.id.0).copied().unwrap_or(0)));
|
||||
@@ -879,9 +894,15 @@ impl<'a> Bake<'a> {
|
||||
self.element(el, layer, f, start, &mut items);
|
||||
}
|
||||
}
|
||||
// (A faded layer with elements stays, drawing or not: it is a group in the
|
||||
// evaluator's list, and the page's groups are numbered the same.)
|
||||
if items.is_empty() && layer.kind != LayerKind::Mask && !(any && entry.contains_key("o")) {
|
||||
// (A layer drawn as a group stays when it has elements, drawing or not: it is
|
||||
// a group in the evaluator's list, and the page's groups are numbered the same.
|
||||
// With no elements it is no group there, so its group keys go.)
|
||||
if !any {
|
||||
for k in ["o", "bl", "fx", "fm"] {
|
||||
entry.remove(k);
|
||||
}
|
||||
}
|
||||
if items.is_empty() && layer.kind != LayerKind::Mask && !(any && grouped) {
|
||||
continue;
|
||||
}
|
||||
entry.insert("e".into(), Value::Array(items));
|
||||
|
||||
@@ -343,13 +343,19 @@ var AniCraft = (function () {
|
||||
let lcx = L.v ? Object.assign({}, cx, { xf: L.v }) : cx;
|
||||
// A layer that follows a parent layer is placed by it (`r`, ADR 0113).
|
||||
if (L.r) lcx = Object.assign({}, lcx, { xf: mul(lcx.xf, L.r) });
|
||||
// A layer below full opacity (ADR 0111) fades as one group, as eval.rs draws it.
|
||||
// A layer below full opacity (ADR 0111), or with a blend mode or filters of its own or
|
||||
// of its keyframe (ADR 0141), draws as one group, as eval.rs draws it; its colour
|
||||
// effect (`lc`) colours each item in it.
|
||||
let faded = null;
|
||||
if (L.o !== undefined && cx.masks === null && !isMask) {
|
||||
faded = dl.groups.length;
|
||||
dl.groups.push({ parent: cx.group, alpha: Math.min(Math.max(L.o, 0), 1), blend: "normal", clip: clip, filters: null, margin: 0 });
|
||||
lcx = Object.assign({}, lcx, { group: faded });
|
||||
if (v) v.groupBegin(faded, dl.groups[faded]);
|
||||
if (cx.masks === null && !isMask) {
|
||||
const filters = Array.isArray(L.fx) && L.fx.length ? L.fx : null;
|
||||
if (L.lc) lcx = Object.assign({}, lcx, { c: concat(lcx.c, L.lc) });
|
||||
if (L.o !== undefined || L.bl || filters) {
|
||||
faded = dl.groups.length;
|
||||
dl.groups.push({ parent: cx.group, alpha: L.o === undefined ? 1 : Math.min(Math.max(L.o, 0), 1), blend: L.bl || "normal", clip: clip, filters: filters, margin: filters ? L.fm || 0 : 0 });
|
||||
lcx = Object.assign({}, lcx, { group: faded });
|
||||
if (v) v.groupBegin(faded, dl.groups[faded]);
|
||||
}
|
||||
}
|
||||
for (const e of L.e) evalElement(M, e, lcx, clip, masks, dl, play, v);
|
||||
if (faded !== null && v) v.groupEnd(faded, dl.groups[faded]);
|
||||
|
||||
@@ -415,6 +415,27 @@ fn the_js_player_matches_on_the_demo_and_on_go_to_loops() {
|
||||
compare(&d, &vec![(vec![], vec![]); 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_and_keyframe_effects_come_across_as_the_evaluator_groups_them() {
|
||||
// The demo with effects on every layer (ADR 0141): a tint and a blend mode on the layer,
|
||||
// and on each layer's first keyframe a fade and a blur that a tween carries on where the
|
||||
// layer has one. The page's groups and item colours match the evaluator's on every frame.
|
||||
let mut doc = sample::demo();
|
||||
let blur = |x: f32| anicraft_doc::FilterEntry::from(anicraft_doc::Filter::Blur { blur_x: x, blur_y: x, quality: 1 });
|
||||
for (i, l) in doc.timeline_mut(0).layers.iter_mut().enumerate() {
|
||||
let l = Arc::make_mut(l);
|
||||
l.effects = anicraft_doc::Effects { color: ColorEffect::Tint { color: Rgba::hex(0x3366ff), amount: 0.25 }, blend: if i % 2 == 0 { BlendMode::Multiply } else { BlendMode::Normal }, filters: vec![blur(2.0)] };
|
||||
for (k, key) in l.keyframes.iter_mut().enumerate() {
|
||||
Arc::make_mut(key).effects = anicraft_doc::Effects { color: ColorEffect::Alpha(if k % 2 == 0 { 0.5 } else { 1.0 }), blend: BlendMode::Normal, filters: vec![blur(k as f32 * 4.0)] };
|
||||
}
|
||||
}
|
||||
doc.sanitize();
|
||||
let m = movie(&doc, &HtmlOptions::default());
|
||||
let layers: Vec<&Value> = m.json["contents"].as_array().unwrap().iter().flat_map(|c| c["l"].as_array().unwrap()).collect();
|
||||
assert!(layers.iter().any(|l| l["bl"] == "multiply") && layers.iter().any(|l| l["fx"].is_array() && l["lc"].is_array() && l["o"].is_number()), "the layers' effects are baked into the frames");
|
||||
compare(&doc, &vec![(vec![], vec![]); 40]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_camera_and_layer_depth_come_across_and_buttons_answer_through_them() {
|
||||
// The fixture through a camera that pans, zooms, turns and tints over frames 1-20, with a
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
//! 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}");
|
||||
}
|
||||
@@ -1278,6 +1278,8 @@ mod nine_slice_tests;
|
||||
#[cfg(test)]
|
||||
mod layer_opacity_tests;
|
||||
#[cfg(test)]
|
||||
mod layer_effects_tests;
|
||||
#[cfg(test)]
|
||||
mod cut_tests;
|
||||
#[cfg(test)]
|
||||
mod join_tests;
|
||||
|
||||
Reference in new issue
Block a user