From d7482417bf85f60cc824cd2d7a7e7d06d3032ca6 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 16:11:06 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Migration/MigrationVrmSpringBone.cs | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index 6e50bcc67..b0a1c705d 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -168,9 +168,16 @@ namespace UniVRM10 // }, if (x.ContainsKey("bones")) { + var comment = x.GetObjectValueOrDefault("comment", ""); + var dragForce = x["dragForce"].GetSingle(); + var gravityDir = MigrateVector3.Migrate(x["gravityDir"]); + var gravityPower = x["gravityPower"].GetSingle(); + var hitRadius = x["hitRadius"].GetSingle(); + var stiffiness = x["stiffiness"].GetSingle(); foreach (var y in x["bones"].ArrayItems()) { - var comment = x.GetObjectValueOrDefault("comment", ""); + var rootBoneIndex = y.GetInt32(); + var spring = new UniGLTF.Extensions.VRMC_springBone.Spring { Name = comment, @@ -178,16 +185,18 @@ namespace UniVRM10 Joints = new List(), }; - foreach (var z in TraverseFirstChild(gltf.nodes, gltf.nodes[y.GetInt32()])) + // create joints + foreach (var z in TraverseFirstChild(gltf.nodes, gltf.nodes[rootBoneIndex])) { + var nodeIndex = gltf.nodes.IndexOf(z); spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { - Node = gltf.nodes.IndexOf(z), - DragForce = x["dragForce"].GetSingle(), - GravityDir = MigrateVector3.Migrate(x["gravityDir"]), - GravityPower = x["gravityPower"].GetSingle(), - HitRadius = x["hitRadius"].GetSingle(), - Stiffness = x["stiffiness"].GetSingle(), + Node = nodeIndex, + DragForce = dragForce, + GravityDir = gravityDir, + GravityPower = gravityPower, + HitRadius = hitRadius, + Stiffness = stiffiness, }); } From 9f9a6898cba3a40b78d676e051e65337b2d0ca5c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 16:29:23 +0900 Subject: [PATCH 2/8] CreateJointsRecursive --- .../Migration/MigrationVrmSpringBone.cs | 69 ++++++++----------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index b0a1c705d..15531f2dc 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using UniJSON; @@ -6,26 +7,31 @@ namespace UniVRM10 { public static class MigrationVrmSpringBone { - static IEnumerable TraverseFirstChild(List nodes, UniGLTF.glTFNode node) + static void CreateJointsRecursive(UniGLTF.Extensions.VRMC_springBone.Spring spring, List nodes, UniGLTF.glTFNode node, Func createJoint) { - yield return node; + spring.Joints.Add(createJoint(nodes.IndexOf(node))); if (node.children != null && node.children.Length > 0) { - foreach (var x in TraverseFirstChild(nodes, nodes[node.children[0]])) - { - yield return x; - } + // 先頭の子ノードを追加する + CreateJointsRecursive(spring, nodes, nodes[node.children[0]], createJoint); + // foreach (var x in TraverseFirstChild(nodes, nodes[node.children[0]])) + // { + // yield return x; + // } + } + else + { + // https://github.com/vrm-c/vrm-specification/pull/255 + // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 + // その差異に対応して、7cmの遠さに node を追加する。 + + // var tail = AddTail7cm(nodes, nodes[spring.Joints.Last().Node]) } } - static void AddTail7cm(UniGLTF.glTF gltf, UniGLTF.glTFNode[] joints) + static int AddTail7cm(List nodes, UniGLTF.glTFNode last) { - 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]; @@ -40,13 +46,14 @@ namespace UniVRM10 delta.z }, }; - var tail_index = gltf.nodes.Count; - gltf.nodes.Add(tail); + var tail_index = nodes.Count; + nodes.Add(tail); if (last.children != null && last.children.Length > 0) { throw new System.Exception(); } last.children = new[] { tail_index }; + return tail_index; } /// @@ -124,21 +131,6 @@ namespace UniVRM10 springBone.ColliderGroups.Add(colliderGroup); } - // https://github.com/vrm-c/vrm-specification/pull/255 - // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 - // その差異に対応して、7cmの遠さに node を追加する。 - foreach (var x in sa["boneGroups"].ArrayItems()) - { - if (x.ContainsKey("bones")) - { - 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()) { // { @@ -176,7 +168,7 @@ namespace UniVRM10 var stiffiness = x["stiffiness"].GetSingle(); foreach (var y in x["bones"].ArrayItems()) { - var rootBoneIndex = y.GetInt32(); + var rootBone = gltf.nodes[y.GetInt32()]; var spring = new UniGLTF.Extensions.VRMC_springBone.Spring { @@ -184,23 +176,20 @@ namespace UniVRM10 ColliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(), Joints = new List(), }; + springBone.Springs.Add(spring); - // create joints - foreach (var z in TraverseFirstChild(gltf.nodes, gltf.nodes[rootBoneIndex])) + CreateJointsRecursive(spring, gltf.nodes, rootBone, (int node) => { - var nodeIndex = gltf.nodes.IndexOf(z); - spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint + return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { - Node = nodeIndex, + Node = node, DragForce = dragForce, GravityDir = gravityDir, GravityPower = gravityPower, HitRadius = hitRadius, Stiffness = stiffiness, - }); - } - - springBone.Springs.Add(spring); + }; + }); } } } From 5f60a386bc9b4cd8051e938a4283aea9e23adcec Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 17:38:47 +0900 Subject: [PATCH 3/8] SpringBoneGroupMigrator --- .../Migration/MigrationVrmSpringBone.cs | 173 +++++++++++------- 1 file changed, 105 insertions(+), 68 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index 15531f2dc..8141a5784 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -7,53 +7,122 @@ namespace UniVRM10 { public static class MigrationVrmSpringBone { - static void CreateJointsRecursive(UniGLTF.Extensions.VRMC_springBone.Spring spring, List nodes, UniGLTF.glTFNode node, Func createJoint) + class SpringBoneGroupMigrator { - spring.Joints.Add(createJoint(nodes.IndexOf(node))); + UniGLTF.glTF _gltf; - if (node.children != null && node.children.Length > 0) + string _comment; + float _dragForce; + float[] _gravityDir; + float _gravityPower; + float _hitRadius; + float _stiffness; + int[] _colliderGroups; + + List _springs = new List(); + public IReadOnlyList Springs => _springs; + + public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode x) { - // 先頭の子ノードを追加する - CreateJointsRecursive(spring, nodes, nodes[node.children[0]], createJoint); - // foreach (var x in TraverseFirstChild(nodes, nodes[node.children[0]])) - // { - // yield return x; - // } + _gltf = gltf; + + _comment = x.GetObjectValueOrDefault("comment", ""); + _dragForce = x["dragForce"].GetSingle(); + _gravityDir = MigrateVector3.Migrate(x["gravityDir"]); + _gravityPower = x["gravityPower"].GetSingle(); + _hitRadius = x["hitRadius"].GetSingle(); + _stiffness = x["stiffiness"].GetSingle(); + _colliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(); + if (x.ContainsKey("bones")) + { + foreach (var y in x["bones"].ArrayItems()) + { + MigrateRootBone(y); + } + } } - else - { - // https://github.com/vrm-c/vrm-specification/pull/255 - // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 - // その差異に対応して、7cmの遠さに node を追加する。 - // var tail = AddTail7cm(nodes, nodes[spring.Joints.Last().Node]) + UniGLTF.Extensions.VRMC_springBone.Spring CreateSpring() + { + var spring = new UniGLTF.Extensions.VRMC_springBone.Spring + { + Name = _comment, + ColliderGroups = _colliderGroups, + Joints = new List(), + }; + _springs.Add(spring); + return spring; } - } - static int AddTail7cm(List nodes, UniGLTF.glTFNode 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 + UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint CreateJoint(int node) { - name = name + "_end", - translation = new float[] { + return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint + { + Node = node, + DragForce = _dragForce, + GravityDir = _gravityDir, + GravityPower = _gravityPower, + HitRadius = _hitRadius, + Stiffness = _stiffness, + }; + } + + void MigrateRootBone(JsonNode y) + { + var rootBoneIndex = y.GetInt32(); + CreateJointsRecursive(CreateSpring(), _gltf.nodes[rootBoneIndex]); + } + + void CreateJointsRecursive(UniGLTF.Extensions.VRMC_springBone.Spring spring, UniGLTF.glTFNode node) + { + spring.Joints.Add(CreateJoint(_gltf.nodes.IndexOf(node))); + + if (node.children != null && node.children.Length > 0) + { + // 先頭の子ノードを追加する + CreateJointsRecursive(spring, _gltf.nodes[node.children[0]]); + + for (int i = 1; i < node.children.Length; ++i) + { + // 2番目以降の子ノードの子から新しい Spring を作る。 + var childIndex = node.children[i]; + } + } + else + { + // https://github.com/vrm-c/vrm-specification/pull/255 + // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 + // その差異に対応して、7cmの遠さに node を追加する。 + + // var tail = AddTail7cm(nodes, nodes[spring.Joints.Last().Node]) + } + } + + int AddTail7cm(List nodes, UniGLTF.glTFNode 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 = nodes.Count; - nodes.Add(tail); - if (last.children != null && last.children.Length > 0) - { - throw new System.Exception(); + }; + var tail_index = nodes.Count; + nodes.Add(tail); + if (last.children != null && last.children.Length > 0) + { + throw new System.Exception(); + } + last.children = new[] { tail_index }; + return tail_index; } - last.children = new[] { tail_index }; - return tail_index; } /// @@ -158,40 +227,8 @@ namespace UniVRM10 // 5 // ] // }, - if (x.ContainsKey("bones")) - { - var comment = x.GetObjectValueOrDefault("comment", ""); - var dragForce = x["dragForce"].GetSingle(); - var gravityDir = MigrateVector3.Migrate(x["gravityDir"]); - var gravityPower = x["gravityPower"].GetSingle(); - var hitRadius = x["hitRadius"].GetSingle(); - var stiffiness = x["stiffiness"].GetSingle(); - foreach (var y in x["bones"].ArrayItems()) - { - var rootBone = gltf.nodes[y.GetInt32()]; - - var spring = new UniGLTF.Extensions.VRMC_springBone.Spring - { - Name = comment, - ColliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(), - Joints = new List(), - }; - springBone.Springs.Add(spring); - - CreateJointsRecursive(spring, gltf.nodes, rootBone, (int node) => - { - return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint - { - Node = node, - DragForce = dragForce, - GravityDir = gravityDir, - GravityPower = gravityPower, - HitRadius = hitRadius, - Stiffness = stiffiness, - }; - }); - } - } + var migrator = new SpringBoneGroupMigrator(gltf, x); + springBone.Springs.AddRange(migrator.Springs); } return springBone; From a90a83a332cf6beab77dfa879cae04d6d512f10d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 18:05:22 +0900 Subject: [PATCH 4/8] fix CreateJointsRecursive --- .../Migration/MigrationVrmSpringBone.cs | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index 8141a5784..852ebda19 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -70,22 +70,53 @@ namespace UniVRM10 void MigrateRootBone(JsonNode y) { var rootBoneIndex = y.GetInt32(); - CreateJointsRecursive(CreateSpring(), _gltf.nodes[rootBoneIndex]); + if (rootBoneIndex >= 0 && rootBoneIndex < _gltf.nodes.Count) + { + // root + CreateJointsRecursive(_gltf.nodes[rootBoneIndex], 1); + } } - void CreateJointsRecursive(UniGLTF.Extensions.VRMC_springBone.Spring spring, UniGLTF.glTFNode node) + /// + /// + /// + /// + /// children[0] のみカウントアップする。その他は0にリセットする + /// + void CreateJointsRecursive(UniGLTF.glTFNode node, int level, UniGLTF.Extensions.VRMC_springBone.Spring spring = null) { - spring.Joints.Add(CreateJoint(_gltf.nodes.IndexOf(node))); + if (spring == null && level > 0) + { + // 2番目以降の子ノードの子から新しい Spring を作る。 + spring = CreateSpring(); + } + if (spring != null) + { + // level==0 のとき(2番目以降の兄弟ボーン)は飛ばす + spring.Joints.Add(CreateJoint(_gltf.nodes.IndexOf(node))); + } if (node.children != null && node.children.Length > 0) { - // 先頭の子ノードを追加する - CreateJointsRecursive(spring, _gltf.nodes[node.children[0]]); - - for (int i = 1; i < node.children.Length; ++i) + for (int i = 0; i < node.children.Length; ++i) { - // 2番目以降の子ノードの子から新しい Spring を作る。 var childIndex = node.children[i]; + if (childIndex < 0 || childIndex >= _gltf.nodes.Count) + { + // -1 など? + continue; + } + + if (i == 0) + { + // spring に joint を追加する + CreateJointsRecursive(_gltf.nodes[childIndex], level + 1, spring); + } + else + { + // 再帰 + CreateJointsRecursive(_gltf.nodes[childIndex], 0); + } } } else From 8ba9fa5555a07d77c768196b85c8a04e43c177f3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 18:12:54 +0900 Subject: [PATCH 5/8] gizmo color --- .../Runtime/Components/SpringBone/VRM10SpringBoneCollider.cs | 4 ++++ .../VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneCollider.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneCollider.cs index 2911a0c96..2c5f8485d 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneCollider.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneCollider.cs @@ -33,10 +33,14 @@ namespace UniVRM10 switch (ColliderType) { case VRM10SpringBoneColliderTypes.Sphere: + Gizmos.color = Color.magenta; Gizmos.DrawWireSphere(Offset, Radius); break; case VRM10SpringBoneColliderTypes.Capsule: + Gizmos.color = Color.cyan; + Gizmos.DrawWireSphere(Offset, Radius); + Gizmos.DrawWireSphere(Tail, Radius); break; } } diff --git a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs index 575cf5104..5d0e7d7b9 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs @@ -106,12 +106,13 @@ namespace UniVRM10 private void OnDrawGizmos() { + Gizmos.color = Color.green; foreach (var spring in SpringBone.Springs) { foreach (var (head, tail) in spring.EnumHeadTail()) { Gizmos.DrawLine(head.transform.position, tail.transform.position); - Gizmos.DrawSphere(tail.transform.position, head.m_jointRadius); + Gizmos.DrawWireSphere(tail.transform.position, head.m_jointRadius); } } } From 0b932a4de5766d2979fca170581bbb354647eaae Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 12 Jan 2022 18:24:15 +0900 Subject: [PATCH 6/8] fix AddTail7cm --- .../Migration/MigrationVrmSpringBone.cs | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index 852ebda19..98f5edfee 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -121,20 +121,27 @@ namespace UniVRM10 } else { - // https://github.com/vrm-c/vrm-specification/pull/255 - // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 - // その差異に対応して、7cmの遠さに node を追加する。 - // var tail = AddTail7cm(nodes, nodes[spring.Joints.Last().Node]) + if (spring != null && spring.Joints.Count > 0) + { + var last = spring.Joints.Last().Node; + if (last.HasValue) + { + var tailJoint = AddTail7cm(last.Value); + spring.Joints.Add(tailJoint); + } + } } } - int AddTail7cm(List nodes, UniGLTF.glTFNode last) + // https://github.com/vrm-c/vrm-specification/pull/255 + // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 + // その差異に対応して、7cmの遠さに node を追加する。 + UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint AddTail7cm(int lastIndex) { + var last = _gltf.nodes[lastIndex]; 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 { @@ -145,14 +152,21 @@ namespace UniVRM10 delta.z }, }; - var tail_index = nodes.Count; - nodes.Add(tail); + 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 }; - return tail_index; + + // 1.0 では、head + tail のペアでスプリングを表し、 + // 揺れ挙動のパラメーターは head の方に入る。 + // 要するに 末端の joint では Node しか使われない。 + return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint + { + Node = tail_index, + }; } } From dcb81e89c0246e03a1e22a1c087155127a0e1d8d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 Jan 2022 16:33:14 +0900 Subject: [PATCH 7/8] rename JsonNode var --- .../Migration/MigrationVrmSpringBone.cs | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index 98f5edfee..f53759cff 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -22,22 +22,22 @@ namespace UniVRM10 List _springs = new List(); public IReadOnlyList Springs => _springs; - public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode x) + public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode vrm0BoneGroup) { _gltf = gltf; - _comment = x.GetObjectValueOrDefault("comment", ""); - _dragForce = x["dragForce"].GetSingle(); - _gravityDir = MigrateVector3.Migrate(x["gravityDir"]); - _gravityPower = x["gravityPower"].GetSingle(); - _hitRadius = x["hitRadius"].GetSingle(); - _stiffness = x["stiffiness"].GetSingle(); - _colliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(); - if (x.ContainsKey("bones")) + _comment = vrm0BoneGroup.GetObjectValueOrDefault("comment", ""); + _dragForce = vrm0BoneGroup["dragForce"].GetSingle(); + _gravityDir = MigrateVector3.Migrate(vrm0BoneGroup["gravityDir"]); + _gravityPower = vrm0BoneGroup["gravityPower"].GetSingle(); + _hitRadius = vrm0BoneGroup["hitRadius"].GetSingle(); + _stiffness = vrm0BoneGroup["stiffiness"].GetSingle(); + _colliderGroups = vrm0BoneGroup["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(); + if (vrm0BoneGroup.ContainsKey("bones")) { - foreach (var y in x["bones"].ArrayItems()) + foreach (var vrm0Bone in vrm0BoneGroup["bones"].ArrayItems()) { - MigrateRootBone(y); + MigrateRootBone(vrm0Bone.GetInt32()); } } } @@ -67,9 +67,8 @@ namespace UniVRM10 }; } - void MigrateRootBone(JsonNode y) + void MigrateRootBone(int rootBoneIndex) { - var rootBoneIndex = y.GetInt32(); if (rootBoneIndex >= 0 && rootBoneIndex < _gltf.nodes.Count) { // root @@ -179,9 +178,9 @@ namespace UniVRM10 /// } /// /// - /// + /// /// - public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode sa) + public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var springBone = new UniGLTF.Extensions.VRMC_springBone.VRMC_springBone { @@ -190,7 +189,7 @@ namespace UniVRM10 Springs = new List(), }; - foreach (var x in sa["colliderGroups"].ArrayItems()) + foreach (var vrm0ColliderGroup in vrm0["colliderGroups"].ArrayItems()) { // { // "node": 14, @@ -222,18 +221,18 @@ namespace UniVRM10 // ] // }, var colliders = new List(); - foreach (var y in x["colliders"].ArrayItems()) + foreach (var vrm0Collider in vrm0ColliderGroup["colliders"].ArrayItems()) { colliders.Add(springBone.Colliders.Count); springBone.Colliders.Add(new UniGLTF.Extensions.VRMC_springBone.Collider { - Node = x["node"].GetInt32(), + Node = vrm0ColliderGroup["node"].GetInt32(), Shape = new UniGLTF.Extensions.VRMC_springBone.ColliderShape { Sphere = new UniGLTF.Extensions.VRMC_springBone.ColliderShapeSphere { - Offset = MigrateVector3.Migrate(y["offset"]), - Radius = y["radius"].GetSingle() + Offset = MigrateVector3.Migrate(vrm0Collider["offset"]), + Radius = vrm0Collider["radius"].GetSingle() } } }); @@ -245,7 +244,7 @@ namespace UniVRM10 springBone.ColliderGroups.Add(colliderGroup); } - foreach (var x in sa["boneGroups"].ArrayItems()) + foreach (var vrm0BoneGroup in vrm0["boneGroups"].ArrayItems()) { // { // "comment": "", @@ -272,7 +271,7 @@ namespace UniVRM10 // 5 // ] // }, - var migrator = new SpringBoneGroupMigrator(gltf, x); + var migrator = new SpringBoneGroupMigrator(gltf, vrm0BoneGroup); springBone.Springs.AddRange(migrator.Springs); } From 65f88a8c7d1f84caf20c7a35989f03afbba18afa Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 Jan 2022 16:39:10 +0900 Subject: [PATCH 8/8] using UniGLTF.Extensions.VRMC_springBone; --- .../Migration/MigrationVrmSpringBone.cs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs index f53759cff..132aad0f7 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmSpringBone.cs @@ -1,6 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; +using UniGLTF.Extensions.VRMC_springBone; using UniJSON; namespace UniVRM10 @@ -19,8 +19,8 @@ namespace UniVRM10 float _stiffness; int[] _colliderGroups; - List _springs = new List(); - public IReadOnlyList Springs => _springs; + List _springs = new List(); + public IReadOnlyList Springs => _springs; public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode vrm0BoneGroup) { @@ -42,21 +42,21 @@ namespace UniVRM10 } } - UniGLTF.Extensions.VRMC_springBone.Spring CreateSpring() + Spring CreateSpring() { - var spring = new UniGLTF.Extensions.VRMC_springBone.Spring + var spring = new Spring { Name = _comment, ColliderGroups = _colliderGroups, - Joints = new List(), + Joints = new List(), }; _springs.Add(spring); return spring; } - UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint CreateJoint(int node) + SpringBoneJoint CreateJoint(int node) { - return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint + return new SpringBoneJoint { Node = node, DragForce = _dragForce, @@ -82,7 +82,7 @@ namespace UniVRM10 /// /// children[0] のみカウントアップする。その他は0にリセットする /// - void CreateJointsRecursive(UniGLTF.glTFNode node, int level, UniGLTF.Extensions.VRMC_springBone.Spring spring = null) + void CreateJointsRecursive(UniGLTF.glTFNode node, int level, Spring spring = null) { if (spring == null && level > 0) { @@ -136,7 +136,7 @@ namespace UniVRM10 // https://github.com/vrm-c/vrm-specification/pull/255 // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 // その差異に対応して、7cmの遠さに node を追加する。 - UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint AddTail7cm(int lastIndex) + SpringBoneJoint AddTail7cm(int lastIndex) { var last = _gltf.nodes[lastIndex]; var name = last.name ?? ""; @@ -162,7 +162,7 @@ namespace UniVRM10 // 1.0 では、head + tail のペアでスプリングを表し、 // 揺れ挙動のパラメーターは head の方に入る。 // 要するに 末端の joint では Node しか使われない。 - return new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint + return new SpringBoneJoint { Node = tail_index, }; @@ -180,13 +180,13 @@ namespace UniVRM10 /// /// /// - public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode vrm0) + public static VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { - var springBone = new UniGLTF.Extensions.VRMC_springBone.VRMC_springBone + var springBone = new VRMC_springBone { - Colliders = new List(), - ColliderGroups = new List(), - Springs = new List(), + Colliders = new List(), + ColliderGroups = new List(), + Springs = new List(), }; foreach (var vrm0ColliderGroup in vrm0["colliderGroups"].ArrayItems()) @@ -224,12 +224,12 @@ namespace UniVRM10 foreach (var vrm0Collider in vrm0ColliderGroup["colliders"].ArrayItems()) { colliders.Add(springBone.Colliders.Count); - springBone.Colliders.Add(new UniGLTF.Extensions.VRMC_springBone.Collider + springBone.Colliders.Add(new Collider { Node = vrm0ColliderGroup["node"].GetInt32(), - Shape = new UniGLTF.Extensions.VRMC_springBone.ColliderShape + Shape = new ColliderShape { - Sphere = new UniGLTF.Extensions.VRMC_springBone.ColliderShapeSphere + Sphere = new ColliderShapeSphere { Offset = MigrateVector3.Migrate(vrm0Collider["offset"]), Radius = vrm0Collider["radius"].GetSingle() @@ -237,7 +237,7 @@ namespace UniVRM10 } }); } - var colliderGroup = new UniGLTF.Extensions.VRMC_springBone.ColliderGroup() + var colliderGroup = new ColliderGroup() { Colliders = colliders.ToArray(), };