Splats under a budget: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: cloud (line 38), tree (line 68), contributor (line 70), budget (line 80), slider (line 85), check (line 97).
1/// A generated cloud of Gaussians cut into a tree of levels of detail and
2/// drawn under a splat budget.
3///
4/// Quoted by `splat_budget.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
8import 'dart:typed_data';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `splat-budget` page: a generated Gaussian cloud cut into a tree of
14/// levels and drawn under a budget. Its check is that what is drawn is a cut
15/// of the tree within it.
16final class SplatBudgetDemo extends ShowcaseDemo {
17 /// Splats around the ring, and around the tube at each step of the ring.
18 static const int _around = 60;
19 static const int _tube = 40;
21 /// How many splats the cut may draw, from the root's count to every leaf's;
22 /// rounded into the `SplatLod`'s budget.
23 double budget = 300.0;
25 late final SplatOctree _tree;
26 late final SplatLod _lod;
29 void configureView(DemoContext context) {
30 context.orbit
31 ..distance = 5.0
32 ..pitch = 0.6
33 ..yaw = 0.3;
34 }
37 Scene build(DemoContext context) {
38 const int count = _around * _tube;
39 final Float32List centers = Float32List(count * 3);
40 final Float32List colors = Float32List(count * 4);
41 final Float32List scales = Float32List(count * 3);
42 final Float32List rotations = Float32List(count * 4);
43 for (var i = 0; i < count; i++) {
44 final double a = (i ~/ _tube) / _around * 2.0 * math.pi;
45 final double b = (i % _tube) / _tube * 2.0 * math.pi;
46 final double ring = 1.4 + 0.45 * math.cos(b);
47 centers.setAll(i * 3, <double>[
48 ring * math.cos(a),
49 0.45 * math.sin(b),
50 ring * math.sin(a),
51 ]);
52 colors.setAll(i * 4, <double>[
53 0.5 + 0.5 * math.cos(a),
54 0.5 + 0.5 * math.sin(b),
55 0.5 - 0.5 * math.cos(a),
56 0.9,
57 ]);
58 scales.setAll(i * 3, <double>[0.05, 0.05, 0.05]);
59 rotations[i * 4 + 3] = 1.0;
60 }
61 final SplatCloud cloud = SplatCloud(
62 centers: centers,
63 colors: colors,
64 scales: scales,
65 rotations: rotations,
66 );
68 _tree = buildSplatOctree(cloud, leafCapacity: 64, grid: 4);
70 _lod = SplatLod(_tree, budget: budget.round());
71 context.renderer.renderSteps.addContributor(SplatContributor.lod(_lod));
73 return Scene()
74 ..ambientColor = LinearColor(0.4, 0.46, 0.6)
75 ..ambientIntensity = 0.24 * LightNode.legacyUnit;
76 }
79 void update(DemoContext context, double dt) {
80 _lod.budget = budget.round();
81 }
84 List<DemoControl> controls(DemoContext context) => <DemoControl>[
85 SliderControl(
86 'Splat budget',
87 min: _tree.nodes.first.splatCount.toDouble(),
88 max: _tree.leafSplatCount.toDouble(),
89 value: () => budget,
90 onChanged: (double v) => budget = v,
91 format: (double v) => '${v.round()} of ${_tree.leafSplatCount}',
92 ),
93 ];
96 void verify(Scene scene, FrameResult frame) {
97 if (_tree.leafSplatCount != _around * _tube ||
98 _lod.cut.isEmpty ||
99 _lod.cutSplatCount > _lod.budget ||
100 _lod.cutSplatCount >= _tree.leafSplatCount ||
101 frame.drawCalls < 1) {
102 throw StateError('the tree was not drawn as a cut under its budget');
103 }
104 }
105}