Exemple #1
0
        static void Exec()
        {
            if (!VRMShaders.PathObject.TryGetFromAsset(Selection.activeObject, out VRMShaders.PathObject path))
            {
                return;
            }

            // migrate
            var vrm1Bytes = MigrationVrm.Migrate(path.ReadAllBytes());

            if (!s_lastPath.TrySaveDialog("Save vrm1 file", $"{path.Stem}_vrm1", out VRMShaders.PathObject dst))
            {
                return;
            }
            s_lastPath = dst.Parent;

            // write result
            dst.WriteAllBytes(vrm1Bytes);

            if (dst.IsUnderAsset)
            {
                // immediately import for GUI update
                Debug.Log($"import: {dst}");
                dst.ImportAsset();
            }
            else
            {
                Debug.Log($"write: {dst}");
            }
        }
Exemple #2
0
        static void Exec()
        {
            var path  = UniGLTF.UnityPath.FromAsset(Selection.activeObject);
            var isVrm = path.Extension.ToLower() == ".vrm";

            var vrm1Bytes = MigrationVrm.Migrate(File.ReadAllBytes(path.FullPath));

            var dst = EditorUtility.SaveFilePanel(
                "Save vrm1 file",
                s_lastPath,
                $"{path.FileNameWithoutExtension}_vrm1",
                "vrm");

            if (string.IsNullOrEmpty(dst))
            {
                return;
            }
            s_lastPath = Path.GetDirectoryName(dst);

            // write result
            File.WriteAllBytes(dst, vrm1Bytes);

            // immediately import for GUI update
            UniGLTF.UnityPath.FromFullpath(dst).ImportAsset();
        }
Exemple #3
0
        public void MaterialImporterTest()
        {
            var migratedBytes = MigrationVrm.Migrate(File.ReadAllBytes(AliciaPath));
            var parser        = new GltfParser();

            parser.Parse(AliciaPath, migratedBytes);

            var materialParam = Vrm10MaterialImporter.GetMaterialParam(parser, 0);

            Assert.AreEqual("VRM/MToon", materialParam.ShaderName);
            Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName);

            var(key, value) = materialParam.EnumerateSubAssetKeyValue().First();
            Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "Alicia_body"), key);
        }
        public void MaterialImporterTest()
        {
            var migratedBytes = MigrationVrm.Migrate(File.ReadAllBytes(AliciaPath));
            var data          = new GlbLowLevelParser(AliciaPath, migratedBytes).Parse();

            var matDesc = new Vrm10MaterialDescriptorGenerator().Get(data, 0);

            Assert.AreEqual("Alicia_body", matDesc.Name);
            Assert.AreEqual("Hidden/VRM10/vrmc_materials_mtoon", matDesc.ShaderName);
            Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_MainTex"].UnityObjectName);
            Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_ShadeTex"].UnityObjectName);

            AreColorEqualApproximately(new Color(1, 1, 1, 1), matDesc.Colors["_Color"]);
            ColorUtility.TryParseHtmlString("#FFDDD6", out var shadeColor);
            AreColorEqualApproximately(shadeColor, matDesc.Colors["_ShadeColor"]);

            Assert.AreEqual(1.0f - 0.1f, matDesc.FloatValues["_GiEqualization"]);

            var(key, value) = matDesc.EnumerateSubAssetKeyValue().First();
            Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "Alicia_body"), key);
        }
        /// <summary>
        /// VRM1 で パースし、失敗したら Migration してから VRM1 でパースする
        /// </summary>
        /// <param name="path"></param>
        /// <param name="migrateToVrm1"></param>
        /// <returns></returns>
        public static GltfParser Parse(string path, bool migrateToVrm1)
        {
            //
            // Parse(parse glb, parser gltf json)
            //
            var parser = new GltfParser();

            parser.ParsePath(path);
            if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm))
            {
                return(parser);
            }

            if (migrateToVrm1)
            {
                // try migrateion
                var migrated = MigrationVrm.Migrate(File.ReadAllBytes(path));
                parser = new GltfParser();
                parser.Parse(path, migrated);
                return(parser);
            }

            return(null);
        }
Exemple #6
0
        /// <summary>
        /// vrm1 をパースする。vrm0 からのマイグレートもできる。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="bytes"></param>
        /// <param name="doMigrate"></param>
        /// <param name="vrm1Data">成功した場合非 null</param>
        /// <param name="migration">doMigrate==true の場合、関連情報が入る</param>
        /// <returns>GltfDataを作成できたときは Return するのでDisposeすること</returns>
        public static GltfData ParseOrMigrate(string path, byte[] bytes, bool doMigrate, out Vrm10Data vrm1Data, out MigrationData migration)
        {
            var data = new GlbLowLevelParser(path, bytes).Parse();

            byte[]             migrated      = default;
            byte[]             migratedBytes = null;
            Migration.Vrm0Meta oldMeta       = default;
            try
            {
                if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(data.GLTF.extensions, out UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm))
                {
                    // success
                    vrm1Data  = new Vrm10Data(data, vrm);
                    migration = default;
                    return(data);
                }

                if (!doMigrate)
                {
                    vrm1Data  = default;
                    migration = new MigrationData("Not vrm1 and no migration");
                    return(data);
                }

                // try migrateion
                // Migration.Vrm0Meta oldMeta = default;
                JsonNode json = data.Json.ParseAsJson();
                if (!json.TryGet("extensions", out JsonNode extensions))
                {
                    vrm1Data  = default;
                    migration = new MigrationData("gltf: no extensions");
                    return(data);
                }

                if (!extensions.TryGet("VRM", out JsonNode vrm0))
                {
                    vrm1Data  = default;
                    migration = new MigrationData("gltf: no vrm0");
                    return(data);
                }

                // found vrm0
                oldMeta = Migration.Vrm0Meta.FromJsonBytes(json);
                if (oldMeta == null)
                {
                    throw new NullReferenceException("oldMeta");
                }

                // try migrate...
                migrated = MigrationVrm.Migrate(data);
                if (migrated == null)
                {
                    vrm1Data  = default;
                    migration = new MigrationData("Found vrm0. But fail to migrate", oldMeta);
                    return(data);
                }

                if (VRMShaders.Symbols.VRM_DEVELOP)
                {
                    // load 時の右手左手座標変換でバッファが破壊的変更されるので、コピーを作っている
                    migratedBytes = migrated.Select(x => x).ToArray();
                }
            }
            catch (Exception ex)
            {
                // 何か起きた。Dispose は頼む
                vrm1Data  = default;
                migration = new MigrationData(ex.Message);
                return(data);
            }

            // マイグレーション前を破棄
            data.Dispose();
            // マイグレーション結果をパースする
            var migratedData = new GlbLowLevelParser(data.TargetPath, migrated).Parse();

            try
            {
                if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(migratedData.GLTF.extensions, out VRMC_vrm vrm))
                {
                    // migration した結果のパースに失敗した !
                    vrm1Data  = default;
                    migration = new MigrationData("vrm0: migrate but error ?", oldMeta, migrated);
                    return(migratedData);
                }

                {
                    // success
                    vrm1Data  = new Vrm10Data(migratedData, vrm);
                    migration = new MigrationData("vrm0: migrated", oldMeta, migratedBytes);
                    return(migratedData);
                }
            }
            catch (Exception ex)
            {
                // 何か起きた。Dispose は頼む
                vrm1Data  = default;
                migration = new MigrationData(ex.Message);
                return(migratedData);
            }
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <param name="vrm1Data"></param>
        /// <param name="migration"></param>
        /// <returns>Migrated GltfData if succeeded. Must Dispose</returns>
        public static GltfData Migrate(GltfData data, out Vrm10Data vrm1Data, out MigrationData migration)
        {
            var json = data.Json.ParseAsJson();

            if (!json.TryGet("extensions", out JsonNode extensions))
            {
                vrm1Data  = default;
                migration = new MigrationData("gltf: no extensions");
                return(null);
            }

            if (!extensions.TryGet("VRM", out JsonNode vrm0))
            {
                vrm1Data  = default;
                migration = new MigrationData("gltf: no vrm0");
                return(null);
            }

            // found vrm0
            var oldMeta = Migration.Vrm0Meta.FromJsonBytes(json);

            if (oldMeta == null)
            {
                throw new NullReferenceException("oldMeta");
            }

            // try migrate...
            byte[] migrated = null;
            try
            {
                migrated = MigrationVrm.Migrate(data);
                if (migrated == null)
                {
                    vrm1Data  = default;
                    migration = new MigrationData("Found vrm0. But fail to migrate", oldMeta);
                    return(null);
                }
            }
            catch (MigrationException ex)
            {
                // migration 失敗
                vrm1Data  = default;
                migration = new MigrationData(ex.ToString(), oldMeta);
                return(null);
            }
            catch (Exception ex)
            {
                // その他のエラー
                vrm1Data  = default;
                migration = new MigrationData(ex.ToString(), oldMeta);
                return(null);
            }

            byte[] debugCopy = null;
            if (VRMShaders.Symbols.VRM_DEVELOP)
            {
                // load 時の右手左手座標変換でバッファが破壊的変更されるので、コピーを作っている
                debugCopy = migrated.Select(x => x).ToArray();
            }

            // マイグレーション結果をパースする
            var migratedData = new GlbLowLevelParser(data.TargetPath, migrated).Parse();

            try
            {
                if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(migratedData.GLTF.extensions, out VRMC_vrm vrm))
                {
                    // migration した結果のパースに失敗した !
                    vrm1Data  = default;
                    migration = new MigrationData("vrm0: migrate but error ?", oldMeta, migrated);
                    // 破棄
                    migratedData.Dispose();
                    return(null);
                }

                {
                    // success. 非null値が返るのはここだけ。
                    vrm1Data  = new Vrm10Data(migratedData, vrm);
                    migration = new MigrationData("vrm0: migrated", oldMeta, debugCopy);
                    return(migratedData);
                }
            }
            catch (Exception ex)
            {
                Debug.LogWarning(ex);
                vrm1Data  = default;
                migration = new MigrationData(ex.Message);
                // 破棄
                migratedData.Dispose();
                return(null);
            }
        }