Preview App output_formats 示例输出 output_formats 示例输出

scenes.rs(共享场景与项目装配)

use std::f64::consts::PI;

use ranim::{
    anims::{fading::FadingAnim, rotating::RotatingAnim},
    color::palettes::manim,
    glam::DVec3,
    items::vitem::{VItem, geometry::Circle},
    prelude::*,
    utils::rate_functions::smooth,
};

#[scene(clear_color = "#00000000")]
#[wasm_demo_doc]
#[output(dir = "./output/output_formats", format = "mp4")]
#[output(dir = "./output/output_formats", format = "webm")]
#[output(dir = "./output/output_formats", format = "mov")]
#[output(dir = "./output/output_formats", format = "gif")]
fn output_formats(r: &mut RanimScene) {
    let colors = [manim::RED_C, manim::GREEN_C, manim::BLUE_C];

    let radius = 1.4;
    let mut circles: Vec<VItem> = colors
        .iter()
        .enumerate()
        .map(|(i, &color)| {
            let angle = i as f64 * 2.0 * PI / colors.len() as f64 + PI / 2.0;
            let offset = DVec3::new(angle.cos() * radius, angle.sin() * radius, 0.0);
            {
                let mut circle: VItem = Circle::new(radius)
                    .with(|c| {
                        c.set_color(color.with_alpha(0.5));
                    })
                    .into();
                circle.shift(offset);
                circle
            }
        })
        .collect();

    let mut content = seq![
        circles
            .iter_mut()
            .map(|circle| circle.fade_in().with_rate_func(smooth))
            .into_lagged(0.3)
            .with_duration(1.0)
    ];
    content
        .hold(1.0)
        .push(
            circles
                .rotating_at(2.0 * PI / 3.0, DVec3::Z, DVec3::ZERO)
                .with_duration(1.0)
                .with_rate_func(smooth),
        )
        .hold(1.0)
        .push(
            circles
                .iter_mut()
                .map(|circle| circle.fade_out().with_rate_func(smooth))
                .into_lagged(0.3)
                .with_duration(1.0),
        );

    r.play(
        CameraFrame::default()
            .show()
            .with_duration(content.cursor_sec()),
    );
    r.play(content);
    r.insert_time_mark(1.5, TimeMark::Capture("preview.png".to_string()));
}

/// Register the scenes provided by this example.
pub struct ScenesPlugin;

impl ranim::Plugin for ScenesPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.add_scenes([output_formats::scene()]);
    }
}

main.rs(平台入口)

mod scenes;

#[cfg(not(target_arch = "wasm32"))]
fn main() {
    #[cfg(feature = "cmd-render")]
    {
        use ranim::prelude::*;
        let mut app = App::new();
        app.add_plugins(scenes::ScenesPlugin);
        run_cli(app.build());
    }
    #[cfg(not(feature = "cmd-render"))]
    compile_error!("native examples require the `cmd-render` feature (enabled by default)");
}

// The WASM module is driven by JavaScript through app().
#[cfg(target_arch = "wasm32")]
fn main() {}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn app() -> ranim::wasm::WasmApp {
    use ranim::prelude::*;

    let mut app = App::new();
    app.add_plugins(scenes::ScenesPlugin);
    ranim::wasm::WasmApp::new(app.build())
}