three_d/renderer/material/
isosurface_material.rs

1use crate::core::*;
2use crate::renderer::*;
3
4///
5/// A material that renders the isosurface defined by the voxel data in the [IsosurfaceMaterial::voxels] and the [IsosurfaceMaterial::threshold].
6/// The surface is defined by all the points in the volume where the red channel of the voxel data is equal to the threshold.
7/// This material should be applied to a cube with center in origo, for example [CpuMesh::cube].
8///
9#[derive(Clone)]
10pub struct IsosurfaceMaterial {
11    /// The voxel data that defines the isosurface.
12    pub voxels: std::sync::Arc<Texture3D>,
13    /// Threshold (in the range [0..1]) that defines the surface in the voxel data.
14    pub threshold: f32,
15    /// Base surface color.
16    pub color: Srgba,
17    /// A value in the range `[0..1]` specifying how metallic the surface is.
18    pub metallic: f32,
19    /// A value in the range `[0..1]` specifying how rough the surface is.
20    pub roughness: f32,
21    /// The size of the cube that is used to render the voxel data. The texture is scaled to fill the entire cube.
22    pub size: Vec3,
23    /// The lighting model used when rendering this material
24    pub lighting_model: LightingModel,
25}
26
27impl Material for IsosurfaceMaterial {
28    fn id(&self) -> EffectMaterialId {
29        EffectMaterialId::IsosurfaceMaterial
30    }
31
32    fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String {
33        let mut source = lights_shader_source(lights);
34        source.push_str(ToneMapping::fragment_shader_source());
35        source.push_str(ColorMapping::fragment_shader_source());
36        source.push_str(include_str!("shaders/isosurface_material.frag"));
37        source
38    }
39
40    fn use_uniforms(&self, program: &Program, viewer: &dyn Viewer, lights: &[&dyn Light]) {
41        program.use_uniform_if_required("lightingModel", lighting_model_to_id(self.lighting_model));
42        viewer.tone_mapping().use_uniforms(program);
43        viewer.color_mapping().use_uniforms(program);
44        for (i, light) in lights.iter().enumerate() {
45            light.use_uniforms(program, i as u32);
46        }
47        program.use_uniform("cameraPosition", viewer.position());
48        program.use_uniform("surfaceColor", self.color.to_linear_srgb());
49        program.use_uniform("metallic", self.metallic);
50        program.use_uniform_if_required("roughness", self.roughness);
51        program.use_uniform("size", self.size);
52        program.use_uniform("threshold", self.threshold);
53        program.use_uniform(
54            "h",
55            vec3(
56                1.0 / self.voxels.width() as f32,
57                1.0 / self.voxels.height() as f32,
58                1.0 / self.voxels.depth() as f32,
59            ),
60        );
61        program.use_texture_3d("tex", &self.voxels);
62    }
63    fn render_states(&self) -> RenderStates {
64        RenderStates {
65            blend: Blend::TRANSPARENCY,
66            ..Default::default()
67        }
68    }
69    fn material_type(&self) -> MaterialType {
70        MaterialType::Transparent
71    }
72}
73
74impl FromCpuVoxelGrid for IsosurfaceMaterial {
75    fn from_cpu_voxel_grid(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> Self {
76        Self {
77            voxels: std::sync::Arc::new(Texture3D::new(context, &cpu_voxel_grid.voxels)),
78            lighting_model: LightingModel::Blinn,
79            size: cpu_voxel_grid.size,
80            threshold: 0.15,
81            color: Srgba::WHITE,
82            roughness: 1.0,
83            metallic: 0.0,
84        }
85    }
86}