flutter3d
Showcase Changelog 62 packages API reference

Light shafts: the source

since 0.7.0 Post-processing

Regions the guide quotes: doorway (line 43), sun (line 60), settings (line 85), ran (line 123).

1/// Light shafts: a view ray marched through the sun's own shadow map, so a
2/// beam through a doorway has the doorway's shape.
3///
4/// Quoted by `light_shafts.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9
10/// The `light-shafts` page: a doorway of two pillars and a lintel with the
11/// sun behind it. Its check is that the light shafts pass ran.
12final class LightShaftsDemo extends ShowcaseDemo {
13 // A density per metre since 0.7.4 — a dusty doorway rather than a clear
14 // day — and the phase that puts the glow towards the sun.
15 /// The air's density per metre, 0 to 0.1.
16 double strength = 0.03;
18 /// How much the air scatters light forward, -0.9 (back) to 0.9.
19 double anisotropy = 0.6;
21 /// How far along the view ray the march goes, in metres, 5 to 60.
22 double distance = 30.0;
24 @override
25 void configureView(DemoContext context) {
26 context.orbit
27 ..distance = 8.0
28 ..pitch = 0.15
29 ..yaw = 0.0;
30 context.orbit.target.setValues(0.0, 1.0, 2.0);
31 }
33 @override
34 Scene build(DemoContext context) {
35 final GraphicsDevice device = context.device;
36 final RenderMaterial stone = RenderMaterial(
37 name: 'stone',
38 baseColor: LinearColor.fromSrgb(0.35, 0.33, 0.3, 1.0),
39 roughness: 0.9,
40 );
41 final RenderMaterial floorStone = stone.copy()..doubleSided = true;
43 final DeviceMesh pillar = DeviceMesh.upload(
44 device,
45 CuboidShape(size: Vector3(1.2, 4.0, 1.2)).build(),
46 );
47 final MeshNode left = MeshNode(pillar, stone, name: 'left pillar')
48 ..setPosition(-1.6, 2.0, -2.0);
49 final MeshNode right = MeshNode(pillar, stone, name: 'right pillar')
50 ..setPosition(1.6, 2.0, -2.0);
51 final MeshNode lintel = MeshNode(
52 DeviceMesh.upload(
53 device,
54 CuboidShape(size: Vector3(4.4, 0.8, 1.2)).build(),
55 ),
56 stone,
57 name: 'lintel',
58 )..setPosition(0.0, 4.4, -2.0);
60 final LightNode sun = LightNode(
61 name: 'sun',
62 intensity: 6.0 * LightNode.legacyUnit,
63 castsShadow: true,
64 )..setLocalForward(Vector3(-0.3, -0.6, 0.7));
66 return Scene()
67 ..add(
68 MeshNode(
69 DeviceMesh.upload(
70 device,
71 const PlaneShape(width: 20, depth: 20).build(),
72 ),
73 floorStone,
74 name: 'floor',
75 ),
76 )
77 ..add(left)
78 ..add(right)
79 ..add(lintel)
80 ..add(sun);
81 }
83 @override
84 RenderSettings settings(DemoContext context) => RenderSettings(
85 lightShafts: LightShaftSettings(
86 enabled: true,
87 strength: strength,
88 anisotropy: anisotropy,
89 distance: distance,
90 ),
91 );
93 @override
94 List<DemoControl> controls(DemoContext context) => <DemoControl>[
95 SliderControl(
96 'Density',
97 min: 0,
98 max: 0.1,
99 value: () => strength,
100 onChanged: (double v) => strength = v,
101 format: (double v) => '${v.toStringAsFixed(3)} /m',
102 ),
103 SliderControl(
104 'Forward scattering',
105 min: -0.9,
106 max: 0.9,
107 value: () => anisotropy,
108 onChanged: (double v) => anisotropy = v,
109 format: (double v) => v.toStringAsFixed(2),
110 ),
111 SliderControl(
112 'Reach',
113 min: 5,
114 max: 60,
115 value: () => distance,
116 onChanged: (double v) => distance = v,
117 format: (double v) => '${v.round()} m',
118 ),
119 ];
121 @override
122 void verify(Scene scene, FrameResult frame) {
123 if (!frame.passes.any((FramePass p) => p.name == 'light shafts')) {
124 throw StateError('the light shafts pass did not run: ${frame.skipped}');
125 }
126 }