Skip to content

Commit

Permalink
Add KHR_materials_clearcoat support
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-ben committed May 7, 2024
1 parent a0236f7 commit 3e15a27
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 64 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ gltf = "1.3"
egui = "0.27"
egui-winit = "0.27"
egui-ash-renderer = { version = "0.3.0", features = ["dynamic-rendering"]}

[patch.crates-io.gltf]
git = "https://github.com/adrien-ben/gltf"
branch = "missing_extensions"
3 changes: 2 additions & 1 deletion crates/libs/model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ features = [
"KHR_lights_punctual",
"KHR_materials_unlit",
"KHR_materials_pbrSpecularGlossiness",
"KHR_materials_emissive_strength"
"KHR_materials_emissive_strength",
"KHR_materials_clearcoat",
]
57 changes: 57 additions & 0 deletions crates/libs/model/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Material {
alpha_cutoff: f32,
double_sided: bool,
is_unlit: bool,
clearcoat: Option<Clearcoat>,
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -85,6 +86,49 @@ impl SpecularGlossinessWorkflow {
}
}

#[derive(Clone, Copy, Debug, Default)]
pub struct Clearcoat {
factor: f32,
roughness: f32,
factor_texture: Option<TextureInfo>,
roughness_texture: Option<TextureInfo>,
normal_texture: Option<TextureInfo>,
}

impl Clearcoat {
pub fn factor(&self) -> f32 {
self.factor
}

pub fn roughness(&self) -> f32 {
self.roughness
}

pub fn factor_texture(&self) -> Option<TextureInfo> {
self.factor_texture
}

pub fn factor_texture_index(&self) -> Option<usize> {
self.factor_texture.map(|info| info.index)
}

pub fn roughness_texture(&self) -> Option<TextureInfo> {
self.roughness_texture
}

pub fn roughness_texture_index(&self) -> Option<usize> {
self.roughness_texture.map(|info| info.index)
}

pub fn normal_texture(&self) -> Option<TextureInfo> {
self.normal_texture
}

pub fn normal_texture_index(&self) -> Option<usize> {
self.normal_texture.map(|info| info.index)
}
}

impl Material {
pub fn get_color(&self) -> [f32; 4] {
self.color
Expand Down Expand Up @@ -150,6 +194,10 @@ impl Material {
self.is_unlit
}

pub fn get_clearcoat(&self) -> Option<Clearcoat> {
self.clearcoat
}

pub fn get_workflow(&self) -> Workflow {
self.workflow
}
Expand Down Expand Up @@ -212,6 +260,14 @@ impl<'a> From<GltfMaterial<'a>> for Material {

let is_unlit = material.unlit();

let clearcoat = material.clearcoat().map(|m| Clearcoat {
factor: m.clearcoat_factor(),
roughness: m.clearcoat_roughness_factor(),
factor_texture: get_texture(m.clearcoat_texture()),
roughness_texture: get_texture(m.clearcoat_roughness_texture()),
normal_texture: get_texture(m.clearcoat_normal_texture()),
});

Material {
color,
emissive,
Expand All @@ -225,6 +281,7 @@ impl<'a> From<GltfMaterial<'a>> for Material {
alpha_cutoff,
double_sided,
is_unlit,
clearcoat,
}
}
}
Expand Down
Loading

0 comments on commit 3e15a27

Please sign in to comment.