parseMesh static method
Implementation
static Uint8List parseMesh(Mesh object, int offset, [ByteData? output]){
BufferGeometry? geometry = object.geometry;
final Float32BufferAttribute? vertices = geometry?.getAttribute(Attribute.position);
final indices = geometry?.getIndex();
if(vertices != null && indices != null){
if(output == null){
int triangles = indices.length~/3;
offset = 80; // skip header
int bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
//final arrayBuffer = new ArrayBuffer( bufferLength );
output = ByteData(bufferLength);
output.setUint32( offset, triangles, Endian.little ); offset += 4;
}
for (int i = 0, l = indices.length; i < l; i+=3) {
final faces = [indices.getX(i)!.toInt(), indices.getX(i+1)!.toInt(), indices.getX(i+2)!.toInt()];
List<Vector3> vecsToNormal = [];
for(int j = 0; j < 3; j ++){
vecsToNormal.add(Vector3(vertices.getX(faces[j])!.toDouble(),vertices.getY(faces[j])!.toDouble(),vertices.getZ(faces[j])!.toDouble()));
}
Vector3 toNormal = _computeNormal(vecsToNormal[0],vecsToNormal[1],vecsToNormal[2]);
output.setFloat32( offset, toNormal.x, Endian.little ); offset += 4; // normal
output.setFloat32( offset, toNormal.y, Endian.little ); offset += 4;
output.setFloat32( offset, toNormal.z, Endian.little ); offset += 4;
for ( int j = 0; j < 3; j ++ ) {
output.setFloat32( offset, vecsToNormal[j].x, Endian.little ); offset += 4; // vertices
output.setFloat32( offset, vecsToNormal[j].y, Endian.little ); offset += 4;
output.setFloat32( offset, vecsToNormal[j].z, Endian.little ); offset += 4;
}
output.setUint16( offset, 0, Endian.little ); offset += 2; // attribute byte count
}
}
else{
throw("There are no verticies or indicies for this mesh.");
}
return output.buffer.asUint8List();
}