update to animation structures

This commit is contained in:
Nick Fisher
2022-12-15 23:15:22 +08:00
parent 31044e8d15
commit 9f032a8392
5 changed files with 67 additions and 43 deletions

View File

@@ -90,16 +90,16 @@ namespace polyvox {
memcpy(mMorphFrameData, morphData, morphSize); memcpy(mMorphFrameData, morphData, morphSize);
} }
if(numBones > 0) { // if(numBones > 0) {
size_t boneSize = numBones * numFrames * 7 * sizeof(float); // size_t boneSize = numBones * numFrames * 7 * sizeof(float);
mBoneFrameData = (float*)malloc(boneSize); // mBoneFrameData = (float*)malloc(boneSize);
memcpy(mBoneFrameData, boneData, boneSize); // memcpy(mBoneFrameData, boneData, boneSize);
} // }
for(int i =0; i < numBones; i++) { // for(int i =0; i < numBones; i++) {
mBoneNames.push_back(string(boneNames[i])); // mBoneNames.push_back(string(boneNames[i]));
mMeshNames.push_back(string(meshNames[i])); // mMeshNames.push_back(string(meshNames[i]));
} // }
} }
~RuntimeAnimation() { ~RuntimeAnimation() {

View File

@@ -105,29 +105,29 @@ void SceneAsset::updateRuntimeAnimation() {
if (frameIndex > _runtimeAnimationBuffer->frameIndex) { if (frameIndex > _runtimeAnimationBuffer->frameIndex) {
_runtimeAnimationBuffer->frameIndex = frameIndex; _runtimeAnimationBuffer->frameIndex = frameIndex;
if(_runtimeAnimationBuffer->mMorphFrameData) { if(_runtimeAnimationBuffer->mMorphFrameData) {
auto morphFramePtrOffset = frameIndex * _runtimeAnimationBuffer->mNumMorphWeights; // auto morphFramePtrOffset = frameIndex * _runtimeAnimationBuffer->mNumMorphWeights;
setMorphTargetWeights(_runtimeAnimationBuffer->mMorphFrameData + morphFramePtrOffset, // setMorphTargetWeights(_runtimeAnimationBuffer->mMorphFrameData + morphFramePtrOffset,
_runtimeAnimationBuffer->mNumMorphWeights); // _runtimeAnimationBuffer->mNumMorphWeights);
} }
if(_runtimeAnimationBuffer->mBoneFrameData) { // if(_runtimeAnimationBuffer->mBoneFrameData) {
for(int i = 0; i < _runtimeAnimationBuffer->mNumBones; i++) { // for(int i = 0; i < _runtimeAnimationBuffer->mNumBones; i++) {
auto boneFramePtrOffset = (frameIndex * _runtimeAnimationBuffer->mNumBones * 7) + (i*7); // auto boneFramePtrOffset = (frameIndex * _runtimeAnimationBuffer->mNumBones * 7) + (i*7);
const char* boneName = _runtimeAnimationBuffer->mBoneNames[i].c_str(); // const char* boneName = _runtimeAnimationBuffer->mBoneNames[i].c_str();
const char* meshName = _runtimeAnimationBuffer->mMeshNames[i].c_str(); // const char* meshName = _runtimeAnimationBuffer->mMeshNames[i].c_str();
float* frame = _runtimeAnimationBuffer->mBoneFrameData + boneFramePtrOffset; // float* frame = _runtimeAnimationBuffer->mBoneFrameData + boneFramePtrOffset;
float transX = frame[0]; // float transX = frame[0];
float transY = frame[1]; // float transY = frame[1];
float transZ = frame[2]; // float transZ = frame[2];
float quatX = frame[3]; // float quatX = frame[3];
float quatY = frame[4]; // float quatY = frame[4];
float quatZ = frame[5]; // float quatZ = frame[5];
float quatW = frame[6]; // float quatW = frame[6];
setBoneTransform(boneName, meshName, transX, transY, transZ, quatX, quatY, quatZ, quatW); // setBoneTransform(boneName, meshName, transX, transY, transZ, quatX, quatY, quatZ, quatW);
} // }
} // }
} }
} }

View File

@@ -3,19 +3,37 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class Animation { class Animation {
final Float32List morphWeights; late final Float32List? morphData;
final int numMorphWeights; final int numMorphWeights;
final int numFrames; final int numFrames;
final double frameLengthInMs; final double frameLengthInMs;
final List<String> boneNames; final List<String>? boneNames;
final List<String> meshNames; final List<String>? meshNames;
final Float32List boneTransforms; final Float32List? boneTransforms;
Animation(this.morphWeights, this.numMorphWeights, this.boneTransforms, Animation(this.morphData, this.numMorphWeights, this.boneTransforms,
this.boneNames, this.meshNames, this.numFrames, this.frameLengthInMs); this.boneNames, this.meshNames, this.numFrames, this.frameLengthInMs);
Animation.from(
{required List<List<double>> morphData,
required this.numMorphWeights,
this.boneTransforms,
this.boneNames,
this.meshNames,
required this.numFrames,
required this.frameLengthInMs}) {
if (morphData.length != numFrames) {
throw Exception("Mismatched animation data with frame length");
}
this.morphData = Float32List(numMorphWeights * numFrames);
for (int i = 0; i < numFrames; i++) {
this.morphData!.setRange((i * numMorphWeights),
(i * numMorphWeights) + numMorphWeights, morphData[i]);
}
}
} }
class AnimationBuilder { class AnimationBuilder {
@@ -39,7 +57,7 @@ class AnimationBuilder {
int numFrames = _duration * 1000 ~/ _frameLengthInMs; int numFrames = _duration * 1000 ~/ _frameLengthInMs;
final _morphWeights = Float32List((numFrames * _numMorphWeights).toInt()); final morphData = Float32List((numFrames * _numMorphWeights).toInt());
var frameStart = (_interpMorphStart! * 1000) ~/ _frameLengthInMs; var frameStart = (_interpMorphStart! * 1000) ~/ _frameLengthInMs;
var frameEnd = (_interpMorphEnd! * 1000) ~/ _frameLengthInMs; var frameEnd = (_interpMorphEnd! * 1000) ~/ _frameLengthInMs;
@@ -50,12 +68,12 @@ class AnimationBuilder {
var val = ((1 - linear) * _interpMorphStartValue!) + var val = ((1 - linear) * _interpMorphStartValue!) +
(linear * _interpMorphEndValue!); (linear * _interpMorphEndValue!);
for (int j = 0; j < _numMorphWeights; j++) { for (int j = 0; j < _numMorphWeights; j++) {
_morphWeights[(i * _numMorphWeights) + j] = val; morphData[(i * _numMorphWeights) + j] = val;
} }
} }
print( print(
"Created morphWeights of size ${_morphWeights.length} (${_morphWeights.lengthInBytes} for ${numFrames} frames"); "Created morphWeights of size ${morphData.length} (${morphData.lengthInBytes} for ${numFrames} frames");
final boneTransforms = Float32List(numFrames * _boneTransforms.length * 7); final boneTransforms = Float32List(numFrames * _boneTransforms.length * 7);
print( print(
@@ -71,8 +89,8 @@ class AnimationBuilder {
"frameData for frame $i ${boneTransforms.sublist(i * _boneTransforms.length * 7, (i * _boneTransforms.length * 7) + 7)}"); "frameData for frame $i ${boneTransforms.sublist(i * _boneTransforms.length * 7, (i * _boneTransforms.length * 7) + 7)}");
} }
return Animation(_morphWeights, _numMorphWeights, boneTransforms, return Animation(morphData, _numMorphWeights, boneTransforms, _boneNames,
_boneNames, _meshNames, numFrames, _frameLengthInMs); _meshNames, numFrames, _frameLengthInMs);
} }
AnimationBuilder setFramerate(int framerate) { AnimationBuilder setFramerate(int framerate) {

View File

@@ -4,6 +4,9 @@ class Vec3 {
final double z; final double z;
Vec3({this.x = 0, this.y = 0, this.z = 0}); Vec3({this.x = 0, this.y = 0, this.z = 0});
factory Vec3.from(List<double> vals) =>
Vec3(x: vals[0], y: vals[1], z: vals[2]);
} }
class Quaternion { class Quaternion {
@@ -13,6 +16,9 @@ class Quaternion {
double w = 1; double w = 1;
Quaternion({this.x = 0, this.y = 0, this.z = 0, this.w = 1.0}); Quaternion({this.x = 0, this.y = 0, this.z = 0, this.w = 1.0});
factory Quaternion.from(List<double> vals) =>
Quaternion(x: vals[0], y: vals[1], z: vals[2], w: vals[3]);
} }
class BoneTransform { class BoneTransform {

View File

@@ -222,7 +222,6 @@ class PolyvoxFilamentController extends FilamentController {
Future<FilamentAsset> loadGlb(String path) async { Future<FilamentAsset> loadGlb(String path) async {
print("Loading GLB at $path "); print("Loading GLB at $path ");
var asset = await _channel.invokeMethod("loadGlb", path); var asset = await _channel.invokeMethod("loadGlb", path);
print("Got asset : $asset ");
return asset as FilamentAsset; return asset as FilamentAsset;
} }
@@ -283,13 +282,14 @@ class PolyvoxFilamentController extends FilamentController {
} }
Future setAnimation(FilamentAsset asset, Animation animation) async { Future setAnimation(FilamentAsset asset, Animation animation) async {
print("Frmael en ${animation.frameLengthInMs}");
await _channel.invokeMethod("setAnimation", [ await _channel.invokeMethod("setAnimation", [
asset, asset,
animation.morphWeights, animation.morphData!,
animation.numMorphWeights, animation.numMorphWeights,
animation.boneTransforms, animation.boneTransforms ?? Float32List(0),
animation.boneNames, animation.boneNames ?? <String>[],
animation.meshNames, animation.meshNames ?? <String>[],
animation.numFrames, animation.numFrames,
animation.frameLengthInMs animation.frameLengthInMs
]); ]);