gltf-reader 0.1.0

A simple glTF 2.0 reader using `serde` and `serde_json`
Documentation
use alloc::borrow::Cow;
use ownable::IntoOwned;
use serde::Deserialize;

use crate::{Extensions, Extras};

/// glTF known types of camera projection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CameraTypeEnum {
    Perspective,
    Orthographic,
}

/// Type of camera projection.
#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
#[serde(transparent)]
pub struct CameraType<'a>(#[serde(borrow)] pub Cow<'a, str>);

impl core::fmt::Debug for CameraType<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(e) = self.to_enum() {
            e.fmt(f)
        } else {
            self.0.fmt(f)
        }
    }
}

impl CameraType<'_> {
    pub const PERSPECTIVE: Self = Self(Cow::Borrowed("perspective"));
    pub const ORTHOGRAPHIC: Self = Self(Cow::Borrowed("orthographic"));

    pub fn to_enum(&self) -> Option<CameraTypeEnum> {
        if *self == Self::PERSPECTIVE {
            return Some(CameraTypeEnum::Perspective);
        } else if *self == Self::ORTHOGRAPHIC {
            return Some(CameraTypeEnum::Orthographic);
        }

        None
    }
}

/// A perspective camera containing properties to create a perspective projection matrix.
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Perspective<'a> {
    /// The floating-point aspect ratio of the field of view.
    #[serde(rename = "aspectRatio")]
    pub aspect_ratio: Option<f32>,
    /// The floating-point vertical field of view in radians.
    pub yfov: f32,
    /// The floating-point distance to the far clipping plane.
    pub zfar: Option<f32>,
    /// The floating-point distance to the near clipping plane.
    pub znear: f32,

    #[serde(borrow)]
    pub extensions: Option<Extensions<'a>>,
    #[serde(borrow)]
    pub extras: Option<Extras<'a>>,
}

/// An orthographic camera containing properties to create an orthographic projection matrix.
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Orthographic<'a> {
    /// The floating-point horizontal magnification of the view.
    pub xmag: f32,
    /// The floating-point vertical magnification of the view.
    pub ymag: f32,
    /// The floating-point distance to the far clipping plane.
    pub zfar: f32,
    /// The floating-point distance to the near clipping plane.
    pub znear: f32,

    #[serde(borrow)]
    pub extensions: Option<Extensions<'a>>,
    #[serde(borrow)]
    pub extras: Option<Extras<'a>>,
}

/// A camera's projection.
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Camera<'a> {
    /// The user-defined name of this object.
    #[serde(borrow)]
    pub name: Option<Cow<'a, str>>,

    /// Specifies if the camera uses a perspective or orthographic projection.
    #[serde(borrow)]
    pub ty: CameraType<'a>,
    /// A perspective camera containing properties to create a perspective projection matrix.
    #[serde(borrow)]
    pub perspective: Option<Perspective<'a>>,
    /// An orthographic camera containing properties to create an orthographic projection matrix.
    #[serde(borrow)]
    pub orthographic: Option<Orthographic<'a>>,

    #[serde(borrow)]
    pub extensions: Option<Extensions<'a>>,
    #[serde(borrow)]
    pub extras: Option<Extras<'a>>,
}