Clear coat: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: paint (line 35), spheres (line 52), live (line 69).
1/// Red paint twice: bare on the left, under a clear coat on the right.
2///
3/// Quoted by `clear_coat.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `clear-coat` page: bare red paint beside the same paint under a clear
10/// coat. Its check is that the coated one is on the layered model with a
11/// coat.
12final class ClearCoatDemo extends ShowcaseDemo {
13 /// How strong the clear coat is, 0 to 1.
14 double coat = 1.0;
16 /// The coat's roughness, 0 to 1.
17 double coatRoughness = 0.15;
19 /// The paint's roughness under it, 0.05 to 1, the same on both spheres.
20 double paintRoughness = 0.6;
22 late final RenderMaterial _bare;
23 late final RenderMaterial _coated;
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 3.4
29 ..yaw = 0.0
30 ..pitch = 0.1;
31 }
34 Scene build(DemoContext context) {
35 _bare = RenderMaterial(
36 name: 'bare paint',
37 lighting: LightingModel.pbrLayered,
38 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
39 roughness: paintRoughness,
40 );
41 _coated = RenderMaterial(
42 name: 'coated paint',
43 lighting: LightingModel.pbrLayered,
44 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
45 roughness: paintRoughness,
46 extensions: MaterialExtensions(
47 clearcoat: coat,
48 clearcoatRoughness: coatRoughness,
49 ),
50 );
52 final DeviceMesh ball = DeviceMesh.upload(
53 context.device,
54 SphereShape(radius: 0.5, segments: 48, rings: 24).build(),
55 );
56 final Scene scene = Scene()
57 ..ambientIntensity = 0.15 * LightNode.legacyUnit
58 ..add(MeshNode(ball, _bare, name: 'bare')..setPosition(-0.6, 0.0, 0.0))
59 ..add(MeshNode(ball, _coated, name: 'coated')..setPosition(0.6, 0.0, 0.0))
60 ..add(
61 LightNode(name: 'key', intensity: 3.0 * LightNode.legacyUnit)
62 ..setLocalForward(Vector3(-0.35, -0.45, -1.0).normalized()),
63 );
64 return scene;
65 }
68 void update(DemoContext context, double dt) {
69 _bare.roughness = paintRoughness;
70 _coated
71 ..roughness = paintRoughness
72 ..extensions = MaterialExtensions(
73 clearcoat: coat,
74 clearcoatRoughness: coatRoughness,
75 );
76 }
79 List<DemoControl> controls(DemoContext context) => <DemoControl>[
80 SliderControl(
81 'Coat',
82 min: 0,
83 max: 1,
84 value: () => coat,
85 onChanged: (double v) => coat = v,
86 ),
87 SliderControl(
88 'Coat roughness',
89 min: 0,
90 max: 1,
91 value: () => coatRoughness,
92 onChanged: (double v) => coatRoughness = v,
93 ),
94 SliderControl(
95 'Paint roughness',
96 min: 0.05,
97 max: 1,
98 value: () => paintRoughness,
99 onChanged: (double v) => paintRoughness = v,
100 ),
101 ];
104 void verify(Scene scene, FrameResult frame) {
105 final MeshNode coated = scene.meshes.firstWhere(
106 (MeshNode m) => m.name == 'coated',
107 );
108 if (!identical(coated.material.lighting, LightingModel.pbrLayered)) {
109 throw StateError('the coated paint is not on the layered model');
110 }
111 if ((coated.material.extensions?.clearcoat ?? 0.0) <= 0.0) {
112 throw StateError('the coated paint has no clear coat');
113 }
114 if (frame.drawCalls < 2) {
115 throw StateError('the two spheres were not both drawn');
116 }
117 }
118}