mirror of
https://github.com/bevyengine/bevy
synced 2024-11-24 13:43:04 +00:00
25bfa80e60
# Objective Yet another PR for migrating stuff to required components. This time, cameras! ## Solution As per the [selected proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected), deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d` and `Camera3d`. Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning, as suggested by Cart [on Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273). I would personally like cameras to work a bit differently and be split into a few more components, to avoid some footguns and confusing semantics, but that is more controversial, and shouldn't block this core migration. ## Testing I ran a few 2D and 3D examples, and tried cameras with and without render graphs. --- ## Migration Guide `Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of `Camera2d` and `Camera3d`. Inserting them will now also insert the other components required by them automatically.
60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
//! A shader and a material that uses it.
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
reflect::TypePath,
|
|
render::render_resource::{AsBindGroup, ShaderRef},
|
|
sprite::{Material2d, Material2dPlugin},
|
|
};
|
|
|
|
/// This example uses a shader source file from the assets subdirectory
|
|
const SHADER_ASSET_PATH: &str = "shaders/custom_material_2d.wgsl";
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins,
|
|
Material2dPlugin::<CustomMaterial>::default(),
|
|
))
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
// Setup a simple 2d scene
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<CustomMaterial>>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
// camera
|
|
commands.spawn(Camera2d);
|
|
|
|
// quad
|
|
commands.spawn((
|
|
Mesh2d(meshes.add(Rectangle::default())),
|
|
MeshMaterial2d(materials.add(CustomMaterial {
|
|
color: LinearRgba::BLUE,
|
|
color_texture: Some(asset_server.load("branding/icon.png")),
|
|
})),
|
|
Transform::default().with_scale(Vec3::splat(128.)),
|
|
));
|
|
}
|
|
|
|
// This is the struct that will be passed to your shader
|
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
|
struct CustomMaterial {
|
|
#[uniform(0)]
|
|
color: LinearRgba,
|
|
#[texture(1)]
|
|
#[sampler(2)]
|
|
color_texture: Option<Handle<Image>>,
|
|
}
|
|
|
|
/// The Material2d trait is very configurable, but comes with sensible defaults for all methods.
|
|
/// You only need to implement functions for features that need non-default behavior. See the Material2d api docs for details!
|
|
impl Material2d for CustomMaterial {
|
|
fn fragment_shader() -> ShaderRef {
|
|
SHADER_ASSET_PATH.into()
|
|
}
|
|
}
|