diff --git a/assets/shaders/custom_material.frag b/assets/shaders/custom_material.frag index c18a67b4e3..bf46d1e533 100644 --- a/assets/shaders/custom_material.frag +++ b/assets/shaders/custom_material.frag @@ -1,4 +1,5 @@ #version 450 +layout(location = 0) in vec2 v_Uv; layout(location = 0) out vec4 o_Target; @@ -6,6 +7,10 @@ layout(set = 1, binding = 0) uniform CustomMaterial { vec4 Color; }; +layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture; +layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler; + + void main() { - o_Target = Color; + o_Target = Color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv); } diff --git a/assets/shaders/custom_material.vert b/assets/shaders/custom_material.vert index f18a9fa534..59af5305ba 100644 --- a/assets/shaders/custom_material.vert +++ b/assets/shaders/custom_material.vert @@ -4,6 +4,8 @@ layout(location = 0) in vec3 Vertex_Position; layout(location = 1) in vec3 Vertex_Normal; layout(location = 2) in vec2 Vertex_Uv; +layout(location = 0) out vec2 v_Uv; + layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; mat4 View; @@ -21,5 +23,6 @@ layout(set = 2, binding = 0) uniform Mesh { }; void main() { + v_Uv = Vertex_Uv; gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); } diff --git a/examples/shader/shader_material_glsl.rs b/examples/shader/shader_material_glsl.rs index 54526ea59f..2af38adc30 100644 --- a/examples/shader/shader_material_glsl.rs +++ b/examples/shader/shader_material_glsl.rs @@ -25,13 +25,16 @@ fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, + asset_server: Res, ) { // cube commands.spawn().insert_bundle(MaterialMeshBundle { mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), transform: Transform::from_xyz(0.0, 0.5, 0.0), material: materials.add(CustomMaterial { - color: Color::GREEN, + color: Color::BLUE, + color_texture: Some(asset_server.load("branding/icon.png")), + alpha_mode: AlphaMode::Blend, }), ..default() }); @@ -43,13 +46,21 @@ fn setup( }); } +// This is the struct that will be passed to your shader #[derive(AsBindGroup, Clone, TypeUuid)] #[uuid = "4ee9c363-1124-4113-890e-199d81b00281"] pub struct CustomMaterial { #[uniform(0)] color: Color, + #[texture(1)] + #[sampler(2)] + color_texture: Option>, + alpha_mode: AlphaMode, } +/// The Material 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 Material api docs for details! +/// When using the GLSL shading language for your shader, the specialize method must be overriden. impl Material for CustomMaterial { fn vertex_shader() -> ShaderRef { "shaders/custom_material.vert".into() @@ -59,6 +70,10 @@ impl Material for CustomMaterial { "shaders/custom_material.frag".into() } + fn alpha_mode(&self) -> AlphaMode { + self.alpha_mode + } + // Bevy assumes by default that vertex shaders use the "vertex" entry point // and fragment shaders use the "fragment" entry point (for WGSL shaders). // GLSL uses "main" as the entry point, so we must override the defaults here