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
use crate::core::*;
use crate::renderer::*;
#[derive(Clone)]
pub struct IsosurfaceMaterial {
pub voxels: std::rc::Rc<Texture3D>,
pub threshold: f32,
pub color: Color,
pub metallic: f32,
pub roughness: f32,
pub size: Vec3,
pub lighting_model: LightingModel,
}
impl Material for IsosurfaceMaterial {
fn fragment_shader_source(&self, _use_vertex_colors: bool, lights: &[&dyn Light]) -> String {
let mut output = lights_shader_source(lights, self.lighting_model);
output.push_str(include_str!("shaders/isosurface_material.frag"));
output
}
fn use_uniforms(
&self,
program: &Program,
camera: &Camera,
lights: &[&dyn Light],
) -> ThreeDResult<()> {
for (i, light) in lights.iter().enumerate() {
light.use_uniforms(program, i as u32)?;
}
program.use_uniform("cameraPosition", camera.position())?;
program.use_uniform("surfaceColor", self.color)?;
program.use_uniform("metallic", self.metallic)?;
program.use_uniform_if_required("roughness", self.roughness)?;
program.use_uniform("size", self.size)?;
program.use_uniform("threshold", self.threshold)?;
program.use_uniform(
"h",
vec3(
1.0 / self.voxels.width() as f32,
1.0 / self.voxels.height() as f32,
1.0 / self.voxels.depth() as f32,
),
)?;
program.use_texture_3d("tex", &self.voxels)
}
fn render_states(&self) -> RenderStates {
RenderStates {
blend: Blend::TRANSPARENCY,
..Default::default()
}
}
fn is_transparent(&self) -> bool {
true
}
}
impl FromCpuVoxelGrid for IsosurfaceMaterial {
fn from_cpu_voxel_grid(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> ThreeDResult<Self> {
Ok(Self {
voxels: std::rc::Rc::new(Texture3D::new(&context, &cpu_voxel_grid.voxels)?),
lighting_model: LightingModel::Blinn,
size: cpu_voxel_grid.size,
threshold: 0.15,
color: Color::WHITE,
roughness: 1.0,
metallic: 0.0,
})
}
}