flutter3d
Showcase Changelog 38 packages API reference

Identical-draw batching: the source

since 0.7.0 Shading and materials

Regions the guide quotes: crates (line 26), settings (line 57), check (line 71).

1/// `RenderSettings.batchIdenticalDraws`: a run of nodes sharing a mesh and a
2/// material collapses into one instanced call.
3///
4/// Quoted by `draw_batching.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9import 'package:vector_math/vector_math.dart';
11final class DrawBatchingDemo extends ShowcaseDemo {
12 bool batch = true;
14 static const int _rows = 3;
15 static const int _columns = 4;
17 @override
18 void configureView(DemoContext context) {
19 context.orbit
20 ..distance = 9.0
21 ..pitch = 0.35;
22 }
24 @override
25 Scene build(DemoContext context) {
26 final DeviceMesh crateMesh = DeviceMesh.upload(
27 context.device,
28 CuboidShape(size: Vector3(0.8, 0.8, 0.8)).build(),
29 );
30 final Material crateMaterial = Material(
31 name: 'crate',
32 baseColor: Vector4(0.7, 0.55, 0.3, 1.0),
33 roughness: 0.7,
34 );
35 final Scene scene = Scene();
36 for (var row = 0; row < _rows; row++) {
37 for (var column = 0; column < _columns; column++) {
38 scene.add(
39 MeshNode(crateMesh, crateMaterial, name: 'crate $row-$column')
40 ..setPosition(
41 (column - (_columns - 1) / 2) * 1.1,
42 0.0,
43 (row - (_rows - 1) / 2) * 1.1,
44 ),
45 );
46 }
47 }
49 return scene..add(
50 LightNode(name: 'sun', intensity: 3.0)
51 ..setLocalForward(Vector3(-0.4, -0.8, -0.5)),
52 );
53 }
55 @override
56 RenderSettings settings(DemoContext context) => RenderSettings(
57 batchIdenticalDraws: batch,
58 );
60 @override
61 List<DemoControl> controls(DemoContext context) => <DemoControl>[
62 ToggleControl(
63 'Batch identical draws',
64 value: () => batch,
65 onChanged: (bool v) => batch = v,
66 ),
67 ];
69 @override
70 void verify(Scene scene, FrameResult frame) {
71 final int crates = _rows * _columns;
72 if (scene.meshes.length != crates) {
73 throw StateError('expected $crates crates, found ${scene.meshes.length}');
74 }
75 if (batch && frame.batchedDraws < RenderSettings.batchRunMinimum) {
76 throw StateError(
77 'batching was on but only ${frame.batchedDraws} draws were merged',
78 );
79 }
80 if (!batch && frame.batchedDraws != 0) {
81 throw StateError(
82 'batching was off but ${frame.batchedDraws} draws merged',
83 );
84 }
85 }