Exemple #1
0
    public void VRMSimpleSceneTest()
    {
        var go      = CreateSimpelScene();
        var context = new VRMImporterContext();

        try
        {
            // export
            var gltf = VRMExporter.Export(go);

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();

                // import
                context.ParseJson(gltf.ToJson(), new SimpleStorage());
                //Debug.LogFormat("{0}", context.Json);
                context.Load();
                AssertAreEqual(go.transform, context.Root.transform);
            }
        }
        finally
        {
            //Debug.LogFormat("Destory, {0}", go.name);
            GameObject.DestroyImmediate(go);
            context.Destroy(true);
        }
    }
Exemple #2
0
        static byte[] ExportSimple(GameObject model)
        {
            var vrm   = VRMExporter.Export(new UniGLTF.GltfExportSettings(), model, new RuntimeTextureSerializer());
            var bytes = vrm.ToGlbBytes();

            return(bytes);
        }
Exemple #3
0
    public void VRMSimpleSceneTest()
    {
        var go      = CreateSimpelScene();
        var context = new VRMImporterContext(null);

        try
        {
            // export
            var gltf = VRMExporter.Export(go);

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();

                // import
                context.Json = gltf.ToJson();
                Debug.LogFormat("{0}", context.Json);
                gltfImporter.Import <glTF>(context, new ArraySegment <byte>());

                AssertAreEqual(go.transform, context.Root.transform);
            }
        }
        finally
        {
            //Debug.LogFormat("Destory, {0}", go.name);
            GameObject.DestroyImmediate(go);
            context.Destroy(true);
        }
    }
Exemple #4
0
    /// <summary>
    /// vrm書き出し
    /// </summary>
    public void Save(string path)
    {
        if (VRM == null)
        {
            return;
        }

        // メタ情報とブレンドシェイプを更新
        Meta.Set();

        var vrm   = VRMExporter.Export(VRM);
        var bytes = vrm.ToGlbBytes();

        File.WriteAllBytes(path, bytes);
    }
Exemple #5
0
    /// <summary>
    /// vrm書き出し
    /// </summary>
    public void Save(string path)
    {
        if (VRM == null)
        {
            return;
        }

        // メタ情報とブレンドシェイプを更新
        Meta.Set();
        BlendShape.Set();

        var vrm   = VRMExporter.Export(MeshExportSettings.Default, VRM);
        var bytes = vrm.ToGlbBytes();

        File.WriteAllBytes(path, bytes);
    }
        void OnExportClicked()
        {
            //#if UNITY_STANDALONE_WIN
#if false
            var path = FileDialogForWindows.SaveDialog("save VRM", Application.dataPath + "/export.vrm");
#else
            var path = Application.dataPath + "/../export.vrm";
#endif
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var vrm   = VRMExporter.Export(m_model);
            var bytes = vrm.ToGlbBytes();
            File.WriteAllBytes(path, bytes);
            Debug.LogFormat("export to {0}", path);
        }
        void OnExportClicked()
        {
            //#if UNITY_STANDALONE_WIN
#if false
            var path = FileDialogForWindows.SaveDialog("save VRM", Application.dataPath + "/export.vrm");
#else
            var path = Application.dataPath + "/../export.vrm";
#endif
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var vrm   = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, (Texture _) => false, TextureExporter.GetTextureBytesWithMime);
            var bytes = vrm.ToGlbBytes();
            File.WriteAllBytes(path, bytes);
            Debug.LogFormat("export to {0}", path);
        }
        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 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);

                // 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);
                 */
            }
        }
Exemple #9
0
        public void ImportExportTest()
        {
            var path = AliciaPath;
            var data = new GlbFileParser(path).Parse();

            using (var context = new VRMImporterContext(new VRMData(data)))
                using (var loaded = context.Load())
                {
                    loaded.ShowMeshes();
                    loaded.EnableUpdateWhenOffscreen();

                    // mesh
                    {
                        foreach (var renderer in loaded.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 = data.GLTF.meshes.Find(x => x.name == mesh.name);
                            Assert.AreEqual(gltfMesh.name, mesh.name);

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

                                var materialIndex = data.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 = loaded.GetComponent <VRMMeta>();
                    }

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


                    // blendshape
                    {
                        var blendshapeProxy = loaded.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(new GltfExportSettings(), loaded.gameObject, new EditorTextureSerializer());

                    // TODO: Check contents in JSON
                    /*var exportJson = */
                    JsonParser.Parse(vrm.GLTF.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);
                     */
                }
        }
Exemple #10
0
    /// <summary>
    /// モデルの出力
    /// </summary>
    public void Save()
    {
        if (Title.text == "" || Version.text == "" || Author.text == "")
        {
            ExportText.GetComponent <RectTransform>().offsetMax = ErrorPos;
            ExportErrorText.text = ErrorMsg;
            return;
        }
        ExportText.GetComponent <RectTransform>().offsetMax = Vector2.zero;
        ExportErrorText.text = "";

#if UNITY_EDITOR
        var path = Application.dataPath + "/../_exe/Export.vrm";
#else
        var path = VRM.Samples.FileDialogForWindows.SaveDialog("save VRM", Application.dataPath + "/../");
#endif
        if (string.IsNullOrEmpty(path))
        {
            return;
        }
        else if (string.Compare(Path.GetExtension(path), ".vrm", true) != 0)
        {
            path += ".vrm";
        }

        // 表情リセット
        var proxy = Model.GetComponent <VRMBlendShapeProxy>();
        proxy.AccumulateValue(BlendShapePreset.Joy, 0);
        proxy.AccumulateValue(BlendShapePreset.Angry, 0);
        proxy.AccumulateValue(BlendShapePreset.Sorrow, 0);
        proxy.AccumulateValue(BlendShapePreset.Fun, 0);
        proxy.Apply();

        // 身長適用
        var scaling = Instantiate(Model);
        scaling.transform.localScale = Vector3.one * float.Parse(Height.text) / 100;

        // 再正規化
        var model = BoneNormalizer.Execute(scaling, false, false);
        VRMExportSettings.CopyVRMComponents(scaling, model.Root, model.BoneMap);

        // メタ情報
        var meta = model.Root.GetComponent <VRMMeta>().Meta;
        meta.Thumbnail          = GetTex2D(Thumbnail.texture);
        meta.Title              = Title.text;
        meta.Version            = Version.text;
        meta.Author             = Author.text;
        meta.ContactInformation = Contact.text;
        meta.Reference          = Reference.text;
        meta.AllowedUser        = (AllowedUser)AllowedUser.value;
        meta.ViolentUssage      = (UssageLicense)Violent.value;
        meta.SexualUssage       = (UssageLicense)Sexual.value;
        meta.CommercialUssage   = (UssageLicense)Commercial.value;
        meta.OtherPermissionUrl = OtherPermissionUrl.text;
        meta.LicenseType        = (LicenseType)License.value;
        meta.OtherLicenseUrl    = OtherLicenseUrl.text;

        // 出力
        var vrm   = VRMExporter.Export(model.Root);
        var bytes = vrm.ToGlbBytes();
        File.WriteAllBytes(path, bytes);

        Destroy(scaling);
        Destroy(model.Root);
    }