flutter3d
Showcase Changelog 62 packages API reference

The held item's own pass: the source

since 0.4.0 Scene and geometry

Regions the guide quotes: world (line 30), hands (line 63), pass (line 91), motion (line 99), check (line 120).

1/// A held item drawn in its own pass, over the finished world.
2library;
3
4import 'dart:math' as math;
5
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8
9/// The `view-model` page: a held tool drawn in its own pass over a room of
10/// pillars. Its check is that the view model pass drew over the world.
11final class ViewModelDemo extends ShowcaseDemo {
12 /// The hand camera's vertical field of view in degrees, 25 to 90, set apart
13 /// from the world camera's.
14 double fovDegrees = 48.0;
15 double _bob = 0.0;
17 late final CameraNode _handCamera;
18 late final MeshNode _tool;
20 @override
21 void configureView(DemoContext context) {
22 context.orbit
23 ..distance = 6.0
24 ..pitch = 0.2
25 ..yaw = 0.4;
26 }
28 @override
29 Scene build(DemoContext context) {
30 final RenderMaterial stone = RenderMaterial(
31 name: 'stone',
32 baseColor: LinearColor.fromSrgb(0.58, 0.56, 0.52, 1.0),
33 roughness: 0.82,
34 );
35 final Scene world = Scene()
36 ..ambientColor = LinearColor(0.4, 0.46, 0.6)
37 ..ambientIntensity = 0.16 * LightNode.legacyUnit
38 ..add(
39 MeshNode(
40 DeviceMesh.upload(
41 context.device,
42 const PlaneShape(width: 20, depth: 20).build(),
43 ),
44 stone.copy()..doubleSided = true,
45 name: 'floor',
46 ),
47 )
48 ..add(
49 MeshNode(
50 DeviceMesh.upload(
51 context.device,
52 CuboidShape(size: Vector3(1.4, 2.4, 1.4)).build(),
53 ),
54 stone,
55 name: 'pillar',
56 )..setPosition(0.0, 1.2, -3.0),
57 )
58 ..add(
59 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
60 ..setLocalForward(Vector3(-0.4, -0.8, -0.3)),
61 );
63 final Scene handsScene = Scene()
64 ..add(
65 _tool = MeshNode(
66 DeviceMesh.upload(
67 context.device,
68 CuboidShape(size: Vector3(0.12, 0.12, 0.6)).build(),
69 ),
70 RenderMaterial(
71 name: 'tool',
72 baseColor: LinearColor.fromSrgb(0.66, 0.68, 0.72, 1.0),
73 metallic: 0.85,
74 roughness: 0.28,
75 ),
76 name: 'held tool',
77 )..setPosition(0.22, -0.2, -0.5),
78 )
79 ..add(
80 LightNode(name: 'hand key', intensity: 2.6 * LightNode.legacyUnit)
81 ..setLocalForward(Vector3(-0.3, -0.5, -0.8)),
82 );
83 _handCamera = CameraNode(
84 name: 'hand camera',
85 projection: PerspectiveProjection(
86 fieldOfView: fovDegrees * math.pi / 180.0,
87 ),
88 );
89 handsScene.add(_handCamera);
91 context.renderer.renderSteps.addNode(
92 ViewModelNode(scene: handsScene, camera: _handCamera),
93 );
94 return world;
95 }
97 @override
98 void update(DemoContext context, double dt) {
99 _bob += dt;
100 _tool.setPosition(0.22, -0.2 + math.sin(_bob * 2.4) * 0.02, -0.5);
101 _handCamera.projection = PerspectiveProjection(
102 fieldOfView: fovDegrees * math.pi / 180.0,
103 );
104 }
106 @override
107 List<DemoControl> controls(DemoContext context) => <DemoControl>[
108 SliderControl(
109 'Hand field of view',
110 min: 25,
111 max: 90,
112 value: () => fovDegrees,
113 onChanged: (double value) => fovDegrees = value,
114 format: (double value) => '${value.round()} deg',
115 ),
116 ];
118 @override
119 void verify(Scene scene, FrameResult frame) {
120 final bool viewModelRan = frame.passes.any(
121 (FramePass pass) => pass.name == 'view model',
122 );
123 if (!viewModelRan || frame.drawCalls < 2) {
124 throw StateError('the view model pass did not draw over the world');
125 }
126 }