#相机 #像素 #创建 #辅助工具 #系统 #计划 #函数

程序+库 basic_bevy_pixel_camera

一组简单的组件、计划、系统辅助函数,用于创建任何类型的像素相机

2 个版本

0.1.1 2024年7月5日
0.1.0 2024年7月5日

1179游戏开发

Download history 182/week @ 2024-07-04 8/week @ 2024-07-11 2/week @ 2024-07-25

每月192 次下载

MIT/Apache

31KB
413

像素相机

一组简单的组件、计划、系统辅助函数,用于创建任何类型的像素相机

注意 这是一个正在进行中的项目,如果您有任何反馈,请随时提交问题或拉取请求

使用方法

以下示例展示了库的简单使用方法。此示例利用了 PixelCameraSnappingPixelCanvasSmoothing 组件,使对象看起来像素化,而不是相机。但如果你想要复古风格的感觉,可以省略这些组件。

use bevy::{
    prelude::*,
    render::{camera::ScalingMode, view::RenderLayers},
};
use pixel_camera::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, PixelCameraPlugin) // Ordering important as Pixel Camera add's its own schedule
        .add_systems(Startup, setup)
        .run();
}

pub const PIXELS_PER_UNIT: f32 = 8.0;
pub const HIGH_RES_LAYER: RenderLayers = RenderLayers::layer(0);
pub const PIXEL_PERFECT_LAYER: RenderLayers = RenderLayers::layer(1);

#[derive(Component)]
pub struct MainCamera;

pub fn setup_cameras(mut commands: Commands, images: ResMut<Assets<Image>>) {
    let mut cam_2d = Camera2dBundle::default();
    cam_2d.projection.scaling_mode = ScalingMode::AutoMin {
        min_width: 16.0,
        min_height: 8.0,
    };

    // Window camera to view the pixel canvas
    let main_cam = commands
        .spawn((cam_2d, HIGH_RES_LAYER, MainCamera))
        .id();

    // Creating the Pixel image to be stored in the assests resource
    let image = create_pixel_image(images);

    // Creating the pixel camera which is the place in which the pixelized image is rendered
    let pixel_camera = create_pixel_camera(commands.reborrow(), image.clone(), PIXEL_PERFECT_LAYER);

    // Creating the pixel canvas which is the image upon which the pixelated render is viewed
    let pixel_canvas = create_pixel_canvas(
        &PixelCanvasConfig::new(PIXELS_PER_UNIT, 8.0, 4.0),
        commands.reborrow(),
        image,
        pixel_camera,
        HIGH_RES_LAYER,
    );

    // Add pixel camera snapping. This stops the flickering artifacts of
    // non-pixelized sprites when the camera moves (this is subjective)
    commands.entity(pixel_camera).insert(PixelCameraSnapping);

    // Add pixel canvas smoothing. This lerps the canvas rect to
    // make the pixelated view move smoothly.
    //
    // This requires pixel camera snapping, or every 5-10 seconds there
    // will be a highly noticeable flicker in the pixelated view
    commands.entity(pixel_canvas).insert(PixelCanvasSmoothing);
}

依赖关系

~23MB
~410K SLoC