Morph targets: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: target (line 32), texture (line 49), morph (line 67), live (line 91).
1/// A grid that grows a bump: the deltas live in a `MorphTexture`, and
2/// `MorphState` says how much of it to add right now.
3///
4/// Quoted by `morph_targets.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 `morph-targets` page: a 2 m grid with one morph target that raises a
14/// bump in the middle. Its check fails if the weighted target does not push the
15/// mesh upward.
16final class MorphTargetsDemo extends ShowcaseDemo {
17 /// How much of the bump is added, from 0 (the flat grid) to 1 (the whole 0.9
18 /// m target). The "Weight" slider writes it.
19 double weight = 0.7;
21 MeshNode? _sheet;
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 4.0
27 ..pitch = 0.5;
28 }
31 Scene build(DemoContext context) {
32 const int segments = 16;
33 final MeshData flat = const PlaneShape(
34 width: 2.0,
35 depth: 2.0,
36 widthSegments: segments,
37 depthSegments: segments,
38 ).build();
39 final Float32List bump = Float32List(flat.vertexCount * 3);
40 for (var v = 0; v < flat.vertexCount; v++) {
41 final Vector3 p = flat.positionAt(v);
42 final double falloff = _gaussian(p.x, p.z);
43 bump[v * 3 + 1] = falloff * 0.9;
44 }
45 final MeshData withBump = flat.withMorphTargets(<MorphTarget>[
46 MorphTarget(vertexCount: flat.vertexCount, positions: bump, name: 'bump'),
47 ]);
49 final MorphTexture packed = MorphTexture.pack(withBump)!;
50 final TextureHandle deltaTexture = context.device.createTextureFromPixels(
51 width: packed.width,
52 height: packed.height,
53 format: TextureFormat.r32g32b32a32Float,
54 pixels: packed.bytes,
55 );
57 final RenderMaterial sheetMaterial = RenderMaterial(
58 name: 'sheet',
59 baseColor: LinearColor.fromSrgb(0.3, 0.6, 0.85, 1.0),
60 doubleSided: true,
61 );
62 final MeshNode sheet = MeshNode(
63 DeviceMesh.upload(context.device, withBump),
64 sheetMaterial,
65 name: 'sheet',
66 );
67 sheet.morph = MorphState(
68 texture: deltaTexture,
69 targetCount: packed.targetCount,
70 reaches: packed.reaches,
71 )..setWeights(<double>[weight]);
72 _sheet = sheet;
74 return Scene()
75 ..add(sheet)
76 ..add(
77 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
78 ..setLocalForward(Vector3(-0.4, -0.8, -0.3)),
79 );
80 }
82 static double _gaussian(double x, double z) {
83 final double r2 = x * x + z * z;
84 return _exp(-r2 / 0.35);
85 }
87 static double _exp(double x) => x < -30 ? 0.0 : math.exp(x);
90 void update(DemoContext context, double dt) {
91 _sheet?.morph?.setWeights(<double>[weight]);
92 }
95 List<DemoControl> controls(DemoContext context) => <DemoControl>[
96 SliderControl(
97 'Weight',
98 min: 0,
99 max: 1,
100 value: () => weight,
101 onChanged: (double v) => weight = v,
102 ),
103 ];
106 void verify(Scene scene, FrameResult frame) {
107 final MorphState? morph = _sheet?.morph;
108 if (morph == null) {
109 throw StateError('the device would not take the morph delta texture');
110 }
111 if (morph.growth.max.y <= 0.0) {
112 throw StateError('the weighted target did not grow the mesh upward');
113 }
114 }
115}