Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VertexExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Linq;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Paradox.Effects.Data;
7 using SiliconStudio.Core;
8 
9 namespace SiliconStudio.Paradox.Extensions
10 {
11  public static class VertexExtensions
12  {
13  /// <summary>
14  /// Extracts a selection of vertices from a vertex buffer stored in this mesh data.
15  /// </summary>
16  /// <param name="meshData">The mesh data.</param>
17  /// <param name="vertexElementToExtract">The declaration to extract (e.g. "POSITION0"...etc.) </param>
18  public static T[] GetVertexBufferData<T>(this MeshDrawData meshData, params string[] vertexElementToExtract) where T : struct
19  {
20  var declaration = meshData.VertexBuffers[0].Declaration;
21 
22  var offsets = declaration.EnumerateWithOffsets().Where(vertexElementOffset => vertexElementToExtract.Contains(vertexElementOffset.VertexElement.SemanticAsText)).ToList();
23 
24  int expectedSize = offsets.Sum(vertexElementWithOffset => vertexElementWithOffset.Size);
25 
26  var count = meshData.VertexBuffers[0].Count;
27 
28  int outputSize = expectedSize * count;
29 
30  int checkSize = (int)(outputSize / Utilities.SizeOf<T>()) * Utilities.SizeOf<T>();
31  if (checkSize != outputSize)
32  throw new ArgumentException(string.Format("Size of T is not a multiple of totalSize {0}", outputSize));
33 
34  var output = new T[outputSize / Utilities.SizeOf<T>()];
35 
36  var handleOutput = GCHandle.Alloc(output, GCHandleType.Pinned);
37  var ptrOutput = handleOutput.AddrOfPinnedObject();
38 
39  var handleInput = GCHandle.Alloc(meshData.VertexBuffers[0].Buffer.Value.Content, GCHandleType.Pinned);
40  var ptrInput = handleInput.AddrOfPinnedObject();
41 
42  for(int i = 0; i < count; i++)
43  {
44  foreach (var vertexElementWithOffset in offsets)
45  {
46  Utilities.CopyMemory(ptrOutput, ptrInput + vertexElementWithOffset.Offset, vertexElementWithOffset.Size);
47  ptrOutput = ptrOutput + vertexElementWithOffset.Size;
48  }
49  ptrInput += declaration.VertexStride;
50  }
51 
52  handleInput.Free();
53  handleOutput.Free();
54  return output;
55  }
56  }
57 }
char int outputSize
Definition: lz4.h:62
_In_ size_t count
Definition: DirectXTexP.h:174
Data type for SiliconStudio.Paradox.Effects.MeshDraw.
Definition: EngineData.cs:165