vrm-runtime 0.1.0

Renderer-agnostic VRM 1.0 loader, validator, animation system, and runtime
Documentation
use vrm_runtime::{
    Expression, Expressions, MaterialColorBind, MaterialColorType, MorphTargetBind,
    TextureTransformBind,
};

#[test]
fn finds_expression_by_name() {
    let expression =
        Expression::new("happy").with_morph_target_binds(vec![MorphTargetBind::new(2, 0, 0.8)]);
    let expressions = Expressions::new(vec![expression]);

    let happy = expressions.get("happy").unwrap();
    assert_eq!(happy.name(), "happy");
    assert_eq!(happy.morph_target_binds()[0].node_index(), 2);
    assert_eq!(happy.morph_target_binds()[0].target_index(), 0);
    assert_eq!(happy.morph_target_binds()[0].weight(), 0.8);
}

#[test]
fn evaluates_and_clamps_a_morph_bind() {
    let expression =
        Expression::new("happy").with_morph_target_binds(vec![MorphTargetBind::new(2, 0, 1.0)]);

    assert_eq!(expression.evaluate(0.7).morph_updates[0].weight(), 0.7);
    assert_eq!(expression.evaluate(-1.0).morph_updates[0].weight(), 0.0);
    assert_eq!(expression.evaluate(2.0).morph_updates[0].weight(), 1.0);
    assert_eq!(expression.evaluate(f32::NAN).morph_updates[0].weight(), 0.0);
}

#[test]
fn public_evaluation_applies_binary_thresholding() {
    let expression = Expression::new("blink")
        .with_binary(true)
        .with_morph_target_binds(vec![MorphTargetBind::new(0, 0, 1.0)]);

    assert_eq!(expression.evaluate(0.49).morph_updates[0].weight(), 0.0);
    assert_eq!(expression.evaluate(0.5).morph_updates[0].weight(), 1.0);
}

#[test]
fn expands_material_and_texture_binds_without_losing_values() {
    let expression = Expression::new("custom")
        .with_material_color_binds(vec![MaterialColorBind {
            material_index: 0,
            color_type: MaterialColorType::Color,
            target_value: [0.2, 0.3, 0.4, 1.0],
        }])
        .with_texture_transform_binds(vec![TextureTransformBind {
            material_index: 0,
            scale: [2.0, 3.0],
            offset: [0.25, 0.5],
        }]);

    let evaluation = expression.evaluate(0.5);
    assert_eq!(
        evaluation.material_color_updates[0].target_value(),
        [0.2, 0.3, 0.4, 1.0]
    );
    assert_eq!(evaluation.material_color_updates[0].weight(), 0.5);
    assert_eq!(evaluation.texture_transform_updates[0].scale(), [2.0, 3.0]);
    assert_eq!(
        evaluation.texture_transform_updates[0].offset(),
        [0.25, 0.5]
    );
}