flutter3d
Showcase Changelog 62 packages API reference

Surfaces of revolution: the source

since 0.1.0 Scene and geometry

Regions the guide quotes: profile (line 25), sweep (line 38), light (line 74), check (line 92).

1/// A vessel made by sweeping a profile around the Y axis.
2library;
3
4import 'dart:math' as math;
5
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8
9/// The `lathe-shape` page: three clay vessels, each a profile swept around
10/// Y. Its check is that all three are drawn.
11final class LatheShapeDemo extends ShowcaseDemo {
12 final List<MeshNode> _vessels = <MeshNode>[];
14 @override
15 void configureView(DemoContext context) {
16 context.orbit
17 ..distance = 9.0
18 ..pitch = 0.24
19 ..yaw = 0.42;
20 context.orbit.target.setValues(0.0, 1.15, 0.0);
21 }
23 @override
24 Scene build(DemoContext context) {
25 final List<Vector2> profile = <Vector2>[
26 Vector2(0.0, -1.25),
27 Vector2(0.72, -1.25),
28 Vector2(0.78, -1.1),
29 Vector2(0.82, -0.55),
30 Vector2(0.62, 0.05),
31 Vector2(0.46, 0.7),
32 Vector2(0.46, 1.05),
33 Vector2(0.62, 1.18),
34 Vector2(0.62, 1.25),
35 Vector2(0.5, 1.25),
36 ];
38 final List<(String, double, int)> variants = <(String, double, int)>[
39 ('faceted', math.pi * 2.0, 12),
40 ('smooth', math.pi * 2.0, 48),
41 ('open sweep', math.pi * 1.5, 36),
42 ];
43 final Scene scene = Scene()
44 ..ambientColor = LinearColor(0.48, 0.56, 0.72)
45 ..ambientIntensity = 0.14 * LightNode.legacyUnit;
46 for (var i = 0; i < variants.length; i++) {
47 final (String name, double sweep, int segments) = variants[i];
48 final MeshData data = LatheShape(
49 profile: profile,
50 segments: segments,
51 sweepAngle: sweep,
52 name: name,
53 ).build();
54 final RenderMaterial clay = RenderMaterial(
55 name: '$name clay',
56 baseColor: LinearColor.fromSrgb(
57 0.78,
58 0.35 + i * 0.12,
59 0.2 + i * 0.16,
60 1.0,
61 ),
62 roughness: 0.48,
63 doubleSided: sweep < math.pi * 2.0,
64 );
65 final MeshNode node = MeshNode(
66 DeviceMesh.upload(context.device, data),
67 clay,
68 name: name,
69 )..setPosition((i - 1) * 2.6, 1.25, 0.0);
70 _vessels.add(node);
71 scene.add(node);
72 }
74 scene
75 ..add(
76 LightNode(name: 'key', intensity: 3.2 * LightNode.legacyUnit)
77 ..setLocalForward(Vector3(-0.45, -0.82, -0.34)),
78 )
79 ..add(
80 LightNode(
81 type: LightType.point,
82 intensity: 8.0 * LightNode.legacyUnit,
83 range: 10.0,
84 name: 'rim',
85 )..setPosition(2.5, 3.8, -2.5),
86 );
87 return scene;
88 }
90 @override
91 void verify(Scene scene, FrameResult frame) {
92 const Set<String> expected = <String>{'faceted', 'smooth', 'open sweep'};
93 final Set<String?> actual = <String?>{
94 for (final MeshNode node in _vessels) node.name,
95 };
96 if (_vessels.length != 3 ||
97 !actual.containsAll(expected) ||
98 frame.drawCalls < 3) {
99 throw StateError('the three lathe variants were not drawn');
100 }
101 }