use crate::{image, Document};
pub use json::texture::{MagFilter, MinFilter, WrappingMode};
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};
lazy_static! {
static ref DEFAULT_SAMPLER: json::texture::Sampler = Default::default();
}
#[derive(Clone, Debug)]
pub struct Info<'a> {
texture: Texture<'a>,
json: &'a json::texture::Info,
}
#[derive(Clone, Debug)]
pub struct Sampler<'a> {
#[allow(dead_code)]
document: &'a Document,
index: Option<usize>,
json: &'a json::texture::Sampler,
}
#[derive(Clone, Debug)]
pub struct Texture<'a> {
document: &'a Document,
index: usize,
json: &'a json::texture::Texture,
}
impl<'a> Sampler<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Sampler,
) -> Self {
Self {
document,
index: Some(index),
json,
}
}
pub(crate) fn default(document: &'a Document) -> Self {
Self {
document,
index: None,
json: &DEFAULT_SAMPLER,
}
}
pub fn index(&self) -> Option<usize> {
self.index
}
pub fn mag_filter(&self) -> Option<MagFilter> {
self.json.mag_filter.map(|filter| filter.unwrap())
}
pub fn min_filter(&self) -> Option<MinFilter> {
self.json.min_filter.map(|filter| filter.unwrap())
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_deref()
}
pub fn wrap_s(&self) -> WrappingMode {
self.json.wrap_s.unwrap()
}
pub fn wrap_t(&self) -> WrappingMode {
self.json.wrap_t.unwrap()
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Texture<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Texture,
) -> Self {
Self {
document,
index,
json,
}
}
pub fn index(&self) -> usize {
self.index
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_deref()
}
pub fn sampler(&self) -> Sampler<'a> {
self.json
.sampler
.as_ref()
.map(|index| self.document.samplers().nth(index.value()).unwrap())
.unwrap_or_else(|| Sampler::default(self.document))
}
pub fn source(&self) -> image::Image<'a> {
self.document
.images()
.nth(self.json.source.value())
.unwrap()
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Info<'a> {
pub(crate) fn new(texture: Texture<'a>, json: &'a json::texture::Info) -> Self {
Self { texture, json }
}
pub fn tex_coord(&self) -> u32 {
self.json.tex_coord
}
pub fn texture(&self) -> Texture<'a> {
self.texture.clone()
}
#[cfg(feature = "KHR_texture_transform")]
#[cfg_attr(docsrs, doc(cfg(feature = "KHR_texture_transform")))]
pub fn texture_transform(&self) -> Option<TextureTransform<'a>> {
self.json
.extensions
.as_ref()?
.texture_transform
.as_ref()
.map(TextureTransform::new)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> AsRef<Texture<'a>> for Info<'a> {
fn as_ref(&self) -> &Texture<'a> {
&self.texture
}
}
#[cfg(feature = "KHR_texture_transform")]
pub struct TextureTransform<'a> {
json: &'a json::extensions::texture::TextureTransform,
}
#[cfg(feature = "KHR_texture_transform")]
impl<'a> TextureTransform<'a> {
pub(crate) fn new(json: &'a json::extensions::texture::TextureTransform) -> Self {
Self { json }
}
pub fn offset(&self) -> [f32; 2] {
self.json.offset.0
}
pub fn rotation(&self) -> f32 {
self.json.rotation.0
}
pub fn scale(&self) -> [f32; 2] {
self.json.scale.0
}
pub fn tex_coord(&self) -> Option<u32> {
self.json.tex_coord
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}