flutter3d
Showcase Changelog 62 packages API reference

Fixed step and interpolation: the source

since 0.4.0 or earlier Simulation, audio and XR

Regions the guide quotes: clock (line 18), gate (line 50), advance (line 66), blend (line 68).

1/// A simulation that always advances by the same amount, and a picture that
2/// still moves smoothly when the display and the simulation disagree about
3/// how fast time passes.
4///
5/// Quoted by `fixed_step.md` and shown whole in the Source tab.
6library;
7
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
10import 'package:flutter3d_sim/flutter3d_sim.dart';
12/// The `fixed-step` page: a marker stepped at a fixed rate and drawn
13/// interpolated. Its check is that a 1/60 s frame runs exactly one step.
14final class FixedStepDemo extends ShowcaseDemo {
15 /// Whether the menu is open, which pauses the step.
16 bool menuOpen = false;
18 late final EngineLoop _loop = EngineLoop(input: InputState())
19 ..addSystem('marker.move', LoopPhase.rules, (LoopContext step) {
20 _stepsRun++;
21 final x = (_stepsRun * step.dt * 1.5) % 2.0 - 1.0;
22 _position.push(Vector3(x, 0.0, 0.0));
23 });
24 final InterpolatedVector3 _position = InterpolatedVector3();
25 int _stepsRun = 0;
27 late final MeshNode _ball;
28 final Vector3 _drawn = Vector3.zero();
30 @override
31 Scene build(DemoContext context) {
32 final material = RenderMaterial(
33 name: 'marker',
34 baseColor: LinearColor.fromSrgb(0.8, 0.5, 0.2, 1.0),
35 roughness: 0.6,
36 );
37 _ball = MeshNode(
38 DeviceMesh.upload(context.device, SphereShape(segments: 24).build()),
39 material,
40 name: 'marker',
41 );
42 return Scene()
43 ..add(_ball)
44 ..add(
45 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
46 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
47 );
48 }
50 /// Whether the step should run at all this frame.
51 ///
52 /// `shouldPause` answers from facts about the player's attention, not from
53 /// a device: a menu open is reason enough on its own, whatever pointer or
54 /// pad is connected.
55 bool get _paused => shouldPause(
56 ready: true,
57 menuOpen: menuOpen,
58 pointerIsTheGate: false,
59 pointerHeld: false,
60 padConnected: false,
61 );
63 @override
64 void update(DemoContext context, double dt) {
65 if (_paused) return;
66 _loop.frame(dt);
68 _position.read(_loop.alpha, _drawn);
69 _ball.setPositionFrom(_drawn);
70 }
72 @override
73 List<DemoControl> controls(DemoContext context) => <DemoControl>[
74 ToggleControl(
75 'Menu open (pauses the step)',
76 value: () => menuOpen,
77 onChanged: (bool v) => menuOpen = v,
78 ),
79 ];
81 @override
82 void verify(Scene scene, FrameResult frame) {
83 if (frame.drawCalls < 1) {
84 throw StateError('the marker was not drawn');
85 }
86 // One frame of exactly one step's worth of time runs exactly one step:
87 // the accumulator starts empty and this page's step is 1/60 second.
88 if (_stepsRun != 1) {
89 throw StateError(
90 'a 1/60s frame should have run exactly one fixed step, ran $_stepsRun',
91 );
92 }
93 }