Lightmaps: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: uv (line 38), bake (line 41), live (line 65).
1/// A baked lightmap: indirect light stored in a texture ahead of time, read
2/// through a second coordinate the vertex colour carries.
3///
4/// Quoted by `lightmaps.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';
12const int _lightmapSize = 4;
14/// The `lightmaps` page: a floor whose indirect light comes from a small
15/// baked texture. Its check is that the floor is marked lightmapped, its
16/// vertex colours carry lightmap coordinates, and the map has a bright corner.
17final class LightmapsDemo extends ShowcaseDemo {
18 /// Whether the floor reads its lightmap; off draws it with direct light
19 /// alone.
20 bool baked = true;
22 late final MeshData _floorMesh;
23 late final ByteData _lightmapPixels;
24 late final TextureHandle _lightmap;
25 late final RenderMaterial _floor;
28 void configureView(DemoContext context) {
29 context.orbit
30 ..distance = 4.0
31 ..pitch = 0.55
32 ..yaw = 0.0;
33 context.orbit.target.setValues(0.0, 0.0, 0.0);
34 }
37 Scene build(DemoContext context) {
38 _floorMesh = const PlaneShape(width: 3, depth: 3).build();
39 _useTexcoordAsLightmapUv(_floorMesh);
41 _lightmapPixels = _bakeBrightCorner();
42 _lightmap = context.device.createTextureFromPixels(
43 width: _lightmapSize,
44 height: _lightmapSize,
45 format: TextureFormat.r8g8b8a8UNormInt,
46 pixels: _lightmapPixels,
47 );
48 _floor = RenderMaterial(
49 name: 'floor',
50 baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.8, 1.0),
51 doubleSided: true,
52 )..lightmap = _lightmap;
54 final MeshNode floor = MeshNode(
55 DeviceMesh.upload(context.device, _floorMesh),
56 _floor,
57 name: 'floor',
58 )..lightmapped = true;
60 return Scene()
61 ..ambientIntensity = 0.0
62 ..add(floor);
63 }
66 void update(DemoContext context, double dt) {
67 _floor.lightmap = baked ? _lightmap : null;
68 }
71 List<DemoControl> controls(DemoContext context) => <DemoControl>[
72 ToggleControl(
73 'Lightmap on',
74 value: () => baked,
75 onChanged: (bool v) => baked = v,
76 ),
77 ];
80 void verify(Scene scene, FrameResult frame) {
81 final MeshNode floor = scene.meshes.first;
82 if (!floor.lightmapped) {
83 throw StateError('the floor was never marked lightmapped');
84 }
85 final VertexLayout layout = _floorMesh.layout;
86 final int stride = layout.floatsPerVertex;
87 final int uvAt = layout.floatOffsetOf(VertexLayout.texcoord.name);
88 final int colorAt = layout.floatOffsetOf(VertexLayout.color.name);
89 for (var v = 0; v < _floorMesh.vertexCount; v++) {
90 final int base = v * stride;
91 final bool matches =
92 _floorMesh.vertices[base + colorAt] ==
93 _floorMesh.vertices[base + uvAt] &&
94 _floorMesh.vertices[base + colorAt + 1] ==
95 _floorMesh.vertices[base + uvAt + 1];
96 if (!matches) {
97 throw StateError('vertex $v carries a tint, not its lightmap UV');
98 }
99 }
100 final int bright = _lightmapPixels.getUint8(0);
101 final int stride2 = _lightmapSize * 4;
102 final int lastRow = stride2 * (_lightmapSize - 1);
103 final int dark = _lightmapPixels.getUint8(
104 lastRow + (_lightmapSize - 1) * 4,
105 );
106 if (bright <= dark) {
107 throw StateError('the baked lightmap has no bright corner to read');
108 }
109 }
110}
112/// Copies each vertex's texture coordinate into its colour, which is the
113/// place `MeshNode.lightmapped` reads a lightmap at.
114void _useTexcoordAsLightmapUv(MeshData mesh) {
115 final VertexLayout layout = mesh.layout;
116 final int stride = layout.floatsPerVertex;
117 final int uvAt = layout.floatOffsetOf(VertexLayout.texcoord.name);
118 final int colorAt = layout.floatOffsetOf(VertexLayout.color.name);
119 for (var v = 0; v < mesh.vertexCount; v++) {
120 final int base = v * stride;
121 mesh.vertices[base + colorAt] = mesh.vertices[base + uvAt];
122 mesh.vertices[base + colorAt + 1] = mesh.vertices[base + uvAt + 1];
123 }
124}
126/// A small lightmap, RGBM-encoded: bright over the corner at UV (0, 0) and
127/// dim everywhere else, so the two ends of the bake read differently.
128ByteData _bakeBrightCorner() {
129 final ByteData pixels = ByteData(_lightmapSize * _lightmapSize * 4);
130 for (var y = 0; y < _lightmapSize; y++) {
131 for (var x = 0; x < _lightmapSize; x++) {
132 final bool corner = x == 0 && y == 0;
133 final (int, int, int) rgb = corner ? (64, 48, 16) : (2, 2, 3);
134 final int at = (y * _lightmapSize + x) * 4;
135 pixels
136 ..setUint8(at, rgb.$1)
137 ..setUint8(at + 1, rgb.$2)
138 ..setUint8(at + 2, rgb.$3)
139 ..setUint8(at + 3, 255);
140 }
141 }
142 return pixels;
143}