allow partial morph animations

This commit is contained in:
Nick Fisher
2023-08-18 15:43:20 +08:00
parent 46ecacde12
commit ed72c16724
13 changed files with 174 additions and 135 deletions

View File

@@ -381,35 +381,41 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
let assetManager = args[0] as? Int64,
let asset = args[1] as? EntityId,
let entityName = args[2] as? String,
let morphData = args[3] as? FlutterStandardTypedData,
let numMorphWeights = args[4] as? Int else {
let morphData = args[3] as? [Double],
let numMorphWeights = args[4] as? Int32 else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Expected correct arguments for setMorphTargetWeights", details: nil))
return
}
let success = morphData.data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: Float.self) {
set_morph_target_weights(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, entityName, $0.baseAddress, Int32(numMorphWeights))
}
}
result(success)
set_morph_target_weights(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, entityName, morphData.map { Float($0) }, Int32(numMorphWeights))
result(true)
case "setMorphAnimation":
guard let args = call.arguments as? [Any], args.count == 7,
guard let args = call.arguments as? [Any], args.count == 8,
let assetManager = args[0] as? Int64,
let asset = args[1] as? EntityId,
let entityName = args[2] as? String,
let morphData = args[3] as? FlutterStandardTypedData,
let numMorphWeights = args[4] as? Int,
let numFrames = args[5] as? Int,
let frameLengthInMs = args[6] as? Double else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Expected correct arguments for set_morph_animation", details: nil))
let morphData = args[3] as? [Double],
let morphIndices = args[4] as? [Int32],
let numMorphTargets = args[5] as? Int32,
let numFrames = args[6] as? Int32,
let frameLengthInMs = args[7] as? Double else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Incorrect arguments provided for setMorphAnimation", details: nil))
return
}
let success = morphData.data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: Float.self) {
set_morph_animation(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, entityName, $0.baseAddress, Int32(numMorphWeights), Int32(numFrames), Float(frameLengthInMs))
}
}
let frameData = morphData.map { Float($0) }
let am = unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self)
let success = set_morph_animation(
am,
asset,
entityName,
frameData,
morphIndices,
Int32(numMorphTargets),
Int32(numFrames),
Float(frameLengthInMs))
result(success)
case "setBoneAnimation":
guard let args = call.arguments as? [Any], args.count == 9,

View File

@@ -45,9 +45,11 @@ namespace polyvox {
EntityId entityId,
const char* entityName,
const float* const morphData,
int numMorphWeights,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs);
void setMorphTargetWeights(EntityId entityId, const char* const entityName, const float* const weights, int count);
bool setBoneAnimationBuffer(

View File

@@ -54,9 +54,11 @@ bool set_morph_animation(
EntityId asset,
const char *const entityName,
const float *const morphData,
int numMorphWeights,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs);
void set_bone_animation(
void* assetManager,
EntityId asset,

View File

@@ -52,7 +52,7 @@ namespace polyvox {
int mNumFrames = -1;
float mFrameLengthInMs = 0;
vector<float> mFrameData;
int mNumMorphWeights = 0;
vector<int> mMorphIndices;
};
//

View File

@@ -272,12 +272,16 @@ void AssetManager::updateAnimations() {
if(anim.mReverse) {
frameNumber = lengthInFrames - frameNumber;
}
// set the weights appropriately
rm.setMorphWeights(
rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget),
asset.mMorphAnimationBuffer.mFrameData.data() + (frameNumber * asset.mMorphAnimationBuffer.mNumMorphWeights),
asset.mMorphAnimationBuffer.mNumMorphWeights
);
auto baseOffset = frameNumber * asset.mMorphAnimationBuffer.mMorphIndices.size();
for(auto morphIndex : asset.mMorphAnimationBuffer.mMorphIndices) {
// set the weights appropriately
rm.setMorphWeights(
rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget),
asset.mMorphAnimationBuffer.mFrameData.data() + baseOffset + morphIndex,
1,
morphIndex
);
}
break;
}
case AnimationType::BONE: {
@@ -416,7 +420,8 @@ bool AssetManager::setMorphAnimationBuffer(
EntityId entityId,
const char* entityName,
const float* const morphData,
int numMorphWeights,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs) {
@@ -438,10 +443,13 @@ bool AssetManager::setMorphAnimationBuffer(
asset.mMorphAnimationBuffer.mFrameData.insert(
asset.mMorphAnimationBuffer.mFrameData.begin(),
morphData,
morphData + (numFrames * numMorphWeights)
morphData + (numFrames * numMorphTargets)
);
asset.mMorphAnimationBuffer.mFrameLengthInMs = frameLengthInMs;
asset.mMorphAnimationBuffer.mNumMorphWeights = numMorphWeights;
asset.mMorphAnimationBuffer.mMorphIndices.resize(numMorphTargets);
for(int i =0; i< numMorphTargets; i++) {
asset.mMorphAnimationBuffer.mMorphIndices[i] = morphIndices[i];
}
AnimationStatus animation;
animation.mDuration = (frameLengthInMs * numFrames) / 1000.0f;

View File

@@ -196,7 +196,8 @@ extern "C" {
EntityId asset,
const char* const entityName,
const float* const morphData,
int numMorphWeights,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs) {
@@ -204,7 +205,8 @@ extern "C" {
asset,
entityName,
morphData,
numMorphWeights,
morphIndices,
numMorphTargets,
numFrames,
frameLengthInMs
);