Adaptive resolution: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: adaptive (line 16), record (line 30), scale (line 35), size (line 52).
1/// Adaptive resolution: `RenderSettings.renderScale` shrinks every target the
2/// frame draws into, and `AdaptiveScale` decides the number from how long a
3/// frame has been taking.
4///
5/// Quoted by `adaptive_resolution.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/pages/post/post_stage.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12final class AdaptiveResolutionDemo extends ShowcaseDemo {
13 double simulatedMs = 20.0;
14 double scale = 1.0;
16 /// A window of one frame, so this page reacts on the very next tick rather
17 /// than averaging several — a real game wants a wider window than this.
18 final AdaptiveScale _adaptive = AdaptiveScale(
19 const AdaptiveScaleSettings(enabled: true, window: 1),
20 );
23 void configureView(DemoContext context) => PostStage.frame(context);
26 Scene build(DemoContext context) => PostStage.build(context).scene;
29 void update(DemoContext context, double dt) {
30 scale = _adaptive.recordFrame((simulatedMs * 1000).round());
31 }
34 RenderSettings settings(DemoContext context) => RenderSettings(
35 renderScale: scale,
36 );
39 List<DemoControl> controls(DemoContext context) => <DemoControl>[
40 SliderControl(
41 'Simulated frame cost',
42 min: 4,
43 max: 40,
44 value: () => simulatedMs,
45 onChanged: (double v) => simulatedMs = v,
46 format: (double v) => '${v.round()} ms',
47 ),
48 ];
51 void verify(Scene scene, FrameResult frame) {
52 final int expectedWidth = (320 * scale).round();
53 final int expectedHeight = (180 * scale).round();
54 if (frame.frame.width != expectedWidth ||
55 frame.frame.height != expectedHeight) {
56 throw StateError(
57 'asked for a frame scaled to $scale and got '
58 '${frame.frame.width}x${frame.frame.height}',
59 );
60 }
61 }
62}