从空间表面网格缓冲区读取顶点和索引

Reading vertexes and indices from a Spatial​Surface​Mesh​Buffer

我正在使用 SharpDX(C# DirectX 包装器)开发 HoloLens UWP 应用程序,我想处理设备生成的空间映射数据。

我设法获得了 Spatial​Surface​Mesh 的列表,其中包含描述环境的网格,并且它们有两个我感兴趣的属性 -TriangleIndices 和 VertexPositions。

问题是它们是 Spatial​Surface​Mesh​Buffer 的实例,其中包含带有原始数据的 IBuffer,以及 ElementCount、Stride(每个元素的长度)和 DirectXPixelFormat,虽然该格式最适合使用 DirectX 渲染网格,但我不知道如何读取顶点的坐标和索引的顺序以在我的应用程序中使用它们。

我想我必须以某种方式使用指定的步幅和格式从 IBuffer 中读取数据,但我还没有找到任何关于如何做到这一点的文档,有什么线索吗?

根据this link IBuffer is just a wrapper around byte[] and the vectors are (most of the time) saved in that order x,y,z,w. The format (if int16, float or whatever is used) can be found out by looking into the Format property of the Spatial​Surface​Mesh​Buffer class。 属性 还定义了值是否以不同的顺序存储,或者是否需要额外的转换才能获得正确的位置。

所以基本上你可以做类似的事情

using System.Runtime.InteropServices.WindowsRuntime;

Spatial​Surface​Mesh​Buffer surfaceMeshBuffer = ...
byte[] data = surfaceMeshBuffer.ToArray();
var vectors = new List<Vector4>();    

for(int i = 0; i < data.length; ) {
    float x  = System.BitConverter.ToSingle(data, i);
    i += sizeof(float) / 8;
    float y  = System.BitConverter.ToSingle(data, i);        
    i += sizeof(float) / 8;
    float z  = System.BitConverter.ToSingle(data, i);        
    i += sizeof(float) / 8;
    float w  = System.BitConverter.ToSingle(data, i);        
    i += sizeof(float) / 8; 

    vectors.Add(new Vector4(x, y, z, w));
}