loadMesh method

dynamic loadMesh(
  1. dynamic meshIndex
)

Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes @param {number} meshIndex @return {Promise<Group|Mesh|SkinnedMesh>}

Implementation

loadMesh(meshIndex) async {
  var parser = this;
  var json = this.json;
  var extensions = this.extensions;

  Map<String, dynamic> meshDef = json["meshes"][meshIndex];
  var primitives = meshDef["primitives"];

  List<Future> pending = [];

  for (var i = 0, il = primitives.length; i < il; i++) {
    var material = primitives[i]["material"] == null
        ? createDefaultMaterial(cache)
        : await getDependency('material', primitives[i]["material"]);

    pending.add(Future.sync(() => material));
  }

  pending.add(parser.loadGeometries(primitives));

  final results = await Future.wait(pending);

  var materials = slice(results, 0, results.length - 1);
  var geometries = results[results.length - 1];

  var meshes = [];

  for (var i = 0, il = geometries.length; i < il; i++) {
    var geometry = geometries[i];
    Map<String, dynamic> primitive = primitives[i];

    // 1. create Mesh

    var mesh;

    var material = materials[i];

    if (primitive["mode"] == webGlConstants["TRIANGLES"] ||
        primitive["mode"] == webGlConstants["TRIANGLE_STRIP"] ||
        primitive["mode"] == webGlConstants["TRIANGLE_FAN"] ||
        primitive["mode"] == null) {
      // .isSkinnedMesh isn't in glTF spec. See ._markDefs()
      mesh = meshDef["isSkinnedMesh"] == true ? SkinnedMesh(geometry, material) : Mesh(geometry, material);

      if (mesh is SkinnedMesh && !mesh.geometry!.attributes["skinWeight"].normalized) {
        // we normalize floating point skin weight array to fix malformed assets (see #15319)
        // it's important to skip this for non-float32 data since normalizeSkinWeights assumes non-normalized inputs
        mesh.normalizeSkinWeights();
      }

      if (primitive["mode"] == webGlConstants["TRIANGLE_STRIP"]) {
        mesh.geometry = toTrianglesDrawMode(mesh.geometry, TriangleStripDrawMode);
      } else if (primitive["mode"] == webGlConstants["TRIANGLE_FAN"]) {
        mesh.geometry = toTrianglesDrawMode(mesh.geometry, TriangleFanDrawMode);
      }
    } else if (primitive["mode"] == webGlConstants["LINES"]) {
      mesh = LineSegments(geometry, material);
    } else if (primitive["mode"] == webGlConstants["LINE_STRIP"]) {
      mesh = Line(geometry, material);
    } else if (primitive["mode"] == webGlConstants["LINE_LOOP"]) {
      mesh = LineLoop(geometry, material);
    } else if (primitive["mode"] == webGlConstants["POINTS"]) {
      mesh = Points(geometry, material);
    } else {
      throw ('THREE.GLTFLoader: Primitive mode unsupported: ${primitive["mode"]}');
    }

    if (mesh.geometry.morphAttributes.keys.length > 0) {
      updateMorphTargets(mesh, meshDef);
    }

    mesh.name = parser.createUniqueName(meshDef["name"] ?? ('mesh_$meshIndex'));

    assignExtrasToUserData(mesh, meshDef);

    if (primitive["extensions"] != null) addUnknownExtensionsToUserData(extensions, mesh, primitive);

    parser.assignFinalMaterial(mesh);

    meshes.add(mesh);
  }

  if (meshes.length == 1) {
    return meshes[0];
  }

  var group = Group();

  for (var i = 0, il = meshes.length; i < il; i++) {
    group.add(meshes[i]);
  }

  return group;
}