Sheen: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: colours (line 30), cloth (line 39), live (line 72).
1/// Red cloth twice: bare on the left, with a sheen at its rim on the right.
2///
3/// Quoted by `sheen.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `sheen` page: bare red cloth beside the same cloth with a sheen. Its
10/// check is that the second one has a layer that changes its shading.
11final class SheenDemo extends ShowcaseDemo {
12 /// The sheen's colour: 0 blue, 1 white, 2 gold.
13 int color = 0;
15 /// The sheen's roughness, 0.07 to 1; the shader floors it at 0.07.
16 double sheenRoughness = 0.5;
18 static const List<String> _colourNames = <String>['Blue', 'White', 'Gold'];
20 late final RenderMaterial _cloth;
23 void configureView(DemoContext context) {
24 context.orbit
25 ..distance = 3.4
26 ..yaw = 0.0
27 ..pitch = 0.1;
28 }
30 /// Linear colours: the sheen is not painted, so it is not sRGB.
31 Vector3 _sheenColour() => switch (color) {
32 0 => Vector3(0.2, 0.4, 1.0),
33 1 => Vector3(1.0, 1.0, 1.0),
34 _ => Vector3(1.0, 0.75, 0.3),
35 };
38 Scene build(DemoContext context) {
39 final RenderMaterial bare = RenderMaterial(
40 name: 'bare cloth',
41 lighting: LightingModel.pbrLayered,
42 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
43 roughness: 0.6,
44 );
45 _cloth = RenderMaterial(
46 name: 'cloth with sheen',
47 lighting: LightingModel.pbrLayered,
48 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
49 roughness: 0.6,
50 extensions: MaterialExtensions(
51 sheenColor: _sheenColour().toLinearColor(),
52 sheenRoughness: sheenRoughness,
53 ),
54 );
56 final DeviceMesh ball = DeviceMesh.upload(
57 context.device,
58 SphereShape(radius: 0.5, segments: 48, rings: 24).build(),
59 );
60 return Scene()
61 ..ambientIntensity = 0.15 * LightNode.legacyUnit
62 ..add(MeshNode(ball, bare, name: 'bare')..setPosition(-0.6, 0.0, 0.0))
63 ..add(MeshNode(ball, _cloth, name: 'sheen')..setPosition(0.6, 0.0, 0.0))
64 ..add(
65 LightNode(name: 'key', intensity: 3.0 * LightNode.legacyUnit)
66 ..setLocalForward(Vector3(-0.35, -0.45, -1.0).normalized()),
67 );
68 }
71 void update(DemoContext context, double dt) {
72 _cloth.extensions = MaterialExtensions(
73 sheenColor: _sheenColour().toLinearColor(),
74 sheenRoughness: sheenRoughness,
75 );
76 }
79 List<DemoControl> controls(DemoContext context) => <DemoControl>[
80 ChoiceControl(
81 'Sheen colour',
82 options: _colourNames,
83 index: () => color,
84 onChanged: (int i) => color = i,
85 ),
86 SliderControl(
87 'Sheen roughness',
88 // The shader floors sheen roughness at 0.07; below it nothing changes.
89 min: 0.07,
90 max: 1,
91 value: () => sheenRoughness,
92 onChanged: (double v) => sheenRoughness = v,
93 ),
94 ];
97 void verify(Scene scene, FrameResult frame) {
98 final MeshNode cloth = scene.meshes.firstWhere(
99 (MeshNode m) => m.name == 'sheen',
100 );
101 if (cloth.material.extensions?.shades != true) {
102 throw StateError('the cloth has no layer that changes its shading');
103 }
104 if (frame.drawCalls < 2) {
105 throw StateError('the two spheres were not both drawn');
106 }
107 }
108}