-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shader_material_2d example (#10542)
# Objective - 2d materials have subtle differences with 3d materials that aren't obvious to beginners ## Solution - Add an example that shows how to make a 2d material
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#import bevy_sprite::mesh2d_vertex_output::VertexOutput | ||
// we can import items from shader modules in the assets folder with a quoted path | ||
#import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER | ||
|
||
@group(1) @binding(0) var<uniform> material_color: vec4<f32>; | ||
@group(1) @binding(1) var base_color_texture: texture_2d<f32>; | ||
@group(1) @binding(2) var base_color_sampler: sampler; | ||
|
||
@fragment | ||
fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> { | ||
return material_color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! A shader and a material that uses it. | ||
use bevy::{ | ||
prelude::*, | ||
reflect::TypePath, | ||
render::render_resource::{AsBindGroup, ShaderRef}, | ||
sprite::{Material2d, Material2dPlugin, MaterialMesh2dBundle}, | ||
}; | ||
|
||
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(Camera2dBundle::default()); | ||
|
||
// quad | ||
commands.spawn(MaterialMesh2dBundle { | ||
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(), | ||
transform: Transform::default().with_scale(Vec3::splat(128.)), | ||
material: materials.add(CustomMaterial { | ||
color: Color::BLUE, | ||
color_texture: Some(asset_server.load("branding/icon.png")), | ||
}), | ||
..default() | ||
}); | ||
} | ||
|
||
// This is the struct that will be passed to your shader | ||
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] | ||
pub struct CustomMaterial { | ||
#[uniform(0)] | ||
color: Color, | ||
#[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 { | ||
"shaders/custom_material_2d.wgsl".into() | ||
} | ||
} |