Exemple #1
0
        public void TextureExtractTest()
        {
            var path = GetGltfTestModelPath("BoomBox.glb");

            if (path == null)
            {
                return;
            }

            // parse
            var parser = new GltfParser();

            parser.ParsePath(path.FullName);

            // load
            var loader = new ImporterContext(parser);

            loader.Load();

            // extractor
            var extractor = new TextureExtractor(parser, UnityPath.FromUnityPath(""), loader.TextureFactory.Textures.Select(x => (new SubAssetKey(typeof(Texture2D), x.Texture.name), x.Texture)).ToArray());
            var m         = GltfTextureEnumerator.EnumerateTexturesReferencedByMaterials(parser, 0).FirstOrDefault(x => x.Item1.Name == "texture_1.standard");

            Assert.Catch <NotImplementedException>(() => extractor.Extract(m.Item1, m.Item2));
        }
Exemple #2
0
        static void RuntimeLoad(FileInfo gltf, int subStrStart)
        {
            var parser = new GltfParser();

            try
            {
                parser.ParsePath(gltf.FullName);
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            try
            {
                using (var importer = new ImporterContext(parser))
                {
                    importer.Load();
                }
            }
            catch (Exception ex)
            {
                Message(gltf.FullName.Substring(subStrStart), ex);
            }
        }
Exemple #3
0
        public void GltfSampleModelsTest_DamagedHelmet()
        {
            var env = System.Environment.GetEnvironmentVariable("GLTF_SAMPLE_MODELS");

            if (string.IsNullOrEmpty(env))
            {
                return;
            }
            var root = new DirectoryInfo($"{env}/2.0");

            if (!root.Exists)
            {
                return;
            }

            {
                var path   = Path.Combine(root.FullName, "DamagedHelmet/glTF-Binary/DamagedHelmet.glb");
                var parser = new GltfParser();
                parser.ParsePath(path);

                var materialParam = new GltfMaterialImporter().GetMaterialParam(parser, 0);
                Assert.AreEqual("Standard", materialParam.ShaderName);
                Assert.AreEqual(5, materialParam.TextureSlots.Count);
                var(key, value) = materialParam.EnumerateSubAssetKeyValue().First();
                Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "texture_0"), key);
            }
        }
        public override void OnEnable()
        {
            base.OnEnable();

            m_importer = target as GltfScriptedImporter;
            m_parser   = new GltfParser();
            m_parser.ParsePath(m_importer.assetPath);
        }
            public TextureExtractor(ScriptedImporter importer)
            {
                // parse GLTF
                m_parser = new GltfParser();
                m_parser.ParsePath(importer.assetPath);

                m_path      = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Textures";
                m_subAssets = importer.GetSubAssets <UnityEngine.Texture2D>(importer.assetPath).ToArray();
            }
        public TextureExtractor(string assetPath, UnityEngine.Texture2D[] subAssets)
        {
            // parse GLTF
            m_parser = new GltfParser();
            m_parser.ParsePath(assetPath);

            m_path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}.Textures";
            SafeCreateDirectory(m_path);

            if (assetPath == null)
            {
                throw new ArgumentNullException();
            }
            m_subAssets = subAssets;
        }
        /// <summary>
        /// glb をパースして、UnityObject化、さらにAsset化する
        /// </summary>
        /// <param name="scriptedImporter"></param>
        /// <param name="context"></param>
        /// <param name="reverseAxis"></param>
        public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axises reverseAxis)
        {
#if VRM_DEVELOP
            Debug.Log("OnImportAsset to " + scriptedImporter.assetPath);
#endif

            //
            // Parse(parse glb, parser gltf json)
            //
            var parser = new GltfParser();
            parser.ParsePath(scriptedImporter.assetPath);

            //
            // Import(create unity objects)
            //
            var externalObjectMap = scriptedImporter.GetExternalObjectMap();

            using (var loaded = new ImporterContext(parser, null,
                                                    externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat(
                                                        EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent))))
            {
                // settings TextureImporters
                foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF))
                {
                    TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap);
                }

                loaded.InvertAxis = reverseAxis;
                loaded.Load();
                loaded.ShowMeshes();

                loaded.TransferOwnership(o =>
                {
#if VRM_DEVELOP
                    Debug.Log($"[{o.GetType().Name}] {o.name} will not destroy");
#endif

                    context.AddObjectToAsset(o.name, o);
                    if (o is GameObject)
                    {
                        // Root GameObject is main object
                        context.SetMainObject(loaded.Root);
                    }

                    return(true);
                });
            }
        }
        public static void ImportAsset(string src, string ext, UnityPath prefabPath)
        {
            if (!prefabPath.IsUnderAssetsFolder)
            {
                Debug.LogWarningFormat("out of asset path: {0}", prefabPath);
                return;
            }

            var parser = new GltfParser();

            parser.ParsePath(src);
            var context = new ImporterContext(parser);

            // Extract textures to assets folder
            context.ExtractImages(prefabPath);

            ImportDelayed(src, prefabPath, context);
        }
Exemple #9
0
        /// <summary>
        /// glb をパースして、UnityObject化、さらにAsset化する
        /// </summary>
        /// <param name="scriptedImporter"></param>
        /// <param name="context"></param>
        /// <param name="reverseAxis"></param>
        public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axises reverseAxis)
        {
#if VRM_DEVELOP
            Debug.Log("OnImportAsset to " + scriptedImporter.assetPath);
#endif

            //
            // Parse(parse glb, parser gltf json)
            //
            var parser = new GltfParser();
            parser.ParsePath(scriptedImporter.assetPath);

            //
            // Import(create unity objects)
            //
            var externalObjectMap = scriptedImporter.GetExternalObjectMap().Select(kv => (kv.Value.name, kv.Value)).ToArray();

            var externalTextures = EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent).ToArray();

            using (var loader = new ImporterContext(parser, externalObjectMap.Concat(externalTextures)))
            {
                // settings TextureImporters
                foreach (var(key, textureInfo) in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser))
                {
                    TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalMap);
                }

                loader.InvertAxis = reverseAxis;
                loader.Load();
                loader.ShowMeshes();

                loader.TransferOwnership(o =>
                {
                    context.AddObjectToAsset(o.name, o);
                    if (o is GameObject)
                    {
                        // Root GameObject is main object
                        context.SetMainObject(loader.Root);
                    }

                    return(true);
                });
            }
        }
Exemple #10
0
        public static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open gltf", "", "gltf,glb");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                //
                // load into scene
                //
                var parser = new GltfParser();
                parser.ParsePath(path);
                var context = new ImporterContext(parser);
                context.Load();
                context.ShowMeshes();
                Selection.activeGameObject = context.Root;
            }
            else
            {
                //
                // save as asset
                //
                if (path.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("disallow import from folder under the Assets");
                    return;
                }

                var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                // import as asset
                gltfAssetPostprocessor.ImportAsset(path, Path.GetExtension(path).ToLower(), UnityPath.FromFullpath(assetPath));
            }
        }
        /// <summary>
        /// Extract をテスト
        /// </summary>
        /// <param name="gltf"></param>
        /// <param name="root"></param>
        static void EditorLoad(FileInfo gltf, int subStrStart)
        {
            var parser = new GltfParser();

            try
            {
                parser.ParsePath(gltf.FullName);
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            // should unique
            var gltfTextures = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray();
            var distinct     = gltfTextures.Distinct().ToArray();

            Assert.True(gltfTextures.SequenceEqual(distinct));
        }
        static void RuntimeLoadExport(FileInfo gltf, int subStrStart)
        {
            var parser = new GltfParser();

            try
            {
                parser.ParsePath(gltf.FullName);
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            using (var loader = new ImporterContext(parser))
            {
                try
                {
                    loader.Load();
                }
                catch (Exception ex)
                {
                    Message(gltf.FullName.Substring(subStrStart), ex);
                }

                if (Skip.Contains(gltf.Directory.Parent.Name))
                {
                    // Export issue:
                    // skip
                    return;
                }

                if (loader.Root == null)
                {
                    Debug.LogWarning($"root is null: ${gltf}");
                    return;
                }

                Export(loader.Root);
            }
        }
Exemple #13
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            Debug.Log("OnImportAsset to " + ctx.assetPath);

            try
            {
                // Parse
                var parser = new GltfParser();
                parser.ParsePath(ctx.assetPath);

                // Build Unity Model
                var externalObjectMap = GetExternalObjectMap()
                                        .Select(kv => (kv.Key.name, kv.Value))
                ;
                var context = new ImporterContext(parser, externalObjectMap);
                context.InvertAxis = m_reverseAxis;
                context.Load();
                context.ShowMeshes();

                // Texture
                foreach (var info in context.TextureFactory.Textures)
                {
                    if (!info.IsUsed)
                    {
                        continue;
                    }
                    if (!info.IsExternal)
                    {
                        var texture = info.Texture;
                        ctx.AddObjectToAsset(texture.name, texture);
                    }
                }

                // Material
                foreach (var info in context.MaterialFactory.Materials)
                {
                    if (!info.UseExternal)
                    {
                        var material = info.Asset;
                        ctx.AddObjectToAsset(material.name, material);
                    }
                }

                // Mesh
                foreach (var mesh in context.Meshes.Select(x => x.Mesh))
                {
                    ctx.AddObjectToAsset(mesh.name, mesh);
                }

                // Animation
                foreach (var clip in context.AnimationClips)
                {
                    ctx.AddObjectToAsset(clip.name, clip);
                }

                // Root
                ctx.AddObjectToAsset(context.Root.name, context.Root);
                ctx.SetMainObject(context.Root);
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
            }
        }
Exemple #14
0
        void LoadModel(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }

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

            switch (ext)
            {
            case ".vrm":
            {
                var parser = new UniGLTF.GltfParser();
                parser.ParsePath(path);

                using (var loader = new RuntimeUnityBuilder(parser))
                {
                    loader.Load();
                    loader.ShowMeshes();
                    loader.EnableUpdateWhenOffscreen();
                    var destroyer = loader.DisposeOnGameObjectDestroyed();
                    SetModel(destroyer.gameObject);
                }
                break;
            }

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

                using (var loader = new UniGLTF.ImporterContext(parser))
                {
                    loader.Load();
                    loader.ShowMeshes();
                    loader.EnableUpdateWhenOffscreen();
                    loader.ShowMeshes();
                    var destroyer = loader.DisposeOnGameObjectDestroyed();
                    SetModel(destroyer.gameObject);
                }
                break;
            }

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

                using (var loader = new UniGLTF.ImporterContext(parser))
                {
                    loader.Load();
                    loader.ShowMeshes();
                    loader.EnableUpdateWhenOffscreen();
                    loader.ShowMeshes();
                    var destroyer = loader.DisposeOnGameObjectDestroyed();
                    SetModel(destroyer.gameObject);
                }
                break;
            }

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