flutter3d
Showcase Changelog 62 packages API reference

Anisotropic texture filtering: the source

since 0.4.3 Shading and materials

Regions the guide quotes: texture (line 34), floor (line 46), settings (line 72).

1/// Anisotropic filtering: a checkerboard floor that stays sharp toward the
2/// horizon.
3///
4/// Quoted by `texture_filtering.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_hardware/flutter3d_hardware.dart' show MipChain;
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `texture-filtering` page: a checkerboard floor running to the
14/// horizon. Its check is that the sampler blends mip levels and that
15/// anisotropy is on.
16final class TextureFilteringDemo extends ShowcaseDemo {
17 /// The anisotropy picked, an index into 1x, 2x, 4x, 8x and 16x.
18 int anisotropyChoice = 3;
20 static const List<int> _levels = <int>[1, 2, 4, 8, 16];
21 static const int _size = 256;
23 @override
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 3.0
27 ..yaw = 0.0
28 ..pitch = 0.07;
29 context.orbit.target.setValues(0.0, 0.2, 0.0);
30 }
32 @override
33 Scene build(DemoContext context) {
34 final ByteData pixels = const CheckerboardTexture(
35 size: _size,
36 cell: 16,
37 ).encode();
38 final TextureHandle checks = context.device.createTextureFromPixels(
39 width: _size,
40 height: _size,
41 format: TextureFormat.r8g8b8a8UNormInt,
42 pixels: pixels,
43 mipLevels: MipChain.build(pixels, _size, _size),
44 );
46 final RenderMaterial floor = RenderMaterial(
47 name: 'checks',
48 albedo: checks,
49 albedoSampler: SamplerDescriptor.trilinearRepeat,
50 roughness: 0.9,
51 doubleSided: true,
52 );
53 final MeshNode ground = MeshNode(
54 DeviceMesh.upload(
55 context.device,
56 const PlaneShape(width: 60, depth: 60).build(),
57 ),
58 floor,
59 name: 'floor',
60 );
62 return Scene()
63 ..add(ground)
64 ..add(
65 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
66 ..setLocalForward(Vector3(-0.3, -1.0, -0.4)),
67 );
68 }
70 @override
71 RenderSettings settings(DemoContext context) => RenderSettings(
72 anisotropy: _levels[anisotropyChoice],
73 );
75 @override
76 List<DemoControl> controls(DemoContext context) => <DemoControl>[
77 ChoiceControl(
78 'Anisotropy',
79 options: <String>[for (final int level in _levels) '${level}x'],
80 index: () => anisotropyChoice,
81 onChanged: (int i) => anisotropyChoice = i,
82 ),
83 ];
85 @override
86 void verify(Scene scene, FrameResult frame) {
87 final RenderMaterial floor = scene.meshes.first.material;
88 if (floor.albedoSampler?.mipFilter != MipFilter.linear) {
89 throw StateError('the floor sampler does not blend mip levels');
90 }
91 if (_levels[anisotropyChoice] <= 1) {
92 throw StateError('anisotropy is off, so nothing here is filtered');
93 }
94 if (frame.drawCalls < 1) throw StateError('the floor was not drawn');
95 }