@@ -33,17 +33,33 @@ use bevy_render::{
33
33
use std:: hash:: Hash ;
34
34
use std:: marker:: PhantomData ;
35
35
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`]).
36
41
pub trait Material : Asset + RenderAsset {
42
+ /// Returns this material's [`BindGroup`]. This should match the layout returned by [`Material::bind_group_layout`].
37
43
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`].
38
46
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`].
39
50
#[ allow( unused_variables) ]
40
51
fn vertex_shader ( asset_server : & AssetServer ) -> Option < Handle < Shader > > {
41
52
None
42
53
}
54
+
55
+ /// Returns this material's fragment shader. If [`None`] is returned, the default mesh fragment shader will be used.
56
+ /// Defaults to [`None`].
43
57
#[ allow( unused_variables) ]
44
58
fn fragment_shader ( asset_server : & AssetServer ) -> Option < Handle < Shader > > {
45
59
None
46
60
}
61
+
62
+ /// Returns this material's [`AlphaMode`]. Defaults to [`AlphaMode::Opaque`].
47
63
#[ allow( unused_variables) ]
48
64
fn alpha_mode ( render_asset : & <Self as RenderAsset >:: PreparedAsset ) -> AlphaMode {
49
65
AlphaMode :: Opaque
@@ -85,26 +101,52 @@ impl<M: Material> SpecializedMaterial for M {
85
101
}
86
102
}
87
103
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`].
88
109
pub trait SpecializedMaterial : Asset + RenderAsset {
110
+ /// The key used to specialize this material's [`RenderPipelineDescriptor`].
89
111
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.
90
116
fn key ( render_asset : & <Self as RenderAsset >:: PreparedAsset ) -> Self :: Key ;
117
+
118
+ /// Specializes the given `descriptor` according to the given `key`.
91
119
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`].
92
122
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`].
93
125
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`].
94
129
#[ allow( unused_variables) ]
95
130
fn vertex_shader ( asset_server : & AssetServer ) -> Option < Handle < Shader > > {
96
131
None
97
132
}
133
+
134
+ /// Returns this material's fragment shader. If [`None`] is returned, the default mesh fragment shader will be used.
135
+ /// Defaults to [`None`].
98
136
#[ allow( unused_variables) ]
99
137
fn fragment_shader ( asset_server : & AssetServer ) -> Option < Handle < Shader > > {
100
138
None
101
139
}
140
+
141
+ /// Returns this material's [`AlphaMode`]. Defaults to [`AlphaMode::Opaque`].
102
142
#[ allow( unused_variables) ]
103
143
fn alpha_mode ( render_asset : & <Self as RenderAsset >:: PreparedAsset ) -> AlphaMode {
104
144
AlphaMode :: Opaque
105
145
}
106
146
}
107
147
148
+ /// Adds the necessary ECS resources and render logic to enable rendering entities using the given [`SpecializedMaterial`]
149
+ /// asset type (which includes [`Material`] types).
108
150
pub struct MaterialPlugin < M : SpecializedMaterial > ( PhantomData < M > ) ;
109
151
110
152
impl < M : SpecializedMaterial > Default for MaterialPlugin < M > {
0 commit comments