use crate::core::*;
use crate::renderer::*;
#[derive(Clone)]
pub struct IsosurfaceMaterial {
pub voxels: std::sync::Arc<Texture3D>,
pub threshold: f32,
pub color: Srgba,
pub metallic: f32,
pub roughness: f32,
pub size: Vec3,
pub lighting_model: LightingModel,
}
impl Material for IsosurfaceMaterial {
fn id(&self) -> u16 {
0b1u16 << 15 | 0b1100u16
}
fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String {
let mut source = lights_shader_source(lights, self.lighting_model);
source.push_str(ToneMapping::fragment_shader_source());
source.push_str(ColorMapping::fragment_shader_source());
source.push_str(include_str!("shaders/isosurface_material.frag"));
source
}
fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
position: true,
..FragmentAttributes::NONE
}
}
fn use_uniforms(&self, program: &Program, camera: &Camera, lights: &[&dyn Light]) {
camera.tone_mapping.use_uniforms(program);
camera.color_mapping.use_uniforms(program);
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.to_linear_srgb());
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 material_type(&self) -> MaterialType {
MaterialType::Transparent
}
}
impl FromCpuVoxelGrid for IsosurfaceMaterial {
fn from_cpu_voxel_grid(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> Self {
Self {
voxels: std::sync::Arc::new(Texture3D::new(context, &cpu_voxel_grid.voxels)),
lighting_model: LightingModel::Blinn,
size: cpu_voxel_grid.size,
threshold: 0.15,
color: Srgba::WHITE,
roughness: 1.0,
metallic: 0.0,
}
}
}