vrm-runtime 0.1.0

Renderer-agnostic VRM 1.0 loader, validator, animation system, and runtime
Documentation
use crate::expressions::{MaterialColorUpdate, TextureTransformUpdate};
use crate::{Avatar, MaterialColorType, MaterialId, TextureTransform};

/// Time-dependent MToon UV animation shared by all texture slots of a material.
///
/// A renderer samples the optional MToon UV animation mask and applies this
/// transform before the expression-wide and per-texture transforms.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct UvAnimationState {
    /// Accumulated MToon UV scroll offset.
    pub scroll: [f32; 2],
    /// Accumulated MToon UV rotation in radians.
    pub rotation: f32,
}

/// Final dynamic values of one material for an evaluated frame.
///
/// Static texture references and render-state settings remain on [`crate::Material`].
/// This value contains the parameters that can change because of VRM expressions
/// or MToon UV animation, so renderers do not need to reimplement VRM blending.
#[derive(Debug, Clone, PartialEq)]
pub struct EvaluatedMaterial {
    /// Stable identifier of the source [`crate::Material`].
    pub material_id: MaterialId,
    /// Base color after all active VRM expression binds are accumulated.
    pub base_color_factor: [f32; 4],
    /// Expression-adjusted emissive color with the selected HDR strength already applied.
    pub emissive_factor: [f32; 3],
    /// MToon shade color after active expression binds are accumulated.
    pub shade_color_factor: [f32; 3],
    /// MToon matcap color after active expression binds are accumulated.
    pub matcap_factor: [f32; 3],
    /// MToon rim color after active expression binds are accumulated.
    pub rim_color_factor: [f32; 3],
    /// MToon outline color after active expression binds are accumulated.
    pub outline_color_factor: [f32; 3],
    /// Material-wide transform produced by VRM expression binds.
    pub texture_transform: TextureTransform,
    /// Time-dependent MToon transform, applied before expression and texture transforms.
    pub uv_animation: UvAnimationState,
}

pub(crate) fn evaluate_materials(
    asset: &Avatar,
    color_updates: &[MaterialColorUpdate],
    texture_updates: &[TextureTransformUpdate],
    elapsed_time: f32,
) -> Vec<EvaluatedMaterial> {
    let mut evaluated = asset
        .materials()
        .iter()
        .enumerate()
        .map(|(index, material)| {
            let mtoon = material.mtoon();
            EvaluatedMaterial {
                material_id: MaterialId::from_index(index),
                base_color_factor: material.base_color_factor,
                emissive_factor: material.emissive_factor,
                shade_color_factor: mtoon
                    .map(|value| value.shade_color.to_array())
                    .unwrap_or([1.0; 3]),
                matcap_factor: mtoon
                    .map(|value| value.matcap_factor.to_array())
                    .unwrap_or([1.0; 3]),
                rim_color_factor: mtoon
                    .map(|value| value.parametric_rim_color_factor.to_array())
                    .unwrap_or([0.0; 3]),
                outline_color_factor: mtoon
                    .map(|value| value.outline_color_factor.to_array())
                    .unwrap_or([0.0; 3]),
                texture_transform: TextureTransform::default(),
                uv_animation: mtoon
                    .map(|value| UvAnimationState {
                        scroll: [
                            value.uv_animation_scroll_x_speed_factor * elapsed_time,
                            value.uv_animation_scroll_y_speed_factor * elapsed_time,
                        ],
                        rotation: value.uv_animation_rotation_speed_factor * elapsed_time,
                    })
                    .unwrap_or_default(),
            }
        })
        .collect::<Vec<_>>();

    for update in color_updates {
        let Some(material) = evaluated.get_mut(update.material_index() as usize) else {
            continue;
        };
        let base = asset.base_color(update.material_index(), update.color_type());
        let target = update.target_value();
        let weight = update.weight();
        match update.color_type() {
            MaterialColorType::Color => {
                accumulate(&mut material.base_color_factor, base, target, weight);
            }
            MaterialColorType::EmissionColor => {
                accumulate_rgb(&mut material.emissive_factor, base, target, weight);
            }
            MaterialColorType::ShadeColor => {
                accumulate_rgb(&mut material.shade_color_factor, base, target, weight);
            }
            MaterialColorType::MatcapColor => {
                accumulate_rgb(&mut material.matcap_factor, base, target, weight);
            }
            MaterialColorType::RimColor => {
                accumulate_rgb(&mut material.rim_color_factor, base, target, weight);
            }
            MaterialColorType::OutlineColor => {
                accumulate_rgb(&mut material.outline_color_factor, base, target, weight);
            }
        }
    }

    for update in texture_updates {
        let Some(material) = evaluated.get_mut(update.material_index() as usize) else {
            continue;
        };
        for channel in 0..2 {
            material.texture_transform.scale[channel] +=
                (update.scale()[channel] - 1.0) * update.weight();
            material.texture_transform.offset[channel] +=
                update.offset()[channel] * update.weight();
        }
    }

    for (value, source) in evaluated.iter_mut().zip(asset.materials()) {
        for component in &mut value.emissive_factor {
            *component *= source.emissive_strength();
        }
    }

    evaluated
}

fn accumulate(current: &mut [f32; 4], base: [f32; 4], target: [f32; 4], weight: f32) {
    for channel in 0..4 {
        current[channel] += (target[channel] - base[channel]) * weight;
    }
}

fn accumulate_rgb(current: &mut [f32; 3], base: [f32; 4], target: [f32; 4], weight: f32) {
    for channel in 0..3 {
        current[channel] += (target[channel] - base[channel]) * weight;
    }
}