flutter3d
Showcase Changelog 62 packages API reference

Texture transform: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: checker (line 21), transform (line 42), check (line 73).

1/// `withTextureTransform`: `KHR_texture_transform`, applied to a mesh's own
2/// texture coordinates.
3///
4/// Quoted by `texture_transform.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `texture-transform` page: a floor whose texture coordinates went
13/// through `withTextureTransform`. Its check is that U was scaled by four and
14/// the vertex count did not change.
15final class TextureTransformDemo extends ShowcaseDemo {
16 late final MeshData _plain;
17 late final MeshData _moved;
19 @override
20 Scene build(DemoContext context) {
21 // An 8x8 checker, small enough that scaling the transform below by
22 // four makes the tiling obvious.
23 final pixels = Uint8List(8 * 8 * 4);
24 for (var y = 0; y < 8; y++) {
25 for (var x = 0; x < 8; x++) {
26 final lit = ((x ~/ 2) + (y ~/ 2)).isEven;
27 final o = (y * 8 + x) * 4;
28 final value = lit ? 235 : 25;
29 pixels[o] = value;
30 pixels[o + 1] = value;
31 pixels[o + 2] = value;
32 pixels[o + 3] = 255;
33 }
34 }
35 final albedo = context.device.createTextureFromPixels(
36 width: 8,
37 height: 8,
38 format: TextureFormat.r8g8b8a8UNormInt,
39 pixels: ByteData.sublistView(pixels),
40 );
42 // `KHR_texture_transform`'s own order: scale, then rotate, then move.
43 // Scaling by four here tiles the checker four times across the plane
44 // instead of stretching one copy of it across the whole surface.
45 _plain = PlaneShape(width: 2, depth: 2).build();
46 final transform = TextureTransform(scale: Vector2(4.0, 4.0));
47 _moved = withTextureTransform(_plain, transform);
49 return Scene()
50 ..add(
51 MeshNode(
52 DeviceMesh.upload(context.device, _moved),
53 RenderMaterial(albedo: albedo, roughness: 0.8, doubleSided: true),
54 name: 'floor',
55 ),
56 )
57 ..add(
58 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
59 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
60 );
61 }
63 @override
64 void configureView(DemoContext context) {
65 context.orbit
66 ..distance = 3.0
67 ..pitch = 0.9
68 ..yaw = 0.4;
69 }
71 @override
72 void verify(Scene scene, FrameResult frame) {
73 final layout = _plain.layout;
74 final stride = layout.floatsPerVertex;
75 final uvOffset = layout.floatOffsetOf(VertexLayout.texcoord.name);
76 // The plane's corner at U=1 should come back at U=4, the same
77 // arithmetic checked independently of whatever `withTextureTransform`
78 // did internally.
79 final cornerOffset = stride + uvOffset;
80 final sourceU = _plain.vertices[cornerOffset];
81 final movedU = _moved.vertices[cornerOffset];
82 if ((movedU - sourceU * 4.0).abs() > 1e-6 || sourceU == 0.0) {
83 throw StateError('the transform did not scale U by four');
84 }
85 if (_moved.vertices.length != _plain.vertices.length) {
86 throw StateError('the transform changed the vertex count');
87 }
88 if (frame.drawCalls < 1) {
89 throw StateError('the floor was not drawn');
90 }
91 }