MeshGeometry.combine constructor

MeshGeometry.combine(List<MeshGeometry> meshes)

Implementation

factory MeshGeometry.combine(List<MeshGeometry> meshes) {
  if (meshes == null || meshes.length < 2) {
    throw new Exception(
        "Must provide at least two MeshGeometry instances to combine.");
  }

  // When combining meshes they must all have a matching set of VertexAttribs
  final MeshGeometry firstMesh = meshes[0];
  int totalVerts = firstMesh.length;
  int totalIndices = firstMesh.indices != null ? firstMesh.indices.length : 0;
  for (int i = 1; i < meshes.length; ++i) {
    final MeshGeometry srcMesh = meshes[i];
    if (!firstMesh.attribsAreCompatible(srcMesh)) {
      throw new Exception(
          "All meshes must have identical attributes to combine.");
    }
    totalVerts += srcMesh.length;
    totalIndices += srcMesh.indices != null ? srcMesh.indices.length : 0;
  }

  final MeshGeometry mesh = new MeshGeometry._internal(
      totalVerts, firstMesh.stride, firstMesh.attribs);

  if (totalIndices > 0) {
    mesh.indices = new Uint16List(totalIndices);
  }

  // Copy over the buffer data:
  int bufferOffset = 0;
  int indexOffset = 0;
  int vertexOffset = 0;
  for (int i = 0; i < meshes.length; ++i) {
    final MeshGeometry srcMesh = meshes[i];
    mesh.buffer.setAll(bufferOffset, srcMesh.buffer);

    if (totalIndices > 0) {
      for (int 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;
}