Volumetric fog: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: torches (line 45), cells (line 75), fog (line 76).
1/// Volumetric fog: air with a thickness that the lights shine through, so
2/// each lamp glows in the air around it.
3///
4/// Quoted by `volumetric_fog.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `volumetric-fog` page: a grid of torches over a floor in air that
13/// glows around each of them. Its check is that the fog pass ran.
14final class VolumetricFogDemo extends ShowcaseDemo {
15 /// Whether the volumetric fog pass runs.
16 bool fog = true;
18 /// Whether the frame clusters its lights, which is how the fog finds the
19 /// torches; off, the air does not glow around them.
20 bool litByLamps = true;
22 /// The air's density per metre, 0 to 0.8.
23 double density = 0.3;
25 /// How fast the air thins with height, per metre, 0 to 3.
26 double heightFalloff = 1.0;
29 void configureView(DemoContext context) {
30 context.orbit
31 ..distance = 9.8
32 ..pitch = 1.15
33 ..yaw = 0.0;
34 }
36 /// A saturated colour at [t] of the way round the hue circle.
37 static Vector3 _hue(double t) {
38 double channel(double offset) =>
39 (0.5 + 0.5 * math.cos(2 * math.pi * (t + offset))).clamp(0.0, 1.0);
40 return Vector3(channel(0.0), channel(2 / 3), channel(1 / 3));
41 }
44 Scene build(DemoContext context) {
45 final Scene scene = Scene()
46 ..ambientIntensity = 0.03 * LightNode.legacyUnit
47 ..add(
48 MeshNode(
49 DeviceMesh.upload(
50 context.device,
51 CuboidShape(size: Vector3(12.0, 0.1, 12.0)).build(),
52 ),
53 RenderMaterial(name: 'floor', lighting: LightingModel.lambert),
54 name: 'floor',
55 )..setPosition(0.0, -0.05, 0.0),
56 );
57 for (var i = 0; i < 8; i++) {
58 for (var j = 0; j < 8; j++) {
59 scene.add(
60 LightNode(
61 name: 'torch $i $j',
62 type: LightType.point,
63 intensity: 1.0 * LightNode.legacyUnit,
64 range: 1.0,
65 color: _hue((i * 8 + j) / 64.0).toLinearColor(),
66 )..setPosition(i - 3.5, 0.3, j - 3.5),
67 );
68 }
69 }
70 return scene;
71 }
74 RenderSettings settings(DemoContext context) => RenderSettings(
75 clusteredLights: litByLamps,
76 volumetricFog: VolumetricFogSettings(
77 enabled: fog,
78 density: density,
79 heightFalloff: heightFalloff,
80 steps: 16,
81 distance: 20.0,
82 color: LinearColor.white,
83 ),
84 );
87 List<DemoControl> controls(DemoContext context) => <DemoControl>[
88 ToggleControl(
89 'Volumetric fog',
90 value: () => fog,
91 onChanged: (bool v) => fog = v,
92 ),
93 ToggleControl(
94 'Clustered lights',
95 value: () => litByLamps,
96 onChanged: (bool v) => litByLamps = v,
97 ),
98 SliderControl(
99 'Density',
100 min: 0,
101 max: 0.8,
102 value: () => density,
103 onChanged: (double v) => density = v,
104 format: (double v) => '${v.toStringAsFixed(2)} per m',
105 ),
106 SliderControl(
107 'Height falloff',
108 min: 0,
109 max: 3,
110 value: () => heightFalloff,
111 onChanged: (double v) => heightFalloff = v,
112 format: (double v) => '${v.toStringAsFixed(2)} per m',
113 ),
114 ];
117 void verify(Scene scene, FrameResult frame) {
118 final PassSkip? skipped = frame.skipReasonOf('volumetric fog');
119 if (skipped != null) {
120 throw StateError('the fog pass was skipped: $skipped');
121 }
122 if (!frame.passes.any((FramePass p) => p.name == 'volumetric fog')) {
123 throw StateError('the fog pass did not run');
124 }
125 }
126}