From a7464374ab0d398b17fdd05f30abe4cb2e94e12e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 1 Jun 2021 17:29:36 +0900 Subject: [PATCH] =?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 {