How to Convert Primitive Object to a Triangle Mesh with Custom Memory Layout in

This Technical tip explains how .NET developers can convert primitive object to a triangle mesh with custom memory layout. Aspose.3D for .NET API allows developers to convert any primitive object to a triangle mesh with custom memory layout. The primitives include many of the most basic and most used objects like box, sphere, plane, cylinder, and torus. These examples show how to, Convert a Sphere to Triangle Mesh (vertex memory layout of triangle mesh is defined in the struct) and Convert a Box to Triangle Mesh (vertex memory layout of triangle mesh is defined dynamically by VertexDeclaration). Primitives are the basic building blocks to start any modelling task. Developers convert primitive to triangle mesh because any complex (surface) structure can be represented as a bunch of triangles. The triangle is the most atomic and primitive geometry. Thus it is used as base for almost anything. Developers can access Indices, actual vertices, vertices before merging and total bytes of vertices in memory.
//your code here...//This code example converts a Sphere to triangle mesh with custom memory layout. // [C# Code Sample] [StructLayout(LayoutKind.Sequential)] struct MyVertex { [Semantic(VertexFieldSemantic.Position)] FVector3 position; [Semantic(VertexFieldSemantic.Normal)] FVector3 normal; } public void TypedTriMesh() { Mesh sphere = (new Sphere()).ToMesh(); //convert any mesh into typed TriMesh var myMesh = TriMesh<MyVertex>.FromMesh(sphere); //Get the vertex data in customized vertex structure. MyVertex[] vertex = myMesh.VerticesToTypedArray(); //get the 32bit and 16bit indices int[] indices32bit; ushort[] indices16bit; myMesh.IndicesToArray(out indices32bit); myMesh.IndicesToArray(out indices16bit); using (MemoryStream ms = new MemoryStream()) { //or we can write the vertex directly into stream like: myMesh.WriteVerticesTo(ms); //the indice data can be directly write to stream, we support 32-bit and 16-bit indice. myMesh.Write16bIndicesTo(ms); myMesh.Write32bIndicesTo(ms); } Console.WriteLine("Indices = {0}, Actual vertices = {1}, vertices before merging = {2}", myMesh.IndicesCount, myMesh.VerticesCount, myMesh.UnmergedVerticesCount); Console.WriteLine("Total bytes of vertices in memory {0}bytes", myMesh.VerticesSizeInBytes); } //[VB.NET Code Sample] <StructLayout(LayoutKind.Sequential)> Structure MyVertex <Semantic(VertexFieldSemantic.Position)> Private position As FVector3 <Semantic(VertexFieldSemantic.Normal)> Private normal As FVector3 End Structure Public Sub TypedTriMesh() Dim sphere As Mesh = (New Sphere()).ToMesh() 'convert any mesh into typed TriMesh Dim myMesh = TriMesh(Of MyVertex).FromMesh(sphere) 'Get the vertex data in customized vertex structure. Dim vertex As MyVertex() = myMesh.VerticesToTypedArray() 'get the 32bit and 16bit indices Dim indices32bit As Integer() Dim indices16bit As UShort() myMesh.IndicesToArray(indices32bit) myMesh.IndicesToArray(indices16bit) Using ms As New MemoryStream() 'or we can write the vertex directly into stream like: myMesh.WriteVerticesTo(ms) 'the indice data can be directly write to stream, we support 32-bit and 16-bit indice. myMesh.Write16bIndicesTo(ms) myMesh.Write32bIndicesTo(ms) End Using Console.WriteLine("Indices = {0}, Actual vertices = {1}, vertices before merging = {2}", myMesh.IndicesCount, myMesh.VerticesCount, myMesh.UnmergedVerticesCount) Console.WriteLine("Total bytes of vertices in memory {0}bytes", myMesh.VerticesSizeInBytes) End Sub //This code example converts a Box to triangle mesh with custom memory layout. // [C# Code Sample] // get mesh of the Box Mesh box = (new Box()).ToMesh(); //create a customized vertex layout VertexDeclaration vd = new VertexDeclaration(); VertexField position = vd.AddField(VertexFieldDataType.FVector4, VertexFieldSemantic.Position); vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Normal); // get a triangle mesh TriMesh triMesh = TriMesh.FromMesh(box); //[VB.NET Code Sample] ' get mesh of the Box Dim box As Mesh = (New Box()).ToMesh() 'create a customized vertex layout Dim vd As New VertexDeclaration() Dim position As VertexField = vd.AddField(VertexFieldDataType.FVector4, VertexFieldSemantic.Position) vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Normal) ' get a triangle mesh Dim triMesh As TriMesh = TriMesh.FromMesh(box)

Url: http://www.aspose.com/.net/3d-component.aspx

Language: C# | User: Sheraz Khan | Created: May 4, 2016 | Tags: Convert Primitives Object to Triangle Mesh Access Vertices of a Triangle Mesh .NET 3D Component work with 3D File Formats Convert a Sphere to Triangle Mesh