flutter3d
Showcase Changelog 62 packages API reference

Mesh editing overlays: the source

since 0.7.0 Scene and geometry

Regions the guide quotes: contributor (line 30), surface (line 36), camera (line 60), batches (line 78), controls (line 140), check (line 159).

1/// A modeller-style overlay drawn against the scene depth buffer.
2library;
3
4import 'dart:math' as math;
5
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8
9/// The `mesh-overlay` page: an editable cube with a modeller's overlay drawn
10/// against the scene's depth. Its check is that the overlay batches reached
11/// the frame.
12final class MeshOverlayDemo extends ShowcaseDemo {
13 static const double _viewportHeight = 720;
15 late final MeshOverlay _overlay;
16 bool _vertices = true;
17 bool _face = true;
18 bool _through = true;
20 @override
21 void configureView(DemoContext context) {
22 context.orbit
23 ..distance = 6.2
24 ..pitch = 0.28
25 ..yaw = 0.62;
26 }
28 @override
29 Scene build(DemoContext context) {
30 _overlay = MeshOverlay(
31 vertexShader: context.renderer.shaders['DebugLineVertex']!,
32 fragmentShader: context.renderer.shaders['DebugLine']!,
33 );
34 context.renderer.renderSteps.addContributor(_overlay);
36 final MeshNode cube = MeshNode(
37 DeviceMesh.upload(
38 context.device,
39 CuboidShape(size: Vector3.all(2.0)).build(),
40 ),
41 RenderMaterial(
42 name: 'editable surface',
43 baseColor: LinearColor.fromSrgb(0.16, 0.32, 0.52, 1.0),
44 roughness: 0.58,
45 ),
46 name: 'editable cube',
47 );
48 final Scene scene = Scene()
49 ..ambientColor = LinearColor(0.45, 0.52, 0.68)
50 ..ambientIntensity = 0.16 * LightNode.legacyUnit
51 ..add(cube)
52 ..add(
53 LightNode(name: 'key', intensity: 3.6 * LightNode.legacyUnit)
54 ..setLocalForward(Vector3(-0.45, -0.8, -0.35)),
55 );
56 _rebuild(context);
57 return scene;
58 }
60 void _rebuild(DemoContext context) {
61 final Matrix4 cameraWorld = context.camera.sceneMatrix;
62 final values = cameraWorld.storage;
63 final Vector3 right = Vector3(values[0], values[1], values[2]);
64 final Vector3 up = Vector3(values[4], values[5], values[6]);
65 final double fieldOfView =
66 context.camera.projection.verticalFieldOfView ?? math.pi / 4;
67 _overlay
68 ..clear()
69 ..lookFrom(
70 eye: context.camera.readScenePosition(),
71 right: right,
72 up: up,
73 pixel: 2.0 * math.tan(fieldOfView * 0.5) / _viewportHeight,
74 );
75 _writeGeometry();
76 }
78 void _writeGeometry() {
79 final List<Vector3> corners = <Vector3>[
80 for (final double z in <double>[-1, 1])
81 for (final double y in <double>[-1, 1])
82 for (final double x in <double>[-1, 1]) Vector3(x, y, z),
83 ];
84 final LinearColor edgeColour = LinearColor.fromSrgb(0.2, 0.82, 1.0);
85 for (final (int a, int b) in <(int, int)>[
86 (0, 1),
87 (0, 2),
88 (1, 3),
89 (2, 3),
90 (4, 5),
91 (4, 6),
92 (5, 7),
93 (6, 7),
94 (0, 4),
95 (1, 5),
96 (2, 6),
97 (3, 7),
98 ]) {
99 _overlay.edge(corners[a], corners[b], edgeColour);
100 }
101 if (_vertices) {
102 for (final Vector3 corner in corners) {
103 _overlay.point(corner, LinearColor.fromSrgb(1.0, 0.72, 0.12), size: 11);
104 }
105 }
106 if (_face) {
107 final LinearColor selected = LinearColor.fromSrgb(0.2, 1.0, 0.45);
108 _overlay
109 ..wash(corners[4], corners[5], corners[7], selected)
110 ..wash(corners[4], corners[7], corners[6], selected)
111 ..ribbon(corners[4], corners[7], selected, width: 5);
112 }
113 if (_through) {
114 _overlay.throughGeometry(() {
115 _overlay
116 ..edge(
117 Vector3(-1.6, 0.0, 0.0),
118 Vector3(1.6, 0.0, 0.0),
119 LinearColor.fromSrgb(0.78, 0.42, 1.0),
120 )
121 ..ribbon(
122 Vector3(0.0, -1.6, 0.0),
123 Vector3(0.0, 1.6, 0.0),
124 LinearColor.fromSrgb(0.78, 0.42, 1.0),
125 width: 7,
126 )
127 ..point(
128 Vector3(0.0, 1.6, 0.0),
129 LinearColor.fromSrgb(0.78, 0.42, 1.0),
130 );
131 });
132 }
133 }
135 @override
136 void update(DemoContext context, double dt) => _rebuild(context);
138 @override
139 List<DemoControl> controls(DemoContext context) => <DemoControl>[
140 ToggleControl(
141 'Vertex handles',
142 value: () => _vertices,
143 onChanged: (bool value) => _vertices = value,
144 ),
145 ToggleControl(
146 'Selected face',
147 value: () => _face,
148 onChanged: (bool value) => _face = value,
149 ),
150 ToggleControl(
151 'Through-mesh gizmo',
152 value: () => _through,
153 onChanged: (bool value) => _through = value,
154 ),
155 ];
157 @override
158 void verify(Scene scene, FrameResult frame) {
159 if (_overlay.lines.vertexCount != 24 ||
160 _overlay.handles.vertexCount != 54 ||
161 _overlay.fill.vertexCount != 6 ||
162 _overlay.linesThrough.isEmpty ||
163 _overlay.handlesThrough.isEmpty ||
164 frame.drawCalls < 6) {
165 throw StateError('the mesh overlay batches did not reach the frame');
166 }
167 }