Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get mesh data #62

Merged
merged 2 commits into from
Nov 18, 2023
Merged
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
83 changes: 83 additions & 0 deletions src/Stride.CommunityToolkit/Extensions/ModelComponentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Stride.Games;
using Stride.Rendering;

namespace Stride.Engine;

public static class ModelComponentExtensions
Expand Down Expand Up @@ -29,4 +32,84 @@ public static Vector3 GetMeshHWL(this ModelComponent modelComponent)

return new Vector3(height, width, length);
}

/// <summary>
/// Gets the ModelComponents Mesh data as vertices and indices.
/// </summary>
/// <param name="model"></param>
/// <param name="game"></param>
/// <returns></returns>
public static (List<Vector3> verts, List<int> indices) GetMeshVerticesAndIndices(this ModelComponent model, IGame game)
{
return GetMeshData(model.Model, game.Services, game);
}

static unsafe (List<Vector3> verts, List<int> indices) GetMeshData(Model model, IServiceRegistry services, IGame game)
{
Matrix[] nodeTransforms = null;

int totalVerts = 0, totalIndices = 0;
foreach (var meshData in model.Meshes)
{
totalVerts += meshData.Draw.VertexBuffers[0].Count;
totalIndices += meshData.Draw.IndexBuffer.Count;
}

var combinedVerts = new List<Vector3>(totalVerts);
var combinedIndices = new List<int>(totalIndices);

foreach (var meshData in model.Meshes)
{
var vBuffer = meshData.Draw.VertexBuffers[0].Buffer;
var iBuffer = meshData.Draw.IndexBuffer.Buffer;
byte[] verticesBytes = vBuffer.GetData<byte>(game.GraphicsContext.CommandList); ;
byte[] indicesBytes = iBuffer.GetData<byte>(game.GraphicsContext.CommandList); ;

if ((verticesBytes?.Length ?? 0) == 0 || (indicesBytes?.Length ?? 0) == 0)
{
// returns empty lists if there is an issue
return (combinedVerts, combinedIndices);
}

int vertMappingStart = combinedVerts.Count;

fixed (byte* bytePtr = verticesBytes)
{
var vBindings = meshData.Draw.VertexBuffers[0];
int count = vBindings.Count;
int stride = vBindings.Declaration.VertexStride;
for (int i = 0, vHead = vBindings.Offset; i < count; i++, vHead += stride)
{
var pos = *(Vector3*)(bytePtr + vHead);
if (nodeTransforms != null)
{
Matrix posMatrix = Matrix.Translation(pos);
Matrix.Multiply(ref posMatrix, ref nodeTransforms[meshData.NodeIndex], out var finalMatrix);
pos = finalMatrix.TranslationVector;
}

combinedVerts.Add(pos);
}
}

fixed (byte* bytePtr = indicesBytes)
{
if (meshData.Draw.IndexBuffer.Is32Bit)
{
foreach (int i in new Span<int>(bytePtr + meshData.Draw.IndexBuffer.Offset, meshData.Draw.IndexBuffer.Count))
{
combinedIndices.Add(vertMappingStart + i);
}
}
else
{
foreach (ushort i in new Span<ushort>(bytePtr + meshData.Draw.IndexBuffer.Offset, meshData.Draw.IndexBuffer.Count))
{
combinedIndices.Add(vertMappingStart + i);
}
}
}
}
return (combinedVerts, combinedIndices);
}
}
1 change: 1 addition & 0 deletions src/Stride.CommunityToolkit/Stride.CommunityToolkit.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Stride.CommunityToolkit</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<Import Project="..\CommonSettings.props" />
Expand Down