Last active
March 27, 2025 00:37
-
-
Save w0wca7a/34f1ee90e7049216d959b483e4518c3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) Stride contributors (https://stride3d.net). | |
| // Distributed under the MIT license. | |
| using System; | |
| using System.Collections.Generic; | |
| using Stride.Core.Mathematics; | |
| using Stride.Rendering; | |
| using Stride.Games; | |
| using Stride.Graphics; | |
| using Buffer = Stride.Graphics.Buffer; | |
| namespace Blsh | |
| { | |
| internal class ExampleExtension | |
| { | |
| // Experimental vertex and index data extraction from mesh | |
| public static (List<Vector3> verts, List<int> indices, List<Vector3> vertNormals, | |
| List<Vector2> vertUV, List<Vector3> vertTan, List<Vector3> vertBitan) GetVerticesMeshData(Mesh mesh, IGame game) | |
| { | |
| //Vertices conditions | |
| VertexBufferBinding vertexBufferBinding = mesh.Draw.VertexBuffers[0]; | |
| Buffer vertexBuffer = vertexBufferBinding.Buffer; | |
| byte[] vertexData = vertexBuffer.GetData<byte>(game.GraphicsContext.CommandList); | |
| int vertexCount = vertexBufferBinding.Count, | |
| vertexOffset = vertexBufferBinding.Offset, | |
| vertexStride = vertexBufferBinding.Declaration.VertexStride; | |
| //Indices conditions | |
| IndexBufferBinding indexBufferBinding = mesh.Draw.IndexBuffer; | |
| Buffer indexBuffer = indexBufferBinding.Buffer; | |
| byte[] indexData = indexBuffer.GetData<byte>(game.GraphicsContext.CommandList); | |
| int indexCount = indexBufferBinding.Count, | |
| indexOffset = indexBufferBinding.Offset; | |
| bool is32Bit = indexBufferBinding.Is32Bit; | |
| // This example not including skinning vertex data definitions | |
| List<Vector3> vertices = []; | |
| List<int> indices = []; | |
| List<Vector3> vertNormals = []; | |
| List<Vector2> vertUV = []; | |
| List<Vector3> vertTan = []; | |
| List<Vector3> vertBitan = []; | |
| List<ushort> vertBlI = []; | |
| List<Vector3> vertBlW = []; | |
| for (int i = 0, offs = vertexOffset; i < vertexCount; i ++, offs += vertexStride) | |
| { | |
| int ptr = offs; | |
| // Vertices positions and normals | |
| if (vertexStride >= 24) | |
| { | |
| float x = BitConverter.ToSingle(vertexData, ptr); | |
| float y = BitConverter.ToSingle(vertexData, ptr + 4); | |
| float z = BitConverter.ToSingle(vertexData, ptr + 8); | |
| vertices.Add(new Vector3(x, y, z)); | |
| float xn = BitConverter.ToSingle(vertexData, ptr + 12); | |
| float yn = BitConverter.ToSingle(vertexData, ptr + 16); | |
| float zn = BitConverter.ToSingle(vertexData, ptr + 20); | |
| vertNormals.Add(new Vector3(xn, yn, zn)); | |
| } | |
| // Vertices UV coordinates | |
| if (vertexStride >= 32) | |
| { | |
| float u = BitConverter.ToSingle(vertexData, ptr + 24); | |
| float v = BitConverter.ToSingle(vertexData, ptr + 28); | |
| vertUV.Add(new Vector2(u, v)); | |
| } | |
| // Vertices tangent and bitangent data | |
| if (vertexStride >= 56) | |
| { | |
| float xt = BitConverter.ToSingle(vertexData, ptr + 32); | |
| float yt = BitConverter.ToSingle(vertexData, ptr + 36); | |
| float zt = BitConverter.ToSingle(vertexData, ptr + 40); | |
| vertTan.Add(new Vector3(xt, yt, zt)); | |
| float xb = BitConverter.ToSingle(vertexData, ptr + 44); | |
| float yb = BitConverter.ToSingle(vertexData, ptr + 48); | |
| float zb = BitConverter.ToSingle(vertexData, ptr + 52); | |
| vertBitan.Add(new Vector3(xb, yb, zb)); | |
| } | |
| if (vertexStride >= 72) | |
| { | |
| ushort bi = BitConverter.ToUInt16(vertexData, ptr + 56); | |
| vertBlI.Add(bi); | |
| float xbw = BitConverter.ToSingle(vertexData, ptr + 60); | |
| float ybw = BitConverter.ToSingle(vertexData, ptr + 64); | |
| float zbw = BitConverter.ToSingle(vertexData, ptr + 68); | |
| vertBlW.Add(new Vector3(xbw, ybw, zbw)); | |
| } | |
| if (vertexData.Length - 200 < ptr) | |
| // Use it for debugging stop point otherwise you may comment it | |
| { int t = 0; } | |
| } | |
| if (is32Bit) | |
| { | |
| for (int i = 0, offs = indexOffset; i < indexCount; i++, offs += 4) // 4 bytes per index | |
| { | |
| int ptr = offs; | |
| uint index = BitConverter.ToUInt32(indexData, ptr); | |
| indices.Add((int)index); | |
| } | |
| } | |
| else | |
| { | |
| for (int i = 0, offs = indexOffset; i < indexCount; i++, offs += 2) // 2 bytes per index | |
| { | |
| int ptr = offs; | |
| ushort index = BitConverter.ToUInt16(indexData, ptr); | |
| indices.Add(index); | |
| //For debug stop points | |
| if (indexData.Length - 20 < ptr) { | |
| int t = 0; | |
| } | |
| } | |
| } | |
| return (vertices, indices, vertNormals, vertUV, vertTan, vertBitan); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works with uncompressed data