Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Better Materials: AsBindGroup trait and derive, simpler Material trait #5053

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions assets/shaders/custom_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
struct CustomMaterial {
color: vec4<f32>;
};

[[group(1), binding(0)]]
var<uniform> material: CustomMaterial;
[[group(1), binding(1)]]
var base_color_texture: texture_2d<f32>;
[[group(1), binding(2)]]
var base_color_sampler: sampler;

[[stage(fragment)]]
fn fragment() -> [[location(0)]] vec4<f32> {
return material.color;
fn fragment([[location(2)]] uv: vec2<f32>) -> [[location(0)]] vec4<f32> {
return material.color * textureSample(base_color_texture, base_color_sampler, uv);
}
39 changes: 10 additions & 29 deletions assets/shaders/shader_defs.wgsl
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_view_bindings

[[group(1), binding(0)]]
var<uniform> mesh: Mesh;

// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions

struct Vertex {
[[location(0)]] position: vec3<f32>;
[[location(1)]] normal: vec3<f32>;
[[location(2)]] uv: vec2<f32>;
struct CustomMaterial {
color: vec4<f32>;
};

struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
};

[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh_position_local_to_clip(mesh.model, vec4<f32>(vertex.position, 1.0));
return out;
}
[[group(1), binding(0)]]
var<uniform> material: CustomMaterial;

[[stage(fragment)]]
fn fragment() -> [[location(0)]] vec4<f32> {
var color = vec4<f32>(0.0, 0.0, 1.0, 1.0);
# ifdef IS_RED
color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
# endif
return color;
fn fragment([[location(2)]] uv: vec2<f32>) -> [[location(0)]] vec4<f32> {
cart marked this conversation as resolved.
Show resolved Hide resolved
#ifdef IS_RED
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
#else
return material.color;
#endif
}
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DirectionalLight, PointLight, SpecializedMaterial, StandardMaterial};
use crate::{DirectionalLight, Material, PointLight, StandardMaterial};
use bevy_asset::Handle;
use bevy_ecs::{bundle::Bundle, component::Component, reflect::ReflectComponent};
use bevy_reflect::Reflect;
Expand All @@ -12,9 +12,9 @@ use bevy_transform::components::{GlobalTransform, Transform};
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;

/// A component bundle for entities with a [`Mesh`] and a [`SpecializedMaterial`].
/// A component bundle for entities with a [`Mesh`] and a [`Material`].
#[derive(Bundle, Clone)]
pub struct MaterialMeshBundle<M: SpecializedMaterial> {
pub struct MaterialMeshBundle<M: Material> {
pub mesh: Handle<Mesh>,
pub material: Handle<M>,
pub transform: Transform,
Expand All @@ -25,7 +25,7 @@ pub struct MaterialMeshBundle<M: SpecializedMaterial> {
pub computed_visibility: ComputedVisibility,
}

impl<M: SpecializedMaterial> Default for MaterialMeshBundle<M> {
impl<M: Material> Default for MaterialMeshBundle<M> {
fn default() -> Self {
Self {
mesh: Default::default(),
Expand Down
Loading