use {json, texture, Gltf};
pub use json::material::AlphaMode;
lazy_static! {
static ref DEFAULT_MATERIAL: json::material::Material = Default::default();
}
pub struct Material<'a> {
gltf: &'a Gltf,
index: Option<usize>,
json: &'a json::material::Material,
}
impl<'a> Material<'a> {
pub(crate) fn new(
gltf: &'a Gltf,
index: usize,
json: &'a json::material::Material,
) -> Self {
Self {
gltf: gltf,
index: Some(index),
json: json,
}
}
pub(crate) fn default(gltf: &'a Gltf) -> Self {
Self {
gltf: gltf,
index: None,
json: &DEFAULT_MATERIAL,
}
}
pub fn index(&self) -> Option<usize> {
self.index
}
#[doc(hidden)]
pub fn as_json(&self) -> &json::material::Material {
self.json
}
pub fn alpha_cutoff(&self) -> f32 {
self.json.alpha_cutoff.0
}
pub fn alpha_mode(&self) -> AlphaMode {
self.json.alpha_mode.unwrap()
}
pub fn double_sided(&self) -> bool {
self.json.double_sided
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn pbr_metallic_roughness(&self) -> PbrMetallicRoughness<'a> {
PbrMetallicRoughness::new(self.gltf, &self.json.pbr_metallic_roughness)
}
pub fn normal_texture(&self) -> Option<NormalTexture<'a>> {
self.json.normal_texture.as_ref().map(|json| {
let texture = self.gltf.textures().nth(json.index.value()).unwrap();
NormalTexture::new(texture, json)
})
}
pub fn occlusion_texture(&self) -> Option<OcclusionTexture<'a>> {
self.json.occlusion_texture.as_ref().map(|json| {
let texture = self.gltf.textures().nth(json.index.value()).unwrap();
OcclusionTexture::new(texture, json)
})
}
pub fn emissive_texture(&self) -> Option<texture::Info<'a>> {
self.json.emissive_texture.as_ref().map(|json| {
let texture = self.gltf.textures().nth(json.index.value()).unwrap();
texture::Info::new(texture, json)
})
}
pub fn emissive_factor(&self) -> [f32; 3] {
self.json.emissive_factor.0
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
pub struct PbrMetallicRoughness<'a> {
gltf: &'a Gltf,
json: &'a json::material::PbrMetallicRoughness,
}
impl<'a> PbrMetallicRoughness<'a> {
pub(crate) fn new(
gltf: &'a Gltf,
json: &'a json::material::PbrMetallicRoughness,
) -> Self {
Self {
gltf: gltf,
json: json,
}
}
#[doc(hidden)]
pub fn as_json(&self) -> &json::material::PbrMetallicRoughness {
self.json
}
pub fn base_color_factor(&self) -> [f32; 4] {
self.json.base_color_factor.0
}
pub fn base_color_texture(&self) -> Option<texture::Info<'a>> {
self.json.base_color_texture.as_ref().map(|json| {
let texture = self.gltf.textures().nth(json.index.value()).unwrap();
texture::Info::new(texture, json)
})
}
pub fn metallic_factor(&self) -> f32 {
self.json.metallic_factor.0
}
pub fn roughness_factor(&self) -> f32 {
self.json.roughness_factor.0
}
pub fn metallic_roughness_texture(&self) -> Option<texture::Info<'a>> {
self.json.metallic_roughness_texture.as_ref().map(|json| {
let texture = self.gltf.textures().nth(json.index.value()).unwrap();
texture::Info::new(texture, json)
})
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
pub struct NormalTexture<'a> {
texture: texture::Texture<'a>,
json: &'a json::material::NormalTexture,
}
impl<'a> NormalTexture<'a> {
pub(crate) fn new(
texture: texture::Texture<'a>,
json: &'a json::material::NormalTexture,
) -> Self {
Self {
texture: texture,
json: json,
}
}
#[doc(hidden)]
pub fn as_json(&self) -> &json::material::NormalTexture {
self.json
}
pub fn scale(&self) -> f32 {
self.json.scale
}
pub fn tex_coord(&self) -> u32 {
self.json.tex_coord
}
pub fn texture(&self) -> texture::Texture<'a> {
self.texture.clone()
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
pub struct OcclusionTexture<'a> {
texture: texture::Texture<'a>,
json: &'a json::material::OcclusionTexture,
}
impl<'a> OcclusionTexture<'a> {
pub(crate) fn new(
texture: texture::Texture<'a>,
json: &'a json::material::OcclusionTexture,
) -> Self {
Self {
texture: texture,
json: json,
}
}
#[doc(hidden)]
pub fn as_json(&self) -> &json::material::OcclusionTexture {
self.json
}
pub fn strength(&self) -> f32 {
self.json.strength.0
}
pub fn tex_coord(&self) -> u32 {
self.json.tex_coord
}
pub fn texture(&self) -> texture::Texture<'a> {
self.texture.clone()
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> AsRef<texture::Texture<'a>> for NormalTexture<'a> {
fn as_ref(&self) -> &texture::Texture<'a> {
&self.texture
}
}
impl<'a> AsRef<texture::Texture<'a>> for OcclusionTexture<'a> {
fn as_ref(&self) -> &texture::Texture<'a> {
&self.texture
}
}