Add a test scene that animates the base color

The test scenes can now supply their own optional base (background)
color. In the with_winit example, we also allow the user to provide a
base color as a CLI option. The precedence is as follows:

   1. Use the color from the CLI options, if any.
   2. Otherwise use the scene provided base color, if any.
   3. Otherwise default to black.
This commit is contained in:
Arman Uguray 2023-03-03 10:47:04 -08:00
parent 3bbf108df5
commit d72ad14059
4 changed files with 32 additions and 8 deletions

View file

@ -96,6 +96,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
time: args.time.unwrap_or(0.), time: args.time.unwrap_or(0.),
text: &mut text, text: &mut text,
resolution: None, resolution: None,
base_color: None,
}; };
(example_scene.function)(&mut builder, &mut scene_params); (example_scene.function)(&mut builder, &mut scene_params);
builder.finish(); builder.finish();
@ -130,6 +131,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
base_color: args base_color: args
.args .args
.get_base_color()? .get_base_color()?
.or(scene_params.base_color)
.unwrap_or(vello::peniko::Color::BLACK), .unwrap_or(vello::peniko::Color::BLACK),
width, width,
height, height,

View file

@ -19,6 +19,7 @@ pub struct SceneParams<'a> {
pub time: f64, pub time: f64,
pub text: &'a mut SimpleText, pub text: &'a mut SimpleText,
pub resolution: Option<Vec2>, pub resolution: Option<Vec2>,
pub base_color: Option<vello::peniko::Color>,
} }
pub struct SceneConfig { pub struct SceneConfig {

View file

@ -32,6 +32,7 @@ pub fn test_scenes() -> SceneSet {
scene!(blend_grid), scene!(blend_grid),
scene!(conflation_artifacts), scene!(conflation_artifacts),
scene!(labyrinth), scene!(labyrinth),
scene!(base_color_test: animated),
]; ];
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
scenes.push(ExampleScene { scenes.push(ExampleScene {
@ -582,6 +583,21 @@ fn labyrinth(sb: &mut SceneBuilder, _: &mut SceneParams) {
) )
} }
fn base_color_test(sb: &mut SceneBuilder, params: &mut SceneParams) {
// Cycle through the hue value every 5 seconds (t % 5) * 360/5
let color = Color::hlc((params.time % 5.0) * 72.0, 80.0, 80.0);
params.base_color = Some(color);
// Blend a white square over it.
sb.fill(
Fill::NonZero,
Affine::IDENTITY,
Color::rgba8(255, 255, 255, 128),
None,
&Rect::new(50.0, 50.0, 500.0, 500.0),
);
}
fn around_center(xform: Affine, center: Point) -> Affine { fn around_center(xform: Affine, center: Point) -> Affine {
Affine::translate(center.to_vec2()) * xform * Affine::translate(-center.to_vec2()) Affine::translate(center.to_vec2()) * xform * Affine::translate(-center.to_vec2())
} }

View file

@ -59,7 +59,7 @@ async fn run(
event_loop: EventLoop<UserEvent>, event_loop: EventLoop<UserEvent>,
window: Window, window: Window,
args: Args, args: Args,
base_color: Color, base_color_arg: Option<Color>,
mut scenes: SceneSet, mut scenes: SceneSet,
) { ) {
use winit::{event::*, event_loop::ControlFlow}; use winit::{event::*, event_loop::ControlFlow};
@ -163,12 +163,6 @@ async fn run(
let height = surface.config.height; let height = surface.config.height;
let device_handle = &render_cx.devices[surface.dev_id]; let device_handle = &render_cx.devices[surface.dev_id];
let render_params = vello::RenderParams {
base_color,
width,
height,
};
// Allow looping forever // Allow looping forever
scene_ix = scene_ix.rem_euclid(scenes.scenes.len() as i32); scene_ix = scene_ix.rem_euclid(scenes.scenes.len() as i32);
let example_scene = &mut scenes.scenes[scene_ix as usize]; let example_scene = &mut scenes.scenes[scene_ix as usize];
@ -182,9 +176,20 @@ async fn run(
time: start.elapsed().as_secs_f64(), time: start.elapsed().as_secs_f64(),
text: &mut simple_text, text: &mut simple_text,
resolution: None, resolution: None,
base_color: None,
}; };
(example_scene.function)(&mut builder, &mut scene_params); (example_scene.function)(&mut builder, &mut scene_params);
builder.finish(); builder.finish();
// If the user specifies a base color in the CLI we use that. Otherwise we use any
// color specified by the scene. The default is black.
let render_params = vello::RenderParams {
base_color: base_color_arg
.or(scene_params.base_color)
.unwrap_or(Color::BLACK),
width,
height,
};
let mut builder = SceneBuilder::for_scene(&mut scene); let mut builder = SceneBuilder::for_scene(&mut scene);
let mut transform = transform; let mut transform = transform;
if let Some(resolution) = scene_params.resolution { if let Some(resolution) = scene_params.resolution {
@ -257,7 +262,7 @@ fn main() -> Result<()> {
env_logger::init(); env_logger::init();
let args = Args::parse(); let args = Args::parse();
let scenes = args.args.select_scene_set(|| Args::command())?; let scenes = args.args.select_scene_set(|| Args::command())?;
let base_color = args.args.get_base_color()?.unwrap_or(Color::BLACK); let base_color = args.args.get_base_color()?;
if let Some(scenes) = scenes { if let Some(scenes) = scenes {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ {