flutter3d
Showcase Changelog 62 packages API reference

Directional, point and spot lights: the source

since 0.2.0 Shading and materials

Regions the guide quotes: directional (line 61), point (line 67), spot (line 74), live (line 92), check (line 116).

1/// The three punctual light types: directional, point and spot, each with
2/// its own sphere so the difference is easy to read.
3///
4/// Quoted by `punctual_lights.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `punctual-lights` page: a sun, a point lamp and a spot torch, each
13/// over a ball of its own. Its check is that all three types are there and
14/// that the point range tracks its slider.
15final class PunctualLightsDemo extends ShowcaseDemo {
16 /// The point lamp's range in metres, 1 to 10.
17 double pointRange = 4.0;
19 /// The spot's outer cone angle in degrees, 5 to 60.
20 double spotOuterDegrees = 25.0;
22 late final LightNode _point;
23 late final LightNode _spot;
25 @override
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 7.0
29 ..pitch = 0.25;
30 }
32 @override
33 Scene build(DemoContext context) {
34 final DeviceMesh sphere = DeviceMesh.upload(
35 context.device,
36 SphereShape(segments: 32, rings: 16).build(),
37 );
38 final Scene scene = Scene()
39 ..add(
40 MeshNode(
41 sphere,
42 RenderMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0)),
43 name: 'sun ball',
44 )..setPosition(-2.2, 0.0, 0.0),
45 )
46 ..add(
47 MeshNode(
48 sphere,
49 RenderMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0)),
50 name: 'point ball',
51 )..setPosition(0.0, 0.0, 0.0),
52 )
53 ..add(
54 MeshNode(
55 sphere,
56 RenderMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0)),
57 name: 'spot ball',
58 )..setPosition(2.2, 0.0, 0.0),
59 );
61 final LightNode sun = LightNode(
62 name: 'sun',
63 type: LightType.directional,
64 intensity: 2.0 * LightNode.legacyUnit,
65 )..setLocalForward(Vector3(-0.3, -0.6, -0.7));
67 _point = LightNode(
68 name: 'lamp',
69 type: LightType.point,
70 intensity: 6.0 * LightNode.legacyUnit,
71 range: pointRange,
72 )..setPosition(0.0, 1.6, 1.2);
74 _spot = LightNode(
75 name: 'torch',
76 type: LightType.spot,
77 intensity: 10.0 * LightNode.legacyUnit,
78 range: 6.0,
79 innerConeAngle: 0.0,
80 outerConeAngle: spotOuterDegrees * math.pi / 180.0,
81 )..setPosition(2.2, 2.2, 1.6);
82 _spot.lookAt(Vector3(2.2, 0.0, 0.0));
84 return scene
85 ..add(sun)
86 ..add(_point)
87 ..add(_spot);
88 }
90 @override
91 void update(DemoContext context, double dt) {
92 _point.range = pointRange;
93 _spot.outerConeAngle = spotOuterDegrees * math.pi / 180.0;
94 }
96 @override
97 List<DemoControl> controls(DemoContext context) => <DemoControl>[
98 SliderControl(
99 'Point range',
100 min: 1,
101 max: 10,
102 value: () => pointRange,
103 onChanged: (double v) => pointRange = v,
104 ),
105 SliderControl(
106 'Spot cone (degrees)',
107 min: 5,
108 max: 60,
109 value: () => spotOuterDegrees,
110 onChanged: (double v) => spotOuterDegrees = v,
111 ),
112 ];
114 @override
115 void verify(Scene scene, FrameResult frame) {
116 final Set<LightType> types = <LightType>{
117 for (final LightNode light in scene.lights) light.type,
118 };
119 if (types.length != 3) {
120 throw StateError('expected all three punctual types, found $types');
121 }
122 if (_point.range != pointRange) {
123 throw StateError('the point light range did not track the slider');
124 }
125 if (frame.drawCalls < 3) {
126 throw StateError('not every ball was drawn');
127 }
128 }