A texture transform per map: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: texture (line 43), transforms (line 59), plates (line 74), live (line 124).
1/// Two plates with the same four-colour texture: one read as it is, one whose
2/// colour map and glow map are each moved by a transform of their own.
3///
4/// Quoted by `texture_transforms.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
8import 'dart:typed_data';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `texture-transforms` page: two plates with one texture, the second
14/// moving its colour and glow maps apart. Its check is that the moved plate
15/// carries a transform per map.
16final class TextureTransformsDemo extends ShowcaseDemo {
17 /// How far the colour map is turned, in radians, 0 to 2π.
18 double turn = math.pi / 4;
20 /// How many times the colour map repeats across the plate, 1 to 4.
21 double repeat = 2.0;
23 /// How far the glow map slides along U, 0 to 0.5 of a tile.
24 double slide = 0.5;
26 static const SamplerDescriptor _nearestRepeat = SamplerDescriptor(
27 minFilter: MinMagFilter.nearest,
28 magFilter: MinMagFilter.nearest,
29 widthAddressMode: SamplerAddressMode.repeat,
30 heightAddressMode: SamplerAddressMode.repeat,
31 );
33 late final RenderMaterial _moved;
36 void configureView(DemoContext context) {
37 context.orbit
38 ..distance = 3.6
39 ..yaw = 0.0
40 ..pitch = 0.0;
41 }
43 /// Four texels, one colour each: red, green, blue and white.
44 TextureHandle _quadrants(GraphicsDevice device) =>
45 device.createTextureFromPixels(
46 width: 2,
47 height: 2,
48 format: TextureFormat.r8g8b8a8UNormInt,
49 pixels: ByteData.sublistView(
50 Uint8List.fromList(<int>[
51 ...<int>[230, 20, 20, 255],
52 ...<int>[20, 230, 20, 255],
53 ...<int>[20, 20, 230, 255],
54 ...<int>[230, 230, 230, 255],
55 ]),
56 ),
57 );
59 /// The colour map turned and tiled, the glow map slid sideways: two maps of
60 /// one material that no longer agree.
61 Map<MaterialMap, TextureTransform> _transforms() =>
62 <MaterialMap, TextureTransform>{
63 MaterialMap.baseColor: TextureTransform(
64 scale: Vector2(repeat, repeat),
65 rotation: turn,
66 ),
67 MaterialMap.emissive: TextureTransform(offset: Vector2(slide, 0.0)),
68 };
71 Scene build(DemoContext context) {
72 final TextureHandle quadrants = _quadrants(context.device);
74 final RenderMaterial asIs = RenderMaterial(
75 name: 'as is',
76 lighting: LightingModel.pbrLayered,
77 baseColor: LinearColor.fromSrgb(0.3, 0.3, 0.3, 1.0),
78 albedo: quadrants,
79 albedoSampler: _nearestRepeat,
80 emissiveTexture: quadrants,
81 emissiveSampler: _nearestRepeat,
82 emissive: LinearColor(0.5, 0.5, 0.5),
83 );
84 _moved = RenderMaterial(
85 name: 'moved',
86 lighting: LightingModel.pbrLayered,
87 baseColor: LinearColor.fromSrgb(0.3, 0.3, 0.3, 1.0),
88 albedo: quadrants,
89 albedoSampler: _nearestRepeat,
90 emissiveTexture: quadrants,
91 emissiveSampler: _nearestRepeat,
92 emissive: LinearColor(0.5, 0.5, 0.5),
93 textureTransforms: _transforms(),
94 );
96 final DeviceMesh plate = DeviceMesh.upload(
97 context.device,
98 const PlaneShape(width: 1.6, depth: 1.6).build(),
99 );
100 final Quaternion upright = Quaternion.axisAngle(
101 Vector3(1.0, 0.0, 0.0),
102 math.pi / 2,
103 );
104 return Scene()
105 ..ambientIntensity = 0.25 * LightNode.legacyUnit
106 ..add(
107 MeshNode(plate, asIs, name: 'as is')
108 ..setRotation(upright)
109 ..setPosition(-0.95, 0.0, 0.0),
110 )
111 ..add(
112 MeshNode(plate, _moved, name: 'moved')
113 ..setRotation(upright)
114 ..setPosition(0.95, 0.0, 0.0),
115 )
116 ..add(
117 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
118 ..setLocalForward(Vector3(-0.2, -0.3, -1.0).normalized()),
119 );
120 }
123 void update(DemoContext context, double dt) {
124 _moved.textureTransforms.addAll(_transforms());
125 }
128 List<DemoControl> controls(DemoContext context) => <DemoControl>[
129 SliderControl(
130 'Colour map: turn',
131 min: 0,
132 max: 2 * math.pi,
133 value: () => turn,
134 onChanged: (double v) => turn = v,
135 format: (double v) => '${(v * 180 / math.pi).round()}°',
136 ),
137 SliderControl(
138 'Colour map: repeat',
139 min: 1,
140 max: 4,
141 value: () => repeat,
142 onChanged: (double v) => repeat = v,
143 format: (double v) => '${v.toStringAsFixed(1)}x',
144 ),
145 SliderControl(
146 'Glow map: slide',
147 min: 0,
148 // Half a tile: the map repeats, so a slide of one looks like nought.
149 max: 0.5,
150 value: () => slide,
151 onChanged: (double v) => slide = v,
152 ),
153 ];
156 void verify(Scene scene, FrameResult frame) {
157 final MeshNode moved = scene.meshes.firstWhere(
158 (MeshNode m) => m.name == 'moved',
159 );
160 if (moved.material.textureTransforms.length != 2) {
161 throw StateError('the moved plate does not carry a transform per map');
162 }
163 if (frame.drawCalls < 2) {
164 throw StateError('the two plates were not both drawn');
165 }
166 }
167}