flutter3d
Showcase Changelog 62 packages API reference

Procedural sky: the source

since 0.2.0 Light and environment

Regions the guide quotes: sun (line 61), dome (line 63), sky (line 112).

1/// A sky that needs no image: a gradient, a glow and a sun disc, drawn by the
2/// renderer from a few numbers.
3///
4/// Quoted by `procedural_sky.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `procedural-sky` page: a ball on the ground under a sky drawn from
13/// numbers. Its check is that the sky costs one draw on top of the meshes.
14final class ProceduralSkyDemo extends ShowcaseDemo {
15 /// The sun's elevation in degrees, 2 to 85; moves the sun light and the
16 /// disc together.
17 double sunHeight = 14.0;
19 /// How bright the sun disc is drawn in the sky, 0 to 20; the sky's
20 /// `sunIntensity`, not the light's.
21 double sunDisc = 6.0;
23 /// How strong the glow around the sun is, 0 to 1.
24 double glow = 0.4;
26 /// Whether a painted dome mesh stands in for the sky; the procedural sky is
27 /// switched off while it shows.
28 bool dome = false;
30 late final LightNode _sun;
31 late final MeshNode _dome;
33 @override
34 void configureView(DemoContext context) {
35 context.orbit
36 ..distance = 7.0
37 ..pitch = 0.1
38 ..yaw = 0.9;
39 context.orbit.target.setValues(0.0, 0.8, 0.0);
40 }
42 // The way to the sun for the current height, at a fixed compass bearing.
43 Vector3 get _towardsSun {
44 final double up = sunHeight * math.pi / 180.0;
45 return Vector3(-math.cos(up) * 0.6, math.sin(up), -math.cos(up) * 0.8);
46 }
48 @override
49 Scene build(DemoContext context) {
50 final RenderMaterial ground = RenderMaterial(
51 name: 'ground',
52 baseColor: LinearColor.fromSrgb(0.42, 0.4, 0.36, 1.0),
53 roughness: 0.9,
54 doubleSided: true,
55 );
56 final RenderMaterial ball = RenderMaterial(
57 name: 'ball',
58 baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.82, 1.0),
59 roughness: 0.35,
60 );
61 _sun = LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit);
63 final MeshData shell = const SkyDome().build();
64 paintSky(
65 shell,
66 SkyGradient(
67 zenith: LinearColor(0.08, 0.2, 0.5),
68 horizon: LinearColor(0.5, 0.58, 0.68),
69 nadir: LinearColor(0.06, 0.06, 0.07),
70 ).color,
71 );
72 _dome = skyNode(DeviceMesh.upload(context.device, shell))
73 ..isVisible = false;
75 return Scene()
76 ..ambientIntensity = 0.4 * LightNode.legacyUnit
77 ..add(
78 MeshNode(
79 DeviceMesh.upload(
80 context.device,
81 const PlaneShape(width: 30, depth: 30).build(),
82 ),
83 ground,
84 name: 'ground',
85 ),
86 )
87 ..add(
88 MeshNode(
89 DeviceMesh.upload(
90 context.device,
91 SphereShape(radius: 0.8, segments: 32, rings: 16).build(),
92 ),
93 ball,
94 name: 'ball',
95 )..setPosition(0.0, 0.8, 0.0),
96 )
97 ..add(_sun)
98 ..add(_dome);
99 }
101 @override
102 void update(DemoContext context, double dt) {
103 _sun.setLocalForward(-_towardsSun);
104 _dome.isVisible = dome;
105 // The dome is small and has to follow the eye, or it is a ball on the
106 // ground.
107 followCamera(_dome, context.camera);
108 }
110 @override
111 RenderSettings settings(DemoContext context) => RenderSettings(
112 sky: SkySettings(
113 enabled: !dome,
114 directionToSun: _towardsSun,
115 sunIntensity: sunDisc,
116 glowStrength: glow,
117 ),
118 );
120 @override
121 List<DemoControl> controls(DemoContext context) => <DemoControl>[
122 SliderControl(
123 'Sun height',
124 min: 2,
125 max: 85,
126 value: () => sunHeight,
127 onChanged: (double v) => sunHeight = v,
128 format: (double v) => '${v.round()} deg',
129 ),
130 SliderControl(
131 'Sun disc',
132 min: 0,
133 max: 20,
134 value: () => sunDisc,
135 onChanged: (double v) => sunDisc = v,
136 ),
137 SliderControl(
138 'Glow',
139 min: 0,
140 max: 1,
141 value: () => glow,
142 onChanged: (double v) => glow = v,
143 ),
144 ToggleControl(
145 'Painted dome instead',
146 value: () => dome,
147 onChanged: (bool v) => dome = v,
148 ),
149 ];
151 @override
152 void verify(Scene scene, FrameResult frame) {
153 final FramePass draw = frame.passes.firstWhere(
154 (FramePass p) => p.name == 'scene',
155 );
156 final int meshes = scene.meshes.where((MeshNode m) => m.isVisible).length;
157 // The sky is one full-screen triangle inside the scene pass, so it is the
158 // draw that comes on top of the meshes.
159 if (draw.drawCalls != meshes + 1) {
160 throw StateError(
161 'expected $meshes mesh draws and one for the sky, '
162 'got ${draw.drawCalls}',
163 );
164 }
165 }