Ambient light: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: ambient (line 48), live (line 65), check (line 81).
1/// `Scene.ambientColor` and `Scene.ambientIntensity`: the flat term a surface
2/// falls back to wherever no direct light reaches it.
3///
4/// Quoted by `ambient_light.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
10/// The `ambient-light` page: a ball with one key light, its unlit side left
11/// to the ambient term. Its check is that the scene's ambient tracks the
12/// slider and that there is exactly one direct light.
13final class AmbientLightDemo extends ShowcaseDemo {
14 /// The scene's ambient intensity as a multiple of
15 /// `LightNode.legacyUnit`, 0 to 1.
16 double ambientIntensity = 0.35;
18 late final Scene _scene;
21 void configureView(DemoContext context) {
22 // Square to the key light's own direction, not merely off to one side:
23 // `yaw = 1.4` looked almost straight down the light's own beam, so the
24 // ball read as fully lit from every frame this page ever drew and the
25 // slider had no side left to brighten. `2.9` looks across the light
26 // instead, which is what puts the terminator in the middle of the
27 // frame — half the ball lit, half carrying nothing but the ambient term.
28 context.orbit
29 ..distance = 4.5
30 ..yaw = 2.9
31 ..pitch = 0.1;
32 }
35 Scene build(DemoContext context) {
36 final MeshNode ball = MeshNode(
37 DeviceMesh.upload(
38 context.device,
39 SphereShape(segments: 40, rings: 20).build(),
40 ),
41 RenderMaterial(
42 baseColor: LinearColor.fromSrgb(0.7, 0.7, 0.75, 1.0),
43 roughness: 0.6,
44 ),
45 name: 'ball',
46 );
48 // One light from one side only, so the far side of the ball is lit by
49 // nothing but the ambient term below.
50 _scene = Scene()
51 ..add(ball)
52 ..add(
53 LightNode(name: 'key', intensity: 3.0 * LightNode.legacyUnit)
54 ..setLocalForward(Vector3(-0.8, -0.2, -0.2)),
55 )
56 ..ambientColor = LinearColor(0.55, 0.65, 1.0)
57 // Lux; the slider is a share of the engine's old unit of light.
58 ..ambientIntensity = ambientIntensity * LightNode.legacyUnit;
60 return _scene;
61 }
64 void update(DemoContext context, double dt) {
65 _scene.ambientIntensity = ambientIntensity * LightNode.legacyUnit;
66 }
69 List<DemoControl> controls(DemoContext context) => <DemoControl>[
70 SliderControl(
71 'Ambient intensity',
72 min: 0,
73 max: 1,
74 value: () => ambientIntensity,
75 onChanged: (double v) => ambientIntensity = v,
76 ),
77 ];
80 void verify(Scene scene, FrameResult frame) {
81 if (scene.ambientIntensity != ambientIntensity * LightNode.legacyUnit) {
82 throw StateError('the scene ambient did not track the slider');
83 }
84 if (scene.lights.length != 1) {
85 throw StateError(
86 'this page needs exactly one direct light to leave a '
87 'side for the ambient term to fill',
88 );
89 }
90 if (frame.drawCalls < 1) {
91 throw StateError('the ball was not drawn');
92 }
93 }
94}