flutter3d
Showcase Changelog 62 packages API reference

A lesson through the rig: the source

since 0.7.0 Simulation, audio and XR

Regions the guide quotes: steps (line 22), view (line 68), apply (line 80).

1/// The same lesson document a step panel authors, played back through a
2/// stereo rig with a Previous and a Next button instead of a keyboard.
3///
4/// Quoted by `stereo_lesson.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter/widgets.dart';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d/flutter3d.dart' as f3d show RenderMaterial;
10import 'package:flutter3d_showcase/src/demo/demo.dart';
11import 'package:flutter3d_sim/flutter3d_sim.dart';
12import 'package:flutter3d_stereo/flutter3d_stereo.dart';
14/// The `stereo-lesson` page: a lesson played through a stereo rig with
15/// Previous and Next. Its check is that the detail node is hidden on the
16/// first step and shown on the second.
17final class StereoLessonDemo extends ShowcaseDemo {
18 final StereoRig _rig = StereoRig();
19 late Scene _scene;
20 late MeshNode _detail;
22 late final LessonPlayer _player = LessonPlayer(<EntityDef>[
23 EntityDef(
24 type: 'edu_step',
25 position: WorldPosition(0, 0, 0),
26 properties: const <String, Object?>{
27 'hidden': <String>['detail'],
28 },
29 ),
30 EntityDef(
31 type: 'edu_step',
32 position: WorldPosition(0.5, 0, 0),
33 properties: const <String, Object?>{
34 'visible': <String>['detail'],
35 },
36 ),
37 ]);
39 @override
40 Scene build(DemoContext context) {
41 final material = f3d.RenderMaterial(
42 name: 'ball',
43 baseColor: LinearColor.fromSrgb(0.8, 0.5, 0.3, 1.0),
44 );
45 final ball = MeshNode(
46 DeviceMesh.upload(context.device, SphereShape(segments: 24).build()),
47 material,
48 )..setPosition(0, 0, -2);
49 _detail = MeshNode(
50 DeviceMesh.upload(context.device, SphereShape(segments: 12).build()),
51 f3d.RenderMaterial(
52 name: 'detail',
53 baseColor: LinearColor.fromSrgb(0.9, 0.9, 0.2, 1.0),
54 ),
55 name: 'detail',
56 )..setPosition(0.4, 0, -2);
57 _scene = Scene()
58 ..add(_rig.stage)
59 ..add(ball)
60 ..add(_detail)
61 ..add(
62 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
63 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
64 );
65 return _scene;
66 }
68 @override
69 Widget? customBody(BuildContext buildContext, DemoContext context) =>
70 LessonStereoView(
71 renderer: context.renderer,
72 scene: _scene,
73 rig: _rig,
74 player: _player,
75 nodes: <String, SceneNode>{'detail': _detail},
76 );
78 @override
79 void verify(Scene scene, FrameResult frame) {
80 // The first step hides the detail node; stepping forward shows it.
81 _player.applyCurrent(_rig, nodes: <String, SceneNode>{'detail': _detail});
82 final hiddenAtStart = !_detail.isVisible;
83 _player.next();
84 _player.applyCurrent(_rig, nodes: <String, SceneNode>{'detail': _detail});
85 final shownAtStepTwo = _detail.isVisible;
86 if (!hiddenAtStart || !shownAtStepTwo) {
87 throw StateError(
88 'the detail node should be hidden on the first step '
89 'and shown on the second',
90 );
91 }
92 }