Exemplo n.º 1
0
        public static IEnumerable <(string, UnityEngine.Object)> EnumerateTexturesFromUri(Dictionary <AssetImporter.SourceAssetIdentifier, UnityEngine.Object> exclude,
                                                                                          GltfParser parser, UnityPath dir)
        {
            foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF))
            {
                switch (texParam.TextureType)
                {
                case GetTextureParam.TextureTypes.StandardMap:
                    break;

                default:
                {
                    var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.GltflName);
                    var gltfImage   = parser.GLTF.images[gltfTexture.source];
                    if (!string.IsNullOrEmpty(gltfImage.uri))
                    {
                        var child = dir.Child(gltfImage.uri);
                        var asset = AssetDatabase.LoadAssetAtPath <Texture2D>(child.Value);
                        if (asset == null)
                        {
                            throw new System.IO.FileNotFoundException($"{child}");
                        }
                        // Debug.Log($"exists: {child}: {asset}");
                        if (exclude == null || !exclude.Any(kv => kv.Value.name == asset.name))
                        {
                            yield return(asset.name, asset);
                        }
                    }
                }
                break;
                }
            }
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
        {
            var canExtract = !importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);

            using (new TmpGuiEnable(canExtract))
            {
                if (GUILayout.Button("Extract Materials And Textures ..."))
                {
                    ExtractMaterialsAndTextures(importer);
                }
            }

            //
            // Draw ExternalObjectMap
            //
            s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials");
            if (s_foldMaterials)
            {
                DrawRemapGUI <UnityEngine.Material>(importer, parser.GLTF.materials.Select(x => x.name));
            }

            s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
            if (s_foldTextures)
            {
                DrawRemapGUI <UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => x.ConvertedName));
            }

            if (GUILayout.Button("Clear"))
            {
                importer.ClearExternalObjects <UnityEngine.Material>();
                importer.ClearExternalObjects <UnityEngine.Texture2D>();
            }
        }
Exemplo n.º 4
0
        public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
        {
            var canExtract = !importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);

            using (new TmpGuiEnable(canExtract))
            {
                if (GUILayout.Button("Extract Materials And Textures ..."))
                {
                    ExtractMaterialsAndTextures(importer, parser);
                }
            }

            //
            // Draw ExternalObjectMap
            //
            s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials");
            if (s_foldMaterials)
            {
                DrawRemapGUI <UnityEngine.Material>(importer, parser.GLTF.materials.Select(x => x.name));
            }

            s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
            if (s_foldTextures)
            {
                var names = GltfTextureEnumerator.Enumerate(parser)
                            .Select(x =>
                {
                    if (x.TextureType != TextureImportTypes.StandardMap && !string.IsNullOrEmpty(x.Uri))
                    {
                        // GLTF の 無変換テクスチャーをスキップする
                        return(null);
                    }

                    switch (x.TextureType)
                    {
                    case TextureImportTypes.NormalMap:
                        return(x.GltfName);

                    default:
                        return(x.ConvertedName);
                    }
                })
                            .Where(x => !string.IsNullOrEmpty(x))
                ;
                DrawRemapGUI <UnityEngine.Texture2D>(importer, names);
            }

            if (GUILayout.Button("Clear"))
            {
                importer.ClearExternalObjects <UnityEngine.Material>();
                importer.ClearExternalObjects <UnityEngine.Texture2D>();
            }
        }
Exemplo n.º 5
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();

            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);
                });
            }
        }
Exemplo n.º 6
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);
                });
            }
        }
Exemplo n.º 7
0
        /// <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));
        }
Exemplo n.º 8
0
        public void TextureEnumerationTest()
        {
            {
                var parser = new GltfParser
                {
                    GLTF = TwoTexture(),
                };
                var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray();
                Assert.AreEqual(2, items.Length);
            }

            {
                var parser = new GltfParser
                {
                    GLTF = TwoTextureOneUri(),
                };
                var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray();
                Assert.AreEqual(1, items.Length);
            }

            {
                var parser = new GltfParser
                {
                    GLTF = TwoTextureOneImage(),
                };
                var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray();
                Assert.AreEqual(1, items.Length);
            }

            {
                var parser = new GltfParser
                {
                    GLTF = CombineMetallicSmoothOcclusion(),
                };
                var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray();
                Assert.AreEqual(1, items.Length);
            }
        }