1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Materials used to shade objects.
use std::sync::Arc;
use wgpu::*;
use super::render_pipeline_manager::{RenderPipelineInfo, RenderPipelineManager};
use super::texture::Texture;
/// Shading material to be applied during rendering.
pub struct Material {
pipeline: Arc<RenderPipeline>,
texture: Option<Arc<Texture>>,
}
impl Material {
/// Get the underlying render pipeline used.
pub fn pipeline(&self) -> &Arc<RenderPipeline> {
&self.pipeline
}
/// Get the texture used by the material (if any).
pub fn texture(&self) -> &Option<Arc<Texture>> {
&self.texture
}
/// Create a custom material.
///
/// # Arguments
///
/// * `pipeline_manager` - Controls where the pipeline should be created.
/// * `shader` - WGSL shader to use.
/// * `label` - Optional label to identify the shader.
/// * `texture` - Optional texture to bind to the shader.
pub fn custom(
pipeline_manager: &mut RenderPipelineManager,
shader: &'static str,
label: Option<&'static str>,
texture: Option<Arc<Texture>>,
) -> Self {
Self {
pipeline: pipeline_manager.pipeline(RenderPipelineInfo {
shader,
label,
has_texture: texture.is_some(),
}),
texture,
}
}
/// Create a textured material.
///
/// # Arguments
///
/// * `pipeline_manager` - Controls where the pipeline should be created.
/// * `texture` - Texture to bind to the shader.
pub fn textured(pipeline_manager: &mut RenderPipelineManager, texture: Arc<Texture>) -> Self {
Self {
pipeline: pipeline_manager.pipeline(RenderPipelineInfo {
shader: include_str!("shaders/textured.wgsl"),
label: Some("textured"),
has_texture: true,
}),
texture: Some(texture),
}
}
/// Create a line renderer material.
///
/// # Arguments
///
/// * `pipeline_manager` - Controls where the pipeline should be created.
///
/// # Remarks
///
/// Note that this material expects a non-standard transformation matrix. The first row of the
/// matrix defines starting position of the line, the second row defines ending position, and
/// the first element of the third row defines line thickness.
pub fn line(pipeline_manager: &mut RenderPipelineManager) -> Self {
Self {
pipeline: pipeline_manager.pipeline(RenderPipelineInfo {
shader: include_str!("shaders/line.wgsl"),
label: Some("line"),
has_texture: false,
}),
texture: None,
}
}
/// Create a untextured and unlit material.
///
/// # Arguments
///
/// * `pipeline_manager` - Controls where the pipeline should be created.
pub fn unlit(pipeline_manager: &mut RenderPipelineManager) -> Self {
Self {
pipeline: pipeline_manager.pipeline(RenderPipelineInfo {
shader: include_str!("shaders/unlit.wgsl"),
label: Some("unlit"),
has_texture: false,
}),
texture: None,
}
}
}