Convert Mesh to Triangle Mesh and Primitive Shape to Mesh

Convert Mesh to Triangle Mesh with Custom Memory Layout of Vertex

Aspose.3D for Java API has support to convert mesh to triangle mesh with custom memory layout of the vertex. The custom memory layout of the Vertex is defined dynamically by VertexDeclaration class in the code examples.

Developers may convert mesh to triangle mesh because any complex (surface) structure can be represented as a bunch of triangles. The triangle is the most atomic geometry. Thus it is used as base for almost anything. This code example converts a Box to triangle mesh with custom memory layout.

// Initialize scene object
Scene scene = new Scene();
// Initialize Node class object
Node cubeNode = new Node("box");
// Get mesh of the Box
Mesh box = (new Box()).toMesh();
// Create a customized vertex layout
VertexDeclaration vd = new VertexDeclaration();
VertexField position = vd.addField(VertexFieldDataType.F_VECTOR4, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
// Get a triangle mesh
TriMesh triMesh = TriMesh.fromMesh(box);
// ExEnd:ConvertBoxMeshtoTriangleMeshCustomMemoryLayout
// Point node to the Mesh geometry
cubeNode.setEntity(box);
// Add Node to a scene
scene.getRootNode().getChildNodes().add(cubeNode);
// The path to the documents directory.
String MyDir = RunExamples.getDataDir() + RunExamples.getOutputFilePath("BoxToTriangleMeshCustomMemoryLayoutScene.fbx");
// Save 3D scene in the supported file formats
scene.save(MyDir, FileFormat.FBX7400ASCII);

Convert Primitive Shape to Mesh

Aspose.3D for Java API has support of converting any primitive shape to mesh. Primitive shapes include most basic and used objects like box, sphere, plane, cylinder, and torus.

Convert Sphere Primitive to Mesh

A sphere is a perfectly round geometrical object in three-dimensional space that appear everywhere from sports balls to planets in space. Let’s use the Sphere primitive to create a mesh. The code example below converts a Sphere to mesh.

// Initialize object by Sphere class
IMeshConvertible convertible = new Sphere();
// Convert a Sphere to Mesh
Mesh mesh = convertible.toMesh();

Convert Box to Mesh

A Box describes a variety of containers and receptacles for permanent use as storage, or for temporary use, often for transporting contents. Let’s use the Box primitive to create a mesh. The code example below converts a Box to mesh.

// Initialize object by Box class
IMeshConvertible convertible = new Box();
// Convert a Box to Mesh
Mesh mesh = convertible.toMesh();

Convert a Plane to Mesh

A plane extends infinitely without thickness. An example of a plane is a coordinate plane. Lets use the Plane primitive to create a mesh. The code example below converts a Plane to mesh.

// Initialize object by Plane class
IMeshConvertible convertible = new Plane();
// Convert a Plane to Mesh
Mesh mesh = convertible.toMesh();

Convert a Cylinder to Mesh

A cylinder is one of the most basic curvilinear geometric shapes, the surface formed by the points at a fixed distance from a given straight line, the axis of the cylinder. It can be used in many places, for example as a pillar in front of a home or as a car driveshaft. Lets use the Cylinder primitive to create a mesh. The code example below converts a Cylinder to mesh.

// Initialize object by Cylinder class
IMeshConvertible convertible = new Cylinder();
// Convert a Cylinder to Mesh
Mesh mesh = convertible.toMesh();

Convert a Torus to Mesh

A torus is a surface of revolution generated by revolving a circle in three-dimensional space about an axis coplanar with the circle. If the axis of revolution does not touch the circle, the surface has a ring shape and is called a torus of revolution. Let’s use the Torus primitive to create a mesh. The code example below converts a Torus to mesh.

// Initialize object by Torus class
IMeshConvertible convertible = new Torus();
// Convert a Torus to Mesh
Mesh mesh = convertible.toMesh();