Path: blob/main/examples/shader/shader_material_glsl.rs
6595 views
//! A shader that uses the GLSL shading language.12use bevy::{3prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, shader::ShaderRef,4};56/// This example uses shader source files from the assets subdirectory7const VERTEX_SHADER_ASSET_PATH: &str = "shaders/custom_material.vert";8const FRAGMENT_SHADER_ASSET_PATH: &str = "shaders/custom_material.frag";910fn main() {11App::new()12.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))13.add_systems(Startup, setup)14.run();15}1617/// set up a simple 3D scene18fn setup(19mut commands: Commands,20mut meshes: ResMut<Assets<Mesh>>,21mut materials: ResMut<Assets<CustomMaterial>>,22asset_server: Res<AssetServer>,23) {24// cube25commands.spawn((26Mesh3d(meshes.add(Cuboid::default())),27MeshMaterial3d(materials.add(CustomMaterial {28color: LinearRgba::BLUE,29color_texture: Some(asset_server.load("branding/icon.png")),30alpha_mode: AlphaMode::Blend,31})),32Transform::from_xyz(0.0, 0.5, 0.0),33));3435// camera36commands.spawn((37Camera3d::default(),38Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),39));40}4142// This is the struct that will be passed to your shader43#[derive(Asset, TypePath, AsBindGroup, Clone)]44struct CustomMaterial {45#[uniform(0)]46color: LinearRgba,47#[texture(1)]48#[sampler(2)]49color_texture: Option<Handle<Image>>,50alpha_mode: AlphaMode,51}5253/// The Material trait is very configurable, but comes with sensible defaults for all methods.54/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!55/// When using the GLSL shading language for your shader, the specialize method must be overridden.56impl Material for CustomMaterial {57fn vertex_shader() -> ShaderRef {58VERTEX_SHADER_ASSET_PATH.into()59}6061fn fragment_shader() -> ShaderRef {62FRAGMENT_SHADER_ASSET_PATH.into()63}6465fn alpha_mode(&self) -> AlphaMode {66self.alpha_mode67}68}697071