using System;
using System.Collections.Generic;
namespace LibMMD.Unity3D
{
public enum MmdPhysicsMode
{
None,
Unity,
Bullet,
MagicaCloth
}
/// Settings captured when a runtime MMD model instance is created.
[Serializable]
public sealed class MmdModelOptions
{
public MmdPhysicsMode Physics { get; set; } = MmdPhysicsMode.Bullet;
public bool AnimationRigging { get; set; } = true;
/// Reuse parsed model, Mesh, material, and texture resources between loads.
public bool UseResourceCache { get; set; } = true;
///
/// Maximum main-thread time spent on a batch of incremental model-build work before
/// yielding to the next frame. Set to 0 to yield after every incremental operation.
///
public float LoadingFrameBudgetMilliseconds { get; set; } = 2f;
public MmdClothOptions Cloth { get; set; } = new MmdClothOptions();
internal MmdModelOptions CopyAndValidate()
{
var cloth = Cloth ?? new MmdClothOptions();
if (cloth.MaterialDominanceThreshold < 0f || cloth.MaterialDominanceThreshold > 1f)
throw new ArgumentOutOfRangeException(
nameof(Cloth.MaterialDominanceThreshold));
if (LoadingFrameBudgetMilliseconds < 0f || float.IsNaN(LoadingFrameBudgetMilliseconds) ||
float.IsInfinity(LoadingFrameBudgetMilliseconds))
throw new ArgumentOutOfRangeException(nameof(LoadingFrameBudgetMilliseconds));
return new MmdModelOptions
{
Physics = Physics,
AnimationRigging = AnimationRigging,
UseResourceCache = UseResourceCache,
LoadingFrameBudgetMilliseconds = LoadingFrameBudgetMilliseconds,
Cloth = cloth.Copy()
};
}
}
/// Optional name overrides used by cloth-capable physics backends.
[Serializable]
public sealed class MmdClothOptions
{
public string[] IncludedBoneNames { get; set; } = Array.Empty();
public string[] ExcludedBoneNames { get; set; } = Array.Empty();
public string[] IncludedMaterialNames { get; set; } = Array.Empty();
public string[] ExcludedMaterialNames { get; set; } = Array.Empty();
public float MaterialDominanceThreshold { get; set; } = 0.1f;
internal MmdClothOptions Copy()
{
return new MmdClothOptions
{
IncludedBoneNames = CopyArray(IncludedBoneNames),
ExcludedBoneNames = CopyArray(ExcludedBoneNames),
IncludedMaterialNames = CopyArray(IncludedMaterialNames),
ExcludedMaterialNames = CopyArray(ExcludedMaterialNames),
MaterialDominanceThreshold = MaterialDominanceThreshold
};
}
private static string[] CopyArray(string[] values) =>
values == null ? Array.Empty() : (string[])values.Clone();
}
}