Skip to content

Commit

Permalink
Merge pull request #62 from Doprez/get-mesh-data
Browse files Browse the repository at this point in the history
Get mesh data
  • Loading branch information
VaclavElias authored Nov 18, 2023
2 parents a19ca85 + d46ce53 commit 7f445dc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
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

0 comments on commit 7f445dc

Please sign in to comment.