fix dynamic bone animations

This commit is contained in:
Nick Fisher
2023-04-27 16:32:32 +08:00
parent d1e15b53c5
commit 62c4be0563
16 changed files with 538 additions and 520 deletions

View File

@@ -7,7 +7,7 @@ import 'package:vector_math/vector_math.dart';
class AnimationBuilder {
final FilamentController controller;
BoneAnimationData? BoneAnimationData;
// BoneAnimationData? BoneAnimationData;
double _frameLengthInMs = 0;
double _duration = 0;
@@ -16,7 +16,7 @@ class AnimationBuilder {
double? _interpMorphStartValue;
double? _interpMorphEndValue;
List<BoneAnimationData>? _BoneAnimationDatas = null;
// List<BoneAnimationData>? _BoneAnimationDatas = null;
FilamentEntity asset;
String meshName;

View File

@@ -5,6 +5,7 @@ import 'package:vector_math/vector_math.dart';
/// Model class for bone animation frame data.
/// To create dynamic/runtime bone animations (as distinct from animations embedded in a glTF asset), create an instance containing the relevant
/// data and pass to the [setBoneAnimation] method on a [FilamentController].
/// [frameData] is laid out as [locX, locY, locZ, rotW, rotX, rotY, rotZ]
///
class BoneAnimationData {
final String boneName;

View File

@@ -13,45 +13,64 @@ import 'package:vector_math/vector_math.dart';
/// 4) min/max rotation values (corresponding to -1/1 on the blendshape)
///
class Transformation {
final Quaternion rotation;
late final Vector3 translation;
Transformation(this.rotation, {Vector3? translation}) {
this.translation = translation ?? Vector3.zero();
}
}
class BoneDriver {
final String bone;
final String blendshape;
final Map<String, Transformation>
transformations; // maps a blendshape key to a Transformation
late final Vector3 transMin;
late final Vector3 transMax;
late final Quaternion rotMin;
late final Quaternion rotMax;
BoneDriver(this.bone, this.blendshape, this.rotMin, this.rotMax,
Vector3? transMin, Vector3? transMax) {
this.transMin = transMin ?? Vector3.zero();
this.transMax = transMax ?? Vector3.zero();
}
factory BoneDriver.fromJsonObject(dynamic jsonObject) {
return BoneDriver(
jsonObject["bone"],
jsonObject["blendshape"],
Quaternion.fromFloat32List(Float32List.fromList(jsonObject["rotMin"])),
Quaternion.fromFloat32List(Float32List.fromList(jsonObject["rotMax"])),
Vector3.fromFloat32List(Float32List.fromList(jsonObject["transMin"])),
Vector3.fromFloat32List(Float32List.fromList(jsonObject["transMax"])),
);
}
BoneDriver(this.bone, this.transformations);
//
// Accepts a Float32List containing [numFrames] frames of data for a single morph target weight (for efficiency, this must be unravelled to a single contiguous Float32List).
// Returns a generator that yields [numFrames] Quaternions, each representing the (weighted) rotation/translation specified by the mapping of this BoneDriver.
//
Iterable<Quaternion> transform(List<double> morphTargetFrameData) sync* {
for (int i = 0; i < morphTargetFrameData.length; i++) {
var weight = (morphTargetFrameData[i] / 2) + 0.5;
Iterable<Quaternion> transform(
Map<String, List<double>> morphTargetFrameData) sync* {
assert(setEquals(
morphTargetFrameData.keys.toSet(), transformations.keys.toSet()));
var numFrames = morphTargetFrameData.values.first.length;
assert(morphTargetFrameData.values.every((x) => x.length == numFrames));
for (int frameNum = 0; frameNum < numFrames; frameNum++) {
var rotations = transformations.keys.map((blendshape) {
var weight = morphTargetFrameData[blendshape]![frameNum];
var rotation = transformations[blendshape]!.rotation.clone();
rotation.x *= weight;
rotation.y *= weight;
rotation.z *= weight;
return rotation;
}).toList();
yield Quaternion(
rotMin.x + (weight * (rotMax.x - rotMin.x)),
rotMin.y + (weight * (rotMax.y - rotMin.y)),
rotMin.z + (weight * (rotMax.z - rotMin.z)),
1.0);
yield rotations.fold(
rotations.first, (Quaternion a, Quaternion b) => a * b);
// todo - bone translations
}
}
factory BoneDriver.fromJsonObject(dynamic jsonObject) {
throw Exception("TODO");
// return BoneDriver(
// jsonObject["bone"],
// Map<String,Transformation>.fromIterable(jsonObject["blendshape"].map((bsName, quats) {
// var q = quats.map(())
// MapEntry(k,
}
}
// }
// yield Quaternion(
// rotMin.x + (weight * (rotMax.x - rotMin.x)),
// rotMin.y + (weight * (rotMax.y - rotMin.y)),
// rotMin.z + (weight * (rotMax.z - rotMin.z)),
// 1.0);

View File

@@ -15,40 +15,52 @@ class DynamicAnimation {
final List<BoneAnimationData> boneAnimation;
factory DynamicAnimation.load(String meshName, String csvPath,
{String? boneDriverConfigPath}) {
{List<BoneDriver>? boneDrivers,
String? boneDriverConfigPath,
double? framerate}) {
// create a MorphAnimationData instance from the given CSV
var llf = _loadLiveLinkFaceCSV(csvPath);
var frameLengthInMs = 1000 / (framerate ?? 60.0);
var morphNames = llf
.item1; //.where((name) => !boneDrivers.any((element) => element.blendshape == name));
var morphAnimationData = MorphAnimationData(
meshName,
llf.item2,
morphNames,
1000 / 60.0,
);
var morphAnimationData =
MorphAnimationData(meshName, llf.item2, morphNames, frameLengthInMs);
final boneAnimations = <BoneAnimationData>[];
// if applicable, load the bone driver config
if (boneDriverConfigPath != null) {
var boneData = json.decode(File(boneDriverConfigPath).readAsStringSync());
// for each driver
for (var key in boneData.keys()) {
var driver = BoneDriver.fromJsonObject(boneData[key]);
if (boneDrivers != null) {
throw Exception(
"Specify either boneDrivers, or the config path, not both");
}
boneDrivers = [
json
.decode(File(boneDriverConfigPath).readAsStringSync())
.map(BoneDriver.fromJsonObject)
.toList()
];
}
// iterate over every bone driver
if (boneDrivers != null) {
for (var driver in boneDrivers) {
// get all frames for the single the blendshape
var morphFrameData =
morphAnimationData.getData(driver.blendshape).toList();
var morphData = driver.transformations
.map((String blendshape, Transformation transformation) {
return MapEntry(
blendshape, morphAnimationData.getData(blendshape).toList());
});
// apply the driver to the blendshape weight
var transformedQ = driver.transform(morphFrameData).toList();
var transformedQ = driver.transform(morphData).toList();
// transform the quaternion to a Float32List
var transformedF = _quaternionToFloatList(transformedQ);
// add to the list of boneAnimations
boneAnimations.add(BoneAnimationData(
driver.bone, meshName, transformedF, 1000.0 / 60.0));
driver.bone, meshName, transformedF, frameLengthInMs));
}
}
@@ -56,9 +68,11 @@ class DynamicAnimation {
}
static Float32List _quaternionToFloatList(List<Quaternion> quats) {
var data = Float32List(quats.length * 4);
var data = Float32List(quats.length * 7);
int i = 0;
for (var quat in quats) {
data.addAll([0, 0, 0, quat.w, quat.x, quat.y, quat.z]);
data.setRange(i, i + 7, [0, 0, 0, quat.w, quat.x, quat.y, quat.z]);
i += 7;
}
return data;
}
@@ -86,7 +100,7 @@ class DynamicAnimation {
// CSVs may contain rows where the "BlendShapeCount" column is set to "0" and/or the weight columns are simply missing.
// This can happen when something went wrong while recording via an app (e.g. LiveLinkFace)
// Whenever we encounter this type of row, we consider that all weights should be set to zero for that frame.
if (numFrameWeights == int.parse(frame[1])) {
if (numFrameWeights != int.parse(frame[1])) {
_data.addAll(List<double>.filled(numBlendShapes, 0.0));
continue;
}

View File

@@ -0,0 +1,12 @@
import 'dart:math';
import 'package:polyvox_filament/animations/bone_driver.dart';
import 'package:vector_math/vector_math.dart';
BoneDriver getLiveLinkFaceBoneDrivers(String bone) {
return BoneDriver(bone, {
"HeadPitch": Transformation(Quaternion.axisAngle(Vector3(1, 0, 0), pi / 2)),
"HeadRoll": Transformation(Quaternion.axisAngle(Vector3(0, 0, 1), pi / 2)),
"HeadYaw": Transformation(Quaternion.axisAngle(Vector3(0, 1, 0), pi / 2)),
});
}

View File

@@ -24,8 +24,9 @@ class MorphAnimationData {
final double frameLengthInMs;
Iterable<double> getData(String morphName) sync* {
int index = morphNames.indexOf(morphName);
for (int i = 0; i < numFrames; i++) {
yield data[i * numMorphWeights];
yield data[(i * numMorphWeights) + index];
}
}
}

View File

@@ -323,15 +323,19 @@ class FilamentController {
///
void setBoneAnimation(
FilamentEntity asset, List<BoneAnimationData> animations) async {
// for future compatibility, instances of BoneAnimationData can specify individual mesh targets
// however on the rendering side we currently only allow one set of frame data for one mesh target (though multiple bones are supported).
// this is a check that all animations are targeting the same mesh
assert(animations.map((e) => e.meshName).toSet().length == 1);
var data =
calloc<Float>(animations.length * animations.first.frameData.length);
int offset = 0;
var numFrames = animations.first.frameData.length;
var meshNames = calloc<Pointer<Char>>(animations.length);
var numFrames = animations.first.frameData.length ~/ 7;
var boneNames = calloc<Pointer<Char>>(animations.length);
int animIdx = 0;
for (var animation in animations) {
if (animation.frameData.length != numFrames) {
if (animation.frameData.length ~/ 7 != numFrames) {
throw Exception(
"All bone animations must share the same animation frame data length.");
}
@@ -339,20 +343,19 @@ class FilamentController {
data.elementAt(offset).value = animation.frameData[i];
offset += 1;
}
meshNames.elementAt(animIdx).value =
animation.meshName.toNativeUtf8().cast<Char>();
boneNames.elementAt(animIdx).value =
animation.boneName.toNativeUtf8().cast<Char>();
animIdx++;
}
_nativeLibrary.set_bone_animation(
_assetManager,
asset,
animations.length,
boneNames,
meshNames,
data,
numFrames,
animations.length,
boneNames,
animations.first.meshName.toNativeUtf8().cast<Char>(),
animations.first.frameLengthInMs);
calloc.free(data);
}
@@ -379,7 +382,6 @@ class FilamentController {
void playAnimation(FilamentEntity asset, int index,
{bool loop = false, bool reverse = false}) async {
print("LOOP $loop");
_nativeLibrary.play_animation(
_assetManager, asset, index, loop ? 1 : 0, reverse ? 1 : 0);
}

View File

@@ -655,21 +655,21 @@ class NativeLibrary {
void set_bone_animation(
ffi.Pointer<ffi.Void> assetManager,
int asset,
int length,
ffi.Pointer<ffi.Pointer<ffi.Char>> boneNames,
ffi.Pointer<ffi.Pointer<ffi.Char>> meshNames,
ffi.Pointer<ffi.Float> frameData,
int numFrames,
int numBones,
ffi.Pointer<ffi.Pointer<ffi.Char>> boneNames,
ffi.Pointer<ffi.Char> meshName,
double frameLengthInMs,
) {
return _set_bone_animation(
assetManager,
asset,
length,
boneNames,
meshNames,
frameData,
numFrames,
numBones,
boneNames,
meshName,
frameLengthInMs,
);
}
@@ -679,21 +679,21 @@ class NativeLibrary {
ffi.Void Function(
ffi.Pointer<ffi.Void>,
EntityId,
ffi.Int,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Float>,
ffi.Int,
ffi.Int,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Char>,
ffi.Float)>>('set_bone_animation');
late final _set_bone_animation = _set_bone_animationPtr.asFunction<
void Function(
ffi.Pointer<ffi.Void>,
int,
int,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Float>,
int,
int,
ffi.Pointer<ffi.Pointer<ffi.Char>>,
ffi.Pointer<ffi.Char>,
double)>();
void play_animation(
@@ -1122,96 +1122,9 @@ class NativeLibrary {
late final _ios_dummy = _ios_dummyPtr.asFunction<void Function()>();
}
class __mbstate_t extends ffi.Union {
@ffi.Array.multi([128])
external ffi.Array<ffi.Char> __mbstate8;
@ffi.LongLong()
external int _mbstateL;
}
class __darwin_pthread_handler_rec extends ffi.Struct {
external ffi
.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>
__routine;
external ffi.Pointer<ffi.Void> __arg;
external ffi.Pointer<__darwin_pthread_handler_rec> __next;
}
class _opaque_pthread_attr_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([56])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_cond_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([40])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_condattr_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([8])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_mutex_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([56])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_mutexattr_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([8])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_once_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([8])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_rwlock_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([192])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_rwlockattr_t extends ffi.Struct {
@ffi.Long()
external int __sig;
@ffi.Array.multi([16])
external ffi.Array<ffi.Char> __opaque;
}
class _opaque_pthread_t extends ffi.Struct {
@ffi.Long()
external int __sig;
external ffi.Pointer<__darwin_pthread_handler_rec> __cleanup_stack;
@ffi.Array.multi([8176])
external ffi.Array<ffi.Char> __opaque;
class __fsid_t extends ffi.Struct {
@ffi.Array.multi([2])
external ffi.Array<ffi.Int> __val;
}
class ResourceBuffer extends ffi.Struct {
@@ -1225,8 +1138,6 @@ class ResourceBuffer extends ffi.Struct {
}
class ResourceLoaderWrapper extends ffi.Struct {
external ffi.Pointer<ffi.Void> mOwner;
external LoadResource mLoadResource;
external FreeResource mFreeResource;
@@ -1234,6 +1145,8 @@ class ResourceLoaderWrapper extends ffi.Struct {
external LoadResourceFromOwner mLoadResourceFromOwner;
external FreeResourceFromOwner mFreeResourceFromOwner;
external ffi.Pointer<ffi.Void> mOwner;
}
typedef LoadResource = ffi.Pointer<
@@ -1248,75 +1161,127 @@ typedef FreeResourceFromOwner = ffi.Pointer<
ffi.Void Function(ResourceBuffer, ffi.Pointer<ffi.Void>)>>;
typedef EntityId = ffi.Int32;
const int _STDINT_H = 1;
const int _FEATURES_H = 1;
const int _DEFAULT_SOURCE = 1;
const int __GLIBC_USE_ISOC2X = 1;
const int __USE_ISOC11 = 1;
const int __USE_ISOC99 = 1;
const int __USE_ISOC95 = 1;
const int _POSIX_SOURCE = 1;
const int _POSIX_C_SOURCE = 200809;
const int __USE_POSIX = 1;
const int __USE_POSIX2 = 1;
const int __USE_POSIX199309 = 1;
const int __USE_POSIX199506 = 1;
const int __USE_XOPEN2K = 1;
const int __USE_XOPEN2K8 = 1;
const int _ATFILE_SOURCE = 1;
const int __WORDSIZE = 64;
const int __DARWIN_ONLY_64_BIT_INO_T = 1;
const int __WORDSIZE_TIME64_COMPAT32 = 1;
const int __DARWIN_ONLY_UNIX_CONFORMANCE = 1;
const int __SYSCALL_WORDSIZE = 64;
const int __DARWIN_ONLY_VERS_1050 = 1;
const int __TIMESIZE = 64;
const int __DARWIN_UNIX03 = 1;
const int __USE_MISC = 1;
const int __DARWIN_64_BIT_INO_T = 1;
const int __USE_ATFILE = 1;
const int __DARWIN_VERS_1050 = 1;
const int __USE_FORTIFY_LEVEL = 0;
const int __DARWIN_NON_CANCELABLE = 0;
const int __GLIBC_USE_DEPRECATED_GETS = 0;
const String __DARWIN_SUF_EXTSN = '\$DARWIN_EXTSN';
const int __GLIBC_USE_DEPRECATED_SCANF = 0;
const int __DARWIN_C_ANSI = 4096;
const int _STDC_PREDEF_H = 1;
const int __DARWIN_C_FULL = 900000;
const int __STDC_IEC_559__ = 1;
const int __DARWIN_C_LEVEL = 900000;
const int __STDC_IEC_60559_BFP__ = 201404;
const int __STDC_WANT_LIB_EXT1__ = 1;
const int __STDC_IEC_559_COMPLEX__ = 1;
const int __DARWIN_NO_LONG_LONG = 0;
const int __STDC_IEC_60559_COMPLEX__ = 201404;
const int _DARWIN_FEATURE_64_BIT_INODE = 1;
const int __STDC_ISO_10646__ = 201706;
const int _DARWIN_FEATURE_ONLY_64_BIT_INODE = 1;
const int __GNU_LIBRARY__ = 6;
const int _DARWIN_FEATURE_ONLY_VERS_1050 = 1;
const int __GLIBC__ = 2;
const int _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE = 1;
const int __GLIBC_MINOR__ = 37;
const int _DARWIN_FEATURE_UNIX_CONFORMANCE = 3;
const int _SYS_CDEFS_H = 1;
const int __has_ptrcheck = 0;
const int __THROW = 1;
const int __DARWIN_NULL = 0;
const int __THROWNL = 1;
const int __PTHREAD_SIZE__ = 8176;
const int __glibc_c99_flexarr_available = 1;
const int __PTHREAD_ATTR_SIZE__ = 56;
const int __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI = 0;
const int __PTHREAD_MUTEXATTR_SIZE__ = 8;
const int __HAVE_GENERIC_SELECTION = 0;
const int __PTHREAD_MUTEX_SIZE__ = 56;
const int __GLIBC_USE_LIB_EXT2 = 1;
const int __PTHREAD_CONDATTR_SIZE__ = 8;
const int __GLIBC_USE_IEC_60559_BFP_EXT = 1;
const int __PTHREAD_COND_SIZE__ = 40;
const int __GLIBC_USE_IEC_60559_BFP_EXT_C2X = 1;
const int __PTHREAD_ONCE_SIZE__ = 8;
const int __GLIBC_USE_IEC_60559_EXT = 1;
const int __PTHREAD_RWLOCK_SIZE__ = 192;
const int __GLIBC_USE_IEC_60559_FUNCS_EXT = 1;
const int __PTHREAD_RWLOCKATTR_SIZE__ = 16;
const int __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X = 1;
const int USER_ADDR_NULL = 0;
const int __GLIBC_USE_IEC_60559_TYPES_EXT = 1;
const int INT8_MAX = 127;
const int _BITS_TYPES_H = 1;
const int INT16_MAX = 32767;
const int _BITS_TYPESIZES_H = 1;
const int INT32_MAX = 2147483647;
const int __OFF_T_MATCHES_OFF64_T = 1;
const int INT64_MAX = 9223372036854775807;
const int __INO_T_MATCHES_INO64_T = 1;
const int __RLIM_T_MATCHES_RLIM64_T = 1;
const int __STATFS_MATCHES_STATFS64 = 1;
const int __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64 = 1;
const int __FD_SETSIZE = 1024;
const int _BITS_TIME64_H = 1;
const int _BITS_WCHAR_H = 1;
const int __WCHAR_MAX = 2147483647;
const int __WCHAR_MIN = -2147483648;
const int _BITS_STDINT_INTN_H = 1;
const int _BITS_STDINT_UINTN_H = 1;
const int INT8_MIN = -128;
@@ -1326,6 +1291,14 @@ const int INT32_MIN = -2147483648;
const int INT64_MIN = -9223372036854775808;
const int INT8_MAX = 127;
const int INT16_MAX = 32767;
const int INT32_MAX = 2147483647;
const int INT64_MAX = 9223372036854775807;
const int UINT8_MAX = 255;
const int UINT16_MAX = 65535;
@@ -1360,66 +1333,54 @@ const int UINT_LEAST64_MAX = -1;
const int INT_FAST8_MIN = -128;
const int INT_FAST16_MIN = -32768;
const int INT_FAST16_MIN = -9223372036854775808;
const int INT_FAST32_MIN = -2147483648;
const int INT_FAST32_MIN = -9223372036854775808;
const int INT_FAST64_MIN = -9223372036854775808;
const int INT_FAST8_MAX = 127;
const int INT_FAST16_MAX = 32767;
const int INT_FAST16_MAX = 9223372036854775807;
const int INT_FAST32_MAX = 2147483647;
const int INT_FAST32_MAX = 9223372036854775807;
const int INT_FAST64_MAX = 9223372036854775807;
const int UINT_FAST8_MAX = 255;
const int UINT_FAST16_MAX = 65535;
const int UINT_FAST16_MAX = -1;
const int UINT_FAST32_MAX = 4294967295;
const int UINT_FAST32_MAX = -1;
const int UINT_FAST64_MAX = -1;
const int INTPTR_MAX = 9223372036854775807;
const int INTPTR_MIN = -9223372036854775808;
const int INTPTR_MAX = 9223372036854775807;
const int UINTPTR_MAX = -1;
const int INTMAX_MIN = -9223372036854775808;
const int INTMAX_MAX = 9223372036854775807;
const int UINTMAX_MAX = -1;
const int INTMAX_MIN = -9223372036854775808;
const int PTRDIFF_MIN = -9223372036854775808;
const int PTRDIFF_MAX = 9223372036854775807;
const int SIZE_MAX = -1;
const int RSIZE_MAX = 9223372036854775807;
const int WCHAR_MAX = 2147483647;
const int WCHAR_MIN = -2147483648;
const int WINT_MIN = -2147483648;
const int WINT_MAX = 2147483647;
const int SIG_ATOMIC_MIN = -2147483648;
const int SIG_ATOMIC_MAX = 2147483647;
const int __DARWIN_WCHAR_MAX = 2147483647;
const int SIZE_MAX = -1;
const int __DARWIN_WCHAR_MIN = -2147483648;
const int WCHAR_MIN = -2147483648;
const int __DARWIN_WEOF = -1;
const int WCHAR_MAX = 2147483647;
const int _FORTIFY_SOURCE = 2;
const int WINT_MIN = 0;
const int NULL = 0;
const int WINT_MAX = 4294967295;