flutter3d
Showcase Changelog 62 packages API reference

Stretched highlights: the source

since 0.8.0 Shading and materials

Regions the guide quotes: materials (line 33), mesh (line 50), live (line 71).

1/// One highlight round, the same highlight stretched: the anisotropy layer.
2///
3/// Quoted by `anisotropic_highlights.md` and shown whole in the Source tab.
4library;
5
6import 'dart:math' as math;
7
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `anisotropic-highlights` page: a plain sphere beside a brushed one.
12/// Its check is that the brushed one is on the layered model with an
13/// anisotropy.
14final class AnisotropicHighlightsDemo extends ShowcaseDemo {
15 /// How far the brushed highlight is stretched, 0 to 1.
16 double strength = 0.9;
18 /// Which way the highlight is stretched, in radians, 0 to π.
19 double rotation = 0.0;
21 late final RenderMaterial _brushed;
23 @override
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 3.4
27 ..yaw = 0.0
28 ..pitch = 0.1;
29 }
31 @override
32 Scene build(DemoContext context) {
33 final RenderMaterial plain = RenderMaterial(
34 name: 'plain',
35 lighting: LightingModel.pbrLayered,
36 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
37 roughness: 0.3,
38 );
39 _brushed = RenderMaterial(
40 name: 'brushed',
41 lighting: LightingModel.pbrLayered,
42 baseColor: LinearColor.fromSrgb(0.8, 0.05, 0.05, 1.0),
43 roughness: 0.3,
44 extensions: MaterialExtensions(
45 anisotropyStrength: strength,
46 anisotropyRotation: rotation,
47 ),
48 );
50 // A sphere from `SphereShape` carries a tangent at every vertex, running
51 // around the sphere along its lines of latitude.
52 final DeviceMesh ball = DeviceMesh.upload(
53 context.device,
54 SphereShape(radius: 0.5, segments: 48, rings: 24).build(),
55 );
57 return Scene()
58 ..ambientIntensity = 0.15 * LightNode.legacyUnit
59 ..add(MeshNode(ball, plain, name: 'plain')..setPosition(-0.6, 0.0, 0.0))
60 ..add(
61 MeshNode(ball, _brushed, name: 'brushed')..setPosition(0.6, 0.0, 0.0),
62 )
63 ..add(
64 LightNode(name: 'key', intensity: 3.0 * LightNode.legacyUnit)
65 ..setLocalForward(Vector3(-0.35, -0.45, -1.0).normalized()),
66 );
67 }
69 @override
70 void update(DemoContext context, double dt) {
71 _brushed.extensions = MaterialExtensions(
72 anisotropyStrength: strength,
73 anisotropyRotation: rotation,
74 );
75 }
77 @override
78 List<DemoControl> controls(DemoContext context) => <DemoControl>[
79 SliderControl(
80 'Strength',
81 min: 0,
82 max: 1,
83 value: () => strength,
84 onChanged: (double v) => strength = v,
85 ),
86 SliderControl(
87 'Rotation',
88 min: 0,
89 max: math.pi,
90 value: () => rotation,
91 onChanged: (double v) => rotation = v,
92 format: (double v) => '${(v * 180 / math.pi).round()}°',
93 ),
94 ];
96 @override
97 void verify(Scene scene, FrameResult frame) {
98 final MeshNode brushed = scene.meshes.firstWhere(
99 (MeshNode m) => m.name == 'brushed',
100 );
101 if (!identical(brushed.material.lighting, LightingModel.pbrLayered)) {
102 throw StateError('the brushed sphere is not on the layered model');
103 }
104 if ((brushed.material.extensions?.anisotropyStrength ?? 0.0) <= 0.0) {
105 throw StateError('the brushed sphere has no anisotropy');
106 }
107 if (frame.drawCalls < 2) {
108 throw StateError('the two spheres were not both drawn');
109 }
110 }