created MeshExtensions
This commit is contained in:
parent
a4b6140a96
commit
88ea2f1f08
3 changed files with 118 additions and 13 deletions
|
|
@ -9,6 +9,15 @@ namespace Raylib_cs;
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
public unsafe partial struct Mesh
|
public unsafe partial struct Mesh
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a mesh ready for default vertex data allocation
|
||||||
|
/// </summary>
|
||||||
|
public Mesh(int vertexCount, int triangleCount)
|
||||||
|
{
|
||||||
|
VertexCount = vertexCount;
|
||||||
|
TriangleCount = triangleCount;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Number of vertices stored in arrays
|
/// Number of vertices stored in arrays
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -24,37 +33,37 @@ public unsafe partial struct Mesh
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
|
/// Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* Vertices;
|
public float* Vertices = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
|
/// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* TexCoords;
|
public float* TexCoords = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
|
/// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* TexCoords2;
|
public float* TexCoords2 = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
|
/// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* Normals;
|
public float* Normals = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4)
|
/// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* Tangents;
|
public float* Tangents = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
|
/// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte* Colors;
|
public byte* Colors = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex indices (in case vertex data comes indexed)
|
/// Vertex indices (in case vertex data comes indexed)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ushort* Indices;
|
public ushort* Indices = default;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
@ -63,22 +72,22 @@ public unsafe partial struct Mesh
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Animated vertex positions (after bones transformations)
|
/// Animated vertex positions (after bones transformations)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* AnimVertices;
|
public float* AnimVertices = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Animated normals (after bones transformations)
|
/// Animated normals (after bones transformations)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* AnimNormals;
|
public float* AnimNormals = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex bone ids, up to 4 bones influence by vertex (skinning)
|
/// Vertex bone ids, up to 4 bones influence by vertex (skinning)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte* BoneIds;
|
public byte* BoneIds = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex bone weight, up to 4 bones influence by vertex (skinning)
|
/// Vertex bone weight, up to 4 bones influence by vertex (skinning)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float* BoneWeights;
|
public float* BoneWeights = default;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
@ -87,12 +96,12 @@ public unsafe partial struct Mesh
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OpenGL Vertex Array Object id
|
/// OpenGL Vertex Array Object id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public uint VaoId;
|
public uint VaoId = default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OpenGL Vertex Buffer Objects id (default vertex data, uint[])
|
/// OpenGL Vertex Buffer Objects id (default vertex data, uint[])
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public uint* VboId;
|
public uint* VboId = default;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
90
Raylib-cs/types/MeshExtensions.cs
Normal file
90
Raylib-cs/types/MeshExtensions.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Raylib_cs;
|
||||||
|
|
||||||
|
internal static unsafe class MeshExtensions
|
||||||
|
{
|
||||||
|
/// <summary>Allocate mesh vertices</summary>
|
||||||
|
public static void AllocVertices(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.Vertices = Raylib.New<float>(3 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh texcoords</summary>
|
||||||
|
public static void AllocTexcoords(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.TexCoords = Raylib.New<float>(2 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh texcoords2</summary>
|
||||||
|
public static void AllocTexcoords2(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.TexCoords2 = Raylib.New<float>(2 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh normals</summary>
|
||||||
|
public static void AllocNormals(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.Normals = Raylib.New<float>(3 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh tangents</summary>
|
||||||
|
public static void AllocTangents(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.Tangents = Raylib.New<float>(4 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh colors</summary>
|
||||||
|
public static void AllocColors(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.Colors = Raylib.New<byte>(4 * mesh.VertexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Allocate mesh indices</summary>
|
||||||
|
public static void AllocIndices(ref this Mesh mesh)
|
||||||
|
{
|
||||||
|
mesh.Indices = Raylib.New<ushort>(3 * mesh.TriangleCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh vertices</summary>
|
||||||
|
public static Span<T> VerticesAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.Vertices, 3 * mesh.VertexCount * sizeof(float) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh texcoords</summary>
|
||||||
|
public static Span<T> TexcoordsAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.TexCoords, 2 * mesh.VertexCount * sizeof(float) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh texcoords2</summary>
|
||||||
|
public static Span<T> Texcoords2As<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.TexCoords2, 2 * mesh.VertexCount * sizeof(float) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh normals</summary>
|
||||||
|
public static Span<T> NormalsAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.Normals, 3 * mesh.VertexCount * sizeof(float) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh tangents</summary>
|
||||||
|
public static Span<T> TangentsAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.Tangents, 4 * mesh.VertexCount * sizeof(float) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh colors</summary>
|
||||||
|
public static Span<T> ColorsAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.Colors, 4 * mesh.VertexCount * sizeof(byte) / sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Access mesh indices</summary>
|
||||||
|
public static Span<T> IndicesAs<T>(this Mesh mesh) where T : unmanaged
|
||||||
|
{
|
||||||
|
return new(mesh.Indices, 3 * mesh.TriangleCount * sizeof(ushort) / sizeof(T));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -222,6 +222,12 @@ public static unsafe partial class Raylib
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>C++ style memory allocator</summary>
|
||||||
|
public static T* New<T>(int count) where T : unmanaged
|
||||||
|
{
|
||||||
|
return (T*)MemAlloc(count * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Load file data as byte array (read)</summary>
|
/// <summary>Load file data as byte array (read)</summary>
|
||||||
public static byte* LoadFileData(string fileName, ref uint bytesRead)
|
public static byte* LoadFileData(string fileName, ref uint bytesRead)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue