Skip to content

Commit

Permalink
Materials and MaterialPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Dec 24, 2021
1 parent 79d36e7 commit 9a7ae2e
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 789 deletions.
1 change: 0 additions & 1 deletion crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ bevy_window = { path = "../bevy_window", version = "0.5.0" }
bitflags = "1.2"
# direct dependency required for derive macro
bytemuck = { version = "1", features = ["derive"] }
wgpu = { version = "0.12.0", features = ["spirv"] }
13 changes: 8 additions & 5 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, StandardMaterial, DEFAULT_STANDARD_MATERIAL_HANDLE};
use crate::{DirectionalLight, PointLight, SpecializedMaterial, StandardMaterial};
use bevy_asset::Handle;
use bevy_ecs::{bundle::Bundle, component::Component};
use bevy_render::{
Expand All @@ -9,10 +9,13 @@ use bevy_render::{
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 [`Material`].
#[derive(Bundle, Clone)]
pub struct PbrBundle {
pub struct MaterialMeshBundle<M: SpecializedMaterial> {
pub mesh: Handle<Mesh>,
pub material: Handle<StandardMaterial>,
pub material: Handle<M>,
pub transform: Transform,
pub global_transform: GlobalTransform,
/// User indication of whether an entity is visible
Expand All @@ -21,11 +24,11 @@ pub struct PbrBundle {
pub computed_visibility: ComputedVisibility,
}

impl Default for PbrBundle {
impl<M: SpecializedMaterial> Default for MaterialMeshBundle<M> {
fn default() -> Self {
Self {
mesh: Default::default(),
material: DEFAULT_STANDARD_MATERIAL_HANDLE.typed(),
material: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
Expand Down
20 changes: 8 additions & 12 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ mod alpha;
mod bundle;
mod light;
mod material;
mod pbr_material;
mod render;

pub use alpha::*;
pub use bundle::*;
pub use light::*;
pub use material::*;
pub use pbr_material::*;
pub use render::*;

pub mod prelude {
#[doc(hidden)]
pub use crate::{
alpha::AlphaMode,
bundle::{DirectionalLightBundle, PbrBundle, PointLightBundle},
bundle::{DirectionalLightBundle, MaterialMeshBundle, PbrBundle, PointLightBundle},
light::{AmbientLight, DirectionalLight, PointLight},
material::StandardMaterial,
material::{Material, MaterialPlugin},
pbr_material::StandardMaterial,
};
}

Expand All @@ -31,7 +34,6 @@ pub mod draw_3d_graph {

use bevy_app::prelude::*;
use bevy_asset::{Assets, Handle, HandleUntyped};
use bevy_core_pipeline::{AlphaMask3d, Opaque3d, Transparent3d};
use bevy_ecs::prelude::*;
use bevy_reflect::TypeUuid;
use bevy_render::{
Expand Down Expand Up @@ -66,8 +68,8 @@ impl Plugin for PbrPlugin {
Shader::from_wgsl(include_str!("render/depth.wgsl")),
);

app.add_plugin(StandardMaterialPlugin)
.add_plugin(MeshRenderPlugin)
app.add_plugin(MeshRenderPlugin)
.add_plugin(MaterialPlugin::<StandardMaterial>::default())
.add_plugin(ExtractComponentPlugin::<Handle<StandardMaterial>>::default())
.init_resource::<AmbientLight>()
.init_resource::<DirectionalLightShadowMap>()
Expand Down Expand Up @@ -127,7 +129,7 @@ impl Plugin for PbrPlugin {
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap()
.set_untracked(
DEFAULT_STANDARD_MATERIAL_HANDLE,
Handle::<StandardMaterial>::default(),
StandardMaterial {
base_color: Color::rgb(1.0, 0.0, 0.5),
unlit: true,
Expand Down Expand Up @@ -166,21 +168,15 @@ impl Plugin for PbrPlugin {
RenderStage::Queue,
render::queue_shadows.label(RenderLightSystems::QueueShadows),
)
.add_system_to_stage(RenderStage::Queue, queue_meshes)
.add_system_to_stage(RenderStage::Queue, render::queue_shadow_view_bind_group)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Shadow>)
.init_resource::<PbrPipeline>()
.init_resource::<ShadowPipeline>()
.init_resource::<DrawFunctions<Shadow>>()
.init_resource::<LightMeta>()
.init_resource::<GlobalLightMeta>()
.init_resource::<SpecializedPipelines<PbrPipeline>>()
.init_resource::<SpecializedPipelines<ShadowPipeline>>();

let shadow_pass_node = ShadowPassNode::new(&mut render_app.world);
render_app.add_render_command::<Opaque3d, DrawPbr>();
render_app.add_render_command::<AlphaMask3d, DrawPbr>();
render_app.add_render_command::<Transparent3d, DrawPbr>();
render_app.add_render_command::<Shadow, DrawShadowMesh>();
let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap();
let draw_3d_graph = graph
Expand Down
Loading

0 comments on commit 9a7ae2e

Please sign in to comment.