three_d/renderer/material/
intersection_material.rs

1use crate::core::*;
2use crate::renderer::*;
3
4///
5/// Used for intersection tests, see [pick] and [ray_intersect].
6/// When rendering with this material, the output in each pixel is:
7/// - Red channel: The depth (same as [DepthMaterial]).
8/// - Green channel: The [IntersectionMaterial::geometry_id].
9/// - Blue channel: The [gl_InstanceID](https://registry.khronos.org/OpenGL-Refpages/gl4/html/gl_InstanceID.xhtml).
10///
11/// Note: The geometry needs to pass the instance ID to the fragment shader, see [Geometry] for more information.
12///
13#[derive(Default, Clone)]
14pub struct IntersectionMaterial {
15    /// The minimum distance from the camera to any object. If None, then the near plane of the camera is used.
16    pub min_distance: Option<f32>,
17    /// The maximum distance from the camera to any object. If None, then the far plane of the camera is used.
18    pub max_distance: Option<f32>,
19    /// Render states.
20    pub render_states: RenderStates,
21    /// A geometry ID for the currently rendered geometry. The result is outputted in the green color channel.
22    pub geometry_id: u32,
23}
24
25impl FromCpuMaterial for IntersectionMaterial {
26    fn from_cpu_material(_context: &Context, _cpu_material: &CpuMaterial) -> Self {
27        Self::default()
28    }
29}
30
31impl Material for IntersectionMaterial {
32    fn id(&self) -> EffectMaterialId {
33        EffectMaterialId::IntersectionMaterial
34    }
35
36    fn fragment_shader_source(&self, _lights: &[&dyn Light]) -> String {
37        include_str!("shaders/intersection_material.frag").to_string()
38    }
39
40    fn use_uniforms(&self, program: &Program, viewer: &dyn Viewer, _lights: &[&dyn Light]) {
41        program.use_uniform(
42            "minDistance",
43            self.min_distance.unwrap_or_else(|| viewer.z_near()),
44        );
45        program.use_uniform(
46            "maxDistance",
47            self.max_distance.unwrap_or_else(|| viewer.z_far()),
48        );
49        program.use_uniform("eye", viewer.position());
50        program.use_uniform("geometryId", self.geometry_id);
51    }
52
53    fn render_states(&self) -> RenderStates {
54        self.render_states
55    }
56
57    fn material_type(&self) -> MaterialType {
58        MaterialType::Opaque
59    }
60}