flutter3d
Showcase Changelog 62 packages API reference

Transparency without sorting: the source

since 0.8.0 Shading and materials

Regions the guide quotes: panes (line 35), wall (line 67), settings (line 87).

1/// Coloured panes at three depths and two that cross through each other,
2/// composited sorted or without a sort.
3///
4/// Quoted by `order_independent_transparency.md` and shown whole in the Source
5/// tab.
6library;
7
8import 'dart:math' as math;
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `order-independent-transparency` page: coloured panes at three depths
14/// and two that cross. Its check is that the scene pass drew at least the
15/// draws the mode needs.
16final class OrderIndependentTransparencyDemo extends ShowcaseDemo {
17 /// How transparency is composited: 0 sorted, 1 weighted blended.
18 int mode = 1;
20 static const List<TransparencyMode> _modes = <TransparencyMode>[
21 TransparencyMode.sorted,
22 TransparencyMode.weightedBlended,
23 ];
25 @override
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 3.2
29 ..yaw = 0.35
30 ..pitch = 0.15;
31 }
33 @override
34 Scene build(DemoContext context) {
35 final DeviceMesh quad = DeviceMesh.upload(
36 context.device,
37 const PlaneShape(width: 1.2, depth: 1.2).build(),
38 );
39 final List<({double x, double z, double yaw, Vector4 color})> panes =
40 <({double x, double z, double yaw, Vector4 color})>[
41 (x: -0.3, z: 0.6, yaw: 0.0, color: Vector4(0.9, 0.1, 0.1, 0.5)),
42 (x: 0.0, z: 0.0, yaw: 0.0, color: Vector4(0.1, 0.9, 0.1, 0.45)),
43 (x: 0.3, z: -0.6, yaw: 0.0, color: Vector4(0.1, 0.2, 0.9, 0.6)),
44 // These two cross through each other: no order of the two is right
45 // for every pixel.
46 (x: 0.1, z: 0.3, yaw: 0.6, color: Vector4(0.9, 0.8, 0.1, 0.4)),
47 (x: 0.1, z: 0.3, yaw: -0.6, color: Vector4(0.8, 0.1, 0.9, 0.35)),
48 ];
49 final Scene scene = Scene();
50 for (final pane in panes) {
51 scene.add(
52 MeshNode(
53 quad,
54 RenderMaterial(
55 lighting: LightingModel.unlit,
56 baseColor: _fromSrgb(pane.color),
57 alphaMode: MaterialAlphaMode.blend,
58 doubleSided: true,
59 ),
60 name: 'pane',
61 )
62 ..setPosition(pane.x, 0.0, pane.z)
63 ..setRotationYawPitchRoll(pane.yaw, math.pi / 2, 0.0),
64 );
65 }
67 return scene..add(
68 MeshNode(
69 DeviceMesh.upload(
70 context.device,
71 const PlaneShape(width: 8, depth: 8).build(),
72 ),
73 RenderMaterial(
74 lighting: LightingModel.unlit,
75 baseColor: LinearColor.fromSrgb(0.45, 0.45, 0.45, 1.0),
76 doubleSided: true,
77 ),
78 name: 'wall',
79 )
80 ..setPosition(0.0, 0.0, -1.6)
81 ..setRotationYawPitchRoll(0.0, math.pi / 2, 0.0),
82 );
83 }
85 @override
86 RenderSettings settings(DemoContext context) => RenderSettings(
87 transparency: _modes[mode],
88 );
90 @override
91 List<DemoControl> controls(DemoContext context) => <DemoControl>[
92 ChoiceControl(
93 'Transparency',
94 options: const <String>['Sorted', 'Weighted blended'],
95 index: () => mode,
96 onChanged: (int i) => mode = i,
97 ),
98 ];
100 @override
101 void verify(Scene scene, FrameResult frame) {
102 final FramePass? pass = frame.passes
103 .where((FramePass p) => p.name == 'scene')
104 .firstOrNull;
105 if (pass == null) throw StateError('the scene pass did not run');
106 // Weighted blended adds one full-screen resolve to the scene pass, on top
107 // of a draw for every mesh.
108 final int meshes = scene.meshes.length;
109 final int expected = _modes[mode] == TransparencyMode.weightedBlended
110 ? meshes + 1
111 : meshes;
112 if (pass.drawCalls < expected) {
113 throw StateError(
114 'the scene pass drew ${pass.drawCalls}, expected at least $expected',
115 );
116 }
117 }
120/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
121LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);