flutter3d
Showcase Changelog 62 packages API reference

Lights in lumens and lux: the source

since 0.7.0 Shading and materials

Regions the guide quotes: directional (line 47), point (line 52), exposure (line 65), live (line 80), check (line 104).

1/// `Photometric`: lux and lumens off a datasheet, converted into
2/// `LightNode.intensity` instead of tuned by eye.
3///
4/// Quoted by `photometric_units.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 `photometric-units` page: a ball under an overcast sky given in lux
11/// and a lamp given in lumens. Its check is that both intensities convert
12/// back to the numbers they came from.
13final class PhotometricUnitsDemo extends ShowcaseDemo {
14 /// The sky's illuminance in lux, 500 to 20000, used as its intensity as it
15 /// is.
16 double overcastLux = 10000.0;
18 /// The lamp's output in lumens, 100 to 1600, converted with
19 /// `Photometric.fromLumens`.
20 double lampLumens = 800.0;
22 late final LightNode _sky;
23 late final LightNode _lamp;
25 @override
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 5.0
29 ..pitch = 0.2;
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: 'ball',
44 ),
45 );
47 // A directional light has no falloff, so its intensity is what a surface
48 // facing it reads: lux is the unit its own datasheet would give.
49 _sky = LightNode(name: 'sky', intensity: overcastLux)
50 ..setLocalForward(Vector3(-0.3, -0.7, -0.5));
52 // An 800 lm bulb is the ordinary sixty-watt incandescent it replaced.
53 _lamp = LightNode(
54 name: 'lamp',
55 type: LightType.point,
56 intensity: Photometric.fromLumens(lampLumens),
57 range: 8.0,
58 )..setPosition(1.6, 1.2, 1.0);
60 return scene
61 ..add(_sky)
62 ..add(_lamp);
63 }
65 // Lux and candela are absolute, so the camera has to be set for them as a
66 // photographer would be: metered once for the overcast sky the page opens
67 // under, plus the scene's flat ambient (in the engine's pre-1.0 unit), and
68 // then left alone. About EV100 12, f/4 at roughly a two-hundred-and-fiftieth;
69 // the sliders then brighten and darken the picture, as the weather would.
70 static final PhysicalCamera _camera = PhysicalCamera.metered(
71 10000.0 + 0.06 * LightNode.legacyUnit,
72 );
74 @override
75 RenderSettings settings(DemoContext context) =>
76 RenderSettings(camera: _camera);
78 @override
79 void update(DemoContext context, double dt) {
80 _sky.intensity = overcastLux;
81 _lamp.intensity = Photometric.fromLumens(lampLumens);
82 }
84 @override
85 List<DemoControl> controls(DemoContext context) => <DemoControl>[
86 SliderControl(
87 'Sky (lux)',
88 min: 500,
89 max: 20000,
90 value: () => overcastLux,
91 onChanged: (double v) => overcastLux = v,
92 ),
93 SliderControl(
94 'Lamp (lumens)',
95 min: 100,
96 max: 1600,
97 value: () => lampLumens,
98 onChanged: (double v) => lampLumens = v,
99 ),
100 ];
102 @override
103 void verify(Scene scene, FrameResult frame) {
104 const double epsilon = 1e-6;
105 if ((_sky.intensity - overcastLux).abs() > epsilon) {
106 throw StateError('the sky intensity does not convert back to its lux');
107 }
108 if ((Photometric.toLumens(_lamp.intensity) - lampLumens).abs() > epsilon) {
109 throw StateError('the lamp intensity does not convert back to lumens');
110 }
111 if (frame.drawCalls < 1) {
112 throw StateError('the ball was not drawn');
113 }
114 }