PBR metal and rough: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: material (line 22), mesh (line 30), light (line 39), live (line 49).
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';
10import 'package:vector_math/vector_math.dart';
12final class PbrLightingDemo extends ShowcaseDemo {
13 double metallic = 0.0;
14 double roughness = 0.4;
15 double sunIntensity = 3.0;
17 late final Material _ball;
18 late final LightNode _sun;
21 Scene build(DemoContext context) {
22 _ball = Material(
23 name: 'ball',
24 lighting: LightingModel.pbr,
25 baseColor: Vector4(0.9, 0.42, 0.28, 1.0),
26 metallic: metallic,
27 roughness: roughness,
28 );
30 final MeshNode ball = MeshNode(
31 DeviceMesh.upload(
32 context.device,
33 SphereShape(segments: 48, rings: 24).build(),
34 ),
35 _ball,
36 name: 'ball',
37 );
39 _sun = LightNode(name: 'sun', intensity: sunIntensity)
40 ..setLocalForward(Vector3(-0.4, -1.0, -0.3));
42 return Scene()
43 ..add(ball)
44 ..add(_sun);
45 }
48 void update(DemoContext context, double dt) {
49 _ball
50 ..metallic = metallic
51 ..roughness = roughness;
52 _sun.intensity = sunIntensity;
53 }
56 List<DemoControl> controls(DemoContext context) => <DemoControl>[
57 SliderControl(
58 'Metallic',
59 min: 0,
60 max: 1,
61 value: () => metallic,
62 onChanged: (double v) => metallic = v,
63 ),
64 SliderControl(
65 'Roughness',
66 min: 0.05,
67 max: 1,
68 value: () => roughness,
69 onChanged: (double v) => roughness = v,
70 ),
71 SliderControl(
72 'Sun',
73 min: 0,
74 max: 6,
75 value: () => sunIntensity,
76 onChanged: (double v) => sunIntensity = v,
77 ),
78 ];
81 void verify(Scene scene, FrameResult frame) {
82 if (frame.drawCalls < 1) {
83 throw StateError('the ball was not drawn');
84 }
85 }
86}