add manual bone transform animation
This commit is contained in:
21
lib/animations/Untitled-1.cpp
Normal file
21
lib/animations/Untitled-1.cpp
Normal 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();
|
||||
149
lib/animations/animation_builder.dart
Normal file
149
lib/animations/animation_builder.dart
Normal 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;
|
||||
}
|
||||
}
|
||||
69
lib/animations/animations.dart
Normal file
69
lib/animations/animations.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user