flutter3d
Showcase Changelog 62 packages API reference

Cardboard viewer profiles: the source

since 0.1.0 Simulation, audio and XR

Regions the guide quotes: viewer (line 44), apply (line 50), compare (line 73).

1/// The numbers a folded cardboard holder and a moulded one differ by, turned
2/// into an off-centre frustum per eye — wider away from the nose than
3/// towards it, which is what a lens actually looks through.
4///
5/// Quoted by `viewer_profiles.md` and shown whole in the Source tab.
6library;
7
8import 'package:flutter/widgets.dart';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d/flutter3d.dart' as f3d show RenderMaterial;
11import 'package:flutter3d_showcase/src/demo/demo.dart';
12import 'package:flutter3d_stereo/flutter3d_stereo.dart';
14/// The `viewer-profiles` page: a ball seen through a holder's per-eye
15/// frustums. Its check is that the older, narrower viewer shows less of the
16/// world than the newer one.
17final class ViewerProfilesDemo extends ShowcaseDemo {
18 /// Whether the rig uses the Cardboard v1 profile rather than v2.
19 bool useV1 = false;
21 final StereoRig _rig = StereoRig();
22 late Scene _scene;
24 @override
25 Scene build(DemoContext context) {
26 final material = f3d.RenderMaterial(
27 name: 'ball',
28 baseColor: LinearColor.fromSrgb(0.8, 0.5, 0.3, 1.0),
29 );
30 final ball = MeshNode(
31 DeviceMesh.upload(context.device, SphereShape(segments: 24).build()),
32 material,
33 )..setPosition(0, 0, -2);
34 _scene = Scene()
35 ..add(_rig.stage)
36 ..add(ball)
37 ..add(
38 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
39 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
40 );
41 return _scene;
42 }
44 StereoViewer get _viewer =>
45 useV1 ? StereoViewer.cardboardV1 : StereoViewer.cardboardV2;
46 static const _screen = StereoScreen(width: 0.13, height: 0.07);
48 @override
49 Widget? customBody(BuildContext buildContext, DemoContext context) {
50 _rig.applyViewer(_viewer, _screen);
51 return StereoSurface(
52 renderer: context.renderer,
53 scene: _scene,
54 rig: _rig,
55 settings: () => const RenderSettings().forStereo(),
56 onBeforeDraw: () {},
57 viewer: _viewer,
58 screen: _screen,
59 );
60 }
62 @override
63 List<DemoControl> controls(DemoContext context) => <DemoControl>[
64 ToggleControl(
65 'Cardboard v1 (narrower)',
66 value: () => useV1,
67 onChanged: (bool v) => useV1 = v,
68 ),
69 ];
71 @override
72 void verify(Scene scene, FrameResult frame) {
73 // V1's lenses show less of the world than v2's wider window.
74 final v1 = StereoViewer.cardboardV1.projectionFor(Eye.left, _screen);
75 final v2 = StereoViewer.cardboardV2.projectionFor(Eye.left, _screen);
76 final v1Width = v1.tanRight - v1.tanLeft;
77 final v2Width = v2.tanRight - v2.tanLeft;
78 if (v1Width >= v2Width) {
79 throw StateError(
80 'the older, narrower viewer should show less of the '
81 'world than the newer one, at the same screen',
82 );
83 }
84 }