Ejemplo n.º 1
0
        protected override IEnumerator OnLoadModel()
        {
            Root.name = "VRM";

            using (MeasureTime("VRM LoadMeta"))
            {
                LoadMeta();
            }
            yield return(null);

            using (MeasureTime("VRM LoadHumanoid"))
            {
                LoadHumanoid();
            }
            yield return(null);

            using (MeasureTime("VRM LoadBlendShapeMaster"))
            {
                LoadBlendShapeMaster();
            }
            yield return(null);

            using (MeasureTime("VRM LoadSecondary"))
            {
                VRMSpringUtility.LoadSecondary(Root.transform, Nodes,
                                               GLTF.extensions.VRM.secondaryAnimation);
            }
            yield return(null);

            using (MeasureTime("VRM LoadFirstPerson"))
            {
                LoadFirstPerson();
            }
        }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
        protected override async Task OnLoadHierarchy(IAwaitCaller awaitCaller, Func <string, IDisposable> MeasureTime)
        {
            Root.name = "VRM";

            using (MeasureTime("VRM LoadMeta"))
            {
                await LoadMetaAsync();
            }
            await awaitCaller.NextFrame();

            using (MeasureTime("VRM LoadHumanoid"))
            {
                LoadHumanoid();
            }
            await awaitCaller.NextFrame();

            using (MeasureTime("VRM LoadBlendShapeMaster"))
            {
                LoadBlendShapeMaster();
            }
            await awaitCaller.NextFrame();

            using (MeasureTime("VRM LoadSecondary"))
            {
                VRMSpringUtility.LoadSecondary(Root.transform, Nodes,
                                               VRM.secondaryAnimation);
            }
            await awaitCaller.NextFrame();

            using (MeasureTime("VRM LoadFirstPerson"))
            {
                LoadFirstPerson();
            }
        }
Ejemplo n.º 4
0
        public static Unit OnLoadModel(VRMImporterContext context)
        {
            using (context.MeasureTime("VRM LoadMeta"))
            {
                LoadMeta(context);
            }

            using (context.MeasureTime("VRM LoadHumanoid"))
            {
                LoadHumanoid(context);
            }

            using (context.MeasureTime("VRM LoadBlendShapeMaster"))
            {
                LoadBlendShapeMaster(context);
            }
            using (context.MeasureTime("VRM LoadSecondary"))
            {
                VRMSpringUtility.LoadSecondary(context.Root.transform, context.Nodes,
                                               context.GLTF.extensions.VRM.secondaryAnimation);
            }
            using (context.MeasureTime("VRM LoadFirstPerson"))
            {
                LoadFirstPerson(context);
            }

            return(Unit.Default);
        }
Ejemplo n.º 5
0
        void OnLoadModel()
        {
            using (MeasureTime("VRM LoadMeta"))
            {
                LoadMeta();
            }

            using (MeasureTime("VRM LoadHumanoid"))
            {
                LoadHumanoid();
            }

            using (MeasureTime("VRM LoadBlendShapeMaster"))
            {
                LoadBlendShapeMaster();
            }
            using (MeasureTime("VRM LoadSecondary"))
            {
                VRMSpringUtility.LoadSecondary(Root.transform, Nodes,
                                               GLTF.extensions.VRM.secondaryAnimation);
            }
            using (MeasureTime("VRM LoadFirstPerson"))
            {
                LoadFirstPerson();
            }
        }
Ejemplo n.º 6
0
        public static void LoadSpringBoneFromJson()
        {
            var path = EditorUtility.OpenFilePanel(
                "Load spring from json",
                null,
                "json");

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

            var json   = File.ReadAllText(path, Encoding.UTF8);
            var spring = JsonUtility.FromJson <glTF_VRM_SecondaryAnimation>(json);

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

            VRMSpringUtility.LoadSecondary(root, nodes, spring);
        }
Ejemplo n.º 7
0
        public static Unit OnLoadModel(VRMImporterContext context)
        {
            LoadMeta(context);

            try
            {
                LoadHumanoidObsolete(context);
                Debug.LogWarning("LoadHumanoidObsolete");
            }
            catch (Exception)
            {
                LoadHumanoid(context);
            }

            LoadBlendShapeMaster(context);
            VRMSpringUtility.LoadSecondary(context.Root.transform, context.Nodes,
                                           context.VRM.extensions.VRM.secondaryAnimation);
            LoadFirstPerson(context);

            return(Unit.Default);
        }
Ejemplo n.º 8
0
        public override void Export(MeshExportSettings configuration)
        {
            base.Export(configuration);

            // avatar
            var animator = Copy.GetComponent <Animator>();

            if (animator != null)
            {
                var humanoid = Copy.GetComponent <VRMHumanoidDescription>();
                UniHumanoid.AvatarDescription description = null;
                var nodes = Copy.transform.Traverse().Skip(1).ToList();
                {
                    var isCreated = false;
                    if (humanoid != null)
                    {
                        description = humanoid.GetDescription(out isCreated);
                    }

                    if (description != null)
                    {
                        // use description
                        VRM.humanoid.Apply(description, nodes);
                    }

                    if (isCreated)
                    {
                        GameObject.DestroyImmediate(description);
                    }
                }

                {
                    // set humanoid bone mapping
                    var avatar = animator.avatar;
                    foreach (HumanBodyBones key in Enum.GetValues(typeof(HumanBodyBones)))
                    {
                        if (key == HumanBodyBones.LastBone)
                        {
                            break;
                        }

                        var transform = animator.GetBoneTransform(key);
                        if (transform != null)
                        {
                            VRM.humanoid.SetNodeIndex(key, nodes.IndexOf(transform));
                        }
                    }
                }
            }

            // morph
            var master = Copy.GetComponent <VRMBlendShapeProxy>();

            if (master != null)
            {
                var avatar = master.BlendShapeAvatar;
                if (avatar != null)
                {
                    foreach (var x in avatar.Clips)
                    {
                        VRM.blendShapeMaster.Add(x, this);
                    }
                }
            }

            // secondary
            VRMSpringUtility.ExportSecondary(Copy.transform, Nodes,
                                             x => VRM.secondaryAnimation.colliderGroups.Add(x),
                                             x => VRM.secondaryAnimation.boneGroups.Add(x)
                                             );

#pragma warning disable 0618
            // meta(obsolete)
            {
                var meta = Copy.GetComponent <VRMMetaInformation>();
                if (meta != null)
                {
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
                    }

                    VRM.meta.licenseType     = meta.LicenseType;
                    VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    VRM.meta.reference       = meta.Reference;
                }
            }
#pragma warning restore 0618

            // meta
            {
                var _meta = Copy.GetComponent <VRMMeta>();
                if (_meta != null && _meta.Meta != null)
                {
                    var meta = _meta.Meta;

                    // info
                    VRM.meta.version            = meta.Version;
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.reference          = meta.Reference;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
                    }

                    // ussage permission
                    VRM.meta.allowedUser        = meta.AllowedUser;
                    VRM.meta.violentUssage      = meta.ViolentUssage;
                    VRM.meta.sexualUssage       = meta.SexualUssage;
                    VRM.meta.commercialUssage   = meta.CommercialUssage;
                    VRM.meta.otherPermissionUrl = meta.OtherPermissionUrl;

                    // distribution license
                    VRM.meta.licenseType = meta.LicenseType;
                    if (meta.LicenseType == LicenseType.Other)
                    {
                        VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    }
                }
            }

            // firstPerson
            var firstPerson = Copy.GetComponent <VRMFirstPerson>();
            if (firstPerson != null)
            {
                if (firstPerson.FirstPersonBone != null)
                {
                    VRM.firstPerson.firstPersonBone       = Nodes.IndexOf(firstPerson.FirstPersonBone);
                    VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
                    VRM.firstPerson.meshAnnotations       = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
                    {
                        mesh            = Meshes.IndexOf(x.SharedMesh),
                        firstPersonFlag = x.FirstPersonFlag.ToString(),
                    }).ToList();
                }

                // lookAt
                {
                    var lookAtHead = Copy.GetComponent <VRMLookAtHead>();
                    if (lookAtHead != null)
                    {
                        var boneApplyer       = Copy.GetComponent <VRMLookAtBoneApplyer>();
                        var blendShapeApplyer = Copy.GetComponent <VRMLookAtBlendShapeApplyer>();
                        if (boneApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.Bone;
                            VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner);
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter);
                            VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp);
                        }
                        else if (blendShapeApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.BlendShape;
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal);
                            VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp);
                        }
                    }
                }
            }

            // materials
            foreach (var m in Materials)
            {
                VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager.Textures));
            }

            // Serialize VRM
            var f = new JsonFormatter();
            VRMSerializer.Serialize(f, VRM);
            var bytes = f.GetStoreBytes();
            glTFExtensionExport.GetOrCreate(ref glTF.extensions).Add("VRM", bytes);
        }
Ejemplo n.º 9
0
        public static void _Export(glTF gltf, VRMExporter exporter, GameObject go)
        {
            exporter.Prepare(go);
            exporter.Export();

            // avatar
            var animator = go.GetComponent <Animator>();

            if (animator != null)
            {
                var humanoid = go.GetComponent <VRMHumanoidDescription>();
                UniHumanoid.AvatarDescription description = null;
                var nodes = go.transform.Traverse().Skip(1).ToList();
                {
                    var isCreated = false;
                    if (humanoid != null)
                    {
                        description = humanoid.GetDescription(out isCreated);
                    }

                    if (description != null)
                    {
                        // use description
                        gltf.extensions.VRM.humanoid.Apply(description, nodes);
                    }
                    if (isCreated)
                    {
                        GameObject.DestroyImmediate(description);
                    }
                }

                {
                    // set humanoid bone mapping
                    var avatar = animator.avatar;
                    foreach (HumanBodyBones key in Enum.GetValues(typeof(HumanBodyBones)))
                    {
                        if (key == HumanBodyBones.LastBone)
                        {
                            break;
                        }

                        var transform = animator.GetBoneTransform(key);
                        if (transform != null)
                        {
                            gltf.extensions.VRM.humanoid.SetNodeIndex(key, nodes.IndexOf(transform));
                        }
                    }
                }
            }

            // morph
            var master = go.GetComponent <VRMBlendShapeProxy>();

            if (master != null)
            {
                var avatar = master.BlendShapeAvatar;
                if (avatar != null)
                {
                    var meshes = exporter.Meshes;
                    foreach (var x in avatar.Clips)
                    {
                        gltf.extensions.VRM.blendShapeMaster.Add(x, exporter.Copy.transform, meshes);
                    }
                }
            }

            // secondary
            VRMSpringUtility.ExportSecondary(exporter.Copy.transform, exporter.Nodes,
                                             x => gltf.extensions.VRM.secondaryAnimation.colliderGroups.Add(x),
                                             x => gltf.extensions.VRM.secondaryAnimation.boneGroups.Add(x)
                                             );

            // meta(obsolete)
            {
                var meta = exporter.Copy.GetComponent <VRMMetaInformation>();
                if (meta != null)
                {
                    gltf.extensions.VRM.meta.author             = meta.Author;
                    gltf.extensions.VRM.meta.contactInformation = meta.ContactInformation;
                    gltf.extensions.VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        gltf.extensions.VRM.meta.texture = TextureIO.ExportTexture(gltf, gltf.buffers.Count - 1, meta.Thumbnail, false);
                    }
                    gltf.extensions.VRM.meta.licenseType     = meta.LicenseType;
                    gltf.extensions.VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    gltf.extensions.VRM.meta.reference       = meta.Reference;
                }
            }
            // meta
            {
                var _meta = exporter.Copy.GetComponent <VRMMeta>();
                if (_meta != null && _meta.Meta != null)
                {
                    var meta = _meta.Meta;

                    // info
                    gltf.extensions.VRM.meta.version            = meta.Version;
                    gltf.extensions.VRM.meta.author             = meta.Author;
                    gltf.extensions.VRM.meta.contactInformation = meta.ContactInformation;
                    gltf.extensions.VRM.meta.reference          = meta.Reference;
                    gltf.extensions.VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        gltf.extensions.VRM.meta.texture = TextureIO.ExportTexture(gltf, gltf.buffers.Count - 1, meta.Thumbnail, false);
                    }

                    // ussage pemission
                    gltf.extensions.VRM.meta.allowedUser        = meta.AllowedUser;
                    gltf.extensions.VRM.meta.violentUssage      = meta.ViolentUssage;
                    gltf.extensions.VRM.meta.sexualUssage       = meta.SexualUssage;
                    gltf.extensions.VRM.meta.commercialUssage   = meta.CommercialUssage;
                    gltf.extensions.VRM.meta.otherPermissionUrl = meta.OtherPermissionUrl;

                    // distribution license
                    gltf.extensions.VRM.meta.licenseType = meta.LicenseType;
                    if (meta.LicenseType == LicenseType.Other)
                    {
                        gltf.extensions.VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    }
                }
            }

            // firstPerson
            var firstPerson = exporter.Copy.GetComponent <VRMFirstPerson>();

            if (firstPerson != null)
            {
                if (firstPerson.FirstPersonBone != null)
                {
                    gltf.extensions.VRM.firstPerson.firstPersonBone       = exporter.Nodes.IndexOf(firstPerson.FirstPersonBone);
                    gltf.extensions.VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
                    gltf.extensions.VRM.firstPerson.meshAnnotations       = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
                    {
                        mesh            = exporter.Meshes.IndexOf(x.SharedMesh),
                        firstPersonFlag = x.FirstPersonFlag.ToString(),
                    }).ToList();
                }

                // lookAt
                {
                    var lookAtHead = exporter.Copy.GetComponent <VRMLookAtHead>();
                    var lookAt     = exporter.Copy.GetComponent <VRMLookAt>();
                    if (lookAtHead != null)
                    {
                        var boneApplyer       = exporter.Copy.GetComponent <VRMLookAtBoneApplyer>();
                        var blendShapeApplyer = exporter.Copy.GetComponent <VRMLookAtBlendShapeApplyer>();
                        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);
                            gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp);
                        }
                        else if (blendShapeApplyer != null)
                        {
                            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)
                    {
                        gltf.extensions.VRM.firstPerson.lookAtHorizontalInner.Apply(lookAt.HorizontalInner);
                        gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(lookAt.HorizontalOuter);
                        gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(lookAt.VerticalDown);
                        gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(lookAt.VerticalUp);
                    }
                }
            }

            // materials
            foreach (var m in exporter.Materials)
            {
                gltf.extensions.VRM.materialProperties.Add(glTF_VRM_Material.CreateFromMaterial(m, exporter.Textures));
            }
        }