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)); }
public void TextureExtractTest() { var path = GetGltfTestModelPath("BoomBox.glb"); if (path == null) { return; } // parse var data = new GlbFileParser(path.FullName).Parse(); // load using (var context = new ImporterContext(data)) { var instance = context.Load(); var textureMap = instance.RuntimeResources .Select(kv => (kv.Item1, kv.Item2 as Texture)) .Where(kv => kv.Item2 != null) .ToDictionary(kv => kv.Item1, kv => kv.Item2) ; // extractor var extractor = new TextureExtractor(data, UnityPath.FromUnityPath(""), textureMap); var m = context.TextureDescriptorGenerator.Get().GetEnumerable() .FirstOrDefault(x => x.SubAssetKey.Name == "texture_1.standard"); Assert.Catch <NotImplementedException>(() => extractor.Extract(m.SubAssetKey, m)); } }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) { if (UnityPath.FromUnityPath(path).IsStreamingAsset) { Debug.LogFormat("Skip StreamingAssets: {0}", path); continue; } var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".gltf": case ".glb": { var gltfPath = UnityPath.FromUnityPath(path); var prefabPath = gltfPath.Parent.Child(gltfPath.FileNameWithoutExtension + ".prefab"); ImportAsset(UnityPath.FromUnityPath(path).FullPath, ext, prefabPath); break; } } } }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) { var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".gltf": case ".glb": Import(UnityPath.FromUnityPath(path)); break; } } }
public static void ImportMenu() { var path = UnityEditor.EditorUtility.OpenFilePanel("open gltf", "", "gltf,glb,zip"); if (string.IsNullOrEmpty(path)) { return; } if (Application.isPlaying) { // load into scene var context = gltfImporter.Load(path); context.ShowMeshes(); Selection.activeGameObject = context.Root; } else { if (path.StartsWithUnityAssetPath()) { Debug.LogWarningFormat("disallow import from folder under the Assets"); return; } var assetPath = UnityEditor.EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab"); if (string.IsNullOrEmpty(path)) { return; } if (!assetPath.StartsWithUnityAssetPath()) { Debug.LogWarningFormat("out of asset path: {0}", assetPath); return; } // import as asset Import(path, UnityPath.FromUnityPath(assetPath)); } }
void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureDescriptorGenerator textureDescriptorGenerator, Func <string, string> textureDir, Func <string, string> materialDir) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <SubAssetKey, Texture2D> addRemap = (key, externalObject) => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { // texture extract 後に importer 発動 AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); ExtractMaterials(self, materialDir); }; // subAsset を ExternalObject として投入する var subAssets = AssetDatabase.LoadAllAssetsAtPath(self.assetPath) .Select(x => x as Texture) .Where(x => x != null) .Select(x => (new SubAssetKey(x), x)) .ToDictionary(kv => kv.Item1, kv => kv.Item2) ; var assetPath = UnityPath.FromUnityPath(self.assetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures( data, assetPath.Parent.Child(dirName), textureDescriptorGenerator, subAssets, addRemap, onCompleted ); }
public void UnityPathTest() { var root = UnityPath.FromUnityPath("."); Assert.IsFalse(root.IsNull); Assert.IsFalse(root.IsUnderAssetsFolder); Assert.AreEqual(UnityPath.FromUnityPath("."), root); var assets = UnityPath.FromUnityPath("Assets"); Assert.IsFalse(assets.IsNull); Assert.IsTrue(assets.IsUnderAssetsFolder); var rootChild = root.Child("Assets"); Assert.AreEqual(assets, rootChild); var assetsChild = assets.Child("Hoge"); var hoge = UnityPath.FromUnityPath("Assets/Hoge"); Assert.AreEqual(assetsChild, hoge); //var children = root.TravserseDir().ToArray(); }
/// <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> /// 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); }); } }