flutter3d
Showcase Changelog 62 packages API reference

Draw order and depth state: the source

since 0.1.0 or earlier Shading and materials

Regions the guide quotes: box (line 49), panel (line 61), flag (line 80), live (line 109), culling (line 117).

1/// Draw order and depth state: which surface is drawn first, whether it writes
2/// depth, how it is tested, and whether the back of a triangle is drawn.
3///
4/// Quoted by `draw_state.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `draw-state` page: a panel, a box and a flag that show draw order,
13/// depth writes, the depth test and back-face culling. Its check is that the
14/// panel is ordered first, writes no depth, and every mesh is drawn.
15final class DrawStateDemo extends ShowcaseDemo {
16 /// The panel's draw bucket, -1 to 1: a lower bucket is drawn earlier.
17 int bucket = -1;
19 /// Whether the panel writes depth; off leaves the material's `depthWrite`
20 /// false.
21 bool panelWritesDepth = false;
23 /// The panel's depth test: 0 the default less, 1 always.
24 int testChoice = 0;
26 /// Whether back faces are culled; the flag shows its back when this is off.
27 bool cullBackFaces = true;
29 static const List<String> _tests = <String>['less', 'always'];
31 late final RenderMaterial _panel;
32 late final MeshNode _box;
34 @override
35 void configureView(DemoContext context) {
36 context.orbit
37 ..distance = 8.0
38 ..yaw = 0.5
39 ..pitch = 0.2;
40 }
42 @override
43 Scene build(DemoContext context) {
44 final Quaternion upright = Quaternion.axisAngle(
45 Vector3(1.0, 0.0, 0.0),
46 math.pi / 2,
47 );
49 _box = MeshNode(
50 DeviceMesh.upload(
51 context.device,
52 CuboidShape(size: Vector3(1.6, 1.6, 1.6)).build(),
53 ),
54 RenderMaterial(
55 name: 'box',
56 baseColor: LinearColor.fromSrgb(0.25, 0.45, 0.85, 1.0),
57 ),
58 name: 'box',
59 );
61 _panel = RenderMaterial(
62 name: 'panel',
63 baseColor: LinearColor.fromSrgb(0.95, 0.55, 0.2, 1.0),
64 drawBucket: bucket,
65 depthWrite: panelWritesDepth ? null : false,
66 depthCompare: testChoice == 0 ? null : CompareFunction.always,
67 );
68 final MeshNode panel =
69 MeshNode(
70 DeviceMesh.upload(
71 context.device,
72 const PlaneShape(width: 5, depth: 3).build(),
73 ),
74 _panel,
75 name: 'panel',
76 )
77 ..setRotation(upright)
78 ..setPosition(0.0, 0.0, 1.8);
80 final MeshNode flag =
81 MeshNode(
82 DeviceMesh.upload(
83 context.device,
84 const PlaneShape(width: 1.4, depth: 1.4).build(),
85 ),
86 RenderMaterial(
87 name: 'flag',
88 baseColor: LinearColor.fromSrgb(0.8, 0.15, 0.2, 1.0),
89 ),
90 name: 'flag',
91 )
92 ..setRotation(
93 Quaternion.axisAngle(Vector3(1.0, 0.0, 0.0), -math.pi / 2),
94 )
95 ..setPosition(-2.9, 0.0, -0.5);
97 return Scene()
98 ..add(_box)
99 ..add(panel)
100 ..add(flag)
101 ..add(
102 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
103 ..setLocalForward(Vector3(-0.3, -0.5, -1.0)),
104 );
105 }
107 @override
108 void update(DemoContext context, double dt) {
109 _panel
110 ..drawBucket = bucket
111 ..depthWrite = panelWritesDepth ? null : false
112 ..depthCompare = testChoice == 0 ? null : CompareFunction.always;
113 }
115 @override
116 RenderSettings settings(DemoContext context) => RenderSettings(
117 backfaceCulling: cullBackFaces,
118 );
120 @override
121 List<DemoControl> controls(DemoContext context) => <DemoControl>[
122 SliderControl(
123 'Panel draw bucket',
124 min: -1,
125 max: 1,
126 divisions: 2,
127 value: () => bucket.toDouble(),
128 onChanged: (double v) => bucket = v.round(),
129 format: (double v) => '${v.round()}',
130 ),
131 ToggleControl(
132 'Panel writes depth',
133 value: () => panelWritesDepth,
134 onChanged: (bool v) => panelWritesDepth = v,
135 ),
136 ChoiceControl(
137 'Panel depth test',
138 options: _tests,
139 index: () => testChoice,
140 onChanged: (int i) => testChoice = i,
141 ),
142 ToggleControl(
143 'Back-face culling',
144 value: () => cullBackFaces,
145 onChanged: (bool v) => cullBackFaces = v,
146 ),
147 ];
149 @override
150 void verify(Scene scene, FrameResult frame) {
151 if (_panel.drawBucket >= _box.material.drawBucket) {
152 throw StateError('the panel is not ordered before the box');
153 }
154 if (_panel.depthWrite != false) {
155 throw StateError('the panel still writes depth');
156 }
157 final int sceneDraws = frame.passes
158 .where((FramePass pass) => pass.name == 'scene')
159 .fold(0, (int sum, FramePass pass) => sum + pass.drawCalls);
160 if (sceneDraws != scene.meshes.length) {
161 throw StateError(
162 'the scene pass drew $sceneDraws of ${scene.meshes.length} meshes',
163 );
164 }
165 }