nightshade-api 0.47.0

Procedural high level API for the nightshade game engine
Documentation
//! Morph target (blend shape) weights for imported models. A glTF model with
//! blend shapes gets a `MorphWeights` component on import, and these drive it at
//! runtime: facial expressions, muscle flex, anything authored as a shape key.

use nightshade::prelude::*;

/// Sets one morph target's weight on an entity by index. A weight of 0.0 is the
/// base mesh, 1.0 is the full target. Does nothing if the entity has no morph
/// targets or the index is out of range.
pub fn set_morph_weight(world: &mut World, entity: Entity, index: u32, weight: f32) {
    if let Some(weights) = world.core.get_morph_weights_mut(entity) {
        weights.set_weight(index as usize, weight);
    }
}

/// Reads one morph target's weight by index, or 0.0 if absent.
pub fn morph_weight(world: &World, entity: Entity, index: u32) -> f32 {
    world
        .core
        .get_morph_weights(entity)
        .map(|weights| weights.get_weight(index as usize))
        .unwrap_or(0.0)
}

/// The number of morph targets the entity has.
pub fn morph_target_count(world: &World, entity: Entity) -> usize {
    world
        .core
        .get_morph_weights(entity)
        .map(|weights| weights.weight_count())
        .unwrap_or(0)
}

/// Sets every morph weight at once, in target order.
pub fn set_morph_weights(world: &mut World, entity: Entity, weights: &[f32]) {
    if let Some(component) = world.core.get_morph_weights_mut(entity) {
        for (index, value) in weights.iter().enumerate() {
            component.set_weight(index, *value);
        }
    }
}