A level is a scene, and the camera frames the whole party
Open the live demo · Read the source · View on GitHub
A game with levels moves from one to the next, and in flutter3d a level is a
scene: its floor, its lights, its air. HasFlutter3d.replaceScene3d moves a
Flame game's 3D layer to the next scene, with the camera, on the same device
and renderer. ViewCamera eases a camera towards wherever a function says,
for a view no single component decides, such as a party of four.
Step 1: Two levels, two scenes #
Both are built up front. The game opens on the first, and the second waits without being drawn.
// Two levels, each a scene of its own: its floor, its light, its air.
// The first is the one the game opens on; the second waits unseen.
final List<Scene> levels = <Scene>[
_level(
context.device,
floor: Vector4(0.36, 0.5, 0.3, 1.0),
air: Vector3(0.55, 0.6, 0.7),
rocks: Vector4(0.6, 0.58, 0.52, 1.0),
seed: 1,
),
_level(
context.device,
floor: Vector4(0.3, 0.28, 0.34, 1.0),
air: Vector3(0.35, 0.3, 0.5),
rocks: Vector4(0.45, 0.4, 0.6, 1.0),
seed: 2,
),
];
_game = _Tour(context.camera, levels)
..open3d(context.device, scene: levels.first);
Step 2: Move on with replaceScene3d #
Every six seconds the party goes to the next level. The game carries its own
nodes across, since what was in the old scene stays there, and then replaces
the scene. The camera goes with it, and Flutter3dFlameWidget draws the
game's scene as it is now, not the one it opened with.
/// The next level: the party is carried across, then the scene is
/// replaced, and the camera goes with it. What the old level holds stays
/// there for the next visit.
void _nextLevel() {
_clock = 0.0;
_level = (_level + 1) % levels.length;
final Scene next = levels[_level];
for (final MeshNode member in party) {
member.removeFromParent();
next.add(member);
}
replaceScene3d(next);
_caption.text = _level == 0 ? 'level one' : 'level two';
}
Step 3: Where the party should be seen from #
The view is worked out from every member at once: above and behind their middle, further back the more they spread. It is asked once a frame and writes the eye and the point looked at into the two vectors it is handed.
/// Above and behind the party's middle, further back the more it spreads.
bool frameParty(Vector3 eye, Vector3 target) {
if (party.isEmpty) return false;
final Vector3 middle = party.fold(
Vector3.zero(),
(Vector3 sum, MeshNode m) => sum..add(m.readPosition()),
)..scale(1.0 / party.length);
final double spread = party.fold(
0.0,
(double far, MeshNode m) =>
math.max(far, m.readPosition().distanceTo(middle)),
);
target.setFrom(middle);
eye.setValues(
middle.x,
middle.y + 3.5 + spread * 1.5,
middle.z + 5.0 + spread * 1.5,
);
return true;
}
Step 4: A camera eased towards it #
ViewCameraComponent runs the view camera after whatever moves what it
frames. On a new level the party starts somewhere else, and the camera eases
across to it rather than jumping.
@override
void onOpen3d() {
final DeviceMesh ball = DeviceMesh.upload(
device,
SphereShape(segments: 20, radius: 0.4).build(),
);
for (var i = 0; i < _colors.length; i++) {
final MeshNode member = MeshNode(
ball,
RenderMaterial(name: 'member $i', baseColor: _fromSrgb(_colors[i])),
name: 'member $i',
);
party.add(member);
scene.add(member);
}
_walk();
// Eased each frame towards wherever `frameParty` says, after the party
// has walked: no one member decides where the camera goes.
addAll(<Component>[
ViewCameraComponent(
ViewCamera(camera: camera3d, view: frameParty, stiffness: 2.5),
),
_caption,
]);
}