three_d/renderer/material/
isosurface_material.rs1use crate::core::*;
2use crate::renderer::*;
3
4#[derive(Clone)]
10pub struct IsosurfaceMaterial {
11 pub voxels: std::sync::Arc<Texture3D>,
13 pub threshold: f32,
15 pub color: Srgba,
17 pub metallic: f32,
19 pub roughness: f32,
21 pub size: Vec3,
23 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}