Merge pull request #1387 from ousttrue/fix10/expression_weight_100

[1.0] ScriptableObject VRM10Expression の MorphTargetBinding.Weight の値域を 0-1 に修正
This commit is contained in:
ousttrue
2021-11-25 15:07:36 +09:00
committed by GitHub
9 changed files with 48 additions and 17 deletions

View File

@@ -57,7 +57,7 @@ namespace UniVRM10
public static bool FloatSlider(Rect rect, SerializedProperty prop, float maxValue)
{
var oldValue = prop.floatValue;
var newValue = EditorGUI.Slider(rect, prop.floatValue, 0, 100f);
var newValue = EditorGUI.Slider(rect, prop.floatValue, 0, maxValue);
if (newValue != oldValue)
{
prop.floatValue = newValue;

View File

@@ -57,7 +57,7 @@ namespace UniVRM10
y += height;
rect = new Rect(position.x, y, position.width, height);
if (ExpressionEditorHelper.FloatSlider(rect, property.FindPropertyRelative(nameof(MorphTargetBinding.Weight)), 100))
if (ExpressionEditorHelper.FloatSlider(rect, property.FindPropertyRelative(nameof(MorphTargetBinding.Weight)), MorphTargetBinding.MAX_WEIGHT))
{
changed = true;
}

View File

@@ -192,11 +192,11 @@ namespace UniVRM10
//EditorGUI.indentLevel += 1;
for (int i = 0; i < mesh.blendShapeCount; ++i)
{
var src = renderer.GetBlendShapeWeight(i);
var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, 100.0f);
var src = renderer.GetBlendShapeWeight(i) * MorphTargetBinding.UNITY_TO_VRM;
var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, MorphTargetBinding.MAX_WEIGHT);
if (dst != src)
{
renderer.SetBlendShapeWeight(i, dst);
renderer.SetBlendShapeWeight(i, dst * MorphTargetBinding.VRM_TO_UNITY);
changed = true;
}
}
@@ -210,7 +210,7 @@ namespace UniVRM10
MorphTargetBinding[] GetBindings(out string _maxWeightName)
{
var maxWeight = 0.0f;
var maxUnityWeight = 0.0f;
var maxWeightName = "";
// weightのついたblendShapeを集める
var values = m_items
@@ -225,18 +225,18 @@ namespace UniVRM10
{
for (int i = 0; i < mesh.blendShapeCount; ++i)
{
var weight = x.SkinnedMeshRenderer.GetBlendShapeWeight(i);
if (weight == 0)
var unityWeight = x.SkinnedMeshRenderer.GetBlendShapeWeight(i);
if (unityWeight == 0)
{
continue;
}
var name = mesh.GetBlendShapeName(i);
if (weight > maxWeight)
if (unityWeight > maxUnityWeight)
{
maxWeightName = name;
maxWeight = weight;
maxUnityWeight = unityWeight;
}
list.Add(new MorphTargetBinding(relativePath, i, weight));
list.Add(new MorphTargetBinding(relativePath, i, unityWeight * MorphTargetBinding.UNITY_TO_VRM));
}
}
return list;

View File

@@ -47,6 +47,10 @@ namespace UniVRM10
public override void OnToolGUI(EditorWindow window)
{
if (Selection.activeTransform == null)
{
return;
}
var root = Selection.activeTransform.GetComponent<Vrm10Instance>();
if (root == null)
{

View File

@@ -1,10 +1,23 @@
using System;
using UnityEngine;
namespace UniVRM10
{
[Serializable]
public struct MorphTargetBinding : IEquatable<MorphTargetBinding>
{
/// <summary>
/// Unity の BlendShape の値域は 0-100
/// </summary>
public const float VRM_TO_UNITY = 100.0f;
/// <summary>
/// VRM-1.0 の MorphTargetBinding.Weight の値域は 0-1.0
/// </summary>
public const float UNITY_TO_VRM = 0.01f;
public const float MAX_WEIGHT = 1.0f;
/// <summary>
/// SkinnedMeshRenderer.BlendShape[Index].Weight を 指し示す。
///
@@ -27,11 +40,15 @@ namespace UniVRM10
/// </summary>
/// <param name="path"></param>
/// <param name="index"></param>
/// <param name="weight">0 to 100</param>
/// <param name="weight">0 to 1.0</param>
public MorphTargetBinding(string path, int index, float weight)
{
RelativePath = path;
Index = index;
if (weight > MAX_WEIGHT)
{
Debug.LogWarning($"MorphTargetBinding: {weight} > {MAX_WEIGHT}");
}
Weight = weight;
}

View File

@@ -59,7 +59,13 @@ namespace UniVRM10
{
m_morphTargetSetterMap.Add(binding, x =>
{
target.SetBlendShapeWeight(binding.Index, x);
if (target == null)
{
// recompile in editor ?
return;
}
// VRM-1.0 weight is 0-1
target.SetBlendShapeWeight(binding.Index, x * MorphTargetBinding.VRM_TO_UNITY);
});
}
else

View File

@@ -79,7 +79,7 @@ namespace UniVRM10
{
if (x.Index >= 0 && x.Index < SkinnedMeshRenderer.sharedMesh.blendShapeCount)
{
SkinnedMeshRenderer.SetBlendShapeWeight(x.Index, x.Weight * weight);
SkinnedMeshRenderer.SetBlendShapeWeight(x.Index, x.Weight * weight * MorphTargetBinding.VRM_TO_UNITY);
}
else
{

View File

@@ -13,8 +13,7 @@ namespace UniVRM10
var node = loader.Nodes[libNode].transform;
var mesh = loader.Meshes[libNode.MeshGroup];
var relativePath = node.RelativePathFrom(root.transform);
// VRM-1.0 では値域は [0-1.0f]
return new UniVRM10.MorphTargetBinding(relativePath, bind.Index.Value, bind.Weight.Value * 100.0f);
return new UniVRM10.MorphTargetBinding(relativePath, bind.Index.Value, bind.Weight.Value);
}
public static UniVRM10.MaterialColorBinding? Build10(this MaterialColorBind bind, IReadOnlyList<VRMShaders.MaterialFactory.MaterialLoadInfo> materials)

View File

@@ -210,9 +210,14 @@ namespace UniVRM10
{
var data = new UniGLTF.Extensions.VRMC_vrm.Expression();
data.OverrideBlink = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.block;
data.MorphTargetBinds = new List<UniGLTF.Extensions.VRMC_vrm.MorphTargetBind>{
new UniGLTF.Extensions.VRMC_vrm.MorphTargetBind{
Weight=1.0f
}
};
var json = Serialize(data, UniGLTF.Extensions.VRMC_vrm.GltfSerializer.__expressions_Serialize_Custom_ITEM);
Assert.AreEqual($"{{{q}overrideBlink{q}:{q}block{q},{q}overrideLookAt{q}:{q}none{q},{q}overrideMouth{q}:{q}none{q}}}", json);
Assert.AreEqual($"{{{q}morphTargetBinds{q}:[{{{q}weight{q}:1}}],{q}overrideBlink{q}:{q}block{q},{q}overrideLookAt{q}:{q}none{q},{q}overrideMouth{q}:{q}none{q}}}", json);
}
{