diff --git a/Assets/UniGLTF/Editor/Menu.cs b/Assets/UniGLTF/Editor/Menu.cs new file mode 100644 index 000000000..3fc3f1fd3 --- /dev/null +++ b/Assets/UniGLTF/Editor/Menu.cs @@ -0,0 +1,161 @@ +using System.IO; +using UnityEditor; +using UnityEngine; + +namespace UniGLTF +{ + /// + /// Menuは一か所で管理する + /// + public static class Menu + { + #region UniGLTF + [MenuItem(UniGLTFVersion.MENU + "/Import(gltf, glb, zip)", priority = 1)] + public static void ImportMenu() + { + var path = EditorUtility.OpenFilePanel("open glb", "", "gltf,glb,zip"); + if (string.IsNullOrEmpty(path)) + { + return; + } + + if (Application.isPlaying) + { + // + // load into scene + // + var data = new AutoGltfFileParser(path).Parse(); + using (var context = new ImporterContext(data)) + { + var loaded = context.Load(); + loaded.ShowMeshes(); + Selection.activeGameObject = loaded.gameObject; + } + return; + } + + // + // save as asset + // + if (path.StartsWithUnityAssetPath()) + { + Debug.LogWarningFormat("disallow import from folder under the Assets"); + return; + } + + var ext = Path.GetExtension(path).ToLower(); + var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), ext.Substring(1)); + if (string.IsNullOrEmpty(assetPath)) + { + return; + } + + // copy + var bytes = File.ReadAllBytes(path); + File.WriteAllBytes(assetPath, bytes); + if (ext == ".gltf") + { + // copy associated files + var src_dir = Path.GetDirectoryName(path); + var dst_dir = Path.GetDirectoryName(assetPath); + var data = new GltfFileWithResourceFilesParser(path, bytes).Parse(); + foreach (var buffer in data.GLTF.buffers) + { + if (!string.IsNullOrEmpty(buffer.uri)) + { + var src_path = Path.Combine(src_dir, buffer.uri); + var src_bytes = File.ReadAllBytes(src_path); + var dst_path = Path.Combine(dst_dir, buffer.uri); + File.WriteAllBytes(dst_path, src_bytes); + UnityPath.FromFullpath(dst_path).ImportAsset(); + } + } + foreach (var buffer in data.GLTF.images) + { + if (!string.IsNullOrEmpty(buffer.uri)) + { + var src_path = Path.Combine(src_dir, buffer.uri); + var src_bytes = File.ReadAllBytes(src_path); + var dst_path = Path.Combine(dst_dir, buffer.uri); + File.WriteAllBytes(dst_path, src_bytes); + UnityPath.FromFullpath(dst_path).ImportAsset(); + } + } + } + + // import as asset + var unitypath = UnityPath.FromFullpath(assetPath); + unitypath.ImportAsset(); + var asset = unitypath.LoadAsset(); + Selection.activeObject = asset; + } + + [MenuItem(UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION, false, 0)] + private static void ExportFromMenu() + { + var window = (GltfExportWindow)GltfExportWindow.GetWindow(typeof(GltfExportWindow)); + window.titleContent = new GUIContent("Gltf Exporter"); + window.Show(); + } + + [MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Deserializer")] + static void GenerateDeserializer() + { + DeserializerGenerator.GenerateSerializer(); + } + + [MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Serializer")] + static void GenerateSerializer() + { + DeserializerGenerator.GenerateSerializer(); + } + #endregion + + #region MeshUtility + // [MenuItem(MeshUtility.MENU_PARENT + "BoneMeshEraser Wizard", priority = 31)] + // static void CreateWizard() + // { + // ScriptableWizard.DisplayWizard("BoneMeshEraser", "Erase triangles by bone", "Erase"); + // } + const string MESH_UTILITY_DICT = "UniGLTF/Mesh Utility/"; + [MenuItem(MESH_UTILITY_DICT + "MeshProcessing Wizard", priority = 30)] + static void MeshProcessFromMenu() + { + var window = (MeshUtility.MeshProcessDialog)EditorWindow.GetWindowWithRect(typeof(MeshUtility.MeshProcessDialog), new Rect(0, 0, 650, 500)); + window.titleContent = new GUIContent("Mesh Processing Window"); + window.Show(); + } + + [MenuItem(MESH_UTILITY_DICT + "MeshUtility Docs", priority = 32)] + public static void MeshUtilityDocs() + { + Application.OpenURL("https://vrm.dev/en/docs/univrm/gltf/mesh_utility/"); + } + + [MenuItem("Assets/SaveAsPng", true)] + [MenuItem("Assets/SaveAsPngLinear", true)] + static bool IsTextureAsset() + { + return Selection.activeObject is Texture2D; + } + + [MenuItem("Assets/SaveAsPng")] + static void SaveAsPng() + { + MeshUtility.EditorChangeTextureType.SaveAsPng(true); + } + + [MenuItem("Assets/SaveAsPngLinear")] + static void SaveAsPngLinear() + { + MeshUtility.EditorChangeTextureType.SaveAsPng(false); + } + #endregion + + [MenuItem("UniGLTF/UniJSON/Generate ConcreteCast")] + static void GenerateConcreteCast() + { + UniJSON.ConcreteCast.GenerateGenericCast(); + } + } +} diff --git a/Assets/UniGLTF/Editor/Menu.cs.meta b/Assets/UniGLTF/Editor/Menu.cs.meta new file mode 100644 index 000000000..b52dfc2e6 --- /dev/null +++ b/Assets/UniGLTF/Editor/Menu.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 727dafd648a17b74c951a2f08187637e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/MeshUtility/BoneMeshEraserWizard.cs b/Assets/UniGLTF/Editor/MeshUtility/BoneMeshEraserWizard.cs index 3b08ccf69..a3650f74a 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/BoneMeshEraserWizard.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/BoneMeshEraserWizard.cs @@ -52,12 +52,6 @@ namespace UniGLTF.MeshUtility [SerializeField] BoneMeshEraser.EraseBone[] m_eraseBones; - // [MenuItem(MeshUtility.MENU_PARENT + "BoneMeshEraser Wizard", priority = 31)] - // static void CreateWizard() - // { - // ScriptableWizard.DisplayWizard("BoneMeshEraser", "Erase triangles by bone", "Erase"); - // } - private void OnEnable() { var root = Selection.activeGameObject; diff --git a/Assets/UniGLTF/Editor/MeshUtility/MeshProcessDialog.cs b/Assets/UniGLTF/Editor/MeshUtility/MeshProcessDialog.cs index 965872ed6..0e392b7e7 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/MeshProcessDialog.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/MeshProcessDialog.cs @@ -15,7 +15,7 @@ namespace UniGLTF.MeshUtility public override void OnInspectorGUI() { serializedObject.Update(); - var skinnedMesh = serializedObject.FindProperty("_cSkinnedMesh"); + var skinnedMesh = serializedObject.FindProperty("_cSkinnedMesh"); EditorGUILayout.PropertyField(skinnedMesh, new GUIContent("Skinned Mesh"), true); var animator = serializedObject.FindProperty("_cAnimator"); EditorGUILayout.PropertyField(animator, new GUIContent("Animator"), false); @@ -29,16 +29,6 @@ namespace UniGLTF.MeshUtility public class MeshProcessDialog : EditorWindow { - const string MESH_UTILITY_DICT = "UniGLTF/Mesh Utility/"; - - [MenuItem(MESH_UTILITY_DICT + "MeshProcessing Wizard", priority = 30)] - static void MeshProcessFromMenu() - { - var window = (MeshProcessDialog)EditorWindow.GetWindowWithRect(typeof(MeshProcessDialog), new Rect(0, 0, 650, 500)); - window.titleContent = new GUIContent("Mesh Processing Window"); - window.Show(); - } - enum Tabs { MeshSeparator, @@ -180,7 +170,7 @@ namespace UniGLTF.MeshUtility } // Create Other Buttons - { + { GUILayout.BeginVertical(); { GUILayout.BeginHorizontal(); @@ -260,13 +250,13 @@ namespace UniGLTF.MeshUtility { if (_exportTarget == null) return GameObjectNull(); var go = _exportTarget; - - Component[] allComponents = go.GetComponents(typeof(Component)); + + Component[] allComponents = go.GetComponents(typeof(Component)); var keyWord = "VRMMeta"; foreach (var component in allComponents) - { - if (component == null) continue; + { + if (component == null) continue; var sourceString = component.ToString(); if (sourceString.Contains(keyWord)) { diff --git a/Assets/UniGLTF/Editor/MeshUtility/MeshUtility.cs b/Assets/UniGLTF/Editor/MeshUtility/MeshUtility.cs index e1e8dc265..0910182e7 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/MeshUtility.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/MeshUtility.cs @@ -8,7 +8,6 @@ namespace UniGLTF.MeshUtility { public class MeshUtility { - public const string MENU_PARENT = "UniGLTF/Mesh Utility/"; public const string ASSET_SUFFIX = ".mesh.asset"; private static readonly Vector3 ZERO_MOVEMENT = Vector3.zero; @@ -27,12 +26,6 @@ namespace UniGLTF.MeshUtility WithoutBlendShape, } - [MenuItem(MENU_PARENT + "MeshUtility Docs", priority = 32)] - public static void MeshUtilityDocs() - { - Application.OpenURL("https://vrm.dev/en/docs/univrm/gltf/mesh_utility/"); - } - public static void SeparationProcessing(GameObject go) { var outputObject = GameObject.Instantiate(go); @@ -319,7 +312,7 @@ namespace UniGLTF.MeshUtility #endif public static void MeshIntegrator(GameObject go) { - MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: true); + MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: true); MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: false); var outputObject = GameObject.Instantiate(go); @@ -328,13 +321,13 @@ namespace UniGLTF.MeshUtility var normalMeshes = outputObject.GetComponentsInChildren(); // destroy integrated meshes in the source - foreach (var skinnedMesh in go.GetComponentsInChildren()) + foreach (var skinnedMesh in go.GetComponentsInChildren()) { - if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME || - skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME) - { - GameObject.DestroyImmediate(skinnedMesh.gameObject); - } + if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME || + skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME) + { + GameObject.DestroyImmediate(skinnedMesh.gameObject); + } } foreach (var skinnedMesh in skinnedMeshes) { @@ -358,14 +351,14 @@ namespace UniGLTF.MeshUtility } foreach (var normalMesh in normalMeshes) { - if (normalMesh.sharedMesh.name != MeshIntegratorUtility.INTEGRATED_MESH_NAME) - { + if (normalMesh.sharedMesh.name != MeshIntegratorUtility.INTEGRATED_MESH_NAME) + { if (normalMesh.gameObject.GetComponent()) { GameObject.DestroyImmediate(normalMesh.gameObject.GetComponent()); } GameObject.DestroyImmediate(normalMesh); - } + } } } diff --git a/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs b/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs index 593c7152f..02bec1797 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs @@ -8,26 +8,7 @@ namespace UniGLTF.MeshUtility { public static class EditorChangeTextureType { - [MenuItem("Assets/SaveAsPng", true)] - [MenuItem("Assets/SaveAsPngLinear", true)] - static bool IsTextureAsset() - { - return Selection.activeObject is Texture2D; - } - - [MenuItem("Assets/SaveAsPng")] - static void SaveAsPng() - { - SaveAsPng(true); - } - - [MenuItem("Assets/SaveAsPngLinear")] - static void SaveAsPngLinear() - { - SaveAsPng(false); - } - - static void SaveAsPng(bool sRGB) + public static void SaveAsPng(bool sRGB) { var texture = Selection.activeObject as Texture2D; var path = SaveDialog(AssetsPath.FromAsset(texture)); diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs index 72b36db88..056118790 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs @@ -10,15 +10,6 @@ namespace UniGLTF { public class GltfExportWindow : ExportDialogBase { - const string MENU_KEY = UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION; - - [MenuItem(MENU_KEY, false, 0)] - private static void ExportFromMenu() - { - var window = (GltfExportWindow)GetWindow(typeof(GltfExportWindow)); - window.titleContent = new GUIContent("Gltf Exporter"); - window.Show(); - } enum Tabs diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/DeserializerGenerator.cs b/Assets/UniGLTF/Editor/UniGLTF/Serialization/DeserializerGenerator.cs index dc2b98be6..188bee5bd 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/Serialization/DeserializerGenerator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/DeserializerGenerator.cs @@ -39,8 +39,7 @@ public static class GltfDeserializer } } - [MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Deserializer")] - static void GenerateSerializer() + public static void GenerateSerializer() { var info = new ObjectSerialization(typeof(glTF), "gltf", "Deserialize_"); Debug.Log(info); diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/SerializerGenerator.cs b/Assets/UniGLTF/Editor/UniGLTF/Serialization/SerializerGenerator.cs index 232d7fb77..201419a7a 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/Serialization/SerializerGenerator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/SerializerGenerator.cs @@ -39,8 +39,7 @@ namespace UniGLTF { } } - [MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Serializer")] - static void GenerateSerializer() + public static void GenerateSerializer() { var info = new ObjectSerialization(typeof(glTF), "gltf", "Serialize_"); Debug.Log(info); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GltfFileWithResourceFilesParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GltfFileWithResourceFilesParser.cs index 8ce0a007a..cd3139fec 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GltfFileWithResourceFilesParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GltfFileWithResourceFilesParser.cs @@ -12,25 +12,26 @@ namespace UniGLTF { private readonly string _gltfFilePath; private readonly string _gltfRootPath; - - public GltfFileWithResourceFilesParser(string gltfFilePath) + + private readonly byte[] _bytes; + + public GltfFileWithResourceFilesParser(string gltfFilePath) : this(gltfFilePath, File.ReadAllBytes(gltfFilePath)) + { + } + + public GltfFileWithResourceFilesParser(string gltfFilePath, byte[] bytes) { - if (!File.Exists(gltfFilePath)) - { - throw new ArgumentException($"no file: {gltfFilePath}"); - } - _gltfFilePath = gltfFilePath; _gltfRootPath = Path.GetDirectoryName(gltfFilePath); + _bytes = bytes; } public GltfData Parse() { - var binary = File.ReadAllBytes(_gltfFilePath); - + return GlbLowLevelParser.ParseGltf( _gltfFilePath, - Encoding.UTF8.GetString(binary), + Encoding.UTF8.GetString(_bytes), new List(), // .gltf file has no chunks. new FileSystemStorage(_gltfRootPath), // .gltf file has resource path at file system. new MigrationFlags() diff --git a/Assets/UniGLTF/Runtime/UniJSON/ConcreteCast.cs b/Assets/UniGLTF/Runtime/UniJSON/ConcreteCast.cs index 10cf4d09e..eb06243d7 100644 --- a/Assets/UniGLTF/Runtime/UniJSON/ConcreteCast.cs +++ b/Assets/UniGLTF/Runtime/UniJSON/ConcreteCast.cs @@ -43,7 +43,6 @@ namespace UniJSON typeof(double), }; - [MenuItem("UniGLTF/UniJSON/Generate ConcreteCast")] public static void GenerateGenericCast() { var s = new StringBuilder();