three_d/renderer/material/
depth_material.rs

1use crate::core::*;
2use crate::renderer::*;
3
4///
5/// Used for rendering the distance from the camera to the object with this material in each pixel.
6/// Can be used for debug purposes but is also used to create shadow maps from light sources.
7///
8#[derive(Default, Clone)]
9pub struct DepthMaterial {
10    /// The minimum distance from the camera to any object. If None, then the near plane of the camera is used.
11    pub min_distance: Option<f32>,
12    /// The maximum distance from the camera to any object. If None, then the far plane of the camera is used.
13    pub max_distance: Option<f32>,
14    /// Render states.
15    pub render_states: RenderStates,
16}
17
18impl FromCpuMaterial for DepthMaterial {
19    fn from_cpu_material(_context: &Context, _cpu_material: &CpuMaterial) -> Self {
20        Self::default()
21    }
22}
23
24impl Material for DepthMaterial {
25    fn id(&self) -> EffectMaterialId {
26        EffectMaterialId::DepthMaterial
27    }
28
29    fn fragment_shader_source(&self, _lights: &[&dyn Light]) -> String {
30        include_str!("shaders/depth_material.frag").to_string()
31    }
32
33    fn use_uniforms(&self, program: &Program, viewer: &dyn Viewer, _lights: &[&dyn Light]) {
34        program.use_uniform(
35            "minDistance",
36            self.min_distance.unwrap_or_else(|| viewer.z_near()),
37        );
38        program.use_uniform(
39            "maxDistance",
40            self.max_distance.unwrap_or_else(|| viewer.z_far()),
41        );
42        program.use_uniform("eye", viewer.position());
43    }
44
45    fn render_states(&self) -> RenderStates {
46        self.render_states
47    }
48
49    fn material_type(&self) -> MaterialType {
50        MaterialType::Opaque
51    }
52}