Mesh — Aspose.3D FOSS for Java
Package: com.aspose.threed (aspose-3d-foss 26.1.0)
Mesh stores polygon geometry as a list of control points (vertex positions) and a list of polygon faces. Each polygon face is a list of zero-based indices into the control points array. Faces may be triangles, quads, or higher-arity polygons. Additional per-vertex data – normals, UV coordinates, vertex colours – is attached as VertexElement layers.
public class Mesh extends GeometryInheritance
A3DObject -> SceneObject -> Entity -> Geometry -> Mesh
Examples
Build a single triangle mesh from scratch:
import com.aspose.threed.Scene;
import com.aspose.threed.*;
// Create the mesh and add three vertex positions
Mesh mesh = new Mesh();
mesh.getControlPoints().add(new Vector4(0.0, 0.0, 0.0, 1.0)); // vertex 0
mesh.getControlPoints().add(new Vector4(1.0, 0.0, 0.0, 1.0)); // vertex 1
mesh.getControlPoints().add(new Vector4(0.5, 1.0, 0.0, 1.0)); // vertex 2
// Define one triangle face using vertex indices
mesh.createPolygon(0, 1, 2);
// Attach to a scene and save
Scene scene = new Scene();
scene.getRootNode().createChildNode("triangle", mesh);
scene.save("triangle.stl");Build a quad mesh and triangulate it before export:
import com.aspose.threed.Scene;
import com.aspose.threed.*;
Mesh mesh = new Mesh();
// Four corners of a unit square in the XZ plane
mesh.getControlPoints().add(new Vector4(0, 0, 0, 1.0));
mesh.getControlPoints().add(new Vector4(1, 0, 0, 1.0));
mesh.getControlPoints().add(new Vector4(1, 0, 1, 1.0));
mesh.getControlPoints().add(new Vector4(0, 0, 1, 1.0));
// One quad face
mesh.createPolygon(0, 1, 2, 3);
System.out.println("Polygons before triangulate: " + mesh.getPolygonCount()); // 1
Mesh triangulated = mesh.triangulate();
System.out.println("Polygons after triangulate: " + triangulated.getPolygonCount()); // 2
Scene scene = new Scene();
scene.getRootNode().createChildNode("quad", triangulated);
scene.save("quad.glb");Properties
| Property | Type | Getter | Setter | Description |
|---|---|---|---|---|
controlPoints | List<Vector4> | getControlPoints() | – | Vertex position array. Each entry is a Vector4(x, y, z, w) where w is 1.0 for position data. Add vertices by calling add() on the returned list. |
polygonCount | int | getPolygonCount() | – | Number of polygon faces defined on this mesh. |
polygons | List<int[]> | getPolygons() | – | All face definitions as a list of index arrays. Each inner array holds the vertex indices (into controlPoints) for one face. |
edges | List<Integer> | getEdges() | – | Raw edge index data. Primarily for internal use and advanced topology queries. |
vertexElements | List<VertexElement> | getVertexElements() | – | All vertex element layers currently attached to this mesh (normals, UVs, colours, etc.). |
visible | boolean | getVisible() | setVisible(boolean) | When false, the mesh is hidden in viewers that respect visibility. |
castShadows | boolean | getCastShadows() | setCastShadows(boolean) | Whether this mesh casts shadows in renderers that support shadow maps. |
receiveShadows | boolean | getReceiveShadows() | setReceiveShadows(boolean) | Whether this mesh receives shadows from other shadow-casting geometry. |
Methods
createPolygon(int... indices)
Define a new polygon face by providing the vertex indices in order. The indices reference positions in getControlPoints(). Accepts three or more indices for triangles, quads, and n-gons.
| Parameter | Type | Description |
|---|---|---|
indices | int... | Vertex index arguments in winding order (typically counter-clockwise when viewed from outside). |
Returns: void
Mesh mesh = new Mesh();
// ... populate control points ...
mesh.createPolygon(0, 1, 2); // triangle
mesh.createPolygon(0, 1, 2, 3); // quad
System.out.println(mesh.getPolygonCount()); // 2triangulate()
Stub: Returns a clone of the original mesh. Intended to split all polygons into triangles using fan triangulation, but actual triangulation logic is not yet implemented. The original mesh is not modified.
Returns: Mesh – a clone of the original mesh (stub behaviour).
import com.aspose.threed.*;
Mesh mesh = new Mesh();
mesh.getControlPoints().add(new Vector4(0, 0, 0, 1.0));
mesh.getControlPoints().add(new Vector4(1, 0, 0, 1.0));
mesh.getControlPoints().add(new Vector4(1, 1, 0, 1.0));
mesh.getControlPoints().add(new Vector4(0, 1, 0, 1.0));
mesh.createPolygon(0, 1, 2, 3); // one quad
Mesh triMesh = mesh.triangulate();
// Note: currently returns a clone, not a triangulated mesh
System.out.println(triMesh.getPolygonCount());toMesh()
Return this Mesh as a Mesh instance. For Mesh objects this is an identity operation (returns this). Defined on the Geometry base class to provide a uniform conversion interface when working with generic Geometry references.
Returns: Mesh
import com.aspose.threed.*;
Mesh ensureMesh(Geometry geom) {
return geom.toMesh();
}createElement(VertexElementType elementType, MappingMode mappingMode, ReferenceMode referenceMode)
Add a new VertexElement layer of the specified type to the mesh. Use this to attach normals, tangents, binormals, vertex colours, and smoothing groups.
| Parameter | Type | Description |
|---|---|---|
elementType | VertexElementType | The kind of data this layer holds (e.g., VertexElementType.NORMAL). |
mappingMode | MappingMode | How the data maps to geometry: CONTROL_POINT, POLYGON_VERTEX, POLYGON, etc. |
referenceMode | ReferenceMode | How indices are used: DIRECT or INDEX_TO_DIRECT. |
Returns: VertexElement
import com.aspose.threed.*;
Mesh mesh = new Mesh();
mesh.getControlPoints().add(new Vector4(0, 0, 0, 1));
mesh.getControlPoints().add(new Vector4(1, 0, 0, 1));
mesh.getControlPoints().add(new Vector4(0.5, 1, 0, 1));
mesh.createPolygon(0, 1, 2);
VertexElement normalElement = mesh.createElement(
VertexElementType.NORMAL,
MappingMode.CONTROL_POINT,
ReferenceMode.DIRECT
);createElementUV(TextureMapping uvMapping, MappingMode mappingMode, ReferenceMode referenceMode)
Add a UV coordinate layer to the mesh. This is the preferred method for attaching texture coordinate data.
| Parameter | Type | Description |
|---|---|---|
uvMapping | TextureMapping | The UV channel purpose: DIFFUSE, SPECULAR, NORMAL, AMBIENT, etc. |
mappingMode | MappingMode | How UVs map to geometry elements. |
referenceMode | ReferenceMode | Indexing mode: DIRECT or INDEX_TO_DIRECT. |
Returns: VertexElementUV
import com.aspose.threed.*;
Mesh mesh = new Mesh();
// ... define control points and polygons ...
VertexElementUV uvElement = mesh.createElementUV(
TextureMapping.DIFFUSE,
MappingMode.POLYGON_VERTEX,
ReferenceMode.INDEX_TO_DIRECT
);Boolean and Optimization Methods (Stubs)
The following methods exist on Mesh but are stubs that return a clone of the original mesh without performing their intended operation:
| Method | Return Type | Description |
|---|---|---|
union(Mesh other) | Mesh | Stub: returns a clone. Intended for CSG union. |
difference(Mesh other) | Mesh | Stub: returns a clone. Intended for CSG subtraction. |
intersect(Mesh other) | Mesh | Stub: returns a clone. Intended for CSG intersection. |
optimize() | Mesh | Stub: returns a clone. Intended for mesh optimization. |