From b2f342d7e12a584a6051df8fe0622fd22466e072 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 1 Jun 2021 16:55:34 +0900 Subject: [PATCH 1/3] rename VRM10SpringJoint to VRM10SpringBoneJoint --- .../{VRM10SpringJoint.cs => VRM10SpringBoneJoint.cs} | 5 +++-- ...RM10SpringJoint.cs.meta => VRM10SpringBoneJoint.cs.meta} | 2 +- .../VRM10/Runtime/Components/VRM10ControllerSpringBone.cs | 6 ++++-- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 2 +- Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) rename Assets/VRM10/Runtime/Components/SpringBone/{VRM10SpringJoint.cs => VRM10SpringBoneJoint.cs} (94%) rename Assets/VRM10/Runtime/Components/SpringBone/{VRM10SpringJoint.cs.meta => VRM10SpringBoneJoint.cs.meta} (83%) diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs similarity index 94% rename from Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs rename to Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs index d9889c509..18ae0f25b 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs @@ -8,7 +8,7 @@ using UnityEditor; namespace UniVRM10 { [Serializable] - public class VRM10SpringJoint : MonoBehaviour + public class VRM10SpringBoneJoint : MonoBehaviour { [SerializeField, Range(0, 4), Header("Settings")] public float m_stiffnessForce = 1.0f; @@ -46,7 +46,7 @@ namespace UniVRM10 } } - public void Process(Transform center, float deltaTime, List colliders, VRM10SpringJoint tail) + public void Process(Transform center, float deltaTime, List colliders, VRM10SpringBoneJoint tail) { if (m_logic == null) { @@ -73,5 +73,6 @@ namespace UniVRM10 m_logic.Update(center, m_stiffnessForce * deltaTime, m_dragForce, m_gravityDir * (m_gravityPower * deltaTime), colliders, m_jointRadius); } + } } diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs.meta b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs.meta similarity index 83% rename from Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs.meta rename to Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs.meta index 0fab6c6f8..1f0db6050 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs.meta +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5a7fc353e202e4f44a1025ba6e806262 +guid: 0a942e03b39600e41a1b161e958048f7 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs b/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs index 55861be9a..01cb8f848 100644 --- a/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs +++ b/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs @@ -32,10 +32,11 @@ namespace UniVRM10 public List ColliderGroups = new List(); [SerializeField] - public List Joints = new List(); + public List Joints = new List(); Transform m_center; List m_colliderList; + List<(VRM10SpringBoneJoint, SpringBoneLogic)> m_logics; public Spring(string name) { @@ -90,7 +91,7 @@ namespace UniVRM10 { // udpate joints - VRM10SpringJoint lastJoint = Joints.FirstOrDefault(x => x != null); + VRM10SpringBoneJoint lastJoint = Joints.FirstOrDefault(x => x != null); foreach (var joint in Joints.Where(x => x != null).Skip(1)) { lastJoint.Process(center, Time.deltaTime, m_colliderList, joint); @@ -100,6 +101,7 @@ namespace UniVRM10 } } } + [SerializeField] public List Springs = new List(); diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 10121ce1a..a0032e254 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -299,7 +299,7 @@ namespace UniVRM10 return shape; } - UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint ExportJoint(VRM10SpringJoint y, Func getIndexFromTransform) + UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint ExportJoint(VRM10SpringBoneJoint y, Func getIndexFromTransform) { var joint = new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index d818d0779..82fd037fe 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -429,7 +429,7 @@ namespace UniVRM10 { throw new IndexOutOfRangeException($"{index} > {Nodes.Count}"); } - var joint = Nodes[gltfJoint.Node.Value].gameObject.AddComponent(); + var joint = Nodes[gltfJoint.Node.Value].gameObject.AddComponent(); joint.m_jointRadius = gltfJoint.HitRadius.Value; joint.m_dragForce = gltfJoint.DragForce.Value; joint.m_gravityDir = Vector3InvertX(gltfJoint.GravityDir); From e3ffffbd6f6d32690ceb4d7473fc95ea478ef50b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 1 Jun 2021 17:03:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?SpringBone=20=E6=9C=AB=E7=AB=AF=E3=81=AE?= =?UTF-8?q?=E4=BB=95=E6=A7=98=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/vrm-c/vrm-specification/pull/255 --- .../SpringBone/VRM10SpringBoneJoint.cs | 29 ------------------- .../Components/VRM10ControllerSpringBone.cs | 27 ++++++++++++----- 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs index 18ae0f25b..9ad55c194 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs @@ -45,34 +45,5 @@ namespace UniVRM10 #endif } } - - public void Process(Transform center, float deltaTime, List colliders, VRM10SpringBoneJoint tail) - { - if (m_logic == null) - { - // 初期化 - if (tail != null) - { - var localPosition = tail.transform.localPosition; - var scale = tail.transform.lossyScale; - m_logic = new SpringBoneLogic(center, transform, - new Vector3( - localPosition.x * scale.x, - localPosition.y * scale.y, - localPosition.z * scale.z - )); - } - else - { - // 親からまっすぐの位置に tail を作成 - var delta = transform.position - transform.parent.position; - var childPosition = transform.position + delta.normalized * 0.07f; - m_logic = new SpringBoneLogic(center, transform, transform.worldToLocalMatrix.MultiplyPoint(childPosition)); - } - } - - m_logic.Update(center, m_stiffnessForce * deltaTime, m_dragForce, m_gravityDir * (m_gravityPower * deltaTime), colliders, m_jointRadius); - } - } } diff --git a/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs b/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs index 01cb8f848..e664d0239 100644 --- a/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs +++ b/Assets/VRM10/Runtime/Components/VRM10ControllerSpringBone.cs @@ -89,15 +89,28 @@ namespace UniVRM10 } } + if (m_logics == null) { - // udpate joints - VRM10SpringBoneJoint lastJoint = Joints.FirstOrDefault(x => x != null); - foreach (var joint in Joints.Where(x => x != null).Skip(1)) + m_logics = Enumerable.Zip(Joints, Joints.Skip(1), (head, tail) => { - lastJoint.Process(center, Time.deltaTime, m_colliderList, joint); - lastJoint = joint; - } - lastJoint.Process(center, Time.deltaTime, m_colliderList, null); + var localPosition = tail.transform.localPosition; + var scale = tail.transform.lossyScale; + var logic = new SpringBoneLogic(center, head.transform, + new Vector3( + localPosition.x * scale.x, + localPosition.y * scale.y, + localPosition.z * scale.z + )); + return (head, logic); + }).ToList(); + } + foreach (var (head, logic) in m_logics) + { + logic.Update(center, + head.m_stiffnessForce * Time.deltaTime, + head.m_dragForce, + head.m_gravityDir * (head.m_gravityPower * Time.deltaTime), + m_colliderList, head.m_jointRadius); } } } From a7464374ab0d398b17fdd05f30abe4cb2e94e12e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 1 Jun 2021 17:29:36 +0900 Subject: [PATCH 3/3] =?UTF-8?q?spring=20bone=20=E3=81=AE=E6=9C=AB=E7=AB=AF?= =?UTF-8?q?=E3=81=AB=E8=BF=BD=E5=8A=A0=E3=81=AE=20joint=20=E3=82=92?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/vrm-c/vrm-specification/pull/255 --- .../Migration/MigrationVrmSpringBone.cs | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index bb8deeb59..47e02e07b 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -6,19 +6,49 @@ namespace UniVRM10 { public static class MigrationVrmSpringBone { - static IEnumerable EnumJoint(List nodes, UniGLTF.glTFNode node) + static IEnumerable TraverseFirstChild(List nodes, UniGLTF.glTFNode node) { yield return node; if (node.children != null && node.children.Length > 0) { - foreach (var x in EnumJoint(nodes, nodes[node.children[0]])) + foreach (var x in TraverseFirstChild(nodes, nodes[node.children[0]])) { yield return x; } } } + static void AddTail7cm(UniGLTF.glTF gltf, UniGLTF.glTFNode[] joints) + { + if (joints.Length < 2) + { + return; + } + var last = joints.Last(); + var name = last.name ?? ""; + var v1 = new UnityEngine.Vector3(last.translation[0], last.translation[1], last.translation[2]); + // var last2 = joints[joints.Length - 2]; + // var v2 = new UnityEngine.Vector3(last2.translation[0], last2.translation[1], last2.translation[2]); + var delta = v1.normalized * 0.07f; // 7cm + var tail = new UniGLTF.glTFNode + { + name = name + "_end", + translation = new float[] { + delta.x, + delta.y, + delta.z + }, + }; + var tail_index = gltf.nodes.Count; + gltf.nodes.Add(tail); + if (last.children != null && last.children.Length > 0) + { + throw new System.Exception(); + } + last.children = new[] { tail_index }; + } + public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode sa) { var colliderNodes = new List(); @@ -86,6 +116,18 @@ namespace UniVRM10 } } + // https://github.com/vrm-c/vrm-specification/pull/255 + // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 + // その差異に対応して、7cmの遠さに node を追加する。 + foreach (var x in sa["boneGroups"].ArrayItems()) + { + foreach (var y in x["bones"].ArrayItems()) + { + var joints = TraverseFirstChild(gltf.nodes, gltf.nodes[y.GetInt32()]).ToArray(); + AddTail7cm(gltf, joints); + } + } + foreach (var x in sa["boneGroups"].ArrayItems()) { // { @@ -123,7 +165,7 @@ namespace UniVRM10 Joints = new List(), }; - foreach (var z in EnumJoint(gltf.nodes, gltf.nodes[y.GetInt32()])) + foreach (var z in TraverseFirstChild(gltf.nodes, gltf.nodes[y.GetInt32()])) { spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint {