Пример #1
0
 public MigrationData(string msg, Migration.Vrm0Meta meta = default, byte[] bytes = default)
 {
     Message = msg;
     OriginalMetaBeforeMigration = meta;
     MigratedBytes = bytes;
 }
Пример #2
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);
            }
        }
Пример #3
0
        public static Vrm0Meta FromJsonBytes(UniJSON.JsonNode glTF)
        {
            var oldMeta    = new Vrm0Meta();
            var extensions = glTF["extensions"];
            var vrm        = extensions["VRM"];
            var meta       = vrm["meta"];

            foreach (var kv in meta.ObjectItems())
            {
                var key = kv.Key.GetString();
                switch (key)
                {
                case "title":
                    oldMeta.title = kv.Value.GetString();
                    break;

                case "version":
                    oldMeta.version = kv.Value.GetString();
                    break;

                case "author":
                    oldMeta.author = kv.Value.GetString();
                    break;

                case "contactInformation":
                    oldMeta.contactInformation = kv.Value.GetString();
                    break;

                case "reference":
                    oldMeta.reference = kv.Value.GetString();
                    break;

                case "texture":
                    oldMeta.texture = kv.Value.GetInt32();
                    break;

                case "allowedUserName":
                    oldMeta.allowedUser = (AllowedUser)Enum.Parse(typeof(AllowedUser), kv.Value.GetString(), true);
                    break;

                case "violentUssageName":
                    oldMeta.violentUsage = kv.Value.GetString() == "Allow";
                    break;

                case "sexualUssageName":
                    oldMeta.sexualUsage = kv.Value.GetString() == "Allow";
                    break;

                case "commercialUssageName":
                    oldMeta.commercialUsage = kv.Value.GetString() == "Allow";
                    break;

                case "otherPermissionUrl":
                    oldMeta.otherPermissionUrl = kv.Value.GetString();
                    break;

                case "licenseName":
                    oldMeta.licenseType = (LicenseType)Enum.Parse(typeof(LicenseType), kv.Value.GetString(), true);
                    break;

                case "otherLicenseUrl":
                    oldMeta.otherLicenseUrl = kv.Value.GetString();
                    break;

                default:
                    UnityEngine.Debug.Log($"{key}");
                    break;
                }
            }
            return(oldMeta);
        }