MeshGeometry.combine constructor
Implementation
factory MeshGeometry.combine(List<MeshGeometry> meshes) {
if (meshes.length < 2) {
throw Exception(
'Must provide at least two MeshGeometry instances to combine.');
}
// When combining meshes they must all have a matching set of VertexAttribs
final firstMesh = meshes[0];
var totalVerts = firstMesh.length;
var totalIndices =
firstMesh.indices != null ? firstMesh.indices!.length : 0;
for (var i = 1; i < meshes.length; ++i) {
final srcMesh = meshes[i];
if (!firstMesh.attribsAreCompatible(srcMesh)) {
throw Exception(
'All meshes must have identical attributes to combine.');
}
totalVerts += srcMesh.length;
totalIndices += srcMesh.indices != null ? srcMesh.indices!.length : 0;
}
final mesh =
MeshGeometry._internal(totalVerts, firstMesh.stride, firstMesh.attribs);
if (totalIndices > 0) {
mesh.indices = Uint16List(totalIndices);
}
// Copy over the buffer data:
var bufferOffset = 0;
var indexOffset = 0;
var vertexOffset = 0;
for (var i = 0; i < meshes.length; ++i) {
final srcMesh = meshes[i];
mesh.buffer.setAll(bufferOffset, srcMesh.buffer);
if (totalIndices > 0) {
for (var j = 0; j < srcMesh.indices!.length; ++j) {
mesh.indices![j + indexOffset] = srcMesh.indices![j] + vertexOffset;
}
vertexOffset += srcMesh.length;
indexOffset += srcMesh.indices!.length;
}
bufferOffset += srcMesh.buffer.length;
}
return mesh;
}