use vrm_spec::vrmc_vrm_1_0::{self, ExpressionPresetName};
pub(crate) fn preset_expression_name(name: &ExpressionPresetName) -> &'static str {
match name {
ExpressionPresetName::Happy => "happy",
ExpressionPresetName::Angry => "angry",
ExpressionPresetName::Sad => "sad",
ExpressionPresetName::Relaxed => "relaxed",
ExpressionPresetName::Surprised => "surprised",
ExpressionPresetName::Aa => "aa",
ExpressionPresetName::Ih => "ih",
ExpressionPresetName::Ou => "ou",
ExpressionPresetName::Ee => "ee",
ExpressionPresetName::Oh => "oh",
ExpressionPresetName::Blink => "blink",
ExpressionPresetName::BlinkLeft => "blinkLeft",
ExpressionPresetName::BlinkRight => "blinkRight",
ExpressionPresetName::Neutral => "neutral",
ExpressionPresetName::LookUp => "lookUp",
ExpressionPresetName::LookDown => "lookDown",
ExpressionPresetName::LookLeft => "lookLeft",
ExpressionPresetName::LookRight => "lookRight",
}
}
pub(crate) fn is_preset_expression_name(name: &str) -> bool {
matches!(
name,
"happy"
| "angry"
| "sad"
| "relaxed"
| "surprised"
| "aa"
| "ih"
| "ou"
| "ee"
| "oh"
| "blink"
| "blinkLeft"
| "blinkRight"
| "lookUp"
| "lookDown"
| "lookLeft"
| "lookRight"
| "neutral"
)
}
#[derive(Debug, Clone)]
pub struct MorphTargetBind {
node_index: u32,
target_index: u32,
weight: f32,
}
impl MorphTargetBind {
pub fn new(node_index: u32, target_index: u32, weight: f32) -> Self {
Self {
node_index,
target_index,
weight,
}
}
pub fn node_index(&self) -> u32 {
self.node_index
}
pub fn target_index(&self) -> u32 {
self.target_index
}
pub fn weight(&self) -> f32 {
self.weight
}
}
#[derive(Debug, Clone)]
pub struct MaterialColorBind {
pub material_index: u32,
pub color_type: MaterialColorType,
pub target_value: [f32; 4],
}
#[derive(Debug, Clone)]
pub struct TextureTransformBind {
pub material_index: u32,
pub scale: [f32; 2],
pub offset: [f32; 2],
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MorphTargetUpdate {
node_index: u32,
target_index: u32,
weight: f32,
}
impl MorphTargetUpdate {
pub(crate) fn new(node_index: u32, target_index: u32, weight: f32) -> Self {
Self {
node_index,
target_index,
weight,
}
}
pub fn node_index(&self) -> u32 {
self.node_index
}
pub fn target_index(&self) -> u32 {
self.target_index
}
pub fn weight(&self) -> f32 {
self.weight
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MaterialColorUpdate {
material_index: u32,
color_type: MaterialColorType,
target_value: [f32; 4],
weight: f32,
}
impl MaterialColorUpdate {
pub fn new(
material_index: u32,
color_type: MaterialColorType,
target_value: [f32; 4],
weight: f32,
) -> Self {
Self {
material_index,
color_type,
target_value,
weight,
}
}
pub fn material_index(&self) -> u32 {
self.material_index
}
pub fn color_type(&self) -> MaterialColorType {
self.color_type
}
pub fn target_value(&self) -> [f32; 4] {
self.target_value
}
pub fn weight(&self) -> f32 {
self.weight
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextureTransformUpdate {
material_index: u32,
scale: [f32; 2],
offset: [f32; 2],
weight: f32,
}
impl TextureTransformUpdate {
pub fn new(material_index: u32, scale: [f32; 2], offset: [f32; 2], weight: f32) -> Self {
Self {
material_index,
scale,
offset,
weight,
}
}
pub fn material_index(&self) -> u32 {
self.material_index
}
pub fn scale(&self) -> [f32; 2] {
self.scale
}
pub fn offset(&self) -> [f32; 2] {
self.offset
}
pub fn weight(&self) -> f32 {
self.weight
}
}
#[derive(Debug, Clone, Default)]
pub struct ExpressionEvaluation {
pub morph_updates: Vec<MorphTargetUpdate>,
pub material_color_updates: Vec<MaterialColorUpdate>,
pub texture_transform_updates: Vec<TextureTransformUpdate>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ExpressionOverrideType {
#[default]
None,
Block,
Blend,
}
impl From<vrmc_vrm_1_0::ExpressionOverrideType> for ExpressionOverrideType {
fn from(schema: vrmc_vrm_1_0::ExpressionOverrideType) -> Self {
match schema {
vrmc_vrm_1_0::ExpressionOverrideType::None => Self::None,
vrmc_vrm_1_0::ExpressionOverrideType::Block => Self::Block,
vrmc_vrm_1_0::ExpressionOverrideType::Blend => Self::Blend,
}
}
}
pub(crate) fn validate_override_semantics(
name: &str,
override_blink: ExpressionOverrideType,
override_mouth: ExpressionOverrideType,
override_look_at: ExpressionOverrideType,
) -> Result<(), String> {
let invalid_override = if matches!(name, "blink" | "blinkLeft" | "blinkRight")
&& override_blink != ExpressionOverrideType::None
{
Some(("overrideBlink", "blink"))
} else if matches!(name, "aa" | "ih" | "ou" | "ee" | "oh")
&& override_mouth != ExpressionOverrideType::None
{
Some(("overrideMouth", "mouth"))
} else if matches!(name, "lookUp" | "lookDown" | "lookLeft" | "lookRight")
&& override_look_at != ExpressionOverrideType::None
{
Some(("overrideLookAt", "look-at"))
} else {
None
};
if let Some((field, category)) = invalid_override {
return Err(format!(
"expression `{name}` must set `{field}` to `none`; an expression cannot override its own {category} category"
));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
pub enum MaterialColorType {
Color,
EmissionColor,
ShadeColor,
MatcapColor,
RimColor,
OutlineColor,
}
#[derive(Debug, Clone)]
pub struct Expression {
name: String,
is_binary: bool,
override_blink: ExpressionOverrideType,
override_mouth: ExpressionOverrideType,
override_look_at: ExpressionOverrideType,
morph_target_binds: Vec<MorphTargetBind>,
material_color_binds: Vec<MaterialColorBind>,
texture_transform_binds: Vec<TextureTransformBind>,
}
impl Expression {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
is_binary: false,
morph_target_binds: Vec::new(),
material_color_binds: Vec::new(),
texture_transform_binds: Vec::new(),
override_blink: ExpressionOverrideType::default(),
override_mouth: ExpressionOverrideType::default(),
override_look_at: ExpressionOverrideType::default(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn with_morph_target_binds(mut self, binds: Vec<MorphTargetBind>) -> Self {
self.morph_target_binds = binds;
self
}
pub fn with_binary(mut self, is_binary: bool) -> Self {
self.is_binary = is_binary;
self
}
pub fn with_override_blink(mut self, value: ExpressionOverrideType) -> Self {
self.override_blink = value;
self
}
pub fn with_override_mouth(mut self, value: ExpressionOverrideType) -> Self {
self.override_mouth = value;
self
}
pub fn with_override_look_at(mut self, value: ExpressionOverrideType) -> Self {
self.override_look_at = value;
self
}
pub fn with_material_color_binds(mut self, binds: Vec<MaterialColorBind>) -> Self {
self.material_color_binds = binds;
self
}
pub fn with_texture_transform_binds(mut self, binds: Vec<TextureTransformBind>) -> Self {
self.texture_transform_binds = binds;
self
}
pub fn is_binary(&self) -> bool {
self.is_binary
}
pub fn override_blink(&self) -> ExpressionOverrideType {
self.override_blink
}
pub fn override_mouth(&self) -> ExpressionOverrideType {
self.override_mouth
}
pub fn override_look_at(&self) -> ExpressionOverrideType {
self.override_look_at
}
pub fn morph_target_binds(&self) -> &[MorphTargetBind] {
&self.morph_target_binds
}
pub fn material_color_binds(&self) -> &[MaterialColorBind] {
&self.material_color_binds
}
pub fn texture_transform_binds(&self) -> &[TextureTransformBind] {
&self.texture_transform_binds
}
fn load_expression(name: impl Into<String>, expression: &vrmc_vrm_1_0::Expression) -> Self {
let is_binary = expression.is_binary.unwrap_or(false);
let override_blink = expression
.override_blink
.map(ExpressionOverrideType::from)
.unwrap_or_default();
let override_mouth = expression
.override_mouth
.map(ExpressionOverrideType::from)
.unwrap_or_default();
let override_look_at = expression
.override_look_at
.map(ExpressionOverrideType::from)
.unwrap_or_default();
let morph_target_binds = expression
.morph_target_binds
.as_deref()
.unwrap_or_default()
.iter()
.map(|bind| {
MorphTargetBind::new(
bind.node.value() as u32,
bind.index as u32,
bind.weight as f32,
)
})
.collect();
let material_color_binds = expression
.material_color_binds
.as_deref()
.unwrap_or_default()
.iter()
.map(|bind| {
let color_type = match bind.r#type {
vrmc_vrm_1_0::MaterialColorType::Color => MaterialColorType::Color,
vrmc_vrm_1_0::MaterialColorType::EmissionColor => {
MaterialColorType::EmissionColor
}
vrmc_vrm_1_0::MaterialColorType::ShadeColor => MaterialColorType::ShadeColor,
vrmc_vrm_1_0::MaterialColorType::MatcapColor => MaterialColorType::MatcapColor,
vrmc_vrm_1_0::MaterialColorType::RimColor => MaterialColorType::RimColor,
vrmc_vrm_1_0::MaterialColorType::OutlineColor => {
MaterialColorType::OutlineColor
}
};
let target = &bind.target_value;
MaterialColorBind {
material_index: bind.material.value() as u32,
color_type,
target_value: [
target[0] as f32,
target[1] as f32,
target[2] as f32,
target[3] as f32,
],
}
})
.collect();
let texture_transform_binds = expression
.texture_transform_binds
.as_deref()
.unwrap_or_default()
.iter()
.map(|bind| {
let scale = bind
.scale
.as_ref()
.map(|s| [s[0] as f32, s[1] as f32])
.unwrap_or([1.0, 1.0]);
let offset = bind
.offset
.as_ref()
.map(|o| [o[0] as f32, o[1] as f32])
.unwrap_or([0.0, 0.0]);
TextureTransformBind {
material_index: bind.material.value() as u32,
scale,
offset,
}
})
.collect();
Self {
name: name.into(),
is_binary,
override_blink,
override_mouth,
override_look_at,
morph_target_binds,
material_color_binds,
texture_transform_binds,
}
}
pub fn evaluate(&self, input_weight: f32) -> ExpressionEvaluation {
let input_weight = if self.is_binary {
f32::from(input_weight >= 0.5)
} else if input_weight.is_finite() {
input_weight.clamp(0.0, 1.0)
} else {
0.0
};
ExpressionEvaluation {
morph_updates: self
.morph_target_binds
.iter()
.map(|bind| {
MorphTargetUpdate::new(
bind.node_index(),
bind.target_index(),
bind.weight() * input_weight,
)
})
.collect(),
material_color_updates: self
.material_color_binds
.iter()
.map(|bind| {
MaterialColorUpdate::new(
bind.material_index,
bind.color_type,
bind.target_value,
input_weight,
)
})
.collect(),
texture_transform_updates: self
.texture_transform_binds
.iter()
.map(|bind| {
TextureTransformUpdate::new(
bind.material_index,
bind.scale,
bind.offset,
input_weight,
)
})
.collect(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Expressions {
items: Vec<Expression>,
}
impl Expressions {
pub fn new(items: Vec<Expression>) -> Self {
Self { items }
}
pub fn iter(&self) -> impl Iterator<Item = &Expression> {
self.items.iter()
}
pub fn get(&self, name: &str) -> Option<&Expression> {
self.items
.iter()
.find(|expression| expression.name() == name)
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub(crate) fn load_expressions(expressions: &vrmc_vrm_1_0::Expressions) -> Self {
let mut items = Vec::new();
if let Some(preset) = &expressions.preset {
for (preset_name, expr) in &preset.0 {
let name = preset_expression_name(preset_name);
items.push(Expression::load_expression(name, expr));
}
}
if let Some(custom) = &expressions.custom {
for (custom_name, expr) in custom {
items.push(Expression::load_expression(custom_name, expr));
}
}
Self::new(items)
}
}