WebGL2: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: fallback (line 41), settings (line 54), read (line 58).
1/// The browser path: WebGL2 over a canvas the browser composites, the
2/// second implementation of `flutter3d_hardware`.
3///
4/// Quoted by `backend_webgl.md` and shown whole in the Source tab.
5library;
7import 'package:flutter/widgets.dart';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `backend-webgl` page: the lit ball drawn through WebGL2. Its check also
12/// holds the device to its word on wireframe: declined exactly when it does not
13/// support it.
14final class BackendWebglDemo extends ShowcaseDemo {
15 late final GraphicsDevice _device;
18 Scene build(DemoContext context) {
19 _device = context.device;
20 final RenderMaterial stone = RenderMaterial(
21 name: 'stone',
22 baseColor: LinearColor.fromSrgb(0.4, 0.55, 0.7, 1.0),
23 roughness: 0.6,
24 );
25 final MeshNode ball = MeshNode(
26 DeviceMesh.upload(
27 context.device,
28 SphereShape(segments: 24, rings: 12).build(),
29 ),
30 stone,
31 name: 'ball',
32 );
33 return Scene()
34 ..add(ball)
35 ..add(
36 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
37 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
38 );
39 }
41 // On the web, `flutter3d_app`'s `openDevice` registers WebGL2 always and
42 // WebGPU only when the build was compiled with
43 // `--dart-define=FLUTTER3D_WEBGPU=true`. Either way it asks the shared
44 // registry for a backend rather than choosing one by name:
45 //
46 // final registry = DeviceRegistry();
47 // registerWebGlBackend(registry);
48 // if (tryWebGpu) registry.addBackend(BackendId.webgpu, openWebGpu);
49 // return registry.open(width: width, height: height, ...);
50 //
51 // A build that never asked for WebGPU never carries its code at all; a
52 // canvas that only WebGL2 can open still draws.
55 RenderSettings settings(DemoContext context) =>
56 const RenderSettings(wireframe: true);
58 String _report(GraphicsDevice device) =>
59 'wireframe: ${device.features.has(DeviceFeature.wireframe)}\n'
60 'offscreen MSAA: ${device.features.has(DeviceFeature.offscreenMultisample)}\n'
61 'preferred samples: ${device.preferredSampleCount}\n'
62 'max anisotropy: ${device.limits.maxSamplerAnisotropy}';
65 Widget? customBody(BuildContext buildContext, DemoContext context) =>
66 Container(
67 color: const Color(0xFF14161A),
68 padding: const EdgeInsets.all(24),
69 alignment: Alignment.topLeft,
70 child: DefaultTextStyle(
71 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
72 child: Text(
73 'WebGL2 has no glPolygonMode, so it answers false to wireframe the '
74 'same way the software rasteriser does: drawing triangles as edges '
75 'needs line primitives this API does not expose. Anisotropic '
76 'filtering and multisampling both depend on extensions the browser '
77 'may or may not hand back.\n\n'
78 'What the device actually open right now answers:\n\n'
79 '${_report(context.device)}',
80 ),
81 ),
82 );
85 void verify(Scene scene, FrameResult frame) {
86 if (frame.drawCalls < 1) {
87 throw StateError('the ball was not drawn');
88 }
89 // Declined and supported at once, or neither, is the disagreement.
90 if (frame.wireframeDeclined ==
91 _device.features.has(DeviceFeature.wireframe)) {
92 throw StateError(
93 'wireframeDeclined disagreed with supportsWireframe for the open '
94 'device',
95 );
96 }
97 }
98}