public static void SaveSpringBoneToJson()
        {
            var path = EditorUtility.SaveFilePanel(
                "Save spring to json",
                null,
                "VRMSpring.json",
                "json");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var go     = Selection.activeObject as GameObject;
            var root   = go.transform;
            var nodes  = root.Traverse().Skip(1).ToList();
            var spring = new glTF_VRM_SecondaryAnimation();

            VRMSpringUtility.ExportSecondary(root, nodes,
                                             spring.colliderGroups.Add,
                                             spring.boneGroups.Add
                                             );

            var f = new UniJSON.JsonFormatter();

            VRM.VRMSerializer.Serialize_vrm_secondaryAnimation(f, spring);
            File.WriteAllBytes(path, f.GetStore().Bytes.ToArray());
        }
        public static string ToJson(this glTF_VRM_SecondaryAnimation self)
        {
            var f = new JsonFormatter();

            VRMSerializer.Serialize_vrm_secondaryAnimation(f, self);
            return(f.ToString());
        }
        static void SaveSpringBoneToJSON()
        {
            var path = EditorUtility.SaveFilePanel(
                "Save spring to json",
                null,
                "VRMSpring.json",
                "json");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var go     = Selection.activeObject as GameObject;
            var root   = go.transform;
            var nodes  = root.Traverse().Skip(1).ToList();
            var spring = new glTF_VRM_SecondaryAnimation();

            ExportSecondary(root, nodes,
                            spring.colliderGroups.Add,
                            spring.boneGroups.Add
                            );

            File.WriteAllText(path, spring.ToJson());
        }
        public void SecondaryAnimationTest()
        {
            var model = new glTF_VRM_SecondaryAnimation();

            var json = model.ToJson();

            Assert.AreEqual(@"{""boneGroups"":[],""colliderGroups"":[]}", json);
            Debug.Log(json);
        }
Exemple #5
0
        public void SecondaryAnimationTest()
        {
            var model = new glTF_VRM_SecondaryAnimation();

            var json = model.ToJson();

            Assert.AreEqual(@"{""boneGroups"":[],""colliderGroups"":[]}", json);
            Debug.Log(json);

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var json2 = JsonSchema.FromType <glTF_VRM_SecondaryAnimation>().Serialize(model, c);

            // NOTE: New serializer outputs values which will not be used...
            Assert.AreEqual(json, json2);
        }
Exemple #6
0
        public static void Serialize_vrm_secondaryAnimation(JsonFormatter f, glTF_VRM_SecondaryAnimation value)
        {
            f.BeginMap();


            if (value.boneGroups != null && value.boneGroups.Count >= 0)
            {
                f.Key("boneGroups");
                Serialize_vrm_secondaryAnimation_boneGroups(f, value.boneGroups);
            }

            if (value.colliderGroups != null && value.colliderGroups.Count >= 0)
            {
                f.Key("colliderGroups");
                Serialize_vrm_secondaryAnimation_colliderGroups(f, value.colliderGroups);
            }

            f.EndMap();
        }
Exemple #7
0
        public static glTF_VRM_SecondaryAnimation Deserialize_vrm_secondaryAnimation(ListTreeNode <JsonValue> parsed)
        {
            var value = new glTF_VRM_SecondaryAnimation();

            foreach (var kv in parsed.ObjectItems())
            {
                var key = kv.Key.GetString();

                if (key == "boneGroups")
                {
                    value.boneGroups = Deserialize_vrm_secondaryAnimation_boneGroups(kv.Value);
                    continue;
                }

                if (key == "colliderGroups")
                {
                    value.colliderGroups = Deserialize_vrm_secondaryAnimation_colliderGroups(kv.Value);
                    continue;
                }
            }
            return(value);
        }
Exemple #8
0
        public static void LoadSecondary(Transform root, List <Transform> nodes,
                                         glTF_VRM_SecondaryAnimation secondaryAnimation)
        {
            var secondary = root.Find("secondary");

            if (secondary == null)
            {
                secondary = new GameObject("secondary").transform;
                secondary.SetParent(root, false);
            }

            // clear components
            var vrmSpringBones             = root.GetComponentsInChildren <VRMSpringBone>();
            var vrmSpringBoneColliderGroup = root.GetComponentsInChildren <VRMSpringBoneColliderGroup>();

            var length = (vrmSpringBones?.Length ?? 0) + (vrmSpringBoneColliderGroup?.Length ?? 0);
            var remove = new Component[length];

            var index = 0;

            if (vrmSpringBones != null)
            {
                foreach (var vrmSpringBone in vrmSpringBones)
                {
                    remove[index++] = vrmSpringBone;
                }
            }

            if (vrmSpringBoneColliderGroup != null)
            {
                foreach (var vrmSpringBoneCollider in vrmSpringBoneColliderGroup)
                {
                    remove[index++] = vrmSpringBoneCollider;
                }
            }

            foreach (var x in remove)
            {
                if (Application.isPlaying)
                {
                    GameObject.Destroy(x);
                }
                else
                {
                    GameObject.DestroyImmediate(x);
                }
            }

            //var secondaryAnimation = context.VRM.extensions.VRM.secondaryAnimation;
            var colliders = new List <VRMSpringBoneColliderGroup>();

            foreach (var colliderGroup in secondaryAnimation.colliderGroups)
            {
                var vrmGroup = nodes[colliderGroup.node].gameObject.AddComponent <VRMSpringBoneColliderGroup>();
                vrmGroup.Colliders = colliderGroup.colliders.Select(x =>
                {
                    return(new VRMSpringBoneColliderGroup.SphereCollider
                    {
                        Offset = x.offset,
                        Radius = x.radius
                    });
                }).ToArray();
                colliders.Add(vrmGroup);
            }

            if (secondaryAnimation.boneGroups.Count > 0)
            {
                foreach (var boneGroup in secondaryAnimation.boneGroups)
                {
                    var vrmBoneGroup = secondary.gameObject.AddComponent <VRMSpringBone>();
                    if (boneGroup.center != -1)
                    {
                        vrmBoneGroup.m_center = nodes[boneGroup.center];
                    }

                    vrmBoneGroup.m_comment        = boneGroup.comment;
                    vrmBoneGroup.m_dragForce      = boneGroup.dragForce;
                    vrmBoneGroup.m_gravityDir     = boneGroup.gravityDir;
                    vrmBoneGroup.m_gravityPower   = boneGroup.gravityPower;
                    vrmBoneGroup.m_hitRadius      = boneGroup.hitRadius;
                    vrmBoneGroup.m_stiffnessForce = boneGroup.stiffiness;

                    if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
                    {
                        vrmBoneGroup.ColliderGroups = new VRMSpringBoneColliderGroup[boneGroup.colliderGroups.Length];
                        for (int i = 0; i < boneGroup.colliderGroups.Length; ++i)
                        {
                            var colliderGroup = boneGroup.colliderGroups[i];
                            vrmBoneGroup.ColliderGroups[i] = colliders[colliderGroup];
                        }
                    }

                    var boneList = new List <Transform>();
                    foreach (var x in boneGroup.bones)
                    {
                        boneList.Add(nodes[x]);
                    }

                    vrmBoneGroup.RootBones = boneList;
                }
            }
            else
            {
                secondary.gameObject.AddComponent <VRMSpringBone>();
            }
        }
Exemple #9
0
        public static void LoadSecondary(Transform root, List <Transform> nodes,
                                         glTF_VRM_SecondaryAnimation secondaryAnimation)
        {
            var secondary = root.Find("secondary");

            if (secondary == null)
            {
                secondary = new GameObject("secondary").transform;
                secondary.SetParent(root, false);
            }

            // clear components
            var remove = root.Traverse()
                         .SelectMany(x => x.GetComponents <Component>())
                         .Where(x => x is VRMSpringBone || x is VRMSpringBoneColliderGroup)
                         .ToArray();

            foreach (var x in remove)
            {
                if (Application.isPlaying)
                {
                    GameObject.Destroy(x);
                }
                else
                {
                    GameObject.DestroyImmediate(x);
                }
            }

            //var secondaryAnimation = context.VRM.extensions.VRM.secondaryAnimation;
            var colliders = new List <VRMSpringBoneColliderGroup>();

            foreach (var colliderGroup in secondaryAnimation.colliderGroups)
            {
                var vrmGroup = nodes[colliderGroup.node].gameObject.AddComponent <VRMSpringBoneColliderGroup>();
                vrmGroup.Colliders = colliderGroup.colliders.Select(x =>
                {
                    return(new VRMSpringBoneColliderGroup.SphereCollider
                    {
                        Offset = x.offset,
                        Radius = x.radius
                    });
                }).ToArray();
                colliders.Add(vrmGroup);
            }

            if (secondaryAnimation.boneGroups.Count > 0)
            {
                foreach (var boneGroup in secondaryAnimation.boneGroups)
                {
                    var vrmBoneGroup = secondary.gameObject.AddComponent <VRMSpringBone>();
                    if (boneGroup.center != -1)
                    {
                        vrmBoneGroup.m_center = nodes[boneGroup.center];
                    }
                    vrmBoneGroup.m_comment        = boneGroup.comment;
                    vrmBoneGroup.m_dragForce      = boneGroup.dragForce;
                    vrmBoneGroup.m_gravityDir     = boneGroup.gravityDir;
                    vrmBoneGroup.m_gravityPower   = boneGroup.gravityPower;
                    vrmBoneGroup.m_hitRadius      = boneGroup.hitRadius;
                    vrmBoneGroup.m_stiffnessForce = boneGroup.stiffiness;
                    if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
                    {
                        vrmBoneGroup.ColliderGroups = boneGroup.colliderGroups.Select(x => colliders[x]).ToArray();
                    }
                    vrmBoneGroup.RootBones = boneGroup.bones.Select(x => nodes[x]).ToList();
                }
            }
            else
            {
                secondary.gameObject.AddComponent <VRMSpringBone>();
            }
        }