4 using System.Collections.Generic;
6 using SiliconStudio.Paradox.Effects.Data;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.Graphics;
10 using SiliconStudio.Paradox.Graphics.Data;
12 namespace SiliconStudio.
Paradox.Extensions
20 throw new ArgumentException();
23 throw new NotImplementedException(
"The mesh Data needs to have index buffer");
25 throw new NotImplementedException(
"Sorting not implemented for multiple vertex buffers by submeshdata");
30 viewDirectionForSorting = -Vector3.UnitZ;
33 const int PolySize = 3;
34 var polyIndicesSize = PolySize * Utilities.SizeOf<
int>();
35 var vertexBuffer = meshData.VertexBuffers[0];
36 var oldIndexBuffer = meshData.IndexBuffer;
37 var vertexStride = vertexBuffer.Declaration.VertexStride;
40 var sortList =
new List<KeyValuePair<int, Vector3>>();
41 var pointList =
new List<Vector3>();
43 fixed (byte* vertexBufferPointerStart = &vertexBuffer.Buffer.Value.Content[vertexBuffer.Offset])
44 fixed (byte* indexBufferPointerStart = &oldIndexBuffer.Buffer.Value.Content[oldIndexBuffer.Offset])
46 for (var i = 0; i < oldIndexBuffer.Count / PolySize; ++i)
50 for (var u = 0; u < PolySize; ++u)
52 var curIndex = *(
int*)(indexBufferPointerStart +
Utilities.SizeOf<
int>() * (i * PolySize + u));
53 var pVertexPos = (
Vector3*)(vertexBufferPointerStart + vertexStride * curIndex);
54 pointList.Add(*pVertexPos);
58 var accu = Vector3.Zero;
59 foreach (var pt
in pointList)
61 var center = accu / pointList.Count;
64 sortList.Add(
new KeyValuePair<int, Vector3>(i,center));
69 var sortedIndices = sortList.OrderBy(x => Vector3.Dot(x.Value, viewDirectionForSorting)).Select(x=>x.Key).ToList();
72 var newIndexBufferData =
new byte[oldIndexBuffer.Count * Utilities.SizeOf<
int>()];
73 fixed (byte* newIndexDataStart = &newIndexBufferData[0])
74 fixed (byte* oldIndexDataStart = &oldIndexBuffer.Buffer.Value.Content[0])
76 var newIndexBufferPointer = newIndexDataStart;
78 foreach (var index
in sortedIndices)
80 Utilities.CopyMemory((IntPtr)(newIndexBufferPointer), (IntPtr)(oldIndexDataStart + index * polyIndicesSize), polyIndicesSize);
82 newIndexBufferPointer += polyIndicesSize;
static unsafe void SortMeshPolygons(this MeshDrawData meshData, Vector3 viewDirectionForSorting)
SiliconStudio.Paradox.Graphics.Data.VertexBufferBindingData[] VertexBuffers
Data field for SiliconStudio.Paradox.Effects.MeshDraw.VertexBuffers.
Represents a three dimensional mathematical vector.
SiliconStudio.Paradox.Graphics.Data.IndexBufferBindingData IndexBuffer
Data field for SiliconStudio.Paradox.Effects.MeshDraw.IndexBuffer.
static readonly Vector3 Zero
A SiliconStudio.Core.Mathematics.Vector3 with all of its components set to zero.
Data type for SiliconStudio.Paradox.Graphics.IndexBufferBinding.
Data type for SiliconStudio.Paradox.Effects.MeshDraw.
Content of a GPU buffer (vertex buffer, index buffer, etc...).