void GPBFile::moveAnimationChannels(Node* node, Animation* dstAnimation) { // Loop through the animations and channels backwards because they will be removed when found. int animationCount = _animations.getAnimationCount(); for (int i = animationCount - 1; i >= 0; --i) { Animation* animation = _animations.getAnimation(i); int channelCount = animation->getAnimationChannelCount(); for (int j = channelCount - 1; j >= 0; --j) { AnimationChannel* channel = animation->getAnimationChannel(j); if (equals(channel->getTargetId(), node->getId())) { animation->remove(channel); dstAnimation->add(channel); } } if (animation->getAnimationChannelCount() == 0) { _animations.removeAnimation(i); } } for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling()) { moveAnimationChannels(child, dstAnimation); } }
void GPBFile::optimizeTransformAnimations() { const unsigned int animationCount = _animations.getAnimationCount(); for (unsigned int animationIndex = 0; animationIndex < animationCount; ++animationIndex) { Animation* animation = _animations.getAnimation(animationIndex); assert(animation); const int channelCount = animation->getAnimationChannelCount(); // loop backwards because we will be adding and removing channels for (int channelIndex = channelCount -1; channelIndex >= 0 ; --channelIndex) { AnimationChannel* channel = animation->getAnimationChannel(channelIndex); assert(channel); // get target node const Object* obj = _refTable.get(channel->getTargetId()); if (obj && obj->getTypeId() == Object::NODE_ID) { const Node* node = static_cast<const Node*>(obj); if (node->isJoint() && channel->getTargetAttribute() == Transform::ANIMATE_SCALE_ROTATE_TRANSLATE) { decomposeTransformAnimationChannel(animation, channel); animation->remove(channel); SAFE_DELETE(channel); } } } } }
void Graphics::updateAnimation(float dT) { for(map<string, AnimationChannel*>::iterator it=animation.begin(); it != animation.end(); ++it) { elapsedTime += dT; AnimationChannel* ch = it->second; while(elapsedTime >= ch->getDuration()) { elapsedTime -= ch->getDuration(); } ch->update(elapsedTime, *scene[it->first]); } }
AnimationChannel* createAnimationChannel(FbxNode* fbxNode, unsigned int targetAttrib, const vector<float>& keyTimes, const vector<float>& keyValues) { AnimationChannel* channel = new AnimationChannel(); channel->setTargetId(fbxNode->GetName()); channel->setKeyTimes(keyTimes); channel->setKeyValues(keyValues); channel->setInterpolation(AnimationChannel::LINEAR); channel->setTargetAttribute(targetAttrib); return channel; }
void MeshSkin::computeBounds() { // Find the offset of the blend indices and blend weights within the mesh vertices int blendIndexOffset = -1; int blendWeightOffset = -1; for (unsigned int i = 0, count = _mesh->getVertexElementCount(); i < count; ++i) { const VertexElement& e = _mesh->getVertexElement(i); switch (e.usage) { case BLENDINDICES: blendIndexOffset = i; break; case BLENDWEIGHTS: blendWeightOffset = i; break; } } if (blendIndexOffset == -1 || blendWeightOffset == -1) { // Need blend indices and blend weights to calculate skinned bounding volume return; } LOG(2, "Computing bounds for skin of mesh: %s\n", _mesh->getId().c_str()); // Get the root joint if (_joints.size() == 0) return; Node* rootJoint = _joints[0]; Node* parent = rootJoint->getParent(); while (parent) { // Is this parent in the list of joints that form the skeleton? // If not, then it's simply a parent node to the root joint if (find(_joints.begin(), _joints.end(), parent) != _joints.end()) { rootJoint = parent; } parent = parent->getParent(); } // If the root joint has a parent node, temporarily detach it so that its transform is // not included in the bounding volume calculation below Node* rootJointParent = rootJoint->getParent(); if (rootJointParent) { rootJointParent->removeChild(rootJoint); } unsigned int jointCount = _joints.size(); unsigned int vertexCount = _mesh->getVertexCount(); LOG(3, " %u joints found.\n", jointCount); std::vector<AnimationChannel*> channels; std::vector<Node*> channelTargets; std::vector<Curve*> curves; std::vector<Vector3> vertices; _jointBounds.resize(jointCount); // Construct a list of all animation channels that target the joints affecting this mesh skin LOG(3, " Collecting animations...\n"); LOG(3, " 0%%\r"); for (unsigned int i = 0; i < jointCount; ++i) { Node* joint = _joints[i]; // Find all animations that target this joint Animations* animations = GPBFile::getInstance()->getAnimations(); for (unsigned int j = 0, animationCount = animations->getAnimationCount(); j < animationCount; ++j) { Animation* animation = animations->getAnimation(j); for (unsigned int k = 0, channelCount = animation->getAnimationChannelCount(); k < channelCount; ++k) { AnimationChannel* channel = animation->getAnimationChannel(k); if (channel->getTargetId() == joint->getId()) { if (find(channels.begin(), channels.end(), channel) == channels.end()) { channels.push_back(channel); channelTargets.push_back(joint); } } } } // Calculate the local bounding volume for this joint vertices.clear(); BoundingVolume jointBounds; jointBounds.min.set(FLT_MAX, FLT_MAX, FLT_MAX); jointBounds.max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX); for (unsigned int j = 0; j < vertexCount; ++j) { const Vertex& v = _mesh->getVertex(j); if ((v.blendIndices.x == i && !ISZERO(v.blendWeights.x)) || (v.blendIndices.y == i && !ISZERO(v.blendWeights.y)) || (v.blendIndices.z == i && !ISZERO(v.blendWeights.z)) || (v.blendIndices.w == i && !ISZERO(v.blendWeights.w))) { vertices.push_back(v.position); // Update box min/max if (v.position.x < jointBounds.min.x) jointBounds.min.x = v.position.x; if (v.position.y < jointBounds.min.y) jointBounds.min.y = v.position.y; if (v.position.z < jointBounds.min.z) jointBounds.min.z = v.position.z; if (v.position.x > jointBounds.max.x) jointBounds.max.x = v.position.x; if (v.position.y > jointBounds.max.y) jointBounds.max.y = v.position.y; if (v.position.z > jointBounds.max.z) jointBounds.max.z = v.position.z; } } if (vertices.size() > 0) { // Compute center point Vector3::add(jointBounds.min, jointBounds.max, &jointBounds.center); jointBounds.center.scale(0.5f); // Compute radius for (unsigned int j = 0, jointVertexCount = vertices.size(); j < jointVertexCount; ++j) { float d = jointBounds.center.distanceSquared(vertices[j]); if (d > jointBounds.radius) jointBounds.radius = d; } jointBounds.radius = sqrt(jointBounds.radius); } _jointBounds[i] = jointBounds; LOG(3, " %d%%\r", (int)((float)(i+1) / (float)jointCount * 100.0f)); } LOG(3, "\n"); unsigned int channelCount = channels.size(); // Create a Curve for each animation channel float maxDuration = 0.0f; LOG(3, " Building animation curves...\n"); LOG(3, " 0%%\r"); for (unsigned int i = 0; i < channelCount; ++i) { AnimationChannel* channel = channels[i]; const std::vector<float>& keyTimes = channel->getKeyTimes(); unsigned int keyCount = keyTimes.size(); if (keyCount == 0) continue; // Create a curve for this animation channel Curve* curve = NULL; switch (channel->getTargetAttribute()) { case Transform::ANIMATE_SCALE_ROTATE_TRANSLATE: curve = new Curve(keyCount, 10); curve->setQuaternionOffset(3); break; } if (curve == NULL) { // Unsupported/not implemented curve type continue; } // Copy key values into a temporary array unsigned int keyValuesCount = channel->getKeyValues().size(); float* keyValues = new float[keyValuesCount]; for (unsigned int j = 0; j < keyValuesCount; ++j) keyValues[j] = channel->getKeyValues()[j]; // Determine animation duration float startTime = keyTimes[0]; float duration = keyTimes[keyCount-1] - startTime; if (duration > maxDuration) maxDuration = duration; if (duration > 0.0f) { // Set curve points float* keyValuesPtr = keyValues; for (unsigned int j = 0; j < keyCount; ++j) { // Store time normalized, between 0-1 float t = (keyTimes[j] - startTime) / duration; // Set the curve point // TODO: Handle other interpolation types curve->setPoint(j, t, keyValuesPtr, gameplay::Curve::LINEAR); // Move to the next point on the curve keyValuesPtr += curve->getComponentCount(); } curves.push_back(curve); } delete[] keyValues; keyValues = NULL; LOG(3, " %d%%\r", (int)((float)(i+1) / (float)channelCount * 100.0f)); } LOG(3, "\n"); // Compute a total combined bounding volume for the MeshSkin that contains all possible // vertex positions for all animations targeting the skin. This rough approximation allows // us to store a volume that can be used for rough intersection tests (such as for visibility // determination) efficiently at runtime. // Backup existing node transforms so we can restore them when we are finished Matrix* oldTransforms = new Matrix[jointCount]; for (unsigned int i = 0; i < jointCount; ++i) { memcpy(oldTransforms[i].m, _joints[i]->getTransformMatrix().m, 16 * sizeof(float)); } float time = 0.0f; float srt[10]; Matrix temp; Matrix* jointTransforms = new Matrix[jointCount]; _mesh->bounds.min.set(FLT_MAX, FLT_MAX, FLT_MAX); _mesh->bounds.max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX); _mesh->bounds.center.set(0, 0, 0); _mesh->bounds.radius = 0; Vector3 skinnedPos; Vector3 tempPos; LOG(3, " Evaluating joints...\n"); LOG(3, " 0%%\r"); BoundingVolume finalBounds; while (time <= maxDuration) { // Evaluate joint transforms at this time interval for (unsigned int i = 0, curveCount = curves.size(); i < curveCount; ++i) { Node* joint = channelTargets[i]; Curve* curve = curves[i]; // Evalulate the curve at this time to get the new value float tn = time / maxDuration; if (tn > 1.0f) tn = 1.0f; curve->evaluate(tn, srt); // Update the joint's local transform Matrix::createTranslation(srt[7], srt[8], srt[9], temp.m); temp.rotate(*((Quaternion*)&srt[3])); temp.scale(srt[0], srt[1], srt[2]); joint->setTransformMatrix(temp.m); } // Store the final matrix pallette of resovled world space joint matrices std::vector<Matrix>::const_iterator bindPoseItr = _bindPoses.begin(); for (unsigned int i = 0; i < jointCount; ++i, bindPoseItr++) { BoundingVolume bounds = _jointBounds[i]; if (ISZERO(bounds.radius)) continue; Matrix& m = jointTransforms[i]; Matrix::multiply(_joints[i]->getWorldMatrix().m, bindPoseItr->m, m.m); Matrix::multiply(m.m, _bindShape, m.m); // Get a world-space bounding volume for this joint bounds.transform(m); if (ISZERO(finalBounds.radius)) finalBounds = bounds; else finalBounds.merge(bounds); } // Increment time by 1/30th of second (~ 33 ms) if (time < maxDuration && (time + 33.0f) > maxDuration) time = maxDuration; else time += 33.0f; LOG(3, " %d%%\r", (int)(time / maxDuration * 100.0f)); } LOG(3, "\n"); // Update the bounding sphere for the mesh _mesh->bounds = finalBounds; // Restore original joint transforms for (unsigned int i = 0; i < jointCount; ++i) { _joints[i]->setTransformMatrix(oldTransforms[i].m); } // Cleanup for (unsigned int i = 0, curveCount = curves.size(); i < curveCount; ++i) { delete curves[i]; } delete[] oldTransforms; delete[] jointTransforms; // Restore removed joints if (rootJointParent) { rootJointParent->addChild(rootJoint); } }
void FBXSceneEncoder::loadAnimationChannels(FbxAnimLayer* animLayer, FbxNode* fbxNode, Animation* animation) { const char* name = fbxNode->GetName(); //Node* node = _gamePlayFile.getNode(name); // Determine which properties are animated on this node // Find the transform at each key frame // TODO: Ignore properties that are not animated (scale, rotation, translation) // This should result in only one animation channel per animated node. float startTime = FLT_MAX, stopTime = -1.0f, frameRate = -FLT_MAX; bool tx = false, ty = false, tz = false, rx = false, ry = false, rz = false, sx = false, sy = false, sz = false; FbxAnimCurve* animCurve = NULL; animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_X); if (animCurve) { tx = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_Y); if (animCurve) { ty = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_Z); if (animCurve) { tz = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_X); if (animCurve) { rx = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_Y); if (animCurve) { ry = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_Z); if (animCurve) { rz = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_X); if (animCurve) { sx = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_Y); if (animCurve) { sy = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_Z); if (animCurve) { sz = true; findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate); } if (!(sx || sy || sz || rx || ry || rz || tx || ty || tz)) return; // no animation channels assert(startTime != FLT_MAX); assert(stopTime >= 0.0f); // Determine which animation channels to create vector<unsigned int> channelAttribs; if (sx && sy && sz) { if (rx || ry || rz) { if (tx && ty && tz) { channelAttribs.push_back(Transform::ANIMATE_SCALE_ROTATE_TRANSLATE); } else { channelAttribs.push_back(Transform::ANIMATE_SCALE_ROTATE); if (tx) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X); if (ty) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y); if (tz) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z); } } else { if (tx && ty && tz) { channelAttribs.push_back(Transform::ANIMATE_SCALE_TRANSLATE); } else { channelAttribs.push_back(Transform::ANIMATE_SCALE); if (tx) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X); if (ty) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y); if (tz) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z); } } } else { if (rx || ry || rz) { if (tx && ty && tz) { channelAttribs.push_back(Transform::ANIMATE_ROTATE_TRANSLATE); } else { channelAttribs.push_back(Transform::ANIMATE_ROTATE); if (tx) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X); if (ty) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y); if (tz) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z); } } else { if (tx && ty && tz) { channelAttribs.push_back(Transform::ANIMATE_TRANSLATE); } else { if (tx) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X); if (ty) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y); if (tz) channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z); } } if (sx) channelAttribs.push_back(Transform::ANIMATE_SCALE_X); if (sy) channelAttribs.push_back(Transform::ANIMATE_SCALE_Y); if (sz) channelAttribs.push_back(Transform::ANIMATE_SCALE_Z); } unsigned int channelCount = channelAttribs.size(); assert(channelCount > 0); // Allocate channel list const int channelStart = animation->getAnimationChannelCount(); for (unsigned int i = 0; i < channelCount; ++i) { AnimationChannel* channel = new AnimationChannel(); channel->setTargetId(name); channel->setInterpolation(AnimationChannel::LINEAR); channel->setTargetAttribute(channelAttribs[i]); animation->add(channel); } // Evaulate animation curve in increments of frameRate and populate channel data. FbxAMatrix fbxMatrix; Matrix matrix; double increment = 1000.0 / (double)frameRate; int frameCount = (int)ceil((double)(stopTime - startTime) / increment) + 1; // +1 because stop time is inclusive // If the animation for this node only has only 1 key frame and it is equal to the node's transform then ignore it. // This is a work around for a bug in the Blender FBX exporter. if (frameCount == 1 && fbxMatrix == fbxNode->EvaluateLocalTransform(0)) { return; } for (int frame = 0; frame < frameCount; ++frame) { double time = startTime + (frame * (double)increment); // Note: We used to clamp time to stop time, but FBX sdk does not always produce // and accurate stopTime (sometimes it is rounded down for some reason), so I'm // disabling this clamping for now as it seems more accurate under normal circumstances. //time = std::min(time, (double)stopTime); // Evalulate the animation at this time FbxTime kTime; kTime.SetMilliSeconds((FbxLongLong)time); fbxMatrix = fbxNode->EvaluateLocalTransform(kTime); copyMatrix(fbxMatrix, matrix); // Decompose the evalulated transformation matrix into separate // scale, rotation and translation. Vector3 scale; Quaternion rotation; Vector3 translation; matrix.decompose(&scale, &rotation, &translation); rotation.normalize(); // Append keyframe data to all channels for (unsigned int i = channelStart, channelEnd = channelStart + channelCount; i < channelEnd; ++i) { appendKeyFrame(fbxNode, animation->getAnimationChannel(i), (float)time, scale, rotation, translation); } } if (_groupAnimation != animation) { _gamePlayFile.addAnimation(animation); } }
void GPBFile::decomposeTransformAnimationChannel(Animation* animation, const AnimationChannel* channel) { const std::vector<float>& keyTimes = channel->getKeyTimes(); const std::vector<float>& keyValues = channel->getKeyValues(); const size_t keyTimesSize = keyTimes.size(); const size_t keyValuesSize = keyValues.size(); std::vector<float> scaleKeyValues; std::vector<float> rotateKeyValues; std::vector<float> translateKeyValues; scaleKeyValues.reserve(keyTimesSize * 3); rotateKeyValues.reserve(keyTimesSize * 4); translateKeyValues.reserve(keyTimesSize * 3); for (size_t kv = 0; kv < keyValuesSize; kv += 10) { scaleKeyValues.push_back(keyValues[kv]); scaleKeyValues.push_back(keyValues[kv+1]); scaleKeyValues.push_back(keyValues[kv+2]); rotateKeyValues.push_back(keyValues[kv+3]); rotateKeyValues.push_back(keyValues[kv+4]); rotateKeyValues.push_back(keyValues[kv+5]); rotateKeyValues.push_back(keyValues[kv+6]); translateKeyValues.push_back(keyValues[kv+7]); translateKeyValues.push_back(keyValues[kv+8]); translateKeyValues.push_back(keyValues[kv+9]); } // replace transform animation channel with translate, rotate and scale animation channels // Don't add the scale channel if all the key values are close to 1.0 size_t oneCount = (size_t)std::count_if(scaleKeyValues.begin(), scaleKeyValues.end(), isAlmostOne); if (scaleKeyValues.size() != oneCount) { AnimationChannel* scaleChannel = new AnimationChannel(); scaleChannel->setTargetId(channel->getTargetId()); scaleChannel->setKeyTimes(channel->getKeyTimes()); scaleChannel->setTangentsIn(channel->getTangentsIn()); scaleChannel->setTangentsOut(channel->getTangentsOut()); scaleChannel->setInterpolations(channel->getInterpolationTypes()); scaleChannel->setTargetAttribute(Transform::ANIMATE_SCALE); scaleChannel->setKeyValues(scaleKeyValues); scaleChannel->removeDuplicates(); animation->add(scaleChannel); } AnimationChannel* rotateChannel = new AnimationChannel(); rotateChannel->setTargetId(channel->getTargetId()); rotateChannel->setKeyTimes(channel->getKeyTimes()); rotateChannel->setTangentsIn(channel->getTangentsIn()); rotateChannel->setTangentsOut(channel->getTangentsOut()); rotateChannel->setInterpolations(channel->getInterpolationTypes()); rotateChannel->setTargetAttribute(Transform::ANIMATE_ROTATE); rotateChannel->setKeyValues(rotateKeyValues); rotateChannel->removeDuplicates(); animation->add(rotateChannel); AnimationChannel* translateChannel = new AnimationChannel(); translateChannel->setTargetId(channel->getTargetId()); translateChannel->setKeyTimes(channel->getKeyTimes()); translateChannel->setTangentsIn(channel->getTangentsIn()); translateChannel->setTangentsOut(channel->getTangentsOut()); translateChannel->setInterpolations(channel->getInterpolationTypes()); translateChannel->setTargetAttribute(Transform::ANIMATE_TRANSLATE); translateChannel->setKeyValues(translateKeyValues); translateChannel->removeDuplicates(); animation->add(translateChannel); }