The six lighting models: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: models (line 28), row (line 38), light (line 50), live (line 58).
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';
8import 'package:vector_math/vector_math.dart';
10final class LightingModelsDemo extends ShowcaseDemo {
11 double roughness = 0.4;
12 double sunIntensity = 3.0;
14 late final List<Material> _materials;
15 late final LightNode _sun;
18 void configureView(DemoContext context) {
19 context.orbit
20 ..distance = 5.6
21 ..pitch = 0.2
22 ..yaw = 0.0
23 ..apply();
24 }
27 Scene build(DemoContext context) {
28 _materials = <Material>[
29 for (final LightingModel model in LightingModel.builtIn)
30 Material(
31 name: model.label,
32 lighting: model,
33 baseColor: Vector4(0.85, 0.45, 0.3, 1.0),
34 roughness: roughness,
35 ),
36 ];
38 final DeviceMesh sphere = DeviceMesh.upload(
39 context.device,
40 const SphereShape(segments: 40, rings: 20).build(),
41 );
42 final Scene scene = Scene();
43 for (var i = 0; i < _materials.length; i++) {
44 scene.add(
45 MeshNode(sphere, _materials[i], name: _materials[i].name)
46 ..setPosition((i % 3 - 1) * 1.35, i < 3 ? 0.7 : -0.7, 0.0),
47 );
48 }
50 _sun = LightNode(name: 'sun', intensity: sunIntensity)
51 ..setLocalForward(Vector3(-0.4, -0.6, -0.7));
52 scene.add(_sun);
53 return scene;
54 }
57 void update(DemoContext context, double dt) {
58 for (final Material material in _materials) {
59 material.roughness = roughness;
60 }
61 _sun.intensity = sunIntensity;
62 }
65 List<DemoControl> controls(DemoContext context) => <DemoControl>[
66 SliderControl(
67 'Roughness',
68 min: 0.05,
69 max: 1,
70 value: () => roughness,
71 onChanged: (double v) => roughness = v,
72 ),
73 SliderControl(
74 'Sun',
75 min: 0,
76 max: 6,
77 value: () => sunIntensity,
78 onChanged: (double v) => sunIntensity = v,
79 ),
80 ];
83 void verify(Scene scene, FrameResult frame) {
84 final int models = LightingModel.builtIn.length;
85 final FramePass drawn = frame.passes.firstWhere(
86 (FramePass pass) => pass.name == 'scene',
87 );
88 if (drawn.drawCalls != models) {
89 throw StateError('${drawn.drawCalls} draws for $models spheres');
90 }
91 if (drawn.pipelineSwitches != models) {
92 throw StateError(
93 'the $models models used ${drawn.pipelineSwitches} pipelines; '
94 'each model is a shader of its own',
95 );
96 }
97 }
98}