From 654edfebb8892e0c2428ff4ff0dacf7adab800e6 Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Wed, 7 Oct 2020 17:38:43 +0530 Subject: [PATCH 1/3] Support for quad in mesh data and loading library assimp before scene load when using from Unity editor. --- Runtime/Plugins/AssimpNet/AssimpUnity.cs | 39 ++++++++++++++++-------- Runtime/Scripts/MeshImporter.cs | 11 +++++-- package.json | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Runtime/Plugins/AssimpNet/AssimpUnity.cs b/Runtime/Plugins/AssimpNet/AssimpUnity.cs index a266708..43b88a0 100644 --- a/Runtime/Plugins/AssimpNet/AssimpUnity.cs +++ b/Runtime/Plugins/AssimpNet/AssimpUnity.cs @@ -25,8 +25,13 @@ * This file is modified by Dongho Kang to distributed as a Unity package 2019. */ +#if (UNITY_64 && UNITY_STANDALONE) || UNITY_STANDALONE_WIN || UNITY_EDITOR + using Assimp.Unmanaged; using System.IO; +#if UNITY_EDITOR +using UnityEditor; +#endif using UnityEngine; namespace Assimp @@ -53,8 +58,12 @@ public static bool IsAssimpAvailable } } + #if UNITY_EDITOR + [InitializeOnLoadMethod] + #else [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] - private static void InitializePlugin() + #endif + public static void InitializePlugin() { //Only try once during runtime if(s_triedLoading) @@ -69,13 +78,13 @@ private static void InitializePlugin() s_triedLoading = true; return; } - //First time initialization, need to set a probing path (at least in editor) to resolve the native dependencies string pluginsFolder = Path.Combine(Application.dataPath, "Plugins"); - string editorPluginNativeFolder = Path.Combine(Path.GetFullPath(string.Format($"Packages/{packageName}")), "Runtime", "Plugins", "AssimpNet", "Native"); +#if UNITY_EDITOR + string editorPluginNativeFolder = Path.Combine(Path.GetFullPath(string.Format($"Assets/{packageName}")), "Runtime", "Plugins", "AssimpNet", "Native"); +#endif string native64LibPath = null; string native32LibPath = null; - //Set if any platform needs to tweak the default name AssimpNet uses for the platform, null clears using an override at all string override64LibName = null; string override32LibName = null; @@ -84,23 +93,15 @@ private static void InitializePlugin() //Unity copies the native DLLs for the specific target architecture into a single Plugin folder. switch(Application.platform) { +#if UNITY_EDITOR case RuntimePlatform.WindowsEditor: native64LibPath = Path.Combine(editorPluginNativeFolder, "win", "x86_64"); native32LibPath = Path.Combine(editorPluginNativeFolder, "win", "x86"); break; - case RuntimePlatform.WindowsPlayer: - native64LibPath = pluginsFolder + "/x86_64"; - native32LibPath = pluginsFolder + "/x86"; - break; case RuntimePlatform.LinuxEditor: native64LibPath = Path.Combine(editorPluginNativeFolder, "linux", "x86_64"); native32LibPath = Path.Combine(editorPluginNativeFolder, "linux", "x86"); break; - case RuntimePlatform.LinuxPlayer: - //Linux also drop package plugins inside Plugins folder - native64LibPath = pluginsFolder; - native32LibPath = pluginsFolder; - break; case RuntimePlatform.OSXEditor: native64LibPath = Path.Combine(editorPluginNativeFolder, "osx", "x86_64"); native32LibPath = Path.Combine(editorPluginNativeFolder, "osx", "x86"); @@ -113,6 +114,16 @@ private static void InitializePlugin() override32LibName = bundlelibName; } break; +#endif + case RuntimePlatform.WindowsPlayer: + native64LibPath = pluginsFolder + "/x86_64"; + native32LibPath = pluginsFolder + "/x86"; + break; + case RuntimePlatform.LinuxPlayer: + //Linux also drop package plugins inside Plugins folder + native64LibPath = pluginsFolder; + native32LibPath = pluginsFolder; + break; case RuntimePlatform.OSXPlayer: native64LibPath = pluginsFolder; native32LibPath = pluginsFolder; @@ -153,3 +164,5 @@ private static void InitializePlugin() } } } + +#endif \ No newline at end of file diff --git a/Runtime/Scripts/MeshImporter.cs b/Runtime/Scripts/MeshImporter.cs index 992bd5a..6033173 100644 --- a/Runtime/Scripts/MeshImporter.cs +++ b/Runtime/Scripts/MeshImporter.cs @@ -72,7 +72,7 @@ public static GameObject Load(string meshPath, float scaleX=1, float scaleY=1, f { foreach (var m in scene.Materials) { - UnityEngine.Material uMaterial = new UnityEngine.Material(Shader.Find("Standard")); + UnityEngine.Material uMaterial = new UnityEngine.Material(Shader.Find("Universal Render Pipeline/Lit")); // Albedo if (m.HasColorDiffuse) @@ -160,12 +160,19 @@ public static GameObject Load(string meshPath, float scaleX=1, float scaleY=1, f foreach (var f in m.Faces) { // Ignore non-triangle faces - if (f.IndexCount != 3) + if (f.IndexCount != 3 && f.IndexCount != 4) continue; uIndices.Add(f.Indices[2]); uIndices.Add(f.Indices[1]); uIndices.Add(f.Indices[0]); + + if (f.IndexCount == 4) + { + uIndices.Add(f.Indices[0]); + uIndices.Add(f.Indices[3]); + uIndices.Add(f.Indices[2]); + } } } diff --git a/package.json b/package.json index a89d675..6832a20 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Unity Mesh Importer", "name": "com.donghok.meshimporter", "unity": "2019.2", - "version": "0.1.1", + "version": "0.1.2", "dependencies": {}, "keywords": [ "Mesh", From 05d0feb50f53122a536a07c7f7a55e99a40ecf91 Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Wed, 7 Oct 2020 17:42:38 +0530 Subject: [PATCH 2/3] Try initialize Assimp plugin in Unity before every load --- Runtime/Scripts/MeshImporter.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Runtime/Scripts/MeshImporter.cs b/Runtime/Scripts/MeshImporter.cs index 6033173..ceabae9 100644 --- a/Runtime/Scripts/MeshImporter.cs +++ b/Runtime/Scripts/MeshImporter.cs @@ -59,6 +59,10 @@ public static GameObject Load(string meshPath, float scaleX=1, float scaleY=1, f if(!File.Exists(meshPath)) return null; + #if UNITY_EDITOR // It is possible the library is not loaded when calling from ExecuteInEditModeScript. + AssimpUnity.InitializePlugin(); + #endif + AssimpContext importer = new AssimpContext(); Scene scene = importer.ImportFile(meshPath); if (scene == null) From 2918f2fe440c995830c374aa12871ba3ff267d72 Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Wed, 7 Oct 2020 17:46:50 +0530 Subject: [PATCH 3/3] Fix packages editor path --- Runtime/Plugins/AssimpNet/AssimpUnity.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Plugins/AssimpNet/AssimpUnity.cs b/Runtime/Plugins/AssimpNet/AssimpUnity.cs index 43b88a0..4616fd9 100644 --- a/Runtime/Plugins/AssimpNet/AssimpUnity.cs +++ b/Runtime/Plugins/AssimpNet/AssimpUnity.cs @@ -81,7 +81,7 @@ public static void InitializePlugin() //First time initialization, need to set a probing path (at least in editor) to resolve the native dependencies string pluginsFolder = Path.Combine(Application.dataPath, "Plugins"); #if UNITY_EDITOR - string editorPluginNativeFolder = Path.Combine(Path.GetFullPath(string.Format($"Assets/{packageName}")), "Runtime", "Plugins", "AssimpNet", "Native"); + string editorPluginNativeFolder = Path.Combine(Path.GetFullPath(string.Format($"Packages/{packageName}")), "Runtime", "Plugins", "AssimpNet", "Native"); #endif string native64LibPath = null; string native32LibPath = null;