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; } } }
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>(); } }
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>(); } }
/// <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); }); } }
/// <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.Enumerate(parser).ToArray(); var distinct = gltfTextures.Distinct().ToArray(); Assert.True(gltfTextures.SequenceEqual(distinct)); }
public void TextureEnumerationTest() { { var parser = new GltfParser { GLTF = TwoTexture(), }; var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); Assert.AreEqual(2, items.Length); } { var parser = new GltfParser { GLTF = TwoTextureOneUri(), }; var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); Assert.AreEqual(1, items.Length); } { var parser = new GltfParser { GLTF = TwoTextureOneImage(), }; var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); Assert.AreEqual(1, items.Length); } { var parser = new GltfParser { GLTF = CombineMetallicSmoothOcclusion(), }; var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); Assert.AreEqual(1, items.Length); } }