new animation structure

remade animation controller
This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 7f09723104
commit 46cd64db9e
11 changed files with 143 additions and 207 deletions
+66 -106
View File
@@ -19,27 +19,41 @@ namespace vb01{
}
}
void AnimationController::transform(AnimationChannel *channel){
Animation *animation = getAnimation(channel->getAnimationName());
Keyframe AnimationController::findKeyframe(AnimationChannel *channel, KeyframeChannel keyframeChannel, bool last){
int pastKeyframeId = -1;
for(int i = 0; i < keyframeChannel.keyframes.size(); i++){
Keyframe keyframe = keyframeChannel.keyframes[i];
if(channel->getCurrentFrame() >= keyframe.frame){
pastKeyframeId = i;
}
}
return keyframeChannel.keyframes[pastKeyframeId + (last ? 0 : 1)];
}
void AnimationController::transform(AnimationChannel *channel){
vector<Node*> transformNodes;
if(node)
transformNodes.push_back(node);
for(Bone *channelBone : channel->getBones())
transformNodes.push_back((Node*)channelBone);
Animation *animation = getAnimation(channel->getAnimationName());
for(Node *transformNode : transformNodes){
Animation::KeyframeGroup *keyframeGroup = animation->getKeyframeGroup(transformNode);
Vector3 pastPos, nextPos, pastScale, nextScale;
Quaternion pastRot, nextRot;
Keyframe::Interpolation interp;
float ratio;
Vector3 currentPos;
Quaternion currentRot;
Vector3 currentScale;
vector<KeyframeChannel> keyframeChannels = animation->getKeyframeChannelsByNode(transformNode);
for(KeyframeChannel keyframeChannel : keyframeChannels){
Keyframe pastKeyframe = findKeyframe(channel, keyframeChannel, true);
Keyframe nextKeyframe = findKeyframe(channel, keyframeChannel, false);
int pastFrame = pastKeyframe.frame;
int nextFrame = nextKeyframe.frame;
prepareAdjacentValues(pastPos, nextPos, pastRot, nextRot, pastScale, nextScale, interp, ratio, keyframeGroup, channel);
Vector3 currentPos = interpolate(pastPos, nextPos, interp, ratio);
Quaternion currentRot = interpolate(pastRot, nextRot, interp, ratio);
Vector3 currentScale = interpolate(pastScale, nextScale, interp, ratio);
Keyframe::Interpolation interp = pastKeyframe.interpolation;
float ratio = (float)(max(0, channel->getCurrentFrame() - pastFrame)) / (nextFrame - pastFrame);
float value = interpolate(pastKeyframe.value, nextKeyframe.value, interp, ratio);
setFrameValue(value, keyframeChannel.type, currentPos, currentRot, currentScale);
}
if(node && node->getName() == transformNode->getName()){
transformNode->setPosition(currentPos);
@@ -55,111 +69,57 @@ namespace vb01{
}
}
void AnimationController::prepareAdjacentValues(
Vector3 &pastPos,
Vector3 &nextPos,
Quaternion &pastRot,
Quaternion &nextRot,
Vector3 &pastScale,
Vector3 &nextScale,
KeyframeInterpolation &interpMode,
float &ratio,
Animation::KeyframeGroup *keyframeGroup,
AnimationChannel *channel
){
for(KeyframeChannel keyframeChannel : keyframeGroup->keyframeChannels){
int pastKeyframeId = -1, nextKeyframeId = -1;
for(int i = 0; i < keyframeChannel.keyframes.size(); i++){
Keyframe keyframe = keyframeChannel.keyframes[i];
if(channel->getCurrentFrame() >= keyframe.frame){
pastKeyframeId = i;
}
}
nextKeyframeId = pastKeyframeId + 1;
Keyframe pastKeyframe = keyframeChannel.keyframes[pastKeyframeId];
Keyframe nextKeyframe = keyframeChannel.keyframes[nextKeyframeId];
int pastFrame = pastKeyframe.frame;
int nextFrame = nextKeyframe.frame;
ratio = (float)(max(0, channel->getCurrentFrame() - pastFrame)) / (nextFrame - pastFrame);
interpMode = pastKeyframe.interpolation;
switch(keyframeChannel.type){
case KeyframeChannel::POS_X:
pastPos.x = pastKeyframe.value;
nextPos.x = nextKeyframe.value;
break;
case KeyframeChannel::POS_Y:
pastPos.y = pastKeyframe.value;
nextPos.y = nextKeyframe.value;
break;
case KeyframeChannel::POS_Z:
pastPos.z = pastKeyframe.value;
nextPos.z = nextKeyframe.value;
break;
case KeyframeChannel::ROT_W:
pastRot.w = pastKeyframe.value;
nextRot.w = nextKeyframe.value;
break;
case KeyframeChannel::ROT_X:
pastRot.x = pastKeyframe.value;
nextRot.x = nextKeyframe.value;
break;
case KeyframeChannel::ROT_Y:
pastRot.y = pastKeyframe.value;
nextRot.y = nextKeyframe.value;
break;
case KeyframeChannel::ROT_Z:
pastRot.z = pastKeyframe.value;
nextRot.z = nextKeyframe.value;
break;
case KeyframeChannel::SCALE_X:
pastScale.x = pastKeyframe.value;
nextScale.x = nextKeyframe.value;
break;
case KeyframeChannel::SCALE_Y:
pastScale.y = pastKeyframe.value;
nextScale.y = nextKeyframe.value;
break;
case KeyframeChannel::SCALE_Z:
pastScale.z = pastKeyframe.value;
nextScale.z = nextKeyframe.value;
break;
}
void AnimationController::setFrameValue(float value, KeyframeChannelType type, Vector3 &currentPos, Quaternion &currentRot, Vector3 &currentScale){
switch(type){
case KeyframeChannel::POS_X:
currentPos.x = value;
break;
case KeyframeChannel::POS_Y:
currentPos.y = value;
break;
case KeyframeChannel::POS_Z:
currentPos.z = value;
break;
case KeyframeChannel::ROT_W:
currentRot.w = value;
break;
case KeyframeChannel::ROT_X:
currentRot.x = value;
break;
case KeyframeChannel::ROT_Y:
currentRot.y = value;
break;
case KeyframeChannel::ROT_Z:
currentRot.z = value;
break;
case KeyframeChannel::SCALE_X:
currentScale.x = value;
break;
case KeyframeChannel::SCALE_Y:
currentScale.y = value;
break;
case KeyframeChannel::SCALE_Z:
currentScale.z = value;
break;
case KeyframeChannel::VALUE:
break;
}
}
Vector3 AnimationController::interpolate(Vector3 pastPos, Vector3 nextPos, Keyframe::Interpolation mode, float ratio){
Vector3 currentPos;
float AnimationController::interpolate(float pastValue, float nextValue, Keyframe::Interpolation mode, float ratio){
float currentValue;
switch(mode){
case Keyframe::CONSTANT:
currentPos = pastPos;
currentValue = pastValue;
break;
case Keyframe::LINEAR:
currentPos = pastPos + (nextPos - pastPos) * ratio;
currentValue = pastValue + (nextValue - pastValue) * ratio;
break;
case Keyframe::BEZIER:
currentPos = pastPos + (nextPos - pastPos) * ratio;
currentValue = pastValue + (nextValue - pastValue) * ratio;
break;
}
return currentPos;
}
Quaternion AnimationController::interpolate(Quaternion pastRot, Quaternion nextRot, Keyframe::Interpolation mode, float ratio){
Quaternion currentRot;
switch(mode){
case Keyframe::CONSTANT:
currentRot = pastRot;
break;
case Keyframe::LINEAR:
currentRot = pastRot + (nextRot - pastRot) * ratio;
break;
case Keyframe::BEZIER:
currentRot = pastRot + (nextRot - pastRot) * ratio;
break;
}
return currentRot;
return currentValue;
}
Animation* AnimationController::getAnimation(string name){