flutter3d
Showcase Changelog 62 packages API reference

Light channels: the source

since 0.7.0 Shading and materials

Regions the guide quotes: channels (line 35), live (line 76), check (line 94).

1/// `LightNode.channels` against `SceneNode.lightChannels`: a light reaches an
2/// object only when the two bit masks share a bit.
3///
4/// Quoted by `light_channels.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9
10/// The `light-channels` page: a red light on channel 1 and two balls, only
11/// the far one on that channel at first. Its check is that the red light
12/// reaches the ball it lights and is restricted to a channel.
13final class LightChannelsDemo extends ShowcaseDemo {
14 /// Whether the near ball opens channel 1 as well, so the red light reaches
15 /// it too.
16 bool sameChannel = false;
18 late final LightNode _redLight;
19 late final MeshNode _redBall;
21 @override
22 void configureView(DemoContext context) {
23 context.orbit
24 ..distance = 6.0
25 ..pitch = 0.15;
26 }
28 @override
29 Scene build(DemoContext context) {
30 final DeviceMesh sphere = DeviceMesh.upload(
31 context.device,
32 SphereShape(segments: 32, rings: 16).build(),
33 );
35 // Channel 0 for the near ball and its own light, channel 1 for the far
36 // one: the mask a light and a node meet on decides whether it reaches it,
37 // never distance or anything the shader has to guess about.
38 _redBall = MeshNode(
39 sphere,
40 RenderMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0)),
41 name: 'near ball',
42 )..lightChannels = LightChannels.only(0);
43 final MeshNode farBall =
44 MeshNode(
45 sphere,
46 RenderMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0)),
47 name: 'far ball',
48 )
49 ..setPosition(2.4, 0.0, 0.0)
50 ..lightChannels = LightChannels.only(1);
52 _redLight =
53 LightNode(
54 name: 'red',
55 type: LightType.point,
56 intensity: 6.0 * LightNode.legacyUnit,
57 )
58 ..color = LinearColor(1.0, 0.25, 0.2)
59 ..channels = LightChannels.only(0)
60 ..setPosition(0.0, 1.6, 1.2);
62 final LightNode fill = LightNode(
63 name: 'fill',
64 intensity: 1.2 * LightNode.legacyUnit,
65 )..setLocalForward(Vector3(-0.3, -0.6, -0.6));
67 return Scene()
68 ..add(_redBall)
69 ..add(farBall)
70 ..add(_redLight)
71 ..add(fill);
72 }
74 @override
75 void update(DemoContext context, double dt) {
76 // With the toggle on, the near ball opens its second bit too and the
77 // light starts reaching it exactly as it always reached the far one.
78 _redBall.lightChannels = sameChannel
79 ? (LightChannels.only(0) | LightChannels.only(1))
80 : LightChannels.only(0);
81 }
83 @override
84 List<DemoControl> controls(DemoContext context) => <DemoControl>[
85 ToggleControl(
86 'Near ball also on channel 1',
87 value: () => sameChannel,
88 onChanged: (bool v) => sameChannel = v,
89 ),
90 ];
92 @override
93 void verify(Scene scene, FrameResult frame) {
94 if (!_redLight.reaches(_redBall.lightChannels)) {
95 throw StateError('the red light does not reach the ball it lights');
96 }
97 if (_redLight.channels == LightChannels.all) {
98 throw StateError('the light was never restricted to a channel');
99 }
100 if (frame.drawCalls < 2) {
101 throw StateError('not every ball was drawn');
102 }
103 }