A face per copy: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: batch (line 59), live (line 91).
1/// One mesh, one draw call, six different faces: `InstancedMeshNode
2/// .setMorphWeights` gives each copy its own weights, read from a texture by
3/// instance id instead of from a uniform every copy would share.
4///
5/// Quoted by `instanced_morphs.md` and shown whole in the Source tab.
6library;
8import 'dart:typed_data';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `instanced-morphs` page: six faces in one instanced draw, each given its
14/// own bump weight. Its check fails if every instance ends up with the same
15/// weight.
16final class InstancedMorphsDemo extends ShowcaseDemo {
17 /// The largest bump weight, the one the last face gets; the faces between
18 /// ramp evenly up to it from 0. At 0 all six are flat and alike. The "Spread"
19 /// slider writes it.
20 double spread = 1.0;
22 static const int _count = 6;
24 InstancedMeshNode? _faces;
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 7.0
30 ..pitch = 0.35;
31 }
34 Scene build(DemoContext context) {
35 const int segments = 10;
36 final MeshData flat = const PlaneShape(
37 width: 0.9,
38 depth: 0.9,
39 widthSegments: segments,
40 depthSegments: segments,
41 ).build();
42 final Float32List bump = Float32List(flat.vertexCount * 3);
43 for (var v = 0; v < flat.vertexCount; v++) {
44 final Vector3 p = flat.positionAt(v);
45 final double r2 = p.x * p.x + p.y * p.y;
46 bump[v * 3 + 2] = r2 < 0.16 ? (0.16 - r2) * 2.0 : 0.0;
47 }
48 final MeshData withBump = flat.withMorphTargets(<MorphTarget>[
49 MorphTarget(vertexCount: flat.vertexCount, positions: bump, name: 'bump'),
50 ]);
51 final MorphTexture packed = MorphTexture.pack(withBump)!;
52 final TextureHandle deltaTexture = context.device.createTextureFromPixels(
53 width: packed.width,
54 height: packed.height,
55 format: TextureFormat.r32g32b32a32Float,
56 pixels: packed.bytes,
57 );
59 final InstancedMeshNode faces = InstancedMeshNode(
60 DeviceMesh.upload(context.device, withBump),
61 RenderMaterial(
62 name: 'faces',
63 baseColor: LinearColor.fromSrgb(0.8, 0.8, 0.85, 1.0),
64 doubleSided: true,
65 ),
66 capacity: _count,
67 name: 'faces',
68 );
69 for (var i = 0; i < _count; i++) {
70 faces.addInstance(
71 Matrix4.translation(Vector3((i - (_count - 1) / 2) * 1.1, 0.0, 0.0)),
72 );
73 }
74 faces.morph = MorphState(
75 texture: deltaTexture,
76 targetCount: packed.targetCount,
77 reaches: packed.reaches,
78 );
79 _faces = faces;
81 return Scene()
82 ..add(faces)
83 ..add(
84 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
85 ..setLocalForward(Vector3(-0.3, -0.6, -0.7)),
86 );
87 }
90 void update(DemoContext context, double dt) {
91 final InstancedMeshNode? faces = _faces;
92 if (faces == null) return;
93 for (var i = 0; i < _count; i++) {
94 final double weight = (i / (_count - 1)) * spread;
95 faces.setMorphWeights(i, <double>[weight]);
96 }
97 }
100 List<DemoControl> controls(DemoContext context) => <DemoControl>[
101 SliderControl(
102 'Spread',
103 min: 0,
104 max: 1,
105 value: () => spread,
106 onChanged: (double v) => spread = v,
107 ),
108 ];
111 void verify(Scene scene, FrameResult frame) {
112 final InstancedMeshNode? faces = _faces;
113 if (faces == null || !faces.hasInstanceMorphWeights) {
114 throw StateError('no instance carries its own morph weights');
115 }
116 if (frame.instances < _count) {
117 throw StateError('the batch did not draw all $_count instances');
118 }
119 final double first = faces.morphWeightsOf(0)[0];
120 final double last = faces.morphWeightsOf(_count - 1)[0];
121 if ((last - first).abs() < 1e-3) {
122 throw StateError('every instance ended up with the same weight');
123 }
124 }
125}