The material expression language: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: source (line 39), specialise (line 53), evaluate (line 64), report (line 97), check (line 122).
1/// The material expression language: a small GLSL-flavoured source parsed
2/// into a tree that either emits a `.frag` or is evaluated directly.
3///
4/// Quoted by `material_language.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter/widgets.dart';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_core/flutter3d_core.dart'
12 show
13 MaterialSurfaceValues,
14 MaterialVariant,
15 emitMaterialFragment,
16 evaluateMaterial,
17 parseMaterial,
18 specializeMaterial;
19import 'package:flutter3d_showcase/src/demo/demo.dart';
21/// The `material-language` page: a ball drawn by a material written in the
22/// material language. Its check is what the body reads, that evaluating it
23/// agrees with the sum worked by hand, and that the fragment has an entry
24/// point.
25final class MaterialLanguageDemo extends ShowcaseDemo {
26 late final MaterialProgram _specialised;
27 late final String _glsl;
28 late final List<double> _shaded;
30 static const double _albedo0 = 0.2;
31 static const double _albedo1 = 0.2;
32 static const double _albedo2 = 0.25;
33 static const double _alpha = 1.0;
34 static const double _nDotV = 0.2;
35 static const double _rimPower = 4.0;
38 Scene build(DemoContext context) {
39 const String source = '''
40material RimLight {
41 param float rimPower = 2.0;
42 param vec3 rimColor = vec3(0.2, 0.6, 1.0);
44 fragment {
45 let facing = clamp(nDotV, 0.0, 1.0);
46 let rim = pow(1.0 - facing, rimPower);
47 return vec4(albedo + rimColor * rim, alpha);
48 }
49}
50''';
51 final MaterialProgram program = parseMaterial(source);
53 _specialised = specializeMaterial(
54 program,
55 const MaterialVariant(
56 'RimLight_sharp',
57 values: {
58 'rimPower': <double>[_rimPower],
59 },
60 ),
61 );
62 _glsl = emitMaterialFragment(_specialised);
64 _shaded = evaluateMaterial(
65 _specialised,
66 MaterialSurfaceValues(
67 inputs: <String, List<double>>{
68 'albedo': <double>[_albedo0, _albedo1, _albedo2],
69 'alpha': <double>[_alpha],
70 'nDotV': <double>[_nDotV],
71 },
72 sample: (MaterialTextureSlot slot, double u, double v) =>
73 throw StateError('this material declares no texture'),
74 ),
75 );
77 return Scene()
78 ..add(
79 MeshNode(
80 DeviceMesh.upload(
81 context.device,
82 SphereShape(segments: 40, rings: 20).build(),
83 ),
84 RenderMaterial(
85 baseColor: LinearColor.fromSrgb(0.8, 0.65, 0.35, 1.0),
86 roughness: 0.5,
87 ),
88 name: 'ball',
89 ),
90 )
91 ..add(
92 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
93 ..setLocalForward(Vector3(-0.4, -0.7, -0.6)),
94 );
95 }
97 String _report() =>
98 'inputs this body reads: ${_specialised.inputsUsed.toList()..sort()}\n\n'
99 'evaluated at nDotV=$_nDotV: $_shaded\n\n'
100 '$_glsl';
103 Widget? customBody(BuildContext buildContext, DemoContext context) =>
104 Container(
105 color: const Color(0xFF14161A),
106 padding: const EdgeInsets.all(24),
107 alignment: Alignment.topLeft,
108 child: SingleChildScrollView(
109 child: DefaultTextStyle(
110 style: const TextStyle(
111 color: Color(0xFFE8E8EC),
112 fontSize: 13,
113 fontFamily: 'monospace',
114 ),
115 child: Text(_report()),
116 ),
117 ),
118 );
121 void verify(Scene scene, FrameResult frame) {
122 if (!_specialised.inputsUsed.containsAll(<String>[
123 'albedo',
124 'alpha',
125 'nDotV',
126 ])) {
127 throw StateError('the body should read albedo, alpha and nDotV');
128 }
129 final double facing = _nDotV.clamp(0.0, 1.0);
130 final double rim = math.pow(1.0 - facing, _rimPower).toDouble();
131 final List<double> expected = <double>[
132 _albedo0 + 0.2 * rim,
133 _albedo1 + 0.6 * rim,
134 _albedo2 + 1.0 * rim,
135 _alpha,
136 ];
137 for (var i = 0; i < expected.length; i++) {
138 if ((_shaded[i] - expected[i]).abs() > 1e-9) {
139 throw StateError('evaluateMaterial disagrees with the hand-worked sum');
140 }
141 }
142 if (!_glsl.contains('void main()')) {
143 throw StateError('the emitted fragment has no entry point');
144 }
145 if (frame.drawCalls < 1) {
146 throw StateError('the ball was not drawn');
147 }
148 }
149}