Distance fog: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: sky (line 14), pillars (line 51), fog (line 63).
1/// Distance fog: things farther from the eye fade toward one colour.
2///
3/// Quoted by `distance_fog.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8import 'package:vector_math/vector_math.dart';
10final class DistanceFogDemo extends ShowcaseDemo {
11 double density = 0.03;
12 bool matchSky = true;
14 static const SkySettings _sky = SkySettings(enabled: true);
17 void configureView(DemoContext context) {
18 context.orbit
19 ..distance = 9.0
20 ..pitch = 0.08
21 ..yaw = 0.0;
22 context.orbit.target.setValues(0.0, 1.2, -10.0);
23 }
26 Scene build(DemoContext context) {
27 final Material stone = Material(
28 name: 'stone',
29 baseColor: Vector4(0.62, 0.58, 0.52, 1.0),
30 roughness: 0.9,
31 );
32 final DeviceMesh pillar = DeviceMesh.upload(
33 context.device,
34 CuboidShape(size: Vector3(1.2, 3.0, 1.2)).build(),
35 );
36 final Scene scene = Scene()
37 ..add(
38 MeshNode(
39 DeviceMesh.upload(
40 context.device,
41 const PlaneShape(width: 120, depth: 120).build(),
42 ),
43 stone.copy()..doubleSided = true,
44 name: 'floor',
45 ),
46 )
47 ..add(
48 LightNode(name: 'sun', intensity: 3.0)
49 ..setLocalForward(Vector3(-0.4, -0.8, -0.5)),
50 );
51 for (var i = 0; i < 12; i++) {
52 scene.add(
53 MeshNode(pillar, stone, name: 'pillar $i')
54 ..setPosition(i.isEven ? -3.0 : 3.0, 1.5, -i * 4.5),
55 );
56 }
57 return scene;
58 }
61 RenderSettings settings(DemoContext context) => RenderSettings(
62 sky: _sky,
63 fog: FogSettings(
64 // Read off the sky at the horizon, so the far end melts into it.
65 color: matchSky
66 ? _sky.sample(Vector3(0.0, 0.0, -1.0))
67 : Vector3(0.6, 0.2, 0.2),
68 density: density,
69 ),
70 );
73 List<DemoControl> controls(DemoContext context) => <DemoControl>[
74 SliderControl(
75 'Density',
76 min: 0,
77 max: 0.2,
78 value: () => density,
79 onChanged: (double v) => density = v,
80 format: (double v) => v.toStringAsFixed(3),
81 ),
82 ToggleControl(
83 'Fog takes the sky colour',
84 value: () => matchSky,
85 onChanged: (bool v) => matchSky = v,
86 ),
87 ];
90 void verify(Scene scene, FrameResult frame) {
91 if (frame.drawCalls < scene.meshes.length) {
92 throw StateError('some of the pillars were not drawn');
93 }
94 }
95}