article

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

Email
Submitted on: 5/4/2016 10:36:35 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 1767
 
     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).

 
				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.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]
 Structure MyVertex

Private position As FVector3

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


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.