flutter3d
Showcase Changelog 62 packages API reference

Rough metals and rough clay: the source

since 0.8.0 Shading and materials

Regions the guide quotes: metals (line 40), clay (line 56), light (line 71), settings (line 79).

1/// Gold from polished to rough above, grey clay from half rough to fully rough
2/// below: the two frame settings that keep rough surfaces from losing light.
3///
4/// Quoted by `rough_surfaces.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 `rough-surfaces` page: gold spheres from polished to rough and clay
11/// ones from half rough to rough. Its check is that every sphere is drawn.
12final class RoughSurfacesDemo extends ShowcaseDemo {
13 /// Whether the frame compensates rough metal for the energy a single bounce
14 /// loses.
15 bool compensate = true;
17 /// Whether diffuse uses the energy-preserving model, `DiffuseModel.eon`,
18 /// rather than Lambert.
19 bool orenNayar = true;
21 static const List<double> _metalRoughness = <double>[0.1, 0.3, 0.5, 0.7, 0.9];
22 static const List<double> _clayRoughness = <double>[0.5, 0.75, 1.0];
24 @override
25 void configureView(DemoContext context) {
26 context.orbit
27 ..distance = 7.0
28 ..yaw = 0.0
29 ..pitch = 0.05;
30 }
32 @override
33 Scene build(DemoContext context) {
34 final DeviceMesh ball = DeviceMesh.upload(
35 context.device,
36 SphereShape(radius: 0.5, segments: 48, rings: 24).build(),
37 );
38 final Scene scene = Scene()..ambientIntensity = 0.1 * LightNode.legacyUnit;
40 for (var i = 0; i < _metalRoughness.length; i++) {
41 scene.add(
42 MeshNode(
43 ball,
44 RenderMaterial(
45 name: 'gold ${_metalRoughness[i]}',
46 lighting: LightingModel.pbr,
47 baseColor: LinearColor.fromSrgb(1.0, 0.78, 0.34, 1.0),
48 metallic: 1.0,
49 roughness: _metalRoughness[i],
50 ),
51 name: 'gold',
52 )..setPosition((i - 2) * 1.15, 0.65, 0.0),
53 );
54 }
56 for (var i = 0; i < _clayRoughness.length; i++) {
57 scene.add(
58 MeshNode(
59 ball,
60 RenderMaterial(
61 name: 'clay ${_clayRoughness[i]}',
62 lighting: LightingModel.pbr,
63 baseColor: LinearColor.fromSrgb(0.42, 0.42, 0.42, 1.0),
64 roughness: _clayRoughness[i],
65 ),
66 name: 'clay',
67 )..setPosition((i - 1) * 1.15, -0.65, 0.0),
68 );
69 }
71 return scene..add(
72 LightNode(name: 'key', intensity: 3.0 * LightNode.legacyUnit)
73 ..setLocalForward(Vector3(-0.3, -0.4, -1.0).normalized()),
74 );
75 }
77 @override
78 RenderSettings settings(DemoContext context) => RenderSettings(
79 energyCompensation: compensate,
80 diffuseModel: orenNayar ? DiffuseModel.eon : DiffuseModel.lambert,
81 );
83 @override
84 List<DemoControl> controls(DemoContext context) => <DemoControl>[
85 ToggleControl(
86 'Energy compensation',
87 value: () => compensate,
88 onChanged: (bool v) => compensate = v,
89 ),
90 ToggleControl(
91 'Energy-preserving diffuse',
92 value: () => orenNayar,
93 onChanged: (bool v) => orenNayar = v,
94 ),
95 ];
97 @override
98 void verify(Scene scene, FrameResult frame) {
99 final FramePass? pass = frame.passes
100 .where((FramePass p) => p.name == 'scene')
101 .firstOrNull;
102 final int spheres = _metalRoughness.length + _clayRoughness.length;
103 if (pass == null || pass.drawCalls < spheres) {
104 throw StateError('the scene pass did not draw all $spheres spheres');
105 }
106 }