add manual bone transform animation

This commit is contained in:
Nick Fisher
2022-12-15 16:56:57 +08:00
parent eb705347fc
commit f50b5328db
18 changed files with 1422 additions and 500 deletions

View File

@@ -0,0 +1,21 @@
// boneMatrices[1] = math::mat4f(1.0, 0.0, 0.0, 0.0, 0.0, 0.26, -0.97, 0.0, 0.0, 0.97, 0.26, 0.0, 0.0, 0.0, 0.0, 1.0);
// Log("Asset instance count : %d asset entity count %d, skin count %d skin name %s joint count @ 0 %d ",
// _asset->getAssetInstanceCount(),
// _asset->getEntityCount(),
// ,
// filamentInstance->getSkinNameAt(0), filamentInstance->getJointCountAt(0));
// for(int i =0; i < numJoints; i++) {
// auto nameInst = _ncm->getInstance(joints[i]);
// const char* jointName = _ncm->getName(nameInst);
// if(strcmp(jointName, boneName) == 0) {
// Log("Set transform for joint %s", jointName);
// // auto boneInst = rm.getInstance(joints[i]);
// }
// }
// bool transformed = false;
// if(!transformed) {
// Log("Failed to find bone %s", boneName);
// }
// Entity root = _asset->getRoot();

View File

@@ -0,0 +1,149 @@
import 'animations.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
class Animation {
final Float32List morphWeights;
final int numMorphWeights;
final int numFrames;
final double frameLengthInMs;
final List<String> boneNames;
final List<String> meshNames;
final Float32List boneTransforms;
Animation(this.morphWeights, this.numMorphWeights, this.boneTransforms,
this.boneNames, this.meshNames, this.numFrames, this.frameLengthInMs);
}
class AnimationBuilder {
BoneAnimation? boneAnimation;
double _frameLengthInMs = 0;
double _duration = 0;
int _numMorphWeights = 0;
double? _interpMorphStart;
double? _interpMorphEnd;
double? _interpMorphStartValue;
double? _interpMorphEndValue;
final List<String> _boneNames = [];
final List<String> _meshNames = [];
final List<BoneTransform> _boneTransforms = [];
Animation build() {
if (_numMorphWeights == 0 || _duration == 0 || _frameLengthInMs == 0)
throw Exception();
int numFrames = _duration * 1000 ~/ _frameLengthInMs;
final _morphWeights = Float32List((numFrames * _numMorphWeights).toInt());
var frameStart = (_interpMorphStart! * 1000) ~/ _frameLengthInMs;
var frameEnd = (_interpMorphEnd! * 1000) ~/ _frameLengthInMs;
for (int i = frameStart; i < frameEnd; i++) {
var linear = (i - frameStart) / frameEnd;
var val = ((1 - linear) * _interpMorphStartValue!) +
(linear * _interpMorphEndValue!);
for (int j = 0; j < _numMorphWeights; j++) {
_morphWeights[(i * _numMorphWeights) + j] = val;
}
}
print(
"Created morphWeights of size ${_morphWeights.length} (${_morphWeights.lengthInBytes} for ${numFrames} frames");
final boneTransforms = Float32List(numFrames * _boneTransforms.length * 7);
print(
"Creating bone transforms of size ${numFrames * _boneTransforms.length * 7}");
for (int i = 0; i < numFrames; i++) {
for (int j = 0; j < _boneTransforms.length; j++) {
var frameData = _boneTransforms[j].getFrameData(i).toList();
var rngStart = ((i * _boneTransforms.length) + j) * 7;
var rngEnd = rngStart + 7;
boneTransforms.setRange(rngStart, rngEnd, frameData);
}
print(
"frameData for frame $i ${boneTransforms.sublist(i * _boneTransforms.length * 7, (i * _boneTransforms.length * 7) + 7)}");
}
return Animation(_morphWeights, _numMorphWeights, boneTransforms,
_boneNames, _meshNames, numFrames, _frameLengthInMs);
}
AnimationBuilder setFramerate(int framerate) {
_frameLengthInMs = 1000 / framerate;
return this;
}
AnimationBuilder setDuration(double secs) {
_duration = secs;
return this;
}
AnimationBuilder setNumMorphWeights(int numMorphWeights) {
_numMorphWeights = numMorphWeights;
return this;
}
AnimationBuilder interpolateMorphWeights(
double start, double end, double startValue, double endValue) {
this._interpMorphStart = start;
this._interpMorphEnd = end;
this._interpMorphStartValue = startValue;
this._interpMorphEndValue = endValue;
return this;
}
AnimationBuilder interpolateBoneTransform(
String boneName,
String meshName,
double start,
double end,
Vec3 transStart,
Vec3 transEnd,
Quaternion quatStart,
Quaternion quatEnd) {
var translations = <Vec3>[];
var quats = <Quaternion>[];
var frameStart = (start * 1000) ~/ _frameLengthInMs;
var frameEnd = (end * 1000) ~/ _frameLengthInMs;
int numFrames = _duration * 1000 ~/ _frameLengthInMs;
if (frameEnd > numFrames) {
throw Exception();
}
for (int i = 0; i < numFrames; i++) {
if (i >= frameStart && i < frameEnd) {
var linear = (i - frameStart) / (frameEnd - frameStart);
translations.add(Vec3(
x: ((1 - linear) * transStart.x) + (linear * transEnd.x),
y: ((1 - linear) * transStart.y) + (linear * transEnd.y),
z: ((1 - linear) * transStart.z) + (linear * transEnd.z),
));
quats.add(Quaternion(
x: ((1 - linear) * quatStart.x) + (linear * quatEnd.x),
y: ((1 - linear) * quatStart.y) + (linear * quatEnd.y),
z: ((1 - linear) * quatStart.z) + (linear * quatEnd.z),
w: ((1 - linear) * quatStart.w) + (linear * quatEnd.w),
));
} else {
translations.add(Vec3());
quats.add(Quaternion());
}
}
_boneTransforms.add(BoneTransform(translations, quats));
_boneNames.add(boneName);
_meshNames.add(meshName);
return this;
}
}

View File

@@ -0,0 +1,69 @@
class Vec3 {
final double x;
final double y;
final double z;
Vec3({this.x = 0, this.y = 0, this.z = 0});
}
class Quaternion {
double x = 0;
double y = 0;
double z = 0;
double w = 1;
Quaternion({this.x = 0, this.y = 0, this.z = 0, this.w = 1.0});
}
class BoneTransform {
final List<Vec3> translations;
final List<Quaternion> quaternions;
///
/// The length of [translations] and [quaternions] must be the same;
/// each entry represents the Vec3/Quaternion for the given frame.
///
BoneTransform(this.translations, this.quaternions) {
if (translations.length != quaternions.length) {
throw Exception("Length of translation/quaternion frames must match");
}
// for (int i = 0; i < quaternions.length; i++) {
// _frameData.add(translations[i].x);
// _frameData.add(translations[i].y);
// _frameData.add(translations[i].z);
// _frameData.add(quaternions[i].x);
// _frameData.add(quaternions[i].y);
// _frameData.add(quaternions[i].z);
// _frameData.add(quaternions[i].w);
// }
}
Iterable<double> getFrameData(int frame) sync* {
yield translations[frame].x;
yield translations[frame].y;
yield translations[frame].z;
yield quaternions[frame].x;
yield quaternions[frame].y;
yield quaternions[frame].z;
yield quaternions[frame].w;
}
}
class BoneAnimation {
final List<BoneTransform> boneTransforms;
final List<String> boneNames;
final List<String> meshNames;
final int numFrames;
BoneAnimation(
this.boneTransforms, this.boneNames, this.meshNames, this.numFrames);
Iterable<double> toFrameData() sync* {
for (int i = 0; i < numFrames; i++) {
for (int j = 0; j < boneTransforms.length; j++) {
yield* boneTransforms[j].getFrameData(i);
}
}
}
}

View File

@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:ui';
import 'animations/animation_builder.dart';
import 'animations/animations.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
@@ -60,8 +62,9 @@ abstract class FilamentController {
Future rotateStart(double x, double y);
Future rotateUpdate(double x, double y);
Future rotateEnd();
Future applyWeights(FilamentAsset asset, List<double> weights);
Future<List<String>> getTargetNames(FilamentAsset asset, String meshName);
Future setMorphTargetWeights(FilamentAsset asset, List<double> weights);
Future<List<String>> getMorphTargetNames(
FilamentAsset asset, String meshName);
Future<List<String>> getAnimationNames(FilamentAsset asset);
Future removeAsset(FilamentAsset asset);
Future clearAssets();
@@ -77,6 +80,8 @@ abstract class FilamentController {
Future setPosition(FilamentAsset asset, double x, double y, double z);
Future setRotation(
FilamentAsset asset, double rads, double x, double y, double z);
Future setBoneTransform(FilamentAsset asset, String boneName, String meshName,
BoneTransform transform);
Future setScale(FilamentAsset asset, double scale);
Future setCameraFocalLength(double focalLength);
Future setCameraFocusDistance(double focusDistance);
@@ -84,13 +89,11 @@ abstract class FilamentController {
Future setCameraRotation(double rads, double x, double y, double z);
///
/// Set the weights of all morph targets in the mesh to the specified weights at successive frames (where each frame requires a duration of [frameLengthInMs].
/// Accepts a list of doubles representing a sequence of "frames", stacked end-to-end.
/// Each frame is [numWeights] in length, where each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
/// In other words, weights is a contiguous sequence of floats of size W*F, where W is the number of weights and F is the number of frames
/// 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.
///
Future animate(FilamentAsset asset, List<double> data, int numWeights,
int numFrames, double frameLengthInMs);
Future setAnimation(FilamentAsset asset, Animation animation);
}
class PolyvoxFilamentController extends FilamentController {
@@ -130,6 +133,7 @@ class PolyvoxFilamentController extends FilamentController {
}
void setPixelRatio(double ratio) {
print("Set pixel ratio to $ratio");
_pixelRatio = ratio;
}
@@ -258,15 +262,16 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("rotateEnd");
}
Future applyWeights(FilamentAsset asset, List<double> weights) async {
await _channel
.invokeMethod("applyWeights", [asset, Float32List.fromList(weights)]);
Future setMorphTargetWeights(
FilamentAsset asset, List<double> weights) async {
await _channel.invokeMethod(
"setMorphTargetWeights", [asset, Float32List.fromList(weights)]);
}
Future<List<String>> getTargetNames(
Future<List<String>> getMorphTargetNames(
FilamentAsset asset, String meshName) async {
var result =
(await _channel.invokeMethod("getTargetNames", [asset, meshName]))
(await _channel.invokeMethod("getMorphTargetNames", [asset, meshName]))
.cast<String>();
return result;
}
@@ -277,14 +282,16 @@ class PolyvoxFilamentController extends FilamentController {
return result;
}
Future animate(FilamentAsset asset, List<double> weights, int numWeights,
int numFrames, double frameLengthInMs) async {
await _channel.invokeMethod("animateWeights", [
Future setAnimation(FilamentAsset asset, Animation animation) async {
await _channel.invokeMethod("setAnimation", [
asset,
Float32List.fromList(weights),
numWeights,
numFrames,
frameLengthInMs
animation.morphWeights,
animation.numMorphWeights,
animation.boneTransforms,
animation.boneNames,
animation.meshNames,
animation.numFrames,
animation.frameLengthInMs
]);
}
@@ -360,6 +367,22 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("setPosition", [asset, x, y, z]);
}
Future setBoneTransform(FilamentAsset asset, String boneName, String meshName,
BoneTransform transform) async {
await _channel.invokeMethod("setBoneTransform", [
asset,
boneName,
meshName,
transform.translations[0].x,
transform.translations[0].y,
transform.translations[0].z,
transform.quaternions[0].x,
transform.quaternions[0].y,
transform.quaternions[0].z,
transform.quaternions[0].w
]);
}
Future setScale(FilamentAsset asset, double scale) async {
await _channel.invokeMethod("setScale", [asset, scale]);
}

View File

@@ -96,7 +96,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
}
var texture = Texture(
textureId: widget.controller.textureId,
filterQuality: FilterQuality.none,
filterQuality: FilterQuality.high,
);
return ResizeObserver(
onResized: (Size oldSize, Size newSize) async {