Skip to content

Commit 1d6d8ef

Browse files
committed
Support up to 16 joints
1 parent 405ab77 commit 1d6d8ef

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed
110 KB
Binary file not shown.

packages/flame_3d/lib/src/resources/material/spatial_material.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ class SpatialMaterial extends Material {
2121
'view',
2222
'projection',
2323
}),
24-
UniformSlot.value('JointMatrices', {
25-
'joint0',
26-
'joint1',
27-
'joint2',
28-
'joint3',
29-
'joint4',
30-
}),
24+
UniformSlot.value(
25+
'JointMatrices',
26+
List.generate(_maxJoints, (idx) => 'joint$idx').toSet(),
27+
),
3128
],
3229
),
3330
fragmentShader: Shader(
@@ -81,9 +78,9 @@ class SpatialMaterial extends Material {
8178

8279
void _bindJointMatrices(GraphicsDevice device) {
8380
final jointTransforms = device.jointsInfo.jointTransforms;
84-
if (jointTransforms.length > 5) {
81+
if (jointTransforms.length > _maxJoints) {
8582
throw Exception(
86-
'At most 5 joints per surface, found ${jointTransforms.length}',
83+
'At most $_maxJoints joints per surface, found ${jointTransforms.length}',
8784
);
8885
}
8986
for (final (idx, transform) in jointTransforms.indexed) {
@@ -113,4 +110,6 @@ class SpatialMaterial extends Material {
113110
static final _library = gpu.ShaderLibrary.fromAsset(
114111
'packages/flame_3d/assets/shaders/spatial_material.shaderbundle',
115112
)!;
113+
114+
static const _maxJoints = 16;
116115
}

packages/flame_3d/lib/src/resources/shader/uniform_value.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33

44
import 'package:flame_3d/graphics.dart';
55
import 'package:flame_3d/resources.dart';
6+
import 'package:ordered_set/comparing.dart';
67

78
/// {@template uniform_value}
89
/// Instance of a uniform value. Represented by a [ByteBuffer].
@@ -21,7 +22,9 @@ class UniformValue extends UniformInstance<ByteBuffer> {
2122
if (super.resource == null) {
2223
var previousIndex = -1;
2324

24-
final data = _storage.entries.fold<List<double>>([], (p, e) {
25+
final entries = _storage.entries.toList()
26+
..sort(Comparing.on((c) => c.key));
27+
final data = entries.fold<List<double>>([], (p, e) {
2528
if (previousIndex + 1 != e.key) {
2629
final field =
2730
slot.fields.indexed.firstWhere((e) => e.$1 == previousIndex + 1);

packages/flame_3d/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
flutter_gpu:
1919
sdk: flutter
2020
meta: ^1.12.0
21+
ordered_set: ^6.0.1
2122
vector_math: ^2.1.4
2223

2324
dev_dependencies:

packages/flame_3d/shaders/spatial_material.vert

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ uniform JointMatrices {
2424
mat4 joint2;
2525
mat4 joint3;
2626
mat4 joint4;
27+
mat4 joint5;
28+
mat4 joint6;
29+
mat4 joint7;
30+
mat4 joint8;
31+
mat4 joint9;
32+
mat4 joint10;
33+
mat4 joint11;
34+
mat4 joint12;
35+
mat4 joint13;
36+
mat4 joint14;
37+
mat4 joint15;
2738
} joints;
2839

2940
mat4 jointMat(float jointIndex) {
@@ -37,6 +48,28 @@ mat4 jointMat(float jointIndex) {
3748
return joints.joint3;
3849
} else if (jointIndex == 4.0) {
3950
return joints.joint4;
51+
} else if (jointIndex == 5.0) {
52+
return joints.joint5;
53+
} else if (jointIndex == 6.0) {
54+
return joints.joint6;
55+
} else if (jointIndex == 7.0) {
56+
return joints.joint7;
57+
} else if (jointIndex == 8.0) {
58+
return joints.joint8;
59+
} else if (jointIndex == 9.0) {
60+
return joints.joint9;
61+
} else if (jointIndex == 10.0) {
62+
return joints.joint10;
63+
} else if (jointIndex == 11.0) {
64+
return joints.joint11;
65+
} else if (jointIndex == 12.0) {
66+
return joints.joint12;
67+
} else if (jointIndex == 13.0) {
68+
return joints.joint13;
69+
} else if (jointIndex == 14.0) {
70+
return joints.joint14;
71+
} else if (jointIndex == 15.0) {
72+
return joints.joint15;
4073
} else {
4174
return mat4(0.0);
4275
}

0 commit comments

Comments
 (0)