awsm-renderer 0.1.7

awsm-renderer
Documentation
//! glTF loader data containers.

use awsm_renderer_core::image::ImageData;

use super::{buffers::GltfBuffers, error::Result, loader::GltfLoader};

/// Loaded glTF document data with buffers and images.
pub struct GltfData {
    pub doc: gltf::Document,
    pub buffers: GltfBuffers,
    pub images: Vec<ImageData>,
    pub hints: GltfDataHints,
}

impl GltfData {
    /// Clones the document and backing buffers for independent use.
    pub fn heavy_clone(&self) -> Self {
        Self {
            doc: self.doc.clone(),
            buffers: self.buffers.heavy_clone(),
            images: self.images.clone(),
            hints: self.hints.clone(),
        }
    }
}

/// Optional hints used during glTF population.
#[derive(Default, Clone)]
pub struct GltfDataHints {
    pub hud: bool,
    pub hidden: bool,
    pub render_timings: bool,
}

impl GltfDataHints {
    /// Sets whether this data is for a HUD overlay.
    pub fn with_hud(mut self, hud: bool) -> Self {
        self.hud = hud;
        self
    }

    /// Sets whether this data is initially hidden.
    pub fn with_hidden(mut self, hidden: bool) -> Self {
        self.hidden = hidden;
        self
    }

    /// Sets whether glTF loading/population timing spans are emitted.
    pub fn with_render_timings(mut self, render_timings: bool) -> Self {
        self.render_timings = render_timings;
        self
    }
}

impl GltfLoader {
    /// Consumes the loader and returns a `GltfData` bundle.
    pub fn into_data(self, hints: Option<GltfDataHints>) -> Result<GltfData> {
        let hints = hints.unwrap_or_default();
        let buffers = GltfBuffers::new(&self.doc, self.buffers, hints.clone())?;

        Ok(GltfData {
            doc: self.doc,
            images: self.images,
            buffers,
            hints,
        })
    }
}