Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions Runtime/Plugins/AssimpNet/AssimpUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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");
#if UNITY_EDITOR
string editorPluginNativeFolder = Path.Combine(Path.GetFullPath(string.Format($"Packages/{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;
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -153,3 +164,5 @@ private static void InitializePlugin()
}
}
}

#endif
15 changes: 13 additions & 2 deletions Runtime/Scripts/MeshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -72,7 +76,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)
Expand Down Expand Up @@ -160,12 +164,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]);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down