use crate::core::*;
use crate::renderer::*;
#[derive(Clone)]
pub struct ORMMaterial {
pub metallic: f32,
pub roughness: f32,
pub metallic_roughness_texture: Option<Texture2DRef>,
pub occlusion_strength: f32,
pub occlusion_texture: Option<Texture2DRef>,
pub render_states: RenderStates,
}
impl ORMMaterial {
pub fn new(context: &Context, cpu_material: &CpuMaterial) -> Self {
let metallic_roughness_texture =
if let Some(ref cpu_texture) = cpu_material.occlusion_metallic_roughness_texture {
Some(Texture2DRef::from_cpu_texture(context, cpu_texture))
} else {
cpu_material
.metallic_roughness_texture
.as_ref()
.map(|cpu_texture| Texture2DRef::from_cpu_texture(context, cpu_texture))
};
let occlusion_texture = if cpu_material.occlusion_metallic_roughness_texture.is_some() {
metallic_roughness_texture.clone()
} else {
cpu_material
.occlusion_texture
.as_ref()
.map(|cpu_texture| Texture2DRef::from_cpu_texture(context, cpu_texture))
};
Self {
metallic: cpu_material.metallic,
roughness: cpu_material.roughness,
metallic_roughness_texture,
occlusion_texture,
occlusion_strength: cpu_material.occlusion_strength,
render_states: RenderStates::default(),
}
}
pub fn from_physical_material(physical_material: &PhysicalMaterial) -> Self {
Self {
metallic: physical_material.metallic,
roughness: physical_material.roughness,
metallic_roughness_texture: physical_material.metallic_roughness_texture.clone(),
occlusion_strength: physical_material.occlusion_strength,
occlusion_texture: physical_material.occlusion_texture.clone(),
render_states: RenderStates {
write_mask: WriteMask::default(),
blend: Blend::Disabled,
..physical_material.render_states
},
}
}
}
impl FromCpuMaterial for ORMMaterial {
fn from_cpu_material(context: &Context, cpu_material: &CpuMaterial) -> Self {
Self::new(context, cpu_material)
}
}
impl Material for ORMMaterial {
fn id(&self) -> u16 {
let mut id = 0b1u16 << 15 | 0b1u16 << 4;
if self.metallic_roughness_texture.is_some() {
id |= 0b1u16;
}
if self.occlusion_texture.is_some() {
id |= 0b1u16 << 1;
}
id
}
fn fragment_shader_source(&self, _lights: &[&dyn Light]) -> String {
let mut source = String::new();
if self.metallic_roughness_texture.is_some() || self.occlusion_texture.is_some() {
source.push_str("in vec2 uvs;\n");
if self.metallic_roughness_texture.is_some() {
source.push_str("#define USE_METALLIC_ROUGHNESS_TEXTURE;\n");
}
if self.occlusion_texture.is_some() {
source.push_str("#define USE_OCCLUSION_TEXTURE;\n");
}
}
source.push_str(include_str!("shaders/orm_material.frag"));
source
}
fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: self.metallic_roughness_texture.is_some() || self.occlusion_texture.is_some(),
..FragmentAttributes::NONE
}
}
fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
program.use_uniform("metallic", self.metallic);
program.use_uniform("roughness", self.roughness);
if let Some(ref texture) = self.metallic_roughness_texture {
program.use_texture("metallicRoughnessTexture", texture);
program.use_uniform("metallicRoughnessTexTransform", texture.transformation);
}
if let Some(ref texture) = self.occlusion_texture {
program.use_uniform("occlusionStrength", self.occlusion_strength);
program.use_texture("occlusionTexture", texture);
program.use_uniform("occlusionTexTransform", texture.transformation);
}
}
fn render_states(&self) -> RenderStates {
self.render_states
}
fn material_type(&self) -> MaterialType {
MaterialType::Opaque
}
}
impl Default for ORMMaterial {
fn default() -> Self {
Self {
metallic: 0.0,
roughness: 1.0,
metallic_roughness_texture: None,
occlusion_texture: None,
occlusion_strength: 1.0,
render_states: RenderStates::default(),
}
}
}