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(this.cache)
        : await this.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"] == WEBGL_CONSTANTS["TRIANGLES"] ||
        primitive["mode"] == WEBGL_CONSTANTS["TRIANGLE_STRIP"] ||
        primitive["mode"] == WEBGL_CONSTANTS["TRIANGLE_FAN"] ||
        primitive["mode"] == null) {
      // .isSkinnedMesh isn't in glTF spec. See ._markDefs()
      mesh = meshDef["isSkinnedMesh"] == true
          ? new SkinnedMesh(geometry, material)
          : new 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"] == WEBGL_CONSTANTS["TRIANGLE_STRIP"]) {
        mesh.geometry =
            toTrianglesDrawMode(mesh.geometry, TriangleStripDrawMode);
      } else if (primitive["mode"] == WEBGL_CONSTANTS["TRIANGLE_FAN"]) {
        mesh.geometry =
            toTrianglesDrawMode(mesh.geometry, TriangleFanDrawMode);
      }
    } else if (primitive["mode"] == WEBGL_CONSTANTS["LINES"]) {
      mesh = new LineSegments(geometry, material);
    } else if (primitive["mode"] == WEBGL_CONSTANTS["LINE_STRIP"]) {
      mesh = new Line(geometry, material);
    } else if (primitive["mode"] == WEBGL_CONSTANTS["LINE_LOOP"]) {
      mesh = new LineLoop(geometry, material);
    } else if (primitive["mode"] == WEBGL_CONSTANTS["POINTS"]) {
      mesh = new 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 = new Group();

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

  return group;
}