public class TriMesh extends Entity implements java.lang.Iterable<Vertex>
//Define a vertex declaration as {FVector3 Position; FVector3 Normal; FVector2 UV}
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
vd.addField(VertexFieldDataType.F_VECTOR2, VertexFieldSemantic.UV);
//convert a mesh to tri-mesh using specified memory layout
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(vd, mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
try(var s = new FileOutputStream("output.bin")) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
name, properties| Constructor and Description |
|---|
TriMesh(java.lang.String name,
VertexDeclaration declaration)
Initialize an instance of
TriMesh |
| Modifier and Type | Method and Description |
|---|---|
void |
addTriangle(int a,
int b,
int c)
Add a new triangle
|
Vertex |
beginVertex()
Begin adding vertex
|
static TriMesh |
copyFrom(TriMesh input,
VertexDeclaration vd)
Copy the
TriMesh from input with new vertex layout |
int |
endVertex()
End adding vertex
|
static TriMesh |
fromMesh(Mesh mesh)
Create a TriMesh from given mesh object, the vertex declaration are based on the input mesh's structure.
|
static TriMesh |
fromMesh(Mesh mesh,
boolean useFloat)
Create a TriMesh from given mesh object, the vertex declaration are based on the input mesh's structure.
|
static TriMesh |
fromMesh(VertexDeclaration declaration,
Mesh mesh)
Create a TriMesh from given mesh object with given vertex layout.
|
static TriMesh |
fromRawData(VertexDeclaration vd,
byte[] vertices,
int[] indices,
boolean generateVertexMapping)
Create TriMesh from raw data
|
int |
getCapacity()
The capacity of pre-allocated vertices.
|
int |
getIndicesCount()
The count of indices in this
TriMesh |
int[] |
getIntIndices()
Convert the indices to 32bit integer array
|
short[] |
getShortIndices()
Convert the indices to 16bit integer array
|
int |
getUnmergedVerticesCount()
The count of unmerged vertices that passed in by
beginVertex() and endVertex(). |
VertexDeclaration |
getVertexDeclaration()
The vertex layout of the
TriMesh. |
int |
getVerticesCount()
The count of vertices in this
TriMesh |
int |
getVerticesSizeInBytes()
The total size of all vertices in bytes
|
void |
indicesToArray(int[][] result)
Deprecated.
|
void |
indicesToArray(short[][] result)
Deprecated.
|
java.util.Iterator<Vertex> |
iterator()
Get the enumerator to enumerate
Vertex |
void |
loadVerticesFromBytes(byte[] verticesInBytes)
Load vertices from bytes, the length of bytes must be an integer multiple of vertex size.
|
double |
readDouble(int idx,
VertexField field)
Read the double field
|
float |
readFloat(int idx,
VertexField field)
Read the float field
|
FVector2 |
readFVector2(int idx,
VertexField field)
Read the vector2 field
|
FVector3 |
readFVector3(int idx,
VertexField field)
Read the vector3 field
|
FVector4 |
readFVector4(int idx,
VertexField field)
Read the vector4 field
|
Vector2 |
readVector2(int idx,
VertexField field)
Read the vector2 field
|
Vector3 |
readVector3(int idx,
VertexField field)
Read the vector3 field
|
Vector4 |
readVector4(int idx,
VertexField field)
Read the vector4 field
|
java.lang.String |
toString()
Gets the string representation of
TriMesh |
byte[] |
verticesToArray()
Convert the vertices data to byte array
|
void |
write16bIndicesTo(java.io.OutputStream stream)
Write the indices data as 16bit integer to the stream
|
void |
write16bIndicesTo(Stream stream)
Write the indices data as 16bit integer to the stream
Example:
|
void |
write32bIndicesTo(java.io.OutputStream stream)
Write the indices data as 32bit integer to the stream
|
void |
write32bIndicesTo(Stream stream)
Write the indices data as 32bit integer to the stream
Example:
|
void |
writeVerticesTo(java.io.OutputStream stream)
Write vertices data to the specified stream
|
void |
writeVerticesTo(Stream stream)
Write vertices data to the specified stream
|
getBoundingBox, getExcluded, getParentNode, getParentNodes, setExcluded, setParentNodegetScenefindProperty, getName, getProperties, getProperty, removeProperty, removeProperty, setName, setPropertypublic TriMesh(java.lang.String name,
VertexDeclaration declaration)
TriMeshname - The name of this TriMeshdeclaration - The vertex's declarationpublic VertexDeclaration getVertexDeclaration()
TriMesh.TriMesh.public int getVerticesCount()
TriMeshTriMeshpublic int getIndicesCount()
TriMeshTriMeshpublic int getUnmergedVerticesCount()
beginVertex() and endVertex().beginVertex() and endVertex().public int getCapacity()
public int getVerticesSizeInBytes()
public static TriMesh fromMesh(VertexDeclaration declaration, Mesh mesh)
declaration - Vertex's type definition, or memory layoutmesh - Source mesh
//Define a vertex declaration as {FVector3 Position; FVector3 Normal; FVector2 UV}
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
vd.addField(VertexFieldDataType.F_VECTOR2, VertexFieldSemantic.UV);
//convert a mesh to tri-mesh using specified memory layout
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(vd, mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
try(var s = new FileOutputStream("output.bin")) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
public static TriMesh copyFrom(TriMesh input, VertexDeclaration vd)
TriMesh from input with new vertex layoutinput - The input TriMesh for copyingvd - The new vertex declaration of the output TriMesh
//Define a vertex declaration as {FVector3 Position; FVector3 Normal; FVector2 UV}
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
vd.addField(VertexFieldDataType.F_VECTOR2, VertexFieldSemantic.UV);
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var oldTriMesh = TriMesh.fromMesh((new Sphere()).toMesh());
//now create a new TriMesh from old TriMesh, using explicit memory layout defined by vd
var newTriMesh = TriMesh.copyFrom(oldTriMesh, vd);
public static TriMesh fromMesh(Mesh mesh, boolean useFloat)
useFloat - Use float type instead of double type for each vertex element component.TriMesh generated from given Mesh
Example:
The following code shows how to create a TriMesh with custom memory layout, and export it to file.
//Define a vertex declaration as {FVector3 Position; FVector3 Normal; FVector2 UV}
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
vd.addField(VertexFieldDataType.F_VECTOR2, VertexFieldSemantic.UV);
//convert a mesh to tri-mesh using specified memory layout
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(vd, mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
try(var s = new FileOutputStream("output.bin")) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
public static TriMesh fromMesh(Mesh mesh)
TriMesh generated from given Mesh
Example:
The following code shows how to create a TriMesh with custom memory layout, and export it to file.
//Define a vertex declaration as {FVector3 Position; FVector3 Normal; FVector2 UV}
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.NORMAL);
vd.addField(VertexFieldDataType.F_VECTOR2, VertexFieldSemantic.UV);
//convert a mesh to tri-mesh using specified memory layout
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(vd, mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
try(var s = new FileOutputStream("output.bin")) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
public Vertex beginVertex()
Vertexpublic int endVertex()
public void writeVerticesTo(Stream stream) throws java.io.IOException
stream - The stream that the vertices data will be written to
Example:
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
try(var s = Stream.wrap(stream)) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
java.io.IOExceptionpublic void write16bIndicesTo(Stream stream) throws java.io.IOException
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
try(var s = Stream.wrap(stream)) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(s);
}
java.io.IOExceptionpublic void write32bIndicesTo(Stream stream) throws java.io.IOException
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
try(var s = Stream.wrap(stream)) {
triMesh.writeVerticesTo(s);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write32bIndicesTo(s);
}
java.io.IOExceptionpublic byte[] verticesToArray()
@Deprecated public void indicesToArray(short[][] result)
public short[] getShortIndices()
public int[] getIntIndices()
@Deprecated public void indicesToArray(int[][] result)
public java.lang.String toString()
TriMeshtoString in class java.lang.Objectpublic static TriMesh fromRawData(VertexDeclaration vd, byte[] vertices, int[] indices, boolean generateVertexMapping)
vd - Vertex declaration, must contains at least one field.vertices - The input vertex data, the minimum length of the vertices must be greater or equal to vertex declaration's sizeindices - The triangle indicesgenerateVertexMapping - Generate Vertex for each vertex, which is not necessary for just serialization/deserialization.TriMesh instance that encapsulated the input byte array.
Remarks:
The returned TriMesh will not copy the input byte array for performance, external changes on the array will be reflected to this instance.
Example:
The following code shows how to construct a TriMesh from raw bytes, this is useful when build your own 3D format
var indices = new int[] { 0, 1, 2 };
var vertices = new byte[]{
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 191,
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 63,
0, 0, 0, 63,
0, 0, 0, 0The string representation,
0, 0, 0, 63
};
VertexDeclaration vd = new VertexDeclaration();
vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
var triMesh = TriMesh.FromRawData(vd, vertices, indices, true);
public void loadVerticesFromBytes(byte[] verticesInBytes)
var indices = new int[] { 0, 1, 2 };
var vertices = new byte[]{
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 191,
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 63,
0, 0, 0, 63,
0, 0, 0, 0,
0, 0, 0, 63
};
VertexDeclaration vd = new VertexDeclaration();
vd.AddField(VertexFieldDataType.FVector3, VertexFieldSemantic.Position);
//create an empty TriMesh with specified vertex declaration
var triMesh = new TriMesh("", vd);
//load vertices directly from bytes
triMesh.LoadVerticesFromBytes(vertices);
triMesh.AddTriangle(0, 1, 2);
int[] indices = new int[] { 0, 1, 2 };
byte[] vertices = new byte[]{
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 191,
0, 0, 0, 191,
0, 0, 0, 0,
0, 0, 0, 63,
0, 0, 0, 63,
0, 0, 0, 0,
0, 0, 0, 63
};
VertexDeclaration vd = new VertexDeclaration();
vd.addField(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION);
//create an empty TriMesh with specified vertex declaration
var triMesh = new TriMesh("", vd);
//load vertices directly from bytes
triMesh.loadVerticesFromBytes(vertices);
triMesh.addTriangle(0, 1, 2);
public void addTriangle(int a,
int b,
int c)
a - The index of first vertexb - The index of second vertexc - The index of third vertexpublic Vector4 readVector4(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector4/FVector4 data typepublic FVector4 readFVector4(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector4/FVector4 data typepublic Vector3 readVector3(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector3/FVector3 data typepublic FVector3 readFVector3(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector3/FVector3 data typepublic Vector2 readVector2(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector2/FVector2 data typepublic FVector2 readFVector2(int idx, VertexField field)
idx - The index of vertex to readfield - The field with a Vector2/FVector2 data typepublic double readDouble(int idx,
VertexField field)
idx - The index of vertex to readfield - The field with a float/double compatible data typepublic float readFloat(int idx,
VertexField field)
idx - The index of vertex to readfield - The field with a float/double compatible data typepublic void writeVerticesTo(java.io.OutputStream stream)
throws java.io.IOException
stream - The stream that the vertices data will be written to
Example:
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
triMesh.writeVerticesTo(stream);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(stream);
java.io.IOExceptionpublic void write16bIndicesTo(java.io.OutputStream stream)
throws java.io.IOException
stream - Example:
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
triMesh.writeVerticesTo(stream);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write16bIndicesTo(stream);
java.io.IOExceptionpublic void write32bIndicesTo(java.io.OutputStream stream)
throws java.io.IOException
stream - Example:
//convert a mesh to TriMesh, the layout is automatically inferred from input mesh
var mesh = (new Sphere()).toMesh();
var triMesh = TriMesh.fromMesh(mesh);
//save it to a stream, 115 vertices * 32bytes per vertex
var stream = new ByteArrayOutputStream();
triMesh.writeVerticesTo(stream);
//save indices as ushort to stream, 504 indices * 2 bytes per index
triMesh.write32bIndicesTo(stream);
java.io.IOException