The scene surface and its status screens: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: scene (line 20), surface (line 39), failure (line 55).
1/// The widget every viewport in this app is built from: renders a scene
2/// once a frame and hands the result to Flutter through `presentFrame`, and
3/// the screen an application shows when a renderer never opened at all.
4///
5/// Quoted by `scene_surface.md` and shown whole in the Source tab.
6library;
8import 'package:flutter/widgets.dart';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d/flutter3d.dart' as f3d show RenderMaterial;
11import 'package:flutter3d_app/flutter3d_app.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `scene-surface` page: a ball drawn through `SceneSurface`, and the
15/// screen shown when no renderer opened. Its check is that the ball is drawn
16/// and the explanation is carried through as given.
17final class SceneSurfaceDemo extends ShowcaseDemo {
18 late Scene _scene;
21 Scene build(DemoContext context) {
22 final material = f3d.RenderMaterial(
23 name: 'ball',
24 baseColor: LinearColor.fromSrgb(0.6, 0.7, 0.9, 1.0),
25 );
26 final ball = MeshNode(
27 DeviceMesh.upload(context.device, SphereShape(segments: 24).build()),
28 material,
29 );
30 _scene = Scene()
31 ..add(ball)
32 ..add(
33 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
34 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
35 );
36 return _scene;
37 }
40 Widget? customBody(BuildContext buildContext, DemoContext context) =>
41 SceneSurface(
42 renderer: context.renderer,
43 scene: _scene,
44 view: context.view,
45 settings: () => const RenderSettings(),
46 onBeforeDraw: () {},
47 presentFrame: presentFrame,
48 );
51 void verify(Scene scene, FrameResult frame) {
52 if (frame.drawCalls < 1) {
53 throw StateError('the ball was not drawn');
54 }
55 // What an application shows instead, when the renderer never opened.
56 final failure = DidNotStart(
57 StateError('no shader bundle for this build'),
58 explaining: 'run tool/build_shaders.sh first',
59 );
60 if (failure.explaining != 'run tool/build_shaders.sh first') {
61 throw StateError('the explanation should be carried through as given');
62 }
63 }
64}