Image-based lighting: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: environments (line 48), balls (line 68), use (line 93).
1/// Light that comes from the surroundings: a cube map of the sky, blurred once
2/// per roughness, is all that lights these spheres.
3///
4/// Quoted by `image_based_lighting.md` and shown whole in the Source tab.
5library;
7import 'dart:typed_data';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `image-based-lighting` page: five spheres from roughness 0.05 to 1.0
13/// with no direct light at all, lit only by an environment cube. Its check is
14/// that the cube was built and bound.
15final class ImageBasedLightingDemo extends ShowcaseDemo {
16 /// Whether the environment cube lights the scene; off leaves the spheres
17 /// with nothing but the ambient term.
18 bool lit = true;
20 /// Which cube lights the spheres: 0 for the one made from the procedural
21 /// sky, 1 for the one made from the studio panorama.
22 int source = 0;
24 /// The scene's ambient intensity as a multiple of
25 /// `LightNode.legacyUnit`, 0 to 2 on the "Strength" slider.
26 double strength = 1.0;
28 /// How metallic every sphere is, 0 to 1; a metal shows the environment in
29 /// its reflection, a dielectric mostly in its diffuse colour.
30 double metallic = 1.0;
32 EnvironmentCube? _fromSky;
33 EnvironmentCube? _fromPanorama;
34 late final List<RenderMaterial> _balls;
35 late final Scene _scene;
38 void configureView(DemoContext context) {
39 context.orbit
40 ..distance = 7.5
41 ..pitch = 0.1
42 ..yaw = 0.0;
43 context.orbit.target.setValues(0.0, 0.0, 0.0);
44 }
47 Scene build(DemoContext context) {
48 final cubes = EnvironmentMap.isSupportedOn(context.device);
49 _fromSky = cubes
50 ? EnvironmentMap.fromSky(
51 context.device,
52 const SkySettings(
53 enabled: true,
54 glowStrength: 0.4,
55 sunIntensity: 2.0,
56 ),
57 )
58 : null;
59 _fromPanorama = cubes
60 ? EnvironmentMap.fromPanorama(
61 context.device,
62 _studioPanorama(),
63 width: 64,
64 height: 32,
65 )
66 : null;
68 final DeviceMesh sphere = DeviceMesh.upload(
69 context.device,
70 SphereShape(radius: 0.8, segments: 32, rings: 16).build(),
71 );
72 final Scene scene = _scene = Scene();
73 _balls = <RenderMaterial>[];
74 for (var i = 0; i < 5; i++) {
75 final RenderMaterial material = RenderMaterial(
76 name: 'ball $i',
77 lighting: LightingModel.pbr,
78 baseColor: LinearColor.fromSrgb(0.95, 0.75, 0.45, 1.0),
79 metallic: metallic,
80 roughness: 0.05 + i * 0.24,
81 );
82 _balls.add(material);
83 scene.add(
84 MeshNode(sphere, material, name: 'ball $i')
85 ..setPosition(i * 1.9 - 3.8, 0.0, 0.0),
86 );
87 }
88 return scene;
89 }
92 void update(DemoContext context, double dt) {
93 final EnvironmentCube? chosen = source == 0 ? _fromSky : _fromPanorama;
94 _scene
95 ..environment = lit ? chosen?.texture : null
96 ..environmentLevels = chosen?.levels ?? 0
97 ..ambientIntensity = strength * LightNode.legacyUnit;
98 for (final RenderMaterial ball in _balls) {
99 ball.metallic = metallic;
100 }
101 }
104 List<DemoControl> controls(DemoContext context) => <DemoControl>[
105 ToggleControl(
106 'Environment on',
107 value: () => lit,
108 onChanged: (bool v) => lit = v,
109 ),
110 ChoiceControl(
111 'Environment',
112 options: const <String>['Sky', 'Studio panorama'],
113 index: () => source,
114 onChanged: (int i) => source = i,
115 ),
116 SliderControl(
117 'Strength',
118 min: 0,
119 max: 2,
120 value: () => strength,
121 onChanged: (double v) => strength = v,
122 ),
123 SliderControl(
124 'Metallic',
125 min: 0,
126 max: 1,
127 value: () => metallic,
128 onChanged: (double v) => metallic = v,
129 ),
130 ];
133 void verify(Scene scene, FrameResult frame) {
134 if (scene.lights.isNotEmpty) {
135 throw StateError('this page has no direct light on purpose');
136 }
137 if (scene.environment == null || scene.environmentLevels < 1) {
138 throw StateError('the environment cube was not built or not bound');
139 }
140 }
141}
143// A 64 by 32 equirectangular picture: a cool sky, a warm floor and one bright
144// window to the side, so the two halves of the spheres differ.
145ByteData _studioPanorama() {
146 const int width = 64;
147 const int height = 32;
148 final ByteData pixels = ByteData(width * height * 4);
149 for (var y = 0; y < height; y++) {
150 for (var x = 0; x < width; x++) {
151 final bool sky = y < height ~/ 2;
152 final bool window = x > 40 && x < 52 && y > 8 && y < 16;
153 final (int, int, int) rgb = window
154 ? (255, 250, 235)
155 : sky
156 ? (70, 100, 150)
157 : (110, 80, 60);
158 final int at = (y * width + x) * 4;
159 pixels
160 ..setUint8(at, rgb.$1)
161 ..setUint8(at + 1, rgb.$2)
162 ..setUint8(at + 2, rgb.$3)
163 ..setUint8(at + 3, 255);
164 }
165 }
166 return pixels;
167}