use glam::Vec3;
use serde::Deserialize;
use vrm_spec::vrmc_materials_mtoon_1_0 as mtoon_spec;
use crate::{ImageId, TextureId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AlphaMode {
#[default]
Opaque,
Mask,
Blend,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutlineWidthMode {
#[default]
None,
WorldCoordinates,
ScreenCoordinates,
}
impl From<mtoon_spec::OutlineWidthMode> for OutlineWidthMode {
fn from(mode: mtoon_spec::OutlineWidthMode) -> Self {
match mode {
mtoon_spec::OutlineWidthMode::None => Self::None,
mtoon_spec::OutlineWidthMode::WorldCoordinates => Self::WorldCoordinates,
mtoon_spec::OutlineWidthMode::ScreenCoordinates => Self::ScreenCoordinates,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextureTransform {
pub offset: [f32; 2],
pub rotation: f32,
pub scale: [f32; 2],
}
impl Default for TextureTransform {
fn default() -> Self {
Self {
offset: [0.0, 0.0],
rotation: 0.0,
scale: [1.0, 1.0],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextureInfo {
texture_id: TextureId,
tex_coord: u32,
transform: TextureTransform,
}
impl TextureInfo {
pub const fn new(texture_id: TextureId) -> Self {
Self {
texture_id,
tex_coord: 0,
transform: TextureTransform {
offset: [0.0, 0.0],
rotation: 0.0,
scale: [1.0, 1.0],
},
}
}
pub const fn with_tex_coord(mut self, tex_coord: u32) -> Self {
self.tex_coord = tex_coord;
self
}
pub const fn with_transform(mut self, transform: TextureTransform) -> Self {
self.transform = transform;
self
}
pub fn texture_id(&self) -> TextureId {
self.texture_id
}
pub fn tex_coord(&self) -> u32 {
self.tex_coord
}
pub fn transform(&self) -> TextureTransform {
self.transform
}
fn from_gltf(info: gltf::texture::Info<'_>) -> Self {
let transform = info
.texture_transform()
.map(|transform| TextureTransform {
offset: transform.offset(),
rotation: transform.rotation(),
scale: transform.scale(),
})
.unwrap_or_default();
let tex_coord = info
.texture_transform()
.and_then(|transform| transform.tex_coord())
.unwrap_or_else(|| info.tex_coord());
Self {
texture_id: TextureId::from_index(info.texture().index()),
tex_coord,
transform,
}
}
fn from_json(info: &gltf::json::texture::Info) -> Self {
let transform = info
.extensions
.as_ref()
.and_then(|extensions| extensions.texture_transform.as_ref());
Self {
texture_id: TextureId::from_index(info.index.value()),
tex_coord: transform
.and_then(|transform| transform.tex_coord)
.unwrap_or(info.tex_coord),
transform: transform
.map(|transform| TextureTransform {
offset: transform.offset.0,
rotation: transform.rotation.0,
scale: transform.scale.0,
})
.unwrap_or_default(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NormalTextureInfo {
pub texture: TextureInfo,
pub scale: f32,
}
impl NormalTextureInfo {
pub const fn new(texture: TextureInfo) -> Self {
Self {
texture,
scale: 1.0,
}
}
pub const fn with_scale(mut self, scale: f32) -> Self {
self.scale = scale;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OcclusionTextureInfo {
pub texture: TextureInfo,
pub strength: f32,
}
impl OcclusionTextureInfo {
pub const fn new(texture: TextureInfo) -> Self {
Self {
texture,
strength: 1.0,
}
}
pub const fn with_strength(mut self, strength: f32) -> Self {
self.strength = strength;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MaterialShadingModel {
#[default]
PbrMetallicRoughness,
Unlit,
MToon,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EmissiveStrengthSource {
#[default]
Default,
KhrMaterialsEmissiveStrength,
VrmcMaterialsHdrEmissiveMultiplier,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MagFilter {
Nearest,
Linear,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MinFilter {
Nearest,
Linear,
NearestMipmapNearest,
LinearMipmapNearest,
NearestMipmapLinear,
LinearMipmapLinear,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WrapMode {
ClampToEdge,
MirroredRepeat,
Repeat,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextureSampler {
pub mag_filter: Option<MagFilter>,
pub min_filter: Option<MinFilter>,
pub wrap_s: WrapMode,
pub wrap_t: WrapMode,
}
impl Default for TextureSampler {
fn default() -> Self {
Self {
mag_filter: None,
min_filter: None,
wrap_s: WrapMode::Repeat,
wrap_t: WrapMode::Repeat,
}
}
}
#[derive(Debug, Clone)]
pub struct AvatarTexture {
image_id: ImageId,
sampler: TextureSampler,
}
impl AvatarTexture {
pub fn new(image_id: ImageId) -> Self {
Self {
image_id,
sampler: TextureSampler::default(),
}
}
pub const fn with_sampler(mut self, sampler: TextureSampler) -> Self {
self.sampler = sampler;
self
}
pub fn image_id(&self) -> ImageId {
self.image_id
}
pub fn sampler(&self) -> TextureSampler {
self.sampler
}
}
#[derive(Debug, Clone)]
pub struct Material {
pub base_color_factor: [f32; 4],
pub metallic_factor: f32,
pub roughness_factor: f32,
pub emissive_factor: [f32; 3],
base_color_texture: Option<TextureInfo>,
metallic_roughness_texture: Option<TextureInfo>,
normal_texture: Option<NormalTextureInfo>,
occlusion_texture: Option<OcclusionTextureInfo>,
emissive_texture: Option<TextureInfo>,
unlit: bool,
emissive_strength: f32,
emissive_strength_source: EmissiveStrengthSource,
legacy_emissive_multiplier: Option<f32>,
pub alpha_mode: AlphaMode,
pub alpha_cutoff: f32,
double_sided: bool,
mtoon: Option<MToonMaterial>,
}
impl Default for Material {
fn default() -> Self {
Self {
base_color_factor: [1.0; 4],
metallic_factor: 1.0,
roughness_factor: 1.0,
emissive_factor: [0.0; 3],
base_color_texture: None,
metallic_roughness_texture: None,
normal_texture: None,
occlusion_texture: None,
emissive_texture: None,
unlit: false,
emissive_strength: 1.0,
emissive_strength_source: EmissiveStrengthSource::Default,
legacy_emissive_multiplier: None,
alpha_mode: AlphaMode::Opaque,
alpha_cutoff: 0.5,
double_sided: false,
mtoon: None,
}
}
}
impl Material {
pub fn new() -> Self {
Self::default()
}
pub const fn with_base_color_factor(mut self, factor: [f32; 4]) -> Self {
self.base_color_factor = factor;
self
}
pub const fn with_metallic_factor(mut self, factor: f32) -> Self {
self.metallic_factor = factor;
self
}
pub const fn with_roughness_factor(mut self, factor: f32) -> Self {
self.roughness_factor = factor;
self
}
pub const fn with_emissive_factor(mut self, factor: [f32; 3]) -> Self {
self.emissive_factor = factor;
self
}
pub const fn with_base_color_texture(mut self, texture: TextureInfo) -> Self {
self.base_color_texture = Some(texture);
self
}
pub const fn with_metallic_roughness_texture(mut self, texture: TextureInfo) -> Self {
self.metallic_roughness_texture = Some(texture);
self
}
pub const fn with_normal_texture(mut self, texture: NormalTextureInfo) -> Self {
self.normal_texture = Some(texture);
self
}
pub const fn with_occlusion_texture(mut self, texture: OcclusionTextureInfo) -> Self {
self.occlusion_texture = Some(texture);
self
}
pub const fn with_emissive_texture(mut self, texture: TextureInfo) -> Self {
self.emissive_texture = Some(texture);
self
}
pub const fn with_unlit(mut self, unlit: bool) -> Self {
self.unlit = unlit;
self
}
pub const fn with_emissive_strength(mut self, strength: f32) -> Self {
self.emissive_strength = strength;
self.emissive_strength_source = EmissiveStrengthSource::KhrMaterialsEmissiveStrength;
self.legacy_emissive_multiplier = None;
self
}
pub const fn with_alpha_mode(mut self, alpha_mode: AlphaMode) -> Self {
self.alpha_mode = alpha_mode;
self
}
pub const fn with_alpha_cutoff(mut self, alpha_cutoff: f32) -> Self {
self.alpha_cutoff = alpha_cutoff;
self
}
pub const fn with_double_sided(mut self, double_sided: bool) -> Self {
self.double_sided = double_sided;
self
}
pub fn with_mtoon(mut self, mtoon: MToonMaterial) -> Self {
self.mtoon = Some(mtoon);
self
}
pub fn base_color_texture(&self) -> Option<&TextureInfo> {
self.base_color_texture.as_ref()
}
pub fn metallic_roughness_texture(&self) -> Option<&TextureInfo> {
self.metallic_roughness_texture.as_ref()
}
pub fn normal_texture(&self) -> Option<&NormalTextureInfo> {
self.normal_texture.as_ref()
}
pub fn occlusion_texture(&self) -> Option<&OcclusionTextureInfo> {
self.occlusion_texture.as_ref()
}
pub fn emissive_texture(&self) -> Option<&TextureInfo> {
self.emissive_texture.as_ref()
}
pub fn unlit(&self) -> bool {
self.unlit
}
pub fn emissive_strength(&self) -> f32 {
self.emissive_strength
}
pub fn emissive_strength_source(&self) -> EmissiveStrengthSource {
self.emissive_strength_source
}
pub fn legacy_emissive_multiplier(&self) -> Option<f32> {
self.legacy_emissive_multiplier
}
pub fn effective_emissive_factor(&self) -> [f32; 3] {
self.emissive_factor
.map(|component| component * self.emissive_strength)
}
pub fn shading_model(&self) -> MaterialShadingModel {
if self.mtoon.is_some() {
MaterialShadingModel::MToon
} else if self.unlit {
MaterialShadingModel::Unlit
} else {
MaterialShadingModel::PbrMetallicRoughness
}
}
pub fn double_sided(&self) -> bool {
self.double_sided
}
pub fn mtoon(&self) -> Option<&MToonMaterial> {
self.mtoon.as_ref()
}
fn load(
material: &gltf::Material<'_>,
mtoon: Option<&mtoon_spec::VrmcMaterialsMtoonSchema>,
texture_count: usize,
) -> Result<Self, crate::LoadError> {
let pbr = material.pbr_metallic_roughness();
let material_index = material.index().unwrap_or(0);
let legacy_emissive_multiplier = load_legacy_emissive_multiplier(material, material_index)?;
let khr_emissive_strength = material.emissive_strength();
if let Some(value) = khr_emissive_strength {
validate_emissive_strength(
value,
material_index,
"KHR_materials_emissive_strength",
"emissiveStrength",
)?;
}
let (emissive_strength, emissive_strength_source) =
if let Some(value) = khr_emissive_strength {
(value, EmissiveStrengthSource::KhrMaterialsEmissiveStrength)
} else if let Some(value) = legacy_emissive_multiplier {
(
value,
EmissiveStrengthSource::VrmcMaterialsHdrEmissiveMultiplier,
)
} else {
(1.0, EmissiveStrengthSource::Default)
};
let mtoon = mtoon
.map(MToonMaterial::load)
.transpose()
.map_err(crate::LoadError::InvalidVrm)?;
let loaded = Self {
base_color_factor: pbr.base_color_factor(),
metallic_factor: pbr.metallic_factor(),
roughness_factor: pbr.roughness_factor(),
emissive_factor: material.emissive_factor(),
base_color_texture: pbr.base_color_texture().map(TextureInfo::from_gltf),
metallic_roughness_texture: pbr
.metallic_roughness_texture()
.map(TextureInfo::from_gltf),
normal_texture: material.normal_texture().map(|info| {
let (transform, transformed_tex_coord) =
extension_transform(info.extension_value("KHR_texture_transform"));
NormalTextureInfo {
texture: TextureInfo {
texture_id: TextureId::from_index(info.texture().index()),
tex_coord: transformed_tex_coord.unwrap_or_else(|| info.tex_coord()),
transform,
},
scale: info.scale(),
}
}),
occlusion_texture: material.occlusion_texture().map(|info| {
let (transform, transformed_tex_coord) =
extension_transform(info.extension_value("KHR_texture_transform"));
OcclusionTextureInfo {
texture: TextureInfo {
texture_id: TextureId::from_index(info.texture().index()),
tex_coord: transformed_tex_coord.unwrap_or_else(|| info.tex_coord()),
transform,
},
strength: info.strength(),
}
}),
emissive_texture: material.emissive_texture().map(TextureInfo::from_gltf),
unlit: material.unlit(),
emissive_strength,
emissive_strength_source,
legacy_emissive_multiplier,
alpha_mode: match material.alpha_mode() {
gltf::material::AlphaMode::Opaque => AlphaMode::Opaque,
gltf::material::AlphaMode::Mask => AlphaMode::Mask,
gltf::material::AlphaMode::Blend => AlphaMode::Blend,
},
alpha_cutoff: material.alpha_cutoff().unwrap_or(0.5),
double_sided: material.double_sided(),
mtoon,
};
loaded
.validate_programmatic(material_index, texture_count)
.map_err(crate::LoadError::InvalidVrm)?;
Ok(loaded)
}
pub(crate) fn validate_programmatic(
&self,
material_index: usize,
texture_count: usize,
) -> Result<(), String> {
let label = format!("material {material_index}");
if !unit_slice(&self.base_color_factor)
|| !unit_value(self.metallic_factor)
|| !unit_value(self.roughness_factor)
|| !unit_slice(&self.emissive_factor)
{
return Err(format!(
"{label} color, metallic, roughness, and emissive factors must be finite and within 0..=1"
));
}
if !self.emissive_strength.is_finite() || self.emissive_strength < 0.0 {
return Err(format!(
"{label} emissive strength must be finite and non-negative"
));
}
if !self.alpha_cutoff.is_finite() || self.alpha_cutoff < 0.0 {
return Err(format!(
"{label} alpha cutoff must be finite and non-negative"
));
}
for (name, texture) in [
("base color", self.base_color_texture.as_ref()),
(
"metallic-roughness",
self.metallic_roughness_texture.as_ref(),
),
(
"normal",
self.normal_texture.as_ref().map(|value| &value.texture),
),
(
"occlusion",
self.occlusion_texture.as_ref().map(|value| &value.texture),
),
("emissive", self.emissive_texture.as_ref()),
] {
validate_programmatic_texture(texture, texture_count, &label, name)?;
}
if self
.normal_texture
.as_ref()
.is_some_and(|texture| !texture.scale.is_finite())
{
return Err(format!("{label} normal texture scale must be finite"));
}
if self.occlusion_texture.as_ref().is_some_and(|texture| {
!texture.strength.is_finite() || !(0.0..=1.0).contains(&texture.strength)
}) {
return Err(format!(
"{label} occlusion texture strength must be finite and within 0..=1"
));
}
if let Some(mtoon) = &self.mtoon {
mtoon.validate_programmatic(&label, texture_count)?;
}
Ok(())
}
}
fn validate_programmatic_texture(
texture: Option<&TextureInfo>,
texture_count: usize,
material_label: &str,
texture_name: &str,
) -> Result<(), String> {
let Some(texture) = texture else {
return Ok(());
};
if texture.texture_id.index() >= texture_count {
return Err(format!(
"{material_label} {texture_name} texture references a missing texture"
));
}
let transform = texture.transform;
if !transform.rotation.is_finite()
|| transform.offset.iter().any(|value| !value.is_finite())
|| transform.scale.iter().any(|value| !value.is_finite())
{
return Err(format!(
"{material_label} {texture_name} texture has a non-finite UV transform"
));
}
Ok(())
}
fn unit_value(value: f32) -> bool {
value.is_finite() && (0.0..=1.0).contains(&value)
}
fn unit_slice(values: &[f32]) -> bool {
values.iter().copied().all(unit_value)
}
const VRMC_MATERIALS_HDR_EMISSIVE_MULTIPLIER: &str = "VRMC_materials_hdr_emissiveMultiplier";
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyHdrEmissiveMultiplier {
emissive_multiplier: f32,
}
fn load_legacy_emissive_multiplier(
material: &gltf::Material<'_>,
material_index: usize,
) -> Result<Option<f32>, crate::LoadError> {
let Some(value) = material.extension_value(VRMC_MATERIALS_HDR_EMISSIVE_MULTIPLIER) else {
return Ok(None);
};
let extension = serde_json::from_value::<LegacyHdrEmissiveMultiplier>(value.to_owned())
.map_err(|source| crate::LoadError::InvalidExtension {
extension: VRMC_MATERIALS_HDR_EMISSIVE_MULTIPLIER,
location: format!(
"/materials/{material_index}/extensions/{VRMC_MATERIALS_HDR_EMISSIVE_MULTIPLIER}"
),
source,
})?;
validate_emissive_strength(
extension.emissive_multiplier,
material_index,
VRMC_MATERIALS_HDR_EMISSIVE_MULTIPLIER,
"emissiveMultiplier",
)?;
Ok(Some(extension.emissive_multiplier))
}
fn validate_emissive_strength(
value: f32,
material_index: usize,
extension: &str,
property: &str,
) -> Result<(), crate::LoadError> {
if value.is_finite() && value >= 0.0 {
return Ok(());
}
Err(crate::LoadError::InvalidVrm(format!(
"/materials/{material_index}/extensions/{extension}/{property} must be finite and non-negative"
)))
}
#[derive(Debug, Clone)]
pub struct MToonMaterial {
pub transparent_with_z_write: bool,
pub render_queue_offset_number: i32,
pub shade_color: Vec3,
pub shade_multiply_texture: Option<TextureInfo>,
pub shading_shift_factor: f32,
pub shading_shift_texture: Option<TextureInfo>,
pub shading_shift_texture_scale: f32,
pub shading_toony_factor: f32,
pub gi_equalization_factor: f32,
pub matcap_factor: Vec3,
pub matcap_texture: Option<TextureInfo>,
pub parametric_rim_color_factor: Vec3,
pub parametric_rim_fresnel_power_factor: f32,
pub parametric_rim_lift_factor: f32,
pub rim_multiply_texture: Option<TextureInfo>,
pub rim_lighting_mix_factor: f32,
pub outline_width_mode: OutlineWidthMode,
pub outline_width_factor: f32,
pub outline_width_multiply_texture: Option<TextureInfo>,
pub outline_color_factor: Vec3,
pub outline_lighting_mix_factor: f32,
pub uv_animation_mask_texture: Option<TextureInfo>,
pub uv_animation_scroll_x_speed_factor: f32,
pub uv_animation_scroll_y_speed_factor: f32,
pub uv_animation_rotation_speed_factor: f32,
}
impl Default for MToonMaterial {
fn default() -> Self {
Self {
transparent_with_z_write: false,
render_queue_offset_number: 0,
shade_color: Vec3::ONE,
shade_multiply_texture: None,
shading_shift_factor: 0.0,
shading_shift_texture: None,
shading_shift_texture_scale: 1.0,
shading_toony_factor: 0.9,
gi_equalization_factor: 0.9,
matcap_factor: Vec3::ONE,
matcap_texture: None,
parametric_rim_color_factor: Vec3::ZERO,
parametric_rim_fresnel_power_factor: 5.0,
parametric_rim_lift_factor: 0.0,
rim_multiply_texture: None,
rim_lighting_mix_factor: 1.0,
outline_width_mode: OutlineWidthMode::None,
outline_width_factor: 0.0,
outline_width_multiply_texture: None,
outline_color_factor: Vec3::ZERO,
outline_lighting_mix_factor: 1.0,
uv_animation_mask_texture: None,
uv_animation_scroll_x_speed_factor: 0.0,
uv_animation_scroll_y_speed_factor: 0.0,
uv_animation_rotation_speed_factor: 0.0,
}
}
}
impl MToonMaterial {
pub fn new() -> Self {
Self::default()
}
fn load(schema: &mtoon_spec::VrmcMaterialsMtoonSchema) -> Result<Self, String> {
let render_queue_offset_number =
i32::try_from(schema.render_queue_offset_number.unwrap_or(0))
.map_err(|_| "MToon render queue offset is not representable as i32".to_string())?;
Ok(Self {
transparent_with_z_write: schema.transparent_with_z_write.unwrap_or(false),
render_queue_offset_number,
shade_color: color(schema.shade_color_factor, Vec3::ONE),
shade_multiply_texture: schema
.shade_multiply_texture
.as_ref()
.map(TextureInfo::from_json),
shading_shift_factor: schema.shading_shift_factor.unwrap_or(0.0) as f32,
shading_shift_texture: schema
.shading_shift_texture
.as_ref()
.map(shading_shift_texture)
.transpose()?,
shading_shift_texture_scale: schema
.shading_shift_texture
.as_ref()
.and_then(|texture| texture.scale)
.unwrap_or(1.0) as f32,
shading_toony_factor: schema.shading_toony_factor.unwrap_or(0.9) as f32,
gi_equalization_factor: schema.gi_equalization_factor.unwrap_or(0.9) as f32,
matcap_factor: color(schema.matcap_factor, Vec3::ONE),
matcap_texture: schema.matcap_texture.as_ref().map(TextureInfo::from_json),
parametric_rim_color_factor: color(schema.parametric_rim_color_factor, Vec3::ZERO),
parametric_rim_fresnel_power_factor: schema
.parametric_rim_fresnel_power_factor
.unwrap_or(5.0) as f32,
parametric_rim_lift_factor: schema.parametric_rim_lift_factor.unwrap_or(0.0) as f32,
rim_multiply_texture: schema
.rim_multiply_texture
.as_ref()
.map(TextureInfo::from_json),
rim_lighting_mix_factor: schema.rim_lighting_mix_factor.unwrap_or(1.0) as f32,
outline_width_mode: schema
.outline_width_mode
.map(Into::into)
.unwrap_or_default(),
outline_width_factor: schema.outline_width_factor.unwrap_or(0.0) as f32,
outline_width_multiply_texture: schema
.outline_width_multiply_texture
.as_ref()
.map(TextureInfo::from_json),
outline_color_factor: color(schema.outline_color_factor, Vec3::ZERO),
outline_lighting_mix_factor: schema.outline_lighting_mix_factor.unwrap_or(1.0) as f32,
uv_animation_mask_texture: schema
.uv_animation_mask_texture
.as_ref()
.map(TextureInfo::from_json),
uv_animation_scroll_x_speed_factor: schema
.uv_animation_scroll_x_speed_factor
.unwrap_or(0.0) as f32,
uv_animation_scroll_y_speed_factor: schema
.uv_animation_scroll_y_speed_factor
.unwrap_or(0.0) as f32,
uv_animation_rotation_speed_factor: schema
.uv_animation_rotation_speed_factor
.unwrap_or(0.0) as f32,
})
}
fn validate_programmatic(
&self,
material_label: &str,
texture_count: usize,
) -> Result<(), String> {
if !(-9..=9).contains(&self.render_queue_offset_number) {
return Err(format!(
"{material_label} MToon render queue offset must be within -9..=9"
));
}
if !unit_slice(&self.shade_color.to_array())
|| !unit_slice(&self.matcap_factor.to_array())
|| !unit_slice(&self.parametric_rim_color_factor.to_array())
|| !unit_slice(&self.outline_color_factor.to_array())
{
return Err(format!(
"{material_label} MToon color factors must be finite and within 0..=1"
));
}
if !unit_value(self.shading_toony_factor)
|| !unit_value(self.gi_equalization_factor)
|| !unit_value(self.rim_lighting_mix_factor)
|| !unit_value(self.outline_lighting_mix_factor)
{
return Err(format!(
"{material_label} MToon mix factors must be finite and within 0..=1"
));
}
if !self.parametric_rim_fresnel_power_factor.is_finite()
|| self.parametric_rim_fresnel_power_factor < 0.0
|| !self.outline_width_factor.is_finite()
|| self.outline_width_factor < 0.0
{
return Err(format!(
"{material_label} MToon rim power and outline width must be finite and non-negative"
));
}
if [
self.shading_shift_factor,
self.shading_shift_texture_scale,
self.parametric_rim_lift_factor,
self.uv_animation_scroll_x_speed_factor,
self.uv_animation_scroll_y_speed_factor,
self.uv_animation_rotation_speed_factor,
]
.iter()
.any(|value| !value.is_finite())
{
return Err(format!(
"{material_label} MToon scalar parameters must be finite"
));
}
for (name, texture) in [
("shade multiply", self.shade_multiply_texture.as_ref()),
("shading shift", self.shading_shift_texture.as_ref()),
("matcap", self.matcap_texture.as_ref()),
("rim multiply", self.rim_multiply_texture.as_ref()),
(
"outline width multiply",
self.outline_width_multiply_texture.as_ref(),
),
("UV animation mask", self.uv_animation_mask_texture.as_ref()),
] {
validate_programmatic_texture(texture, texture_count, material_label, name)?;
}
Ok(())
}
}
fn color(value: Option<[f64; 3]>, default: Vec3) -> Vec3 {
value
.map(|value| Vec3::new(value[0] as f32, value[1] as f32, value[2] as f32))
.unwrap_or(default)
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawTextureTransform {
#[serde(default)]
offset: Option<[f32; 2]>,
#[serde(default)]
rotation: Option<f32>,
#[serde(default)]
scale: Option<[f32; 2]>,
#[serde(default)]
tex_coord: Option<u32>,
}
pub(crate) fn extension_transform(
value: Option<&serde_json::Value>,
) -> (TextureTransform, Option<u32>) {
let Some(transform) =
value.and_then(|value| serde_json::from_value::<RawTextureTransform>(value.clone()).ok())
else {
return (TextureTransform::default(), None);
};
(
TextureTransform {
offset: transform.offset.unwrap_or([0.0, 0.0]),
rotation: transform.rotation.unwrap_or(0.0),
scale: transform.scale.unwrap_or([1.0, 1.0]),
},
transform.tex_coord,
)
}
fn shading_shift_texture(
info: &mtoon_spec::ShadingShiftTextureInfo,
) -> Result<TextureInfo, String> {
let extension_value = info
.extensions
.as_ref()
.and_then(|extensions| extensions.get("KHR_texture_transform"))
.map(|extension| {
serde_json::Value::Object(
extension
.iter()
.filter_map(|(key, value)| value.clone().map(|value| (key.clone(), value)))
.collect(),
)
});
let (transform, transformed_tex_coord) = match extension_value {
Some(value) => {
let transform = serde_json::from_value::<RawTextureTransform>(value)
.map_err(|error| format!("invalid MToon shading-shift UV transform: {error}"))?;
(
TextureTransform {
offset: transform.offset.unwrap_or([0.0, 0.0]),
rotation: transform.rotation.unwrap_or(0.0),
scale: transform.scale.unwrap_or([1.0, 1.0]),
},
transform.tex_coord,
)
}
None => (TextureTransform::default(), None),
};
Ok(TextureInfo {
texture_id: TextureId::from_index(info.index.value()),
tex_coord: transformed_tex_coord.unwrap_or_else(|| {
info.tex_coord
.map(|value| value.value() as u32)
.unwrap_or(0)
}),
transform,
})
}
fn map_mag_filter(filter: gltf::texture::MagFilter) -> MagFilter {
match filter {
gltf::texture::MagFilter::Nearest => MagFilter::Nearest,
gltf::texture::MagFilter::Linear => MagFilter::Linear,
}
}
fn map_min_filter(filter: gltf::texture::MinFilter) -> MinFilter {
match filter {
gltf::texture::MinFilter::Nearest => MinFilter::Nearest,
gltf::texture::MinFilter::Linear => MinFilter::Linear,
gltf::texture::MinFilter::NearestMipmapNearest => MinFilter::NearestMipmapNearest,
gltf::texture::MinFilter::LinearMipmapNearest => MinFilter::LinearMipmapNearest,
gltf::texture::MinFilter::NearestMipmapLinear => MinFilter::NearestMipmapLinear,
gltf::texture::MinFilter::LinearMipmapLinear => MinFilter::LinearMipmapLinear,
}
}
fn map_wrap_mode(mode: gltf::texture::WrappingMode) -> WrapMode {
match mode {
gltf::texture::WrappingMode::ClampToEdge => WrapMode::ClampToEdge,
gltf::texture::WrappingMode::MirroredRepeat => WrapMode::MirroredRepeat,
gltf::texture::WrappingMode::Repeat => WrapMode::Repeat,
}
}
pub(crate) fn load_textures(doc: &gltf::Document) -> Vec<AvatarTexture> {
doc.textures()
.map(|texture| {
let sampler = texture.sampler();
AvatarTexture {
image_id: ImageId::from_index(texture.source().index()),
sampler: TextureSampler {
mag_filter: sampler.mag_filter().map(map_mag_filter),
min_filter: sampler.min_filter().map(map_min_filter),
wrap_s: map_wrap_mode(sampler.wrap_s()),
wrap_t: map_wrap_mode(sampler.wrap_t()),
},
}
})
.collect()
}
pub(crate) fn load_materials(doc: &gltf::Document) -> Result<Vec<Material>, crate::LoadError> {
use mtoon_spec::VRMC_MATERIALS_MTOON;
doc.materials()
.map(|material| {
let mtoon = match material.extension_value(VRMC_MATERIALS_MTOON) {
Some(value) => {
let schema = serde_json::from_value::<mtoon_spec::VrmcMaterialsMtoonSchema>(
value.to_owned(),
)
.map_err(|source| crate::LoadError::InvalidExtension {
extension: VRMC_MATERIALS_MTOON,
location: format!("/materials/{}", material.index().unwrap_or(0)),
source,
})?;
if schema.spec_version != "1.0" {
return Err(crate::LoadError::InvalidVrm(format!(
"/materials/{}/extensions/{VRMC_MATERIALS_MTOON}/specVersion must be `1.0`",
material.index().unwrap_or(0)
)));
}
Some(schema)
}
None => None,
};
Material::load(&material, mtoon.as_ref(), doc.textures().count())
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn load_test_material(material: serde_json::Value) -> Result<Material, crate::LoadError> {
let document = serde_json::json!({
"asset": { "version": "2.0" },
"extensionsUsed": [
"KHR_materials_unlit",
"KHR_materials_emissive_strength",
"KHR_texture_transform",
"VRMC_materials_hdr_emissiveMultiplier",
"VRMC_materials_mtoon"
],
"images": [
{ "uri": "first.png" },
{ "uri": "second.png" }
],
"textures": [
{ "source": 0 },
{ "source": 1 }
],
"materials": [material]
});
let bytes = serde_json::to_vec(&document).expect("test glTF JSON must serialize");
let gltf = gltf::Gltf::from_slice(&bytes).expect("test glTF JSON must parse");
load_materials(&gltf.document).map(|mut materials| materials.remove(0))
}
#[test]
fn shading_shift_texture_preserves_khr_texture_transform() {
let info: mtoon_spec::ShadingShiftTextureInfo = serde_json::from_value(serde_json::json!({
"index": 0,
"texCoord": 1,
"extensions": {
"KHR_texture_transform": {
"offset": [0.25, 0.5],
"rotation": 0.75,
"scale": [2.0, 3.0],
"texCoord": 2
}
}
}))
.expect("the texture fixture must deserialize");
let texture = shading_shift_texture(&info).expect("the UV transform must be valid");
assert_eq!(texture.tex_coord(), 2);
assert_eq!(texture.transform().offset, [0.25, 0.5]);
assert_eq!(texture.transform().rotation, 0.75);
assert_eq!(texture.transform().scale, [2.0, 3.0]);
}
#[test]
fn loads_unlit_and_common_pbr_textures() {
let material = load_test_material(serde_json::json!({
"pbrMetallicRoughness": {
"metallicRoughnessTexture": {
"index": 0,
"texCoord": 1,
"extensions": {
"KHR_texture_transform": {
"offset": [0.25, 0.5],
"texCoord": 2
}
}
}
},
"occlusionTexture": {
"index": 1,
"texCoord": 1,
"strength": 0.4,
"extensions": {
"KHR_texture_transform": {
"scale": [2.0, 3.0]
}
}
},
"extensions": {
"KHR_materials_unlit": {}
}
}))
.expect("the material must load");
assert!(material.unlit());
assert_eq!(material.shading_model(), MaterialShadingModel::Unlit);
let metallic_roughness = material
.metallic_roughness_texture()
.expect("metallic-roughness texture must be retained");
assert_eq!(metallic_roughness.texture_id().index(), 0);
assert_eq!(metallic_roughness.tex_coord(), 2);
assert_eq!(metallic_roughness.transform().offset, [0.25, 0.5]);
let occlusion = material
.occlusion_texture()
.expect("occlusion texture must be retained");
assert_eq!(occlusion.texture.texture_id().index(), 1);
assert_eq!(occlusion.texture.tex_coord(), 1);
assert_eq!(occlusion.texture.transform().scale, [2.0, 3.0]);
assert_eq!(occlusion.strength, 0.4);
}
#[test]
fn khr_emissive_strength_takes_precedence_over_the_archived_multiplier() {
let material = load_test_material(serde_json::json!({
"emissiveFactor": [0.25, 0.5, 1.0],
"extensions": {
"KHR_materials_emissive_strength": {
"emissiveStrength": 4.0
},
"VRMC_materials_hdr_emissiveMultiplier": {
"emissiveMultiplier": 10.0
}
}
}))
.expect("the material must load");
assert_eq!(material.emissive_strength(), 4.0);
assert_eq!(material.effective_emissive_factor(), [1.0, 2.0, 4.0]);
assert_eq!(
material.emissive_strength_source(),
EmissiveStrengthSource::KhrMaterialsEmissiveStrength
);
assert_eq!(material.legacy_emissive_multiplier(), Some(10.0));
}
#[test]
fn archived_emissive_multiplier_is_used_as_a_fallback() {
let material = load_test_material(serde_json::json!({
"emissiveFactor": [0.25, 0.5, 1.0],
"extensions": {
"VRMC_materials_hdr_emissiveMultiplier": {
"emissiveMultiplier": 2.0
}
}
}))
.expect("the archived material extension must load");
assert_eq!(material.emissive_strength(), 2.0);
assert_eq!(material.effective_emissive_factor(), [0.5, 1.0, 2.0]);
assert_eq!(
material.emissive_strength_source(),
EmissiveStrengthSource::VrmcMaterialsHdrEmissiveMultiplier
);
}
#[test]
fn mtoon_takes_precedence_over_unlit_without_losing_the_unlit_flag() {
let material = load_test_material(serde_json::json!({
"extensions": {
"KHR_materials_unlit": {},
"VRMC_materials_mtoon": {
"specVersion": "1.0"
}
}
}))
.expect("the material must load");
assert!(material.unlit());
assert!(material.mtoon().is_some());
assert_eq!(material.shading_model(), MaterialShadingModel::MToon);
}
#[test]
fn rejects_negative_emissive_strength_values() {
for extension in [
serde_json::json!({
"KHR_materials_emissive_strength": { "emissiveStrength": -1.0 }
}),
serde_json::json!({
"VRMC_materials_hdr_emissiveMultiplier": { "emissiveMultiplier": -1.0 }
}),
] {
let result = load_test_material(serde_json::json!({ "extensions": extension }));
assert!(
result.is_err(),
"negative emissive strength must be rejected"
);
}
}
}