Rendering in tiles: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: base (line 23), tile (line 104).
1/// Rendering a scene as a grid of tiles, one at a time, for a picture larger
2/// than any single render target. Every tile is drawn into its own cell of
3/// this page's frame, so the whole picture can be seen put back together.
4///
5/// Quoted by `tiled_render.md` and shown whole in the Source tab.
6library;
8import 'dart:math' as math;
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `tiled-render` page: a scene drawn as a grid of tiles, each into its
14/// own cell. Its check is that every tile drew and that one tile reproduces
15/// the whole frustum.
16final class TiledRenderDemo extends ShowcaseDemo {
17 /// How many tiles across, and down, the grid has, 1 to 4.
18 int tilesAcross = 3;
20 /// How far the cells are pulled apart, as a share of a cell, 0 to 0.4.
21 double gap = 0.0;
23 final Projection _base = const PerspectiveProjection(fieldOfView: 0.9);
25 static const int _largestGrid = 4;
27 /// One camera per tile, kept apart from the viewport's own: each carries a
28 /// different crop of the same view.
29 final List<CameraNode> _cameras = <CameraNode>[];
32 void configureView(DemoContext context) {
33 context.orbit
34 ..distance = 7.0
35 ..pitch = 0.3
36 ..yaw = 0.0;
37 }
40 Scene build(DemoContext context) {
41 final Scene scene = Scene()
42 ..ambientColor = LinearColor(0.5, 0.55, 0.65)
43 ..ambientIntensity = 0.25 * LightNode.legacyUnit
44 ..add(
45 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
46 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
47 );
49 // Enough that no tile is empty: a corner of the frame with nothing in it
50 // would say nothing about whether the pieces fit.
51 scene.add(
52 MeshNode(
53 DeviceMesh.upload(
54 context.device,
55 const PlaneShape(width: 16.0, depth: 10.0).build(),
56 ),
57 RenderMaterial(
58 name: 'floor',
59 baseColor: LinearColor.fromSrgb(0.4, 0.45, 0.42, 1.0),
60 ),
61 name: 'floor',
62 )..setPosition(0.0, -0.6, 0.0),
63 );
64 final DeviceMesh ball = DeviceMesh.upload(
65 context.device,
66 SphereShape(segments: 32, rings: 16).build(),
67 );
68 final DeviceMesh box = DeviceMesh.upload(
69 context.device,
70 CuboidShape(size: Vector3.all(1.2)).build(),
71 );
72 final DeviceMesh ring = DeviceMesh.upload(
73 context.device,
74 TorusShape(radius: 0.7, tubeRadius: 0.25).build(),
75 );
76 RenderMaterial color(String name, double r, double g, double b) =>
77 RenderMaterial(
78 name: name,
79 baseColor: LinearColor.fromSrgb(r, g, b, 1.0),
80 roughness: 0.6,
81 );
82 scene
83 ..add(
84 MeshNode(ball, color('ball', 0.9, 0.4, 0.3), name: 'ball')
85 ..setPosition(-2.6, 0.0, 0.0),
86 )
87 ..add(
88 MeshNode(box, color('box', 0.3, 0.6, 0.9), name: 'box')
89 ..setPosition(0.0, 0.0, 0.0),
90 )
91 ..add(
92 MeshNode(ring, color('ring', 0.4, 0.8, 0.5), name: 'ring')
93 ..setPosition(2.6, 0.0, 0.0),
94 );
96 for (var i = 0; i < _largestGrid * _largestGrid; i++) {
97 final CameraNode camera = CameraNode(name: 'tile $i');
98 _cameras.add(camera);
99 scene.add(camera);
100 }
101 return scene;
102 }
105 List<CameraView> views(DemoContext context) {
106 final int n = tilesAcross;
107 final double cell = 1.0 / n;
108 final double inset = cell * gap / 2;
109 // Cells that tile exactly leave a hairline wherever a cell edge falls
110 // between two pixels, which reads as the pieces not fitting. Reaching a
111 // hair over the neighbour's edge (its own tile is drawn after and takes
112 // the pixels back) closes it; pulled apart, there is nothing to close.
113 final double reach = gap == 0.0 ? 0.002 : 0.0;
114 final Vector3 eye = context.camera.readPosition();
115 final Quaternion facing = context.camera.readRotation();
116 return <CameraView>[
117 for (var row = 0; row < n; row++)
118 for (var column = 0; column < n; column++)
119 CameraView(
120 // The same eye every tile looks from: what differs is which
121 // square of the picture it keeps.
122 camera: _cameras[row * n + column]
123 ..setPositionFrom(eye)
124 ..setRotation(facing)
125 ..projection = TiledProjection(
126 _base,
127 tileX: column,
128 tileY: row,
129 tilesX: n,
130 tilesY: n,
131 ),
132 viewportFraction: ViewportRect(
133 column * cell + inset,
134 row * cell + inset,
135 math.min(cell - 2 * inset + reach, 1.0 - column * cell - inset),
136 math.min(cell - 2 * inset + reach, 1.0 - row * cell - inset),
137 ),
138 ),
139 ];
140 }
143 List<DemoControl> controls(DemoContext context) => <DemoControl>[
144 ChoiceControl(
145 'Grid',
146 options: const <String>['1x1', '2x2', '3x3', '4x4'],
147 index: () => tilesAcross - 1,
148 onChanged: (int i) => tilesAcross = i + 1,
149 ),
150 SliderControl(
151 'Pull apart',
152 min: 0,
153 max: 0.4,
154 value: () => gap,
155 onChanged: (double v) => gap = v,
156 format: (double v) => v.toStringAsFixed(2),
157 ),
158 ];
161 void verify(Scene scene, FrameResult frame) {
162 if (frame.drawCalls < tilesAcross * tilesAcross) {
163 throw StateError('every tile of the grid should have drawn something');
164 }
165 // A 1x1 grid has exactly one tile, and that tile is the whole frustum:
166 // the crop this projection applies must be the identity in that case,
167 // whatever grid is currently on screen.
168 const double aspect = 320 / 180;
169 final Matrix4 whole = _base.toMatrix(ProjectionRequest(aspect: aspect));
170 final Matrix4 singleTile = TiledProjection(
171 _base,
172 tileX: 0,
173 tileY: 0,
174 tilesX: 1,
175 tilesY: 1,
176 ).toMatrix(ProjectionRequest(aspect: aspect));
177 for (var i = 0; i < 16; i++) {
178 if ((whole.storage[i] - singleTile.storage[i]).abs() > 1e-9) {
179 throw StateError('a single tile did not reproduce the whole frustum');
180 }
181 }
182 }
183}