Physical sky: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: air (line 29), sun (line 87), sunlight (line 94), sky (line 103), fog (line 110), check (line 150).
1/// A physical sky: sunlight scattered by the air, so the colours come from
2/// where the sun is, and a fog that lies on the ground and thins with height.
3///
4/// Quoted by `physical_sky.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 `physical-sky` page: a stone tower and houses on open ground under a
13/// scattering sky, with height fog. Its check is that the sky is one extra
14/// draw and that a sun twenty degrees up comes out warm.
15final class PhysicalSkyDemo extends ShowcaseDemo {
16 /// The sun's elevation in degrees, -15 to 85; the sky's colours and the sun
17 /// light's both follow it.
18 double sunHeight = 20.0;
20 /// The fog's density per metre at the ground, 0 to 0.1.
21 double fogDensity = 0.03;
23 /// How fast the fog thins with height, per metre, 0 to 0.3; starts at
24 /// `FogSettings.defaultHeightFalloff`.
25 double heightFalloff = FogSettings.defaultHeightFalloff;
27 late final LightNode _sun;
29 // The Earth's air, as the defaults have it. Every colour below is read off
30 // this one value: the sky, the sun's light and the fog.
31 static const PhysicalSky _air = PhysicalSky();
34 void configureView(DemoContext context) {
35 context.orbit
36 ..distance = 14.0
37 ..pitch = 0.05
38 ..yaw = 0.0;
39 context.orbit.target.setValues(0.0, 3.0, -12.0);
40 }
42 /// The way to the sun at [sunHeight] degrees, in front of the camera.
43 Vector3 get _towardsSun {
44 final double up = sunHeight * math.pi / 180.0;
45 return Vector3(0.35 * math.cos(up), math.sin(up), -math.cos(up));
46 }
49 Scene build(DemoContext context) {
50 final RenderMaterial stone = RenderMaterial(
51 name: 'stone',
52 baseColor: LinearColor.fromSrgb(0.6, 0.57, 0.52, 1.0),
53 roughness: 0.9,
54 );
55 final DeviceMesh house = DeviceMesh.upload(
56 context.device,
57 CuboidShape(size: Vector3(2.0, 2.0, 2.0)).build(),
58 );
59 final Scene scene = Scene()
60 ..add(
61 MeshNode(
62 DeviceMesh.upload(
63 context.device,
64 const PlaneShape(width: 200, depth: 200).build(),
65 ),
66 stone.copy()..doubleSided = true,
67 name: 'ground',
68 ),
69 )
70 ..add(
71 MeshNode(
72 DeviceMesh.upload(
73 context.device,
74 CuboidShape(size: Vector3(2.0, 16.0, 2.0)).build(),
75 ),
76 stone,
77 name: 'tower',
78 )..setPosition(4.0, 8.0, -40.0),
79 );
80 // A street of low houses running away from the eye, into the fog.
81 for (var i = 0; i < 10; i++) {
82 scene.add(
83 MeshNode(house, stone, name: 'house $i')
84 ..setPosition(i.isEven ? -4.0 : 2.0, 1.0, -6.0 - i * 6.0),
85 );
86 }
87 _sun = LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit);
88 scene.add(_sun);
89 return scene;
90 }
93 void update(DemoContext context, double dt) {
94 // The light that stands for the sun takes the colour the air leaves of
95 // it: white at noon, orange low down, nothing once it has set.
96 _sun
97 ..setLocalForward(-_towardsSun)
98 ..color = _air.sunlight(_towardsSun).toLinearColor();
99 }
102 RenderSettings settings(DemoContext context) {
103 final SkySettings sky = SkySettings(
104 enabled: true,
105 physical: _air,
106 directionToSun: _towardsSun,
107 );
108 return RenderSettings(
109 sky: sky,
110 fog: FogSettings(
111 // The sky along the horizon ahead, so the far end of the street
112 // melts into it at any hour.
113 color: sky.sample(Vector3(0.0, 0.02, -1.0)).toLinearColor(),
114 density: fogDensity,
115 heightFalloff: heightFalloff,
116 ),
117 );
118 }
121 List<DemoControl> controls(DemoContext context) => <DemoControl>[
122 SliderControl(
123 'Sun height',
124 min: -15,
125 max: 85,
126 value: () => sunHeight,
127 onChanged: (double v) => sunHeight = v,
128 format: (double v) => '${v.round()} deg',
129 ),
130 SliderControl(
131 'Fog density',
132 min: 0,
133 max: 0.1,
134 value: () => fogDensity,
135 onChanged: (double v) => fogDensity = v,
136 format: (double v) => '${v.toStringAsFixed(3)} per m',
137 ),
138 SliderControl(
139 'Height falloff',
140 min: 0,
141 max: 0.3,
142 value: () => heightFalloff,
143 onChanged: (double v) => heightFalloff = v,
144 format: (double v) => '${v.toStringAsFixed(2)} per m',
145 ),
146 ];
149 void verify(Scene scene, FrameResult frame) {
150 // The sky is one full-screen draw in the scene pass, after the meshes.
151 final FramePass draw = frame.passes.firstWhere(
152 (FramePass p) => p.name == 'scene',
153 );
154 final int meshes = scene.meshes.where((MeshNode m) => m.isVisible).length;
155 if (draw.drawCalls != meshes + 1) {
156 throw StateError(
157 'expected $meshes mesh draws and one for the sky, '
158 'got ${draw.drawCalls}',
159 );
160 }
161 // Twenty degrees up, the sun has come through enough air to lose more
162 // blue than red, and the light standing for it says so.
163 final Vector3 light = _sun.color.toVector3();
164 if (!(light.x > light.z && light.z > 0.0)) {
165 throw StateError('a sun twenty degrees up should be warm: $light');
166 }
167 }
168}