From 87a7212a4cdcde85b67ee867445f13bd8ebdac10 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 5 Apr 2018 18:01:46 +0900 Subject: [PATCH] implement BlendShape LookAt --- Scripts/Format/Editor/VRMExporter.cs | 7 +++-- Scripts/Format/VRMImporter.cs | 27 ++++++++++++---- Scripts/Format/VRMVersion.cs | 8 ++--- Scripts/Format/glTF_VRM_FirstPerson.cs | 21 +++++++++++++ Scripts/LookAt/Editor/VRMLookAtHeadEditor.cs | 2 +- Scripts/LookAt/VRMLookAtBlendShapeApplyer.cs | 33 ++++++++++---------- 6 files changed, 68 insertions(+), 30 deletions(-) diff --git a/Scripts/Format/Editor/VRMExporter.cs b/Scripts/Format/Editor/VRMExporter.cs index ec747f886..8f29b7ea1 100644 --- a/Scripts/Format/Editor/VRMExporter.cs +++ b/Scripts/Format/Editor/VRMExporter.cs @@ -208,6 +208,7 @@ namespace VRM var blendShapeApplyer = exporter.Copy.GetComponent(); if (boneApplyer != null) { + gltf.extensions.VRM.firstPerson.lookAtType = LookAtType.Bone; gltf.extensions.VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner); gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter); gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown); @@ -215,8 +216,10 @@ namespace VRM } else if (blendShapeApplyer != null) { - // ToDo - throw new NotImplementedException(); + gltf.extensions.VRM.firstPerson.lookAtType = LookAtType.BlendShape; + gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal); + gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown); + gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp); } } else if (lookAt != null) diff --git a/Scripts/Format/VRMImporter.cs b/Scripts/Format/VRMImporter.cs index f7f350082..e407f1e33 100644 --- a/Scripts/Format/VRMImporter.cs +++ b/Scripts/Format/VRMImporter.cs @@ -192,12 +192,27 @@ namespace VRM // LookAt var lookAtHead = context.Root.AddComponent(); - var lookAt = context.Root.AddComponent(); - lookAt.HorizontalInner.Apply(gltfFirstPerson.lookAtHorizontalInner); - lookAt.HorizontalOuter.Apply(gltfFirstPerson.lookAtHorizontalOuter); - lookAt.VerticalDown.Apply(gltfFirstPerson.lookAtVerticalDown); - lookAt.VerticalUp.Apply(gltfFirstPerson.lookAtVerticalUp); - //lookAt.GetBones(); + switch(gltfFirstPerson.lookAtType) + { + case LookAtType.Bone: + { + var applyer = context.Root.AddComponent(); + applyer.HorizontalInner.Apply(gltfFirstPerson.lookAtHorizontalInner); + applyer.HorizontalOuter.Apply(gltfFirstPerson.lookAtHorizontalOuter); + applyer.VerticalDown.Apply(gltfFirstPerson.lookAtVerticalDown); + applyer.VerticalUp.Apply(gltfFirstPerson.lookAtVerticalUp); + } + break; + + case LookAtType.BlendShape: + { + var applyer = context.Root.AddComponent(); + applyer.Horizontal.Apply(gltfFirstPerson.lookAtHorizontalOuter); + applyer.VerticalDown.Apply(gltfFirstPerson.lookAtVerticalDown); + applyer.VerticalUp.Apply(gltfFirstPerson.lookAtVerticalUp); + } + break; + } } static void LoadSecondaryMotions(VRMImporterContext context) diff --git a/Scripts/Format/VRMVersion.cs b/Scripts/Format/VRMVersion.cs index 3d4a3dd0d..640f3ae5d 100644 --- a/Scripts/Format/VRMVersion.cs +++ b/Scripts/Format/VRMVersion.cs @@ -4,11 +4,11 @@ namespace VRM public static class VRMVersion { public const int MAJOR = 0; - public const int MINOR = 14; + public const int MINOR = 15; - public const string VERSION = "0.14"; + public const string VERSION = "0.15"; - public const string DecrementMenuName = "VRM/Version(0.14) Decrement"; - public const string IncrementMenuName = "VRM/Version(0.14) Increment"; + public const string DecrementMenuName = "VRM/Version(0.15) Decrement"; + public const string IncrementMenuName = "VRM/Version(0.15) Increment"; } } diff --git a/Scripts/Format/glTF_VRM_FirstPerson.cs b/Scripts/Format/glTF_VRM_FirstPerson.cs index 7f6c1b7e5..1b07b839c 100644 --- a/Scripts/Format/glTF_VRM_FirstPerson.cs +++ b/Scripts/Format/glTF_VRM_FirstPerson.cs @@ -55,6 +55,13 @@ namespace VRM } } + public enum LookAtType + { + None, + Bone, + BlendShape, + } + [Serializable] public class glTF_VRM_Firstperson : UniGLTF.JsonSerializableBase { @@ -65,6 +72,19 @@ namespace VRM public List meshAnnotations = new List(); // lookat + public string lookAtTypeName = "Bone"; + public LookAtType lookAtType + { + get { + if (string.IsNullOrEmpty(lookAtTypeName)) + { + // fallback + return LookAtType.Bone; + } + return (LookAtType)Enum.Parse(typeof(LookAtType), lookAtTypeName, true); + } + set { lookAtTypeName = value.ToString(); } + } public glTF_VRM_DegreeMap lookAtHorizontalInner = new glTF_VRM_DegreeMap(); public glTF_VRM_DegreeMap lookAtHorizontalOuter = new glTF_VRM_DegreeMap(); public glTF_VRM_DegreeMap lookAtVerticalDown = new glTF_VRM_DegreeMap(); @@ -76,6 +96,7 @@ namespace VRM f.KeyValue(() => firstPersonBoneOffset); f.KeyValue(() => meshAnnotations); + f.KeyValue(() => lookAtTypeName); f.KeyValue(() => lookAtHorizontalInner); f.KeyValue(() => lookAtHorizontalOuter); f.KeyValue(() => lookAtVerticalDown); diff --git a/Scripts/LookAt/Editor/VRMLookAtHeadEditor.cs b/Scripts/LookAt/Editor/VRMLookAtHeadEditor.cs index 670965201..409006e72 100644 --- a/Scripts/LookAt/Editor/VRMLookAtHeadEditor.cs +++ b/Scripts/LookAt/Editor/VRMLookAtHeadEditor.cs @@ -18,7 +18,7 @@ namespace VRM void OnSceneGUI() { - if (!Application.isPlaying) return; + //if (!Application.isPlaying) return; if (!m_target.DrawGizmo) return; if (m_target.Target == null) return; if (m_target.Head.Transform == null) return; diff --git a/Scripts/LookAt/VRMLookAtBlendShapeApplyer.cs b/Scripts/LookAt/VRMLookAtBlendShapeApplyer.cs index f54b752a2..afcef38b8 100644 --- a/Scripts/LookAt/VRMLookAtBlendShapeApplyer.cs +++ b/Scripts/LookAt/VRMLookAtBlendShapeApplyer.cs @@ -8,16 +8,14 @@ namespace VRM { public bool DrawGizmo = true; + [SerializeField, Header("Degree Mapping")] + public CurveMapper Horizontal = new CurveMapper(); + [SerializeField] - float Range = 20.0f; + public CurveMapper VerticalDown = new CurveMapper(); - private void Reset() - { - } - - private void OnValidate() - { - } + [SerializeField] + public CurveMapper VerticalUp = new CurveMapper(); VRMLookAtHead m_head; VRMBlendShapeProxy m_propxy; @@ -48,26 +46,27 @@ namespace VRM if (yaw < 0) { // Left - m_propxy.SetValue(BlendShapePreset.LookLeft, -yaw / Range); + m_propxy.SetValue(BlendShapePreset.LookLeft, Horizontal.Map(-yaw)); m_propxy.SetValue(BlendShapePreset.LookRight, 0); } - else{ + else + { // Right m_propxy.SetValue(BlendShapePreset.LookLeft, 0); - m_propxy.SetValue(BlendShapePreset.LookRight, yaw / Range); + m_propxy.SetValue(BlendShapePreset.LookRight, Horizontal.Map(yaw)); } if (pitch < 0) { - // Up - m_propxy.SetValue(BlendShapePreset.LookUp, -pitch / Range); - m_propxy.SetValue(BlendShapePreset.LookDown, 0); + // Down + m_propxy.SetValue(BlendShapePreset.LookUp, 0); + m_propxy.SetValue(BlendShapePreset.LookDown, VerticalDown.Map(-pitch)); } else { - // Down - m_propxy.SetValue(BlendShapePreset.LookUp, 0); - m_propxy.SetValue(BlendShapePreset.LookDown, pitch / Range); + // Up + m_propxy.SetValue(BlendShapePreset.LookUp, VerticalUp.Map(pitch)); + m_propxy.SetValue(BlendShapePreset.LookDown, 0); } } }