public VRMImporterContext(
     VRMData data,
     IReadOnlyDictionary <SubAssetKey, Object> externalObjectMap = null,
     ITextureDeserializer textureDeserializer       = null,
     IMaterialDescriptorGenerator materialGenerator = null)
     : base(data.Data, externalObjectMap, textureDeserializer, materialGenerator ?? new VRMMaterialDescriptorGenerator(data.VrmExtension))
 {
     _data = data;
     TextureDescriptorGenerator = new VrmTextureDescriptorGenerator(Data, VRM);
 }
Beispiel #2
0
        static GameObject Load(byte[] bytes, string path)
        {
            var gltf = new GlbLowLevelParser(path, bytes).Parse();
            var data = new VRMData(gltf);

            using (var loader = new VRMImporterContext(data))
            {
                var loaded = loader.Load();
                loaded.ShowMeshes();
                return(loaded.gameObject);
            }
        }
Beispiel #3
0
        public static async Task <RuntimeGltfInstance> LoadAsync(string path,
                                                                 IAwaitCaller awaitCaller = null,
                                                                 MaterialGeneratorCallback materialGeneratorCallback = null,
                                                                 MetaCallback metaCallback = null,
                                                                 bool loadAnimation        = false
                                                                 )
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            if (awaitCaller == null)
            {
                Debug.LogWarning("VrmUtility.LoadAsync: awaitCaller argument is null. ImmediateCaller is used as the default fallback. When playing, we recommend RuntimeOnlyAwaitCaller.");
                awaitCaller = new ImmediateCaller();
            }

            using (GltfData data = new AutoGltfFileParser(path).Parse())
            {
                try
                {
                    var vrm = new VRMData(data);
                    IMaterialDescriptorGenerator materialGen = default;
                    if (materialGeneratorCallback != null)
                    {
                        materialGen = materialGeneratorCallback(vrm.VrmExtension);
                    }
                    using (var loader = new VRMImporterContext(vrm, materialGenerator: materialGen, loadAnimation: loadAnimation))
                    {
                        if (metaCallback != null)
                        {
                            var meta = await loader.ReadMetaAsync(awaitCaller, true);

                            metaCallback(meta);
                        }
                        return(await loader.LoadAsync(awaitCaller));
                    }
                }
                catch (NotVrm0Exception)
                {
                    // retry
                    Debug.LogWarning("file extension is vrm. but not vrm ?");
                    using (var loader = new UniGLTF.ImporterContext(data))
                    {
                        return(await loader.LoadAsync(awaitCaller));
                    }
                }
            }
        }
Beispiel #4
0
        static void ImportRuntime(string path)
        {
            // load into scene
            var data = new GlbFileParser(path).Parse();
            // VRM extension を parse します
            var vrm = new VRMData(data);

            using (var context = new VRMImporterContext(vrm))
            {
                var loaded = context.Load();
                loaded.EnableUpdateWhenOffscreen();
                loaded.ShowMeshes();
                Selection.activeGameObject = loaded.gameObject;
            }
        }
Beispiel #5
0
        public void MToonMaterialParamTest()
        {
            if (!VRMTestAssets.TryGetPath("Models/VRoid/VictoriaRubin/VictoriaRubin.vrm", out string path))
            {
                return;
            }

            var data = new GlbFileParser(path).Parse();
            var vrm  = new VRMData(data);

            var importer = new VRMImporterContext(vrm, null);

            Assert.AreEqual(73, vrm.Data.GLTF.materials.Count);
            Assert.True(VRMMToonMaterialImporter.TryCreateParam(vrm.Data, importer.VRM, 0, out MaterialDescriptor matDesc));
        }
Beispiel #6
0
        public static async Task <RuntimeGltfInstance> LoadAsync(string path,
                                                                 IAwaitCaller awaitCaller = null,
                                                                 MaterialGeneratorCallback materialGeneratorCallback = null,
                                                                 MetaCallback metaCallback = null
                                                                 )
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            using (GltfData data = new AutoGltfFileParser(path).Parse())
            {
                try
                {
                    var vrm = new VRMData(data);
                    IMaterialDescriptorGenerator materialGen = default;
                    if (materialGeneratorCallback != null)
                    {
                        materialGen = materialGeneratorCallback(vrm.VrmExtension);
                    }
                    using (var loader = new VRMImporterContext(vrm, materialGenerator: materialGen))
                    {
                        if (metaCallback != null)
                        {
                            var meta = await loader.ReadMetaAsync(new ImmediateCaller(), true);

                            metaCallback(meta);
                        }
                        return(await loader.LoadAsync(awaitCaller));
                    }
                }
                catch (NotVrm0Exception)
                {
                    // retry
                    Debug.LogWarning("file extension is vrm. but not vrm ?");
                    using (var loader = new UniGLTF.ImporterContext(data))
                    {
                        return(await loader.LoadAsync(awaitCaller));
                    }
                }
            }
        }
Beispiel #7
0
        static void ImportAsset(string path, UnityPath prefabPath)
        {
            if (!prefabPath.IsUnderAssetsFolder)
            {
                Debug.LogWarningFormat("out of asset path: {0}", prefabPath);
                return;
            }

            // import as asset
            var data = new GlbFileParser(path).Parse();
            var vrm  = new VRMData(data);

            Action <IEnumerable <UnityPath> > onCompleted = texturePaths =>
            {
                //
                // after textures imported
                //
                var map = texturePaths
                          .Select(x => x.LoadAsset <Texture2D>())
                          .Where(x => x != null)
                          .ToDictionary(x => new SubAssetKey(x), x => x as Object);

                using (var context = new VRMImporterContext(vrm, externalObjectMap: map))
                {
                    var editor = new VRMEditorImporterContext(context, prefabPath);
                    foreach (var textureInfo in editor.TextureDescriptorGenerator.Get().GetEnumerable())
                    {
                        VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures);
                    }
                    var loaded = context.Load();
                    editor.SaveAsAsset(loaded);
                }
            };

            using (var context = new VRMImporterContext(vrm))
            {
                var editor = new VRMEditorImporterContext(context, prefabPath);
                editor.ConvertAndExtractImages(onCompleted);
            }
        }
        static void ImportVrm(UnityPath vrmPath)
        {
            if (!vrmPath.IsUnderAssetsFolder)
            {
                throw new Exception();
            }

            var data = new GlbFileParser(vrmPath.FullPath).Parse();
            var vrm  = new VRMData(data);

            var prefabPath = vrmPath.Parent.Child(vrmPath.FileNameWithoutExtension + ".prefab");

            Action <IEnumerable <UnityPath> > onCompleted = texturePaths =>
            {
                var map = texturePaths
                          .Select(x => x.LoadAsset <Texture>())
                          .ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object);

                using (var context = new VRMImporterContext(vrm, externalObjectMap: map))
                {
                    var editor = new VRMEditorImporterContext(context, prefabPath);
                    foreach (var textureInfo in context.TextureDescriptorGenerator.Get().GetEnumerable())
                    {
                        VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures);
                    }
                    var loaded = context.Load();
                    editor.SaveAsAsset(loaded);
                }
            };

            // extract texture images
            using (var context = new VRMImporterContext(vrm))
            {
                var editor = new VRMEditorImporterContext(context, prefabPath);
                editor.ConvertAndExtractImages(onCompleted);
            }
        }