The six lighting models: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: models (line 33), row (line 43), light (line 55), live (line 65).
1/// The six lighting models the engine ships, one sphere each.
2///
3/// Quoted by `lighting_models.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `lighting-models` page: one sphere per lighting model the engine
10/// ships. Its check is one draw per sphere, each model a pipeline of its own.
11final class LightingModelsDemo extends ShowcaseDemo {
12 /// Every sphere's roughness, 0.05 to 1.
13 double roughness = 0.4;
15 /// The sun's intensity as a multiple of `LightNode.legacyUnit`, 0 to
16 /// 6.
17 double sunIntensity = 3.0;
19 late final List<RenderMaterial> _materials;
20 late final LightNode _sun;
23 void configureView(DemoContext context) {
24 context.orbit
25 ..distance = 5.6
26 ..pitch = 0.2
27 ..yaw = 0.0
28 ..apply();
29 }
32 Scene build(DemoContext context) {
33 _materials = <RenderMaterial>[
34 for (final LightingModel model in LightingModel.builtIn)
35 RenderMaterial(
36 name: model.label,
37 lighting: model,
38 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
39 roughness: roughness,
40 ),
41 ];
43 final DeviceMesh sphere = DeviceMesh.upload(
44 context.device,
45 const SphereShape(segments: 40, rings: 20).build(),
46 );
47 final Scene scene = Scene();
48 for (var i = 0; i < _materials.length; i++) {
49 scene.add(
50 MeshNode(sphere, _materials[i], name: _materials[i].name)
51 ..setPosition((i % 3 - 1) * 1.35, i < 3 ? 0.7 : -0.7, 0.0),
52 );
53 }
55 _sun = LightNode(
56 name: 'sun',
57 intensity: sunIntensity * LightNode.legacyUnit,
58 )..setLocalForward(Vector3(-0.4, -0.6, -0.7));
59 scene.add(_sun);
60 return scene;
61 }
64 void update(DemoContext context, double dt) {
65 for (final RenderMaterial material in _materials) {
66 material.roughness = roughness;
67 }
68 _sun.intensity = sunIntensity * LightNode.legacyUnit;
69 }
72 List<DemoControl> controls(DemoContext context) => <DemoControl>[
73 SliderControl(
74 'Roughness',
75 min: 0.05,
76 max: 1,
77 value: () => roughness,
78 onChanged: (double v) => roughness = v,
79 ),
80 SliderControl(
81 'Sun',
82 min: 0,
83 max: 6,
84 value: () => sunIntensity,
85 onChanged: (double v) => sunIntensity = v,
86 ),
87 ];
90 void verify(Scene scene, FrameResult frame) {
91 final int models = LightingModel.builtIn.length;
92 final FramePass drawn = frame.passes.firstWhere(
93 (FramePass pass) => pass.name == 'scene',
94 );
95 if (drawn.drawCalls != models) {
96 throw StateError('${drawn.drawCalls} draws for $models spheres');
97 }
98 if (drawn.pipelineSwitches != models) {
99 throw StateError(
100 'the $models models used ${drawn.pipelineSwitches} pipelines; '
101 'each model is a shader of its own',
102 );
103 }
104 }
105}