ModelResolver

Struct ModelResolver 

Source
pub struct ModelResolver;
Expand description

Methods for resolving the properties of a Model with respect to its parents.

Implementations§

Source§

impl ModelResolver

Source

pub fn resolve_model<'a>( models: impl IntoIterator<Item = &'a Model> + Clone, ) -> Model

Iterates through a Model and all of its parents to resolve all of the model’s properties in a way that reflects the intended inheritance and/or override behavior of the Minecraft model format.

The method takes in an iterator of Models where the first element is the model being resolved, and the subsequent elements (if any) are the chain of parents of that model.

§Example
use maplit::hashmap;

use minecraft_assets::schemas::models::*;

let parent = Model {
    textures: Some(Textures::from(hashmap! {
        "up" => "#side",
        "down" => "#side"
    })),
    elements: Some(vec![
        Element {
            faces: hashmap! {
                BlockFace::Up => ElementFace {
                    texture: Texture::from("#up"),
                    ..Default::default()
                },
                BlockFace::Down => ElementFace {
                    texture: Texture::from("#down"),
                    ..Default::default()
                },
                BlockFace::East => ElementFace {
                    texture: Texture::from("#side"),
                    ..Default::default()
                },
                BlockFace::West => ElementFace {
                    texture: Texture::from("#side"),
                    ..Default::default()
                }
            },
            ..Default::default()
        }
    ]),
    ..Default::default()
};

let child = Model {
    textures: Some(Textures::from(hashmap! {
        "up" => "textures/up",
        "side" => "textures/side"
    })),
    ..Default::default()
};

let expected = Model {
    textures: Some(Textures::from(hashmap! {
        "up" => "textures/up",
        "down" => "textures/side",
        "side" => "textures/side"
    })),
    elements: Some(vec![
        Element {
            faces: hashmap! {
                BlockFace::Up => ElementFace {
                    texture: Texture::from("textures/up"),
                    ..Default::default()
                },
                BlockFace::Down => ElementFace {
                    texture: Texture::from("textures/side"),
                    ..Default::default()
                },
                BlockFace::East => ElementFace {
                    texture: Texture::from("textures/side"),
                    ..Default::default()
                },
                BlockFace::West => ElementFace {
                    texture: Texture::from("textures/side"),
                    ..Default::default()
                }
            },
            ..Default::default()
        }
    ]),
    ..Default::default()
};

let resolved = ModelResolver::resolve_model([&child, &parent].into_iter());

assert_eq!(resolved, expected);
Source

pub fn resolve_textures<'a>( models: impl IntoIterator<Item = &'a Model>, ) -> Textures

Iterates through a Model and all of its parents to resolve all of the model’s texture variables.

This works by merging together the Textures maps from all models in the parent-child chain, and then substituting texture variables with concrete values where possible.

§Example
use maplit::hashmap;

use minecraft_assets::schemas::models::{Model, Textures};

let child = Model {
    textures: Some(Textures::from(hashmap! {
        "child_texture" => "textures/child",
        "bar" => "#parent_texture"
    })),
    ..Default::default()
};

let parent = Model {
    textures: Some(Textures::from(hashmap! {
        "parent_texture" => "textures/parent",
        "foo" => "#child_texture"
    })),
    ..Default::default()
};

// Provide models in increasing level of parenthood.
let models = [child, parent];
let resolved = ModelResolver::resolve_textures(models.iter());

let expected = Textures::from(hashmap! {
    "parent_texture" => "textures/parent",
    "foo" => "textures/child",              // <------- resolved
    "child_texture" => "textures/child",
    "bar" => "textures/parent"              // <------- resolved    
});

assert_eq!(resolved, expected);
Source

pub fn resolve_elements<'a>( models: impl IntoIterator<Item = &'a Model>, ) -> Option<Vec<Element>>

Iterates through a Model and all of its parents to resolve the model’s cuboid Elements.

This works by taking the first set of elements present in the chain of parents. Unlike textures, child definitions for model elements completely override elements from the parent(s).

§Example
use minecraft_assets::schemas::models::{Model, Element};

let element1 = Element {
    from: [0.0, 0.0, 0.0],
    to: [1.0, 1.0, 1.0],
    ..Default::default()
};

let element2 = Element {
    from: [5.0, 6.0, 7.0],
    to: [4.0, 3.0, 2.0],
    ..Default::default()
};

let model1 = Model {
    elements: Some(vec![element1.clone()]),
    ..Default::default()
};

let model2 = Model {
    elements: Some(vec![element2.clone()]),
    ..Default::default()
};

let empty = Model::default();

let resolved = ModelResolver::resolve_elements([&empty, &model1].into_iter());
assert_eq!(resolved, Some(vec![element1.clone()]));

let resolved = ModelResolver::resolve_elements([&empty, &model2].into_iter());
assert_eq!(resolved, Some(vec![element2.clone()]));

let resolved = ModelResolver::resolve_elements([&model1, &model2].into_iter());
assert_eq!(resolved, Some(vec![element1.clone()]));

let resolved = ModelResolver::resolve_elements([&model2, &model1].into_iter());
assert_eq!(resolved, Some(vec![element2.clone()]));

let resolved = ModelResolver::resolve_elements([&empty, &empty].into_iter());
assert_eq!(resolved, None);
Source

pub fn resolve_element_textures<'a>( elements: impl IntoIterator<Item = &'a mut Element>, textures: &Textures, )

Iterates through each ElementFace in each Element and resolves any texture variables using the provided map.

Source

pub fn resolve_display<'a>( models: impl IntoIterator<Item = &'a Model>, ) -> Option<Display>

Iterates through a Model and all of its parents to resolve the model’s Display properties.

Similar to elements works by taking the first set of properties present in the chain of parents.

Source

pub fn resolve_ambient_occlusion<'a>( models: impl IntoIterator<Item = &'a Model>, ) -> Option<bool>

Iterates through a Model and all of its parents to resolve the model’s ambient occlusion setting.

Similar to elements works by taking the first property value present in the chain of parents.

Source

pub fn resolve_gui_light_mode<'a>( models: impl IntoIterator<Item = &'a Model>, ) -> Option<GuiLightMode>

Iterates through a Model and all of its parents to resolve the model’s GUI light mode setting.

Similar to elements works by taking the first property value present in the chain of parents.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.