Skip to content

Commit 1fba323

Browse files
committed
add docs
1 parent 9a7ae2e commit 1fba323

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

crates/bevy_pbr/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bevy_transform::components::{GlobalTransform, Transform};
1111
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
1212
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;
1313

14-
/// A component bundle for entities with a [`Mesh`] and a [`Material`].
14+
/// A component bundle for entities with a [`Mesh`] and a [`SpecializedMaterial`].
1515
#[derive(Bundle, Clone)]
1616
pub struct MaterialMeshBundle<M: SpecializedMaterial> {
1717
pub mesh: Handle<Mesh>,

crates/bevy_pbr/src/material.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,33 @@ use bevy_render::{
3333
use std::hash::Hash;
3434
use std::marker::PhantomData;
3535

36+
/// Materials are used alongside [`MaterialPlugin`] and [`MaterialMeshBundle`](crate::MaterialMeshBundle)
37+
/// to spawn entities that are rendered with a specific [`Material`] type. They serve as an easy to use high level
38+
/// way to render [`Mesh`] entities with custom shader logic. For materials that can specialize their [`RenderPipelineDescriptor`]
39+
/// based on specific material values, see [`SpecializedMaterial`]. [`Material`] automatically implements [`SpecializedMaterial`]
40+
/// and can be used anywhere that type is used (such as [`MaterialPlugin`]).
3641
pub trait Material: Asset + RenderAsset {
42+
/// Returns this material's [`BindGroup`]. This should match the layout returned by [`Material::bind_group_layout`].
3743
fn bind_group(render_asset: &<Self as RenderAsset>::PreparedAsset) -> &BindGroup;
44+
45+
/// Returns this material's [`BindGroupLayout`]. This should match the [`BindGroup`] returned by [`Material::bind_group`].
3846
fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout;
47+
48+
/// Returns this material's vertex shader. If [`None`] is returned, the default mesh vertex shader will be used.
49+
/// Defaults to [`None`].
3950
#[allow(unused_variables)]
4051
fn vertex_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
4152
None
4253
}
54+
55+
/// Returns this material's fragment shader. If [`None`] is returned, the default mesh fragment shader will be used.
56+
/// Defaults to [`None`].
4357
#[allow(unused_variables)]
4458
fn fragment_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
4559
None
4660
}
61+
62+
/// Returns this material's [`AlphaMode`]. Defaults to [`AlphaMode::Opaque`].
4763
#[allow(unused_variables)]
4864
fn alpha_mode(render_asset: &<Self as RenderAsset>::PreparedAsset) -> AlphaMode {
4965
AlphaMode::Opaque
@@ -85,26 +101,52 @@ impl<M: Material> SpecializedMaterial for M {
85101
}
86102
}
87103

104+
/// Materials are used alongside [`MaterialPlugin`] and [`MaterialMeshBundle`](crate::MaterialMeshBundle)
105+
/// to spawn entities that are rendered with a specific [`SpecializedMaterial`] type. They serve as an easy to use high level
106+
/// way to render [`Mesh`] entities with custom shader logic. [`SpecializedMaterials`](SpecializedMaterial) use their [`SpecializedMaterial::Key`]
107+
/// to customize their [`RenderPipelineDescriptor`] based on specific material values. The slightly simpler [`Material`] trait
108+
/// should be used for materials that do not need specialization. [`Material`] types automatically implement [`SpecializedMaterial`].
88109
pub trait SpecializedMaterial: Asset + RenderAsset {
110+
/// The key used to specialize this material's [`RenderPipelineDescriptor`].
89111
type Key: PartialEq + Eq + Hash + Clone + Send + Sync;
112+
113+
/// Extract the [`SpecializedMaterial::Key`] for the "prepared" version of this material. This key will be
114+
/// passed in to the [`SpecializedMaterial::specialize`] function when compiling the [`RenderPipeline`](bevy_render::render_resource::RenderPipeline)
115+
/// for a given entity's material.
90116
fn key(render_asset: &<Self as RenderAsset>::PreparedAsset) -> Self::Key;
117+
118+
/// Specializes the given `descriptor` according to the given `key`.
91119
fn specialize(key: Self::Key, descriptor: &mut RenderPipelineDescriptor);
120+
121+
/// Returns this material's [`BindGroup`]. This should match the layout returned by [`SpecializedMaterial::bind_group_layout`].
92122
fn bind_group(render_asset: &<Self as RenderAsset>::PreparedAsset) -> &BindGroup;
123+
124+
/// Returns this material's [`BindGroupLayout`]. This should match the [`BindGroup`] returned by [`SpecializedMaterial::bind_group`].
93125
fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout;
126+
127+
/// Returns this material's vertex shader. If [`None`] is returned, the default mesh vertex shader will be used.
128+
/// Defaults to [`None`].
94129
#[allow(unused_variables)]
95130
fn vertex_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
96131
None
97132
}
133+
134+
/// Returns this material's fragment shader. If [`None`] is returned, the default mesh fragment shader will be used.
135+
/// Defaults to [`None`].
98136
#[allow(unused_variables)]
99137
fn fragment_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
100138
None
101139
}
140+
141+
/// Returns this material's [`AlphaMode`]. Defaults to [`AlphaMode::Opaque`].
102142
#[allow(unused_variables)]
103143
fn alpha_mode(render_asset: &<Self as RenderAsset>::PreparedAsset) -> AlphaMode {
104144
AlphaMode::Opaque
105145
}
106146
}
107147

148+
/// Adds the necessary ECS resources and render logic to enable rendering entities using the given [`SpecializedMaterial`]
149+
/// asset type (which includes [`Material`] types).
108150
pub struct MaterialPlugin<M: SpecializedMaterial>(PhantomData<M>);
109151

110152
impl<M: SpecializedMaterial> Default for MaterialPlugin<M> {

0 commit comments

Comments
 (0)