buildMesh function

Future<List<Mesh>> buildMesh(
  1. List<Vector3> vertices,
  2. List<Vector3> normals,
  3. List<Offset> texcoords,
  4. List<Triangle> triangles,
  5. Map<String, Material>? materials,
  6. List<String> elementNames,
  7. List<String> elementMaterials,
  8. List<int> elementOffsets,
  9. String basePath,
  10. bool isAsset,
)

Load the texture image file and rebuild vertices and texcoords to keep the same length.

Implementation

Future<List<Mesh>> buildMesh(
  List<Vector3> vertices,
  List<Vector3> normals,
  List<Offset> texcoords,
  List<Triangle> triangles,
  Map<String, Material>? materials,
  List<String> elementNames,
  List<String> elementMaterials,
  List<int> elementOffsets,
  String basePath,
  bool isAsset,
) async {
  if (elementOffsets.isEmpty) {
    elementNames.add('');
    elementMaterials.add('');
    elementOffsets.add(0);
  }

  final List<Mesh> meshes = <Mesh>[];
  for (int index = 0; index < elementOffsets.length; index++) {
    int faceStart = elementOffsets[index];
    int faceEnd = (index + 1 < elementOffsets.length) ? elementOffsets[index + 1] : triangles.length;

    var newVertices = <Vector3>[];
    var newNormals = <Vector3>[];
    var newTexcoords = <Offset>[];
    var newTriangles = <Triangle>[];

    if (faceStart == 0 && faceEnd == triangles.length) {
      newVertices = vertices;
      newNormals = normals;
      newTexcoords = texcoords;
      newTriangles = triangles;
    }
    else {
      _copyRangeIndices(faceStart, faceEnd, vertices, normals, texcoords ,triangles, newVertices, newNormals, newTexcoords, newTriangles);
    }

    // load texture image from assets.
    final Material? material = (materials != null) ? materials[elementMaterials[index]] : null;
    final MapEntry<String, Image>?imageEntry = await loadTexture(material, basePath,isAsset: false);

    // fix zero texture area
    if (imageEntry != null){
      _remapZeroAreaUVs(newTexcoords, newTriangles, imageEntry.value.width.toDouble(), imageEntry.value.height.toDouble());
    }
    // If a vertex has multiple different texture coordinates,
    // then create a vertex for each texture coordinate.
    _rebuildVertices(newVertices, newNormals, newTexcoords, newTriangles);
    final Mesh mesh = Mesh(
      vertices: newVertices,
      normals: newNormals,
      texcoords: newTexcoords,
      indices: newTriangles,
      texture: imageEntry?.value,
      texturePath: imageEntry?.key,
      name: elementNames[index],
    );

    meshes.add(mesh);
  }

  return meshes;
}