addModel method

List<MeshInstance> addModel(
  1. GpuModel model,
  2. Matrix4 worldFromModel, {
  3. bool correctAxes = true,
  4. PosedModel? posed,
})

Adds an instance for every primitive of every mesh node of model, placed at worldFromModel (engine world from the model's root). glTF axes are converted to the engine's unless correctAxes is false.

With posed, the instances follow its pose (see PosedModel.place) and skinned primitives are drawn skinned; without, the model shows its rest pose and skins their bind pose.

Implementation

List<MeshInstance> addModel(
  GpuModel model,
  Matrix4 worldFromModel, {
  bool correctAxes = true,
  PosedModel? posed,
}) {
  final added = <MeshInstance>[];
  final root = correctAxes
      ? worldFromModel.multiplied(gltfToEngine)
      : Matrix4.copy(worldFromModel);
  final skins = model.data.skins.length;
  model.data.forEachNode((node, modelFromNode) {
    final data = model.data.nodes[node];
    final mesh = data.mesh;
    if (mesh == null || mesh >= model.meshes.length) return;
    final skin = data.skin != null && data.skin! < skins ? data.skin : null;
    // A skinned mesh is placed by its joints, not by its node.
    final world = skin != null
        ? Matrix4.copy(root)
        : root.multiplied(modelFromNode);
    for (final primitive in model.meshes[mesh]) {
      final instance = MeshInstance(
        primitive,
        model.materialOf(primitive),
        world,
      )..node = node;
      if (posed != null && skin != null && primitive.skin != null) {
        instance
          ..skin = posed.palettes[skin]
          ..localBounds = posed.skinBounds[skin];
      }
      added.add(instance);
    }
  });
  instances.addAll(added);
  if (posed != null) {
    posed.instances.addAll(added);
    posed.place(root);
  }
  return added;
}