flutter3d
Showcase Changelog 62 packages API reference

KTX2 and Basis textures: the source

since 0.4.2 Formats and I/O

Regions the guide quotes: bitstream (line 23), parse (line 38), upload (line 41), check (line 67).

1/// Reading a KTX2 container that carries Basis Universal ETC1S pixels, and
2/// putting the transcoded texture on a cube.
3///
4/// Quoted by `ktx2.md` and shown whole in the Source tab.
5library;
6
7import 'dart:convert';
8import 'dart:typed_data';
9
10import 'package:flutter3d/flutter3d.dart';
11// The engine's own `Ktx2Texture` (from `flutter3d/flutter3d.dart`) answers
12// a `TextureFormat`, already resolved against a device. This page reads
13// the container itself, which answers the file's own `vkFormat`.
14import 'package:flutter3d_core/formats.dart' show Ktx2Container, VkFormat;
15import 'package:flutter3d_showcase/src/demo/demo.dart';
17/// The `ktx2` page: a cube wearing an 8 by 8 Basis texture transcoded to
18/// RGBA8. Its check is the size, the format, and a top-left quadrant close to
19/// red.
20final class Ktx2Demo extends ShowcaseDemo {
21 late final Ktx2Container _texture;
23 // An 8x8 image, four flat-coloured quadrants, encoded by a real build of
24 // `basisu`. `vkFormat` is left undefined, KTX2's own way of saying "these
25 // pixels are Basis Universal", with the actual flavour named in the
26 // container's data format descriptor.
27 static const String _fixtureBase64 =
28 'q0tUWCAyMLsNChoKAAAAAAEAAAAIAAAACAAAAAAAAAAAAAAAAQAAAAEAAAABAAAAaAAAACw'
29 'AAACUAAAAJAAAALgAAAAAAAAAkQAAAAAAAABJAQAAAAAAAAIAAAAAAAAAAAAAAAAAAAAsA'
30 'AAAAAAAAAIAKACjAQEAAwMAAAgAAAAAAAAAAAA/AAAAAAAAAAAA/////x8AAABLVFh3cml'
31 '0ZXIAQmFzaXMgVW5pdmVyc2FsIDIuNTAAAAQABAAsAAAAEQAAACwAAAAAAAAAAAAAAAAAA'
32 'AACAAAAAAAAAAAAAAAgQEQAAAAAAAgjRAATAQAAAAAASAIDwASAAAAAAADiAGACAAAAAAA'
33 'AATkjAKyqUlWtqqqqrKqqqlJVVVUFAMFEAAAAAAAA8l+NAJgBAAAAAABAQgkQIUAAAAAAU'
34 'pgiAJgAAAAAAABAAAEEEw==';
36 @override
37 Scene build(DemoContext context) {
38 final bytes = base64Decode(_fixtureBase64.replaceAll('\n', ''));
39 _texture = Ktx2Container.parse(bytes);
41 // The level this reader hands back is already plain RGBA8, so it goes
42 // straight onto a device texture the same way any other decoded image
43 // would.
44 final albedo = context.device.createTextureFromPixels(
45 width: _texture.pixelWidth,
46 height: _texture.pixelHeight,
47 format: TextureFormat.r8g8b8a8UNormInt,
48 pixels: _texture.levels.first,
49 );
51 return Scene()
52 ..add(
53 MeshNode(
54 DeviceMesh.upload(context.device, CuboidShape().build()),
55 RenderMaterial(albedo: albedo, roughness: 0.7),
56 name: 'block',
57 ),
58 )
59 ..add(
60 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
61 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
62 );
63 }
65 @override
66 void verify(Scene scene, FrameResult frame) {
67 if (_texture.pixelWidth != 8 || _texture.pixelHeight != 8) {
68 throw StateError('this fixture is always 8x8');
69 }
70 if (_texture.vkFormat != VkFormat.r8g8b8a8UNorm) {
71 throw StateError('a Basis file should transcode to plain RGBA8');
72 }
73 // The top-left quadrant is a flat, near-pure red.
74 final topLeft = _texture.levels.first.getUint32(0, Endian.little);
75 final red = topLeft & 0xff;
76 final green = (topLeft >> 8) & 0xff;
77 if (red < 240 || green > 15) {
78 throw StateError('the top-left quadrant should transcode close to red');
79 }
80 if (frame.drawCalls < 1) {
81 throw StateError('the block was not drawn');
82 }
83 }