Skip to content

Commit

Permalink
chore: bump native
Browse files Browse the repository at this point in the history
This fixes some smaller bugs and improves mesh loading performance be a factor of up to 15 on some systems.
  • Loading branch information
lmichaelis committed Jun 8, 2024
1 parent edd8002 commit 23229ad
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions ZenKit/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,22 @@ public bool IsCached()
public class Polygon : IPolygon
{
private readonly UIntPtr _handle;
private readonly Mesh _mesh;

public Polygon(UIntPtr handle)
internal Polygon(UIntPtr handle, Mesh mesh)
{
_handle = handle;
_mesh = mesh;
}

public int MaterialIndex => (int)Native.ZkPolygon_getMaterialIndex(_handle);
public int LightMapIndex => Native.ZkPolygon_getLightMapIndex(_handle);

public List<int> PositionIndices =>
Native.ZkPolygon_getPositionIndices(_handle, out var count).MarshalAsList<int>(count);
Native.ZkPolygon_getPositionIndices(_handle, _mesh.Handle, out var count).MarshalAsList<int>(count);

public List<int> FeatureIndices =>
Native.ZkPolygon_getFeatureIndices(_handle, out var count).MarshalAsList<int>(count);
Native.ZkPolygon_getFeatureIndices(_handle, _mesh.Handle, out var count).MarshalAsList<int>(count);

public bool IsPortal => Native.ZkPolygon_getIsPortal(_handle);
public bool IsOccluder => Native.ZkPolygon_getIsOccluder(_handle);
Expand Down Expand Up @@ -259,44 +261,44 @@ public IPolygon GetPolygon(int i)
public class Mesh : IMesh
{
private readonly bool _delete = true;
private readonly UIntPtr _handle;
internal readonly UIntPtr Handle;


public Mesh(string path)
{
_handle = Native.ZkMesh_loadPath(path);
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
Handle = Native.ZkMesh_loadPath(path);
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
}

public Mesh(Read buf)
{
_handle = Native.ZkMesh_load(buf.Handle);
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
Handle = Native.ZkMesh_load(buf.Handle);
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
}

public Mesh(Vfs vfs, string name)
{
_handle = Native.ZkMesh_loadVfs(vfs.Handle, name);
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
Handle = Native.ZkMesh_loadVfs(vfs.Handle, name);
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load mesh");
}

internal Mesh(UIntPtr handle)
{
_handle = handle;
Handle = handle;
_delete = false;
}

public DateTime? SourceDate => Native.ZkMesh_getSourceDate(_handle).AsDateTime();
public DateTime? SourceDate => Native.ZkMesh_getSourceDate(Handle).AsDateTime();

public string Name => Native.ZkMesh_getName(_handle).MarshalAsString() ??
public string Name => Native.ZkMesh_getName(Handle).MarshalAsString() ??
throw new Exception("Failed to load mesh name");

public AxisAlignedBoundingBox BoundingBox => Native.ZkMesh_getBoundingBox(_handle);
public AxisAlignedBoundingBox BoundingBox => Native.ZkMesh_getBoundingBox(Handle);

public IOrientedBoundingBox OrientedBoundingBox =>
new OrientedBoundingBox(Native.ZkMesh_getOrientedBoundingBox(_handle));
new OrientedBoundingBox(Native.ZkMesh_getOrientedBoundingBox(Handle));

public int MaterialCount => (int)Native.ZkMesh_getMaterialCount(_handle);
public int MaterialCount => (int)Native.ZkMesh_getMaterialCount(Handle);

public List<IMaterial> Materials
{
Expand All @@ -309,7 +311,7 @@ public List<IMaterial> Materials
}
}

public int PositionCount => (int)Native.ZkMesh_getPositionCount(_handle);
public int PositionCount => (int)Native.ZkMesh_getPositionCount(Handle);

public List<Vector3> Positions
{
Expand All @@ -322,7 +324,7 @@ public List<Vector3> Positions
}
}

public int FeatureCount => (int)Native.ZkMesh_getVertexCount(_handle);
public int FeatureCount => (int)Native.ZkMesh_getVertexCount(Handle);

public List<Vertex> Features
{
Expand All @@ -335,7 +337,7 @@ public List<Vertex> Features
}
}

public int LightMapCount => (int)Native.ZkMesh_getLightMapCount(_handle);
public int LightMapCount => (int)Native.ZkMesh_getLightMapCount(Handle);

public List<ILightMap> LightMap
{
Expand All @@ -348,7 +350,7 @@ public List<ILightMap> LightMap
}
}

public int PolygonCount => (int)Native.ZkMesh_getPolygonCount(_handle);
public int PolygonCount => (int)Native.ZkMesh_getPolygonCount(Handle);

public List<IPolygon> Polygons
{
Expand Down Expand Up @@ -384,32 +386,32 @@ public bool IsCached()

public IMaterial GetMaterial(int i)
{
return new Material(Native.ZkMesh_getMaterial(_handle, (ulong)i));
return new Material(Native.ZkMesh_getMaterial(Handle, (ulong)i));
}

public Vector3 GetPosition(int i)
{
return Native.ZkMesh_getPosition(_handle, (ulong)i);
return Native.ZkMesh_getPosition(Handle, (ulong)i);
}

public Vertex GetFeature(int i)
{
return Native.ZkMesh_getVertex(_handle, (ulong)i);
return Native.ZkMesh_getVertex(Handle, (ulong)i);
}

public ILightMap GetLightMap(int i)
{
return new LightMap(Native.ZkMesh_getLightMap(_handle, (ulong)i));
return new LightMap(Native.ZkMesh_getLightMap(Handle, (ulong)i));
}

public IPolygon GetPolygon(int i)
{
return new Polygon(Native.ZkMesh_getPolygon(_handle, (ulong)i));
return new Polygon(Native.ZkMesh_getPolygon(Handle, (ulong)i), this);
}

~Mesh()
{
if (_delete) Native.ZkMesh_del(_handle);
if (_delete) Native.ZkMesh_del(Handle);
}
}
}
4 changes: 2 additions & 2 deletions ZenKit/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,10 @@ public static extern void ZkModelMesh_enumerateAttachments(UIntPtr slf, ZkAttach
public static extern int ZkPolygon_getLightMapIndex(UIntPtr slf);

[DllImport(DllName)]
public static extern IntPtr ZkPolygon_getPositionIndices(UIntPtr slf, out ulong count);
public static extern IntPtr ZkPolygon_getPositionIndices(UIntPtr slf, UIntPtr msh, out ulong count);

[DllImport(DllName)]
public static extern IntPtr ZkPolygon_getFeatureIndices(UIntPtr slf, out ulong count);
public static extern IntPtr ZkPolygon_getFeatureIndices(UIntPtr slf, UIntPtr msh, out ulong count);

[DllImport(DllName)]
public static extern bool ZkPolygon_getIsPortal(UIntPtr slf);
Expand Down
Binary file modified ZenKit/runtimes/android-arm64/native/libzenkitcapi.so
Binary file not shown.
Binary file modified ZenKit/runtimes/linux-x64/native/libzenkitcapi.so
Binary file not shown.
Binary file modified ZenKit/runtimes/osx-x64/native/libzenkitcapi.dylib
Binary file not shown.
Binary file modified ZenKit/runtimes/win-x64/native/zenkitcapi.dll
Binary file not shown.

0 comments on commit 23229ad

Please sign in to comment.