From 07b2bd0aa9057d28857d0c52ef47e96e31b7e8a2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 8 Mar 2021 13:26:39 +0900 Subject: [PATCH] ScriptedImporterImpl.cs --- .../ScriptedImporter/GlbScriptedImporter.cs | 88 +------------------ .../ScriptedImporterExtension.cs | 30 +++++++ .../ScriptedImporter/ScriptedImporterImpl.cs | 76 ++++++++++++++++ .../ScriptedImporterImpl.cs.meta | 11 +++ .../IO/MaterialLoader/MaterialFactory.cs | 2 + .../IO/TextureLoader/TextureFactory.cs | 2 + 6 files changed, 122 insertions(+), 87 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs index d48bf964f..ae92a0387 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporter.cs @@ -1,5 +1,3 @@ -using System.Linq; -using UnityEditor; using UnityEditor.Experimental.AssetImporters; using UnityEngine; @@ -12,100 +10,16 @@ namespace UniGLTF [SerializeField] Axises m_reverseAxis = default; - const string MaterialDirName = "Materials"; - 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, null, 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); + ScriptedImporterImpl.Import(this, ctx, m_reverseAxis); } catch (System.Exception ex) { Debug.LogError(ex); } } - - public void ExtractMaterialsAndTextures() - { - if (string.IsNullOrEmpty(assetPath)) - { - return; - } - - TextureExtractor.ExtractTextures(assetPath, - this.GetSubAssets(this.assetPath).ToArray(), - externalObject => - { - this.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); - }, - () => - { - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - this.ExtractAssets(MaterialDirName, ".mat"); - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - }); - } - - public void SetExternalUnityObject(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object - { - this.AddRemap(sourceAssetIdentifier, obj); - AssetDatabase.WriteImportSettingsIfDirty(this.assetPath); - AssetDatabase.ImportAsset(this.assetPath, ImportAssetOptions.ForceUpdate); - } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs index 7eeef7bfa..1f70950be 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs @@ -82,5 +82,35 @@ namespace UniGLTF ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false); } } + + const string MaterialDirName = "Materials"; + + public static void ExtractMaterialsAndTextures(this ScriptedImporter self) + { + if (string.IsNullOrEmpty(self.assetPath)) + { + return; + } + + TextureExtractor.ExtractTextures(self.assetPath, + self.GetSubAssets(self.assetPath).ToArray(), + externalObject => + { + self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); + }, + () => + { + AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); + self.ExtractAssets(MaterialDirName, ".mat"); + AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); + }); + } + + public static void SetExternalUnityObject(this ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object + { + self.AddRemap(sourceAssetIdentifier, obj); + AssetDatabase.WriteImportSettingsIfDirty(self.assetPath); + AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); + } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs new file mode 100644 index 000000000..f261057c9 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -0,0 +1,76 @@ + +using System.Linq; +using UnityEditor.Experimental.AssetImporters; +using UnityEngine; + +namespace UniGLTF +{ + public static class ScriptedImporterImpl + { + public static void Import(ScriptedImporter scriptedImporter, AssetImportContext importerContext, Axises reverseAxis) + { +#if VRM_DEVELOP + Debug.Log("OnImportAsset to " + importerContext.assetPath); +#endif + + // + // Parse(parse glb, parser gltf json) + // + var parser = new GltfParser(); + parser.ParsePath(importerContext.assetPath); + + // + // Import(create unity objects) + // + var context = new ImporterContext(parser, null, + scriptedImporter.GetExternalObjectMap() + .Select(kv => (kv.Key.name, kv.Value)) + ); + context.InvertAxis = reverseAxis; + context.Load(); + context.ShowMeshes(); + + // + // SubAsset + // + + // Texture + foreach (var info in context.TextureFactory.Textures) + { + if (info.IsSubAsset) + { + var texture = info.Texture; + importerContext.AddObjectToAsset(texture.name, texture); + } + } + + // Material + foreach (var info in context.MaterialFactory.Materials) + { + if (info.IsSubAsset) + { + var material = info.Asset; + importerContext.AddObjectToAsset(material.name, material); + } + } + + // Mesh + foreach (var mesh in context.Meshes.Select(x => x.Mesh)) + { + // all mesh is subasset + importerContext.AddObjectToAsset(mesh.name, mesh); + } + + // Animation + foreach (var clip in context.AnimationClips) + { + // all animation is subasset + importerContext.AddObjectToAsset(clip.name, clip); + } + + // Root GameObject is main object + importerContext.AddObjectToAsset(context.Root.name, context.Root); + importerContext.SetMainObject(context.Root); + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs.meta new file mode 100644 index 000000000..0581f26f1 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 85bf14b48ed135743828ee49a830b1c6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs index fb3f6ca5f..27408be56 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs @@ -64,6 +64,8 @@ namespace UniGLTF public readonly Material Asset; public readonly bool UseExternal; + public bool IsSubAsset => !UseExternal; + public MaterialLoadInfo(Material asset, bool useExternal) { Asset = asset; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index 667b45b11..780564da1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -24,6 +24,8 @@ namespace UniGLTF public bool IsUsed => Flags.HasFlag(TextureLoadFlags.Used); public bool IsExternal => Flags.HasFlag(TextureLoadFlags.External); + public bool IsSubAsset => IsUsed && !IsExternal; + public TextureLoadInfo(Texture2D texture, bool used, bool isExternal) { Texture = texture;