From 2588cf45ee846d1e782a86accd9bba1df9ee3e4a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 20:16:48 +0900 Subject: [PATCH 1/8] =?UTF-8?q?editor=20=E3=81=AE=20null=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs index 15285914d..dbd828659 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs @@ -47,7 +47,7 @@ namespace UniVRM10 public override void OnToolGUI(EditorWindow window) { - var root = Selection.activeTransform.GetComponent(); + var root = Selection.activeTransform?.GetComponent(); if (root == null) { return; From 9ce7cabc1727a82423de462b5026f9bb90d5013a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 20:17:51 +0900 Subject: [PATCH 2/8] =?UTF-8?q?ScriptableObject=20=E3=81=AB=20100=E5=80=8D?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=81=9F=E5=80=A4=E3=82=92=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=AE=E3=82=92=E3=82=84=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SetBlendShapeWeight 時に 100 倍する --- .../Runtime/Components/Expression/MorphTargetBinding.cs | 7 ++++++- .../Components/Expression/MorphTargetBindingMerger.cs | 3 ++- Assets/VRM10/Runtime/IO/ExpressionExtensions.cs | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs index 6d5fb1c4d..38326ba25 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs @@ -1,4 +1,5 @@ using System; +using UnityEngine; namespace UniVRM10 { @@ -27,11 +28,15 @@ namespace UniVRM10 /// /// /// - /// 0 to 100 + /// 0 to 1.0 public MorphTargetBinding(string path, int index, float weight) { RelativePath = path; Index = index; + if (weight > 1.0f) + { + Debug.LogWarning($"weight exceed 1.0"); + } Weight = weight; } diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs index 0606f7fc5..b09a0e574 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs @@ -59,7 +59,8 @@ namespace UniVRM10 { m_morphTargetSetterMap.Add(binding, x => { - target.SetBlendShapeWeight(binding.Index, x); + // VRM-1.0 weight is 0-1 + target.SetBlendShapeWeight(binding.Index, x * 100.0f); }); } else diff --git a/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs b/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs index efba8431b..189c60547 100644 --- a/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs +++ b/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs @@ -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 materials) From 73f2b39907247fe5a76802d8025c10ccb56105a5 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 20:45:04 +0900 Subject: [PATCH 3/8] add test --- Assets/VRM10/Tests/SerializationTests.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Assets/VRM10/Tests/SerializationTests.cs b/Assets/VRM10/Tests/SerializationTests.cs index 85f0aca51..1b7f2c6de 100644 --- a/Assets/VRM10/Tests/SerializationTests.cs +++ b/Assets/VRM10/Tests/SerializationTests.cs @@ -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{ + 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); } { From b0d8cfa567d01d6f5c47645d92a121e77678c1cc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 21:10:58 +0900 Subject: [PATCH 4/8] =?UTF-8?q?GetBlendShapeWeight,=20SetBlendShapeWeight?= =?UTF-8?q?=20=E5=89=8D=E5=BE=8C=E3=81=A7100=E5=80=8D=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Expression/SerializedExpressionEditor.cs | 8 ++++---- .../Components/Expression/Preview/PreviewMeshItem.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs b/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs index 249bea2f0..aa3c9c9c0 100644 --- a/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs +++ b/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs @@ -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) * 0.01f; + var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, 1.0f); if (dst != src) { - renderer.SetBlendShapeWeight(i, dst); + renderer.SetBlendShapeWeight(i, dst * 100.0f); changed = true; } } @@ -236,7 +236,7 @@ namespace UniVRM10 maxWeightName = name; maxWeight = weight; } - list.Add(new MorphTargetBinding(relativePath, i, weight)); + list.Add(new MorphTargetBinding(relativePath, i, weight * 0.01f)); } } return list; diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs index e5a4348ae..0961410c1 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs @@ -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 * 100.0f); } else { From b1c431a04e91483cfed939d3beff0c576ca01f84 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 21:13:10 +0900 Subject: [PATCH 5/8] fix slider --- .../Editor/Components/Expression/ExpressionEditorHelper.cs | 2 +- .../Components/Expression/ReorderableMorphTargetBindingList.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Expression/ExpressionEditorHelper.cs b/Assets/VRM10/Editor/Components/Expression/ExpressionEditorHelper.cs index d14f699ed..bfeb668cf 100644 --- a/Assets/VRM10/Editor/Components/Expression/ExpressionEditorHelper.cs +++ b/Assets/VRM10/Editor/Components/Expression/ExpressionEditorHelper.cs @@ -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; diff --git a/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs b/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs index 5f1bdabe6..ec0277a4e 100644 --- a/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs +++ b/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs @@ -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)), 1.0f)) { changed = true; } From 4533a7f0bfed65249ad6012a87d65703d62af59c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 21:52:47 +0900 Subject: [PATCH 6/8] const --- .../ReorderableMorphTargetBindingList.cs | 2 +- .../Expression/SerializedExpressionEditor.cs | 18 +++++++++--------- .../Expression/MorphTargetBinding.cs | 12 ++++++++++++ .../Expression/MorphTargetBindingMerger.cs | 7 ++++++- .../Expression/Preview/PreviewMeshItem.cs | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs b/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs index ec0277a4e..98f962115 100644 --- a/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs +++ b/Assets/VRM10/Editor/Components/Expression/ReorderableMorphTargetBindingList.cs @@ -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)), 1.0f)) + if (ExpressionEditorHelper.FloatSlider(rect, property.FindPropertyRelative(nameof(MorphTargetBinding.Weight)), MorphTargetBinding.MAX_WEIGHT)) { changed = true; } diff --git a/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs b/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs index aa3c9c9c0..782d355a3 100644 --- a/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs +++ b/Assets/VRM10/Editor/Components/Expression/SerializedExpressionEditor.cs @@ -192,11 +192,11 @@ namespace UniVRM10 //EditorGUI.indentLevel += 1; for (int i = 0; i < mesh.blendShapeCount; ++i) { - var src = renderer.GetBlendShapeWeight(i) * 0.01f; - var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, 1.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 * 100.0f); + 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 * 0.01f)); + list.Add(new MorphTargetBinding(relativePath, i, unityWeight * MorphTargetBinding.UNITY_TO_VRM)); } } return list; diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs index 38326ba25..2195f51fa 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs @@ -6,6 +6,18 @@ namespace UniVRM10 [Serializable] public struct MorphTargetBinding : IEquatable { + /// + /// Unity の BlendShape の値域は 0-100 + /// + public const float VRM_TO_UNITY = 100.0f; + + /// + /// VRM-1.0 の MorphTargetBinding.Weight の値域は 0-1.0 + /// + public const float UNITY_TO_VRM = 0.01f; + + public const float MAX_WEIGHT = 1.0f; + /// /// SkinnedMeshRenderer.BlendShape[Index].Weight を 指し示す。 /// diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs index b09a0e574..f9758414f 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger.cs @@ -59,8 +59,13 @@ namespace UniVRM10 { m_morphTargetSetterMap.Add(binding, x => { + if (target == null) + { + // recompile in editor ? + return; + } // VRM-1.0 weight is 0-1 - target.SetBlendShapeWeight(binding.Index, x * 100.0f); + target.SetBlendShapeWeight(binding.Index, x * MorphTargetBinding.VRM_TO_UNITY); }); } else diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs index 0961410c1..913f9cf53 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs @@ -79,7 +79,7 @@ namespace UniVRM10 { if (x.Index >= 0 && x.Index < SkinnedMeshRenderer.sharedMesh.blendShapeCount) { - SkinnedMeshRenderer.SetBlendShapeWeight(x.Index, x.Weight * weight * 100.0f); + SkinnedMeshRenderer.SetBlendShapeWeight(x.Index, x.Weight * weight * MorphTargetBinding.VRM_TO_UNITY); } else { From f4120c7722ff894bbd7d1d55b5082c50c2836475 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 21:54:02 +0900 Subject: [PATCH 7/8] =?UTF-8?q?UnityObject=20=E3=81=A0=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs index dbd828659..2a9cba1e5 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs @@ -47,7 +47,11 @@ namespace UniVRM10 public override void OnToolGUI(EditorWindow window) { - var root = Selection.activeTransform?.GetComponent(); + if (Selection.activeTransform == null) + { + return; + } + var root = Selection.activeTransform.GetComponent(); if (root == null) { return; From 5ed63e6aef4478658c12576cac0d175b24e7a6a3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 24 Nov 2021 21:56:10 +0900 Subject: [PATCH 8/8] warning message use MAX_WEIGHT --- .../VRM10/Runtime/Components/Expression/MorphTargetBinding.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs index 2195f51fa..9a4ca77c3 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBinding.cs @@ -45,9 +45,9 @@ namespace UniVRM10 { RelativePath = path; Index = index; - if (weight > 1.0f) + if (weight > MAX_WEIGHT) { - Debug.LogWarning($"weight exceed 1.0"); + Debug.LogWarning($"MorphTargetBinding: {weight} > {MAX_WEIGHT}"); } Weight = weight; }