PBR metal and rough: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: material (line 29), mesh (line 37), light (line 46), live (line 58).
1/// A physically based material: how metallic it is, how rough, and a light.
2///
3/// This page is quoted by its guide, `pbr_lighting.md`, region by region, and
4/// shown whole in the Source tab. Everything outside the regions is the small
5/// amount the host needs to run it.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `pbr-lighting` page: one ball and a sun. Its check is that the ball is
12/// drawn.
13final class PbrLightingDemo extends ShowcaseDemo {
14 /// How metallic the ball is, 0 to 1.
15 double metallic = 0.0;
17 /// The ball's roughness, 0.05 to 1.
18 double roughness = 0.4;
20 /// The sun's intensity as a multiple of `LightNode.legacyUnit`, 0 to
21 /// 6.
22 double sunIntensity = 3.0;
24 late final RenderMaterial _ball;
25 late final LightNode _sun;
28 Scene build(DemoContext context) {
29 _ball = RenderMaterial(
30 name: 'ball',
31 lighting: LightingModel.pbr,
32 baseColor: LinearColor.fromSrgb(0.9, 0.42, 0.28, 1.0),
33 metallic: metallic,
34 roughness: roughness,
35 );
37 final MeshNode ball = MeshNode(
38 DeviceMesh.upload(
39 context.device,
40 SphereShape(segments: 48, rings: 24).build(),
41 ),
42 _ball,
43 name: 'ball',
44 );
46 _sun = LightNode(
47 name: 'sun',
48 intensity: sunIntensity * LightNode.legacyUnit,
49 )..setLocalForward(Vector3(-0.4, -1.0, -0.3));
51 return Scene()
52 ..add(ball)
53 ..add(_sun);
54 }
57 void update(DemoContext context, double dt) {
58 _ball
59 ..metallic = metallic
60 ..roughness = roughness;
61 _sun.intensity = sunIntensity * LightNode.legacyUnit;
62 }
65 List<DemoControl> controls(DemoContext context) => <DemoControl>[
66 SliderControl(
67 'Metallic',
68 min: 0,
69 max: 1,
70 value: () => metallic,
71 onChanged: (double v) => metallic = v,
72 ),
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 if (frame.drawCalls < 1) {
92 throw StateError('the ball was not drawn');
93 }
94 }
95}