Exemplo n.º 1
0
        /// <summary>
        /// 読み込んだモデルを実際に表示する
        /// </summary>
        public void ShowMeshes()
        {
            if (Model == null)
            {
                throw new InvalidOperationException("Need to load VRM model first.");
            }

            currentContext.ShowMeshes();

#if UNIVRM_0_68_IMPORTER
            currentContext.DisposeOnGameObjectDestroyed();
#endif
        }
Exemplo n.º 2
0
        /// <summary>
        /// 読み込んだモデルを実際に表示する
        /// </summary>
        public void ShowMeshes()
        {
#if UNIVRM_LEGACY_IMPORTER || UNIVRM_0_68_IMPORTER || UNIVRM_0_77_IMPORTER
            if (Model == null)
            {
                throw new InvalidOperationException("Need to load VRM model first.");
            }
#endif

#if UNIVRM_LEGACY_IMPORTER || UNIVRM_0_68_IMPORTER
            currentContext.ShowMeshes();
#elif UNIVRM_0_77_IMPORTER
            currentInstance.ShowMeshes();
#else
#endif

#if UNIVRM_0_68_IMPORTER
            currentContext.DisposeOnGameObjectDestroyed();
#endif
        }
Exemplo n.º 3
0
        void OnLoaded(VRMImporterContext context)
        {
            var root = context.Root;

            root.transform.SetParent(transform, false);

            //メッシュを表示します
            context.ShowMeshes();
            context.DisposeOnGameObjectDestroyed();

            // add motion
            var humanPoseTransfer = root.AddComponent <UniHumanoid.HumanPoseTransfer>();

            if (m_target != null)
            {
                GameObject.Destroy(m_target.gameObject);
            }
            m_target = humanPoseTransfer;
            SetupTarget();
        }
Exemplo n.º 4
0
        public void ImportExportTest()
        {
            var path   = AliciaPath;
            var parser = new GltfParser();

            parser.ParseGlb(File.ReadAllBytes(path));

            using (var context = new VRMImporterContext(parser))
            {
                context.Load();
                context.ShowMeshes();
                context.EnableUpdateWhenOffscreen();

                var destroyer = context.DisposeOnGameObjectDestroyed();
                try
                {
                    // mesh
                    {
                        foreach (var renderer in destroyer.GetComponentsInChildren <Renderer>())
                        {
                            Mesh mesh = default;
                            if (renderer is MeshRenderer)
                            {
                                var f = renderer.GetComponent <MeshFilter>();
                                mesh = f.sharedMesh;
                            }
                            else if (renderer is SkinnedMeshRenderer smr)
                            {
                                mesh = smr.sharedMesh;
                            }

                            var gltfMesh = parser.GLTF.meshes.Find(x => x.name == mesh.name);
                            Assert.AreEqual(gltfMesh.name, mesh.name);

                            // materials
                            foreach (var material in renderer.sharedMaterials)
                            {
                                var gltfMaterial = parser.GLTF.materials.Find(x => x.name == material.name);
                                Assert.AreEqual(gltfMaterial.name, material.name);

                                var materialIndex = parser.GLTF.materials.IndexOf(gltfMaterial);
                                var vrmMaterial   = context.VRM.materialProperties[materialIndex];
                                // Debug.Log($"shaderName: '{vrmMaterial.shader}'");
                                if (vrmMaterial.shader == "VRM/MToon")
                                {
                                    // MToon
                                    // Debug.Log($"{material.name} is MToon");
                                    foreach (var kv in vrmMaterial.textureProperties)
                                    {
                                        var texture = material.GetTexture(kv.Key);
                                        // Debug.Log($"{kv.Key}: {texture}");
                                        Assert.NotNull(texture);
                                    }
                                }
                                else if (glTF_KHR_materials_unlit.IsEnable(gltfMaterial))
                                {
                                    // Unlit
                                    // Debug.Log($"{material.name} is unlit");
                                    throw new NotImplementedException();
                                }
                                else
                                {
                                    // PBR
                                    // Debug.Log($"{material.name} is PBR");
                                    throw new NotImplementedException();
                                }
                            }
                        }
                    }

                    // meta
                    {
                        var meta = destroyer.GetComponent <VRMMeta>();
                    }

                    // humanoid
                    {
                        var animator = destroyer.GetComponent <Animator>();
                    }


                    // blendshape
                    {
                        var blendshapeProxy = destroyer.GetComponent <VRMBlendShapeProxy>();
                        for (int i = 0; i < context.VRM.blendShapeMaster.blendShapeGroups.Count; ++i)
                        {
                            var gltfBlendShapeClip  = context.VRM.blendShapeMaster.blendShapeGroups[i];
                            var unityBlendShapeClip = blendshapeProxy.BlendShapeAvatar.Clips[i];
                            Assert.AreEqual(Enum.Parse(typeof(BlendShapePreset), gltfBlendShapeClip.presetName, true), unityBlendShapeClip.Preset);
                        }
                    }

                    var importedJson = JsonParser.Parse(context.Json);
                    importedJson.SetValue("/extensions/VRM/exporterVersion", VRMVersion.VRM_VERSION, (f, x) => f.Value(x));
                    importedJson.SetValue("/asset/generator", UniGLTF.UniGLTFVersion.UNIGLTF_VERSION, (f, x) => f.Value(x));
                    importedJson.SetValue("/scene", 0, (f, x) => f.Value(x));
                    importedJson.SetValue("/materials/*/doubleSided", false, (f, x) => f.Value(x));
                    //importJson.SetValue("/materials/*/pbrMetallicRoughness/roughnessFactor", 0);
                    //importJson.SetValue("/materials/*/pbrMetallicRoughness/baseColorFactor", new float[] { 1, 1, 1, 1 });
                    importedJson.SetValue("/accessors/*/normalized", false, (f, x) => f.Value(x));
                    importedJson.RemoveValue(Utf8String.From("/nodes/*/extras"));

                    /*
                     * importJson.SetValue("/bufferViews/12/byteStride", 4);
                     * importJson.SetValue("/bufferViews/13/byteStride", 4);
                     * importJson.SetValue("/bufferViews/14/byteStride", 4);
                     * importJson.SetValue("/bufferViews/15/byteStride", 4);
                     * importJson.SetValue("/bufferViews/22/byteStride", 4);
                     * importJson.SetValue("/bufferViews/29/byteStride", 4);
                     * importJson.SetValue("/bufferViews/45/byteStride", 4);
                     * importJson.SetValue("/bufferViews/46/byteStride", 4);
                     * importJson.SetValue("/bufferViews/47/byteStride", 4);
                     * importJson.SetValue("/bufferViews/201/byteStride", 4);
                     * importJson.SetValue("/bufferViews/202/byteStride", 4);
                     * importJson.SetValue("/bufferViews/203/byteStride", 4);
                     * importJson.SetValue("/bufferViews/204/byteStride", 4);
                     * importJson.SetValue("/bufferViews/211/byteStride", 4);
                     * importJson.SetValue("/bufferViews/212/byteStride", 4);
                     * importJson.SetValue("/bufferViews/213/byteStride", 4);
                     * importJson.SetValue("/bufferViews/214/byteStride", 4);
                     * importJson.SetValue("/bufferViews/215/byteStride", 4);
                     * importJson.SetValue("/bufferViews/243/byteStride", 4);
                     * importJson.SetValue("/bufferViews/247/byteStride", 64);
                     * importJson.SetValue("/bufferViews/248/byteStride", 64);
                     * importJson.SetValue("/bufferViews/249/byteStride", 64);
                     * importJson.SetValue("/bufferViews/250/byteStride", 64);
                     * importJson.SetValue("/bufferViews/251/byteStride", 64);
                     * importJson.SetValue("/bufferViews/252/byteStride", 64);
                     * importJson.SetValue("/bufferViews/253/byteStride", 64);
                     */
                    importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride"));

                    var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, new EditorTextureSerializer());

                    // TODO: Check contents in JSON
                    /*var exportJson = */
                    JsonParser.Parse(vrm.ToJson());

                    // TODO: Check contents in JSON
                    /*var newExportedJson = */
                    // JsonParser.Parse(JsonSchema.FromType<glTF>().Serialize(vrm));

                    /*
                     * foreach (var kv in importJson.Diff(exportJson))
                     * {
                     *  Debug.Log(kv);
                     * }
                     *
                     * Assert.AreEqual(importJson, exportJson);
                     */
                }
                finally
                {
                    UnityEngine.Object.DestroyImmediate(destroyer.gameObject);
                }
            }
        }
Exemplo n.º 5
0
        async void LoadModelAsync(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }

            Debug.LogFormat("{0}", path);
            var ext = Path.GetExtension(path).ToLower();

            switch (ext)
            {
            case ".vrm":
            {
                var file = File.ReadAllBytes(path);

                var parser = new GltfParser();
                parser.ParseGlb(file);

                using (var context = new VRMImporterContext(parser))
                {
                    await m_texts.UpdateMetaAsync(context);

                    await context.LoadAsync();

                    context.EnableUpdateWhenOffscreen();
                    context.ShowMeshes();
                    context.DisposeOnGameObjectDestroyed();
                    SetModel(context.Root);
                }
                break;
            }

            case ".glb":
            {
                var file   = File.ReadAllBytes(path);
                var parser = new GltfParser();
                parser.ParseGlb(file);

                var context = new UniGLTF.ImporterContext(parser);
                context.Load();
                context.EnableUpdateWhenOffscreen();
                context.ShowMeshes();
                context.DisposeOnGameObjectDestroyed();
                SetModel(context.Root);
                break;
            }

            case ".gltf":
            case ".zip":
            {
                var parser = new GltfParser();
                parser.ParsePath(path);

                var context = new UniGLTF.ImporterContext(parser);
                context.Load();
                context.EnableUpdateWhenOffscreen();
                context.ShowMeshes();
                context.DisposeOnGameObjectDestroyed();
                SetModel(context.Root);
                break;
            }

            default:
                Debug.LogWarningFormat("unknown file type: {0}", path);
                break;
            }
        }