flutter3d
Showcase Changelog 62 packages API reference

Distance fog: the source

since 0.5.1 Light and environment

Regions the guide quotes: sky (line 20), pillars (line 57), fog (line 69).

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;
5
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8
9/// The `distance-fog` page: a row of stone pillars running away from the
10/// eye into fog. Its check is that every mesh was still drawn, however far
11/// into the fog.
12final class DistanceFogDemo extends ShowcaseDemo {
13 /// The fog's density per metre, 0 to 0.2 on the "Density" slider.
14 double density = 0.03;
16 /// Whether the fog takes the sky's colour, so the far pillars dissolve into
17 /// it, rather than a colour of its own.
18 bool matchSky = true;
20 static const SkySettings _sky = SkySettings(enabled: true);
22 @override
23 void configureView(DemoContext context) {
24 context.orbit
25 ..distance = 9.0
26 ..pitch = 0.08
27 ..yaw = 0.0;
28 context.orbit.target.setValues(0.0, 1.2, -10.0);
29 }
31 @override
32 Scene build(DemoContext context) {
33 final RenderMaterial stone = RenderMaterial(
34 name: 'stone',
35 baseColor: LinearColor.fromSrgb(0.62, 0.58, 0.52, 1.0),
36 roughness: 0.9,
37 );
38 final DeviceMesh pillar = DeviceMesh.upload(
39 context.device,
40 CuboidShape(size: Vector3(1.2, 3.0, 1.2)).build(),
41 );
42 final Scene scene = Scene()
43 ..add(
44 MeshNode(
45 DeviceMesh.upload(
46 context.device,
47 const PlaneShape(width: 120, depth: 120).build(),
48 ),
49 stone.copy()..doubleSided = true,
50 name: 'floor',
51 ),
52 )
53 ..add(
54 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
55 ..setLocalForward(Vector3(-0.4, -0.8, -0.5)),
56 );
57 for (var i = 0; i < 12; i++) {
58 scene.add(
59 MeshNode(pillar, stone, name: 'pillar $i')
60 ..setPosition(i.isEven ? -3.0 : 3.0, 1.5, -i * 4.5),
61 );
62 }
63 return scene;
64 }
66 @override
67 RenderSettings settings(DemoContext context) => RenderSettings(
68 sky: _sky,
69 fog: FogSettings(
70 // Read off the sky at the horizon, so the far end melts into it.
71 color: matchSky
72 ? _sky.sample(Vector3(0.0, 0.0, -1.0)).toLinearColor()
73 : const LinearColor(0.6, 0.2, 0.2),
74 density: density,
75 ),
76 );
78 @override
79 List<DemoControl> controls(DemoContext context) => <DemoControl>[
80 SliderControl(
81 'Density',
82 min: 0,
83 max: 0.2,
84 value: () => density,
85 onChanged: (double v) => density = v,
86 format: (double v) => v.toStringAsFixed(3),
87 ),
88 ToggleControl(
89 'Fog takes the sky colour',
90 value: () => matchSky,
91 onChanged: (bool v) => matchSky = v,
92 ),
93 ];
95 @override
96 void verify(Scene scene, FrameResult frame) {
97 if (frame.drawCalls < scene.meshes.length) {
98 throw StateError('some of the pillars were not drawn');
99 }
100 }