## Objective Bevy 0.15 introduced new method in `Material2d` trait- `alpha_mode`. Before that when new material was created it had alpha blending, now it does not. ## Solution While I am okay with it, it could be useful to add the new trait method implementation to one of the samples so users are more aware of it. --------- Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! A shader and a material that uses it.
 | 
						|
 | 
						|
use bevy::{
 | 
						|
    prelude::*,
 | 
						|
    reflect::TypePath,
 | 
						|
    render::render_resource::{AsBindGroup, ShaderRef},
 | 
						|
    sprite::{AlphaMode2d, 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()
 | 
						|
    }
 | 
						|
 | 
						|
    fn alpha_mode(&self) -> AlphaMode2d {
 | 
						|
        AlphaMode2d::Mask(0.5)
 | 
						|
    }
 | 
						|
}
 |