Procedural textures: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: encode (line 37), upload (line 44), scene (line 58), check (line 95).
1/// Two textures encoded in Dart and uploaded through the active device.
2library;
4import 'dart:typed_data';
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `procedural-textures` page: a solid blue sphere and a checkered torus
10/// wearing textures encoded in Dart. Its check is each texture's byte count
11/// and that both meshes are drawn.
12final class ProceduralTexturesDemo extends ShowcaseDemo {
13 final SolidColorTexture _solid = SolidColorTexture(
14 LinearColor.fromSrgb(0.12, 0.55, 0.92),
15 );
16 static const CheckerboardTexture _checker = CheckerboardTexture(
17 size: 64,
18 cell: 8,
19 light: 0xF0C86A,
20 dark: 0x314A72,
21 );
23 late final RenderMaterial _solidMaterial;
24 late final RenderMaterial _checkerMaterial;
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 7.5
30 ..pitch = 0.24
31 ..yaw = 0.35;
32 context.orbit.target.setValues(0.0, 0.8, 0.0);
33 }
36 Scene build(DemoContext context) {
37 final ByteData solidPixels = _solid.encode();
38 final ByteData checkerPixels = _checker.encode();
39 if (solidPixels.lengthInBytes != 4 ||
40 checkerPixels.lengthInBytes != 64 * 64 * 4) {
41 throw StateError('a procedural texture returned the wrong byte count');
42 }
44 _solidMaterial = RenderMaterial(
45 name: 'solid blue',
46 baseColor: LinearColor.fromSrgb(1.0, 1.0, 1.0, 1.0),
47 albedo: _solid.upload(context.device),
48 roughness: 0.42,
49 );
50 _checkerMaterial = RenderMaterial(
51 name: 'checkerboard',
52 baseColor: LinearColor.fromSrgb(1.0, 1.0, 1.0, 1.0),
53 albedo: _checker.upload(context.device),
54 albedoSampler: SamplerDescriptor.linearRepeat,
55 roughness: 0.58,
56 );
58 final Scene scene = Scene()
59 ..ambientColor = LinearColor(0.45, 0.54, 0.72)
60 ..ambientIntensity = 0.14 * LightNode.legacyUnit
61 ..add(
62 MeshNode(
63 DeviceMesh.upload(
64 context.device,
65 const SphereShape(radius: 1.05, segments: 36, rings: 18).build(),
66 ),
67 _solidMaterial,
68 name: 'solid sphere',
69 )..setPosition(-1.65, 1.05, 0.0),
70 )
71 ..add(
72 MeshNode(
73 DeviceMesh.upload(
74 context.device,
75 const TorusShape(
76 radius: 0.82,
77 tubeRadius: 0.34,
78 segments: 48,
79 tubeSegments: 24,
80 ).build(),
81 ),
82 _checkerMaterial,
83 name: 'checker torus',
84 )..setPosition(1.65, 1.05, 0.0),
85 )
86 ..add(
87 LightNode(name: 'key', intensity: 3.3 * LightNode.legacyUnit)
88 ..setLocalForward(Vector3(-0.45, -0.82, -0.35)),
89 );
90 return scene;
91 }
94 void verify(Scene scene, FrameResult frame) {
95 final Uint8List checker = _checker.encode().buffer.asUint8List();
96 final bool twoColours =
97 checker[0] != checker[_checker.cell * 4] ||
98 checker[1] != checker[_checker.cell * 4 + 1] ||
99 checker[2] != checker[_checker.cell * 4 + 2];
100 if (_solidMaterial.albedo == null ||
101 _checkerMaterial.albedo == null ||
102 !twoColours ||
103 frame.drawCalls < 2) {
104 throw StateError('the procedural textures were not drawn');
105 }
106 }
107}