Home > Code > C# > How to Convert Primitive Object to a Triangle Mesh with Custom Memory Layout in .NET Apps

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

by sher azam   on May 04, 2016   Category: C#   |  Views: 386    |  Points: 25   |  Gold 


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.

//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)

Overview: Aspose.3D for .NET

Aspose.3D for .NET is a feature-rich component and class library for .NET that empowers Mono and .NET application including ASP.NET, Windows Forms and Web Services to connect with prevalent 3D document formats automatically without the 3D modeling and rendering software being installed on the server. It supports FBX (ASCII, Binary) and STL (ASCII, Binary) file formats and developers can easily create, read, convert, modify and control the substance of these 3D document formats using Aspose.3D API.

More about Aspose.3D for .NET

- Homepage of Aspose.3D for .NET: http://www.aspose.com/.net/3d-component.aspx

- Download Aspose.3D for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.3d-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts