add support for multiple bone animations using quaternions

This commit is contained in:
Nick Fisher
2023-11-17 23:44:30 +08:00
parent f5d5a36f22
commit 572a945025
13 changed files with 1266 additions and 1254 deletions

View File

@@ -1,5 +1,7 @@
import 'dart:typed_data';
import 'package:vector_math/vector_math_64.dart';
///
/// Specifies frame data (i.e. weights) to animate the morph targets contained in [morphTargets] under a mesh named [mesh].
/// [data] is laid out as numFrames x numMorphTargets.
@@ -40,7 +42,7 @@ class MorphAnimationData {
class BoneAnimationData {
final String boneName;
final List<String> meshNames;
final Float32List frameData;
final List<Quaternion> frameData;
double frameLengthInMs;
BoneAnimationData(
this.boneName, this.meshNames, this.frameData, this.frameLengthInMs);

View File

@@ -272,12 +272,9 @@ abstract class FilamentController {
FilamentEntity entity, MorphAnimationData animation);
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
/// [morphWeights] is a list of doubles in frame-major format.
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
/// for now we only allow animating a single bone (though multiple skinned targets are supported)
/// Starts animating a bone (joint) according to the specified [animation].
///
Future setBoneAnimation(FilamentEntity entity, BoneAnimationData animation);
Future addBoneAnimation(FilamentEntity entity, BoneAnimationData animation);
///
/// Sets the local joint transform for the bone at the given index in [entity] for the mesh under [meshName].

View File

@@ -691,41 +691,40 @@ class FilamentControllerFFI extends FilamentController {
}
@override
Future setBoneAnimation(
Future addBoneAnimation(
FilamentEntity entity, BoneAnimationData animation) async {
if (_viewer == null) {
throw Exception("No viewer available, ignoring");
}
// var data = calloc<Float>(animation.frameData.length);
// int offset = 0;
// var numFrames = animation.frameData.length ~/ 7;
// var boneNames = calloc<Pointer<Char>>(1);
// boneNames.elementAt(0).value =
// animation.boneName.toNativeUtf8().cast<Char>();
// var meshNames = calloc<Pointer<Char>>(animation.meshNames.length);
// for (int i = 0; i < animation.meshNames.length; i++) {
// meshNames.elementAt(i).value =
// animation.meshNames[i].toNativeUtf8().cast<Char>();
// }
var numFrames = animation.frameData.length;
// for (int i = 0; i < animation.frameData.length; i++) {
// data.elementAt(offset).value = animation.frameData[i];
// offset += 1;
// }
var meshNames = calloc<Pointer<Char>>(animation.meshNames.length);
for (int i = 0; i < animation.meshNames.length; i++) {
meshNames.elementAt(i).value =
animation.meshNames[i].toNativeUtf8().cast<Char>();
}
// await _channel.invokeMethod("setBoneAnimation", [
// _assetManager!,
// asset,
// data,
// numFrames,
// 1,
// boneNames,
// meshNames,
// animation.meshNames.length,
// animation.frameLengthInMs
// ]);
// calloc.free(data);
var data = calloc<Float>(numFrames * 4);
for (int i = 0; i < numFrames; i++) {
data.elementAt(i * 4).value = animation.frameData[i].w;
data.elementAt((i * 4) + 1).value = animation.frameData[i].x;
data.elementAt((i * 4) + 2).value = animation.frameData[i].y;
data.elementAt((i * 4) + 3).value = animation.frameData[i].z;
}
add_bone_animation(
_assetManager!,
entity,
data,
numFrames,
animation.boneName.toNativeUtf8().cast<Char>(),
meshNames,
animation.meshNames.length,
animation.frameLengthInMs);
calloc.free(data);
calloc.free(meshNames);
}
@override

View File

@@ -370,20 +370,18 @@ external bool set_morph_animation(
EntityId,
ffi.Pointer<ffi.Float>,
ffi.Int,
ffi.Int,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Int,
ffi.Float)>(
symbol: 'set_bone_animation', assetId: 'flutter_filament_plugin')
external void set_bone_animation(
symbol: 'add_bone_animation', assetId: 'flutter_filament_plugin')
external void add_bone_animation(
ffi.Pointer<ffi.Void> assetManager,
int asset,
ffi.Pointer<ffi.Float> frameData,
int numFrames,
int numBones,
ffi.Pointer<ffi.Pointer<ffi.Char>> boneNames,
ffi.Pointer<ffi.Pointer<ffi.Char>> meshName,
ffi.Pointer<ffi.Char> boneName,
ffi.Pointer<ffi.Pointer<ffi.Char>> meshNames,
int numMeshTargets,
double frameLengthInMs,
);