Update shader_material_glsl example to include texture sampling (#5215)

# Objective

Add texture sampling to the GLSL shader example, as naga does not support the commonly used sampler2d type.
Fixes #5059

## Solution

- Align the shader_material_glsl example behaviour with the shader_material example,  as the later includes texture sampling.
- Update the GLSL shader to do texture sampling the way naga supports it, and document the way naga does not support it.

## Changelog

- The shader_material_glsl example has been updated to demonstrate texture sampling using the GLSL shading language.


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
This commit is contained in:
Boutillier 2022-07-08 01:14:22 +00:00
parent 4ee73ed904
commit 6b073ee412
3 changed files with 25 additions and 2 deletions

View file

@ -1,4 +1,5 @@
#version 450 #version 450
layout(location = 0) in vec2 v_Uv;
layout(location = 0) out vec4 o_Target; layout(location = 0) out vec4 o_Target;
@ -6,6 +7,10 @@ layout(set = 1, binding = 0) uniform CustomMaterial {
vec4 Color; vec4 Color;
}; };
layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture;
layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler;
void main() { void main() {
o_Target = Color; o_Target = Color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv);
} }

View file

@ -4,6 +4,8 @@ layout(location = 0) in vec3 Vertex_Position;
layout(location = 1) in vec3 Vertex_Normal; layout(location = 1) in vec3 Vertex_Normal;
layout(location = 2) in vec2 Vertex_Uv; layout(location = 2) in vec2 Vertex_Uv;
layout(location = 0) out vec2 v_Uv;
layout(set = 0, binding = 0) uniform CameraViewProj { layout(set = 0, binding = 0) uniform CameraViewProj {
mat4 ViewProj; mat4 ViewProj;
mat4 View; mat4 View;
@ -21,5 +23,6 @@ layout(set = 2, binding = 0) uniform Mesh {
}; };
void main() { void main() {
v_Uv = Vertex_Uv;
gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0);
} }

View file

@ -25,13 +25,16 @@ fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>, mut materials: ResMut<Assets<CustomMaterial>>,
asset_server: Res<AssetServer>,
) { ) {
// cube // cube
commands.spawn().insert_bundle(MaterialMeshBundle { commands.spawn().insert_bundle(MaterialMeshBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
transform: Transform::from_xyz(0.0, 0.5, 0.0), transform: Transform::from_xyz(0.0, 0.5, 0.0),
material: materials.add(CustomMaterial { material: materials.add(CustomMaterial {
color: Color::GREEN, color: Color::BLUE,
color_texture: Some(asset_server.load("branding/icon.png")),
alpha_mode: AlphaMode::Blend,
}), }),
..default() ..default()
}); });
@ -43,13 +46,21 @@ fn setup(
}); });
} }
// This is the struct that will be passed to your shader
#[derive(AsBindGroup, Clone, TypeUuid)] #[derive(AsBindGroup, Clone, TypeUuid)]
#[uuid = "4ee9c363-1124-4113-890e-199d81b00281"] #[uuid = "4ee9c363-1124-4113-890e-199d81b00281"]
pub struct CustomMaterial { pub struct CustomMaterial {
#[uniform(0)] #[uniform(0)]
color: Color, color: Color,
#[texture(1)]
#[sampler(2)]
color_texture: Option<Handle<Image>>,
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 { impl Material for CustomMaterial {
fn vertex_shader() -> ShaderRef { fn vertex_shader() -> ShaderRef {
"shaders/custom_material.vert".into() "shaders/custom_material.vert".into()
@ -59,6 +70,10 @@ impl Material for CustomMaterial {
"shaders/custom_material.frag".into() "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 // Bevy assumes by default that vertex shaders use the "vertex" entry point
// and fragment shaders use the "fragment" entry point (for WGSL shaders). // 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 // GLSL uses "main" as the entry point, so we must override the defaults here