FBX SDK开发快速上手指南

news2024/9/24 13:20:22

一段时间以来,我一直想制作一个 FBX Exporter 将 FBX 文件转换为我自己的格式。 整个过程不是很顺利,主要是FBX的官方文档不是很清楚。 另外,由于 FBX 格式被许多应用程序使用,而不仅仅是游戏引擎,因此提供的示例代码没有使用我们在游戏开发中使用的更常见的技术术语。 我搜索了互联网上几乎所有的角落来澄清事情,以便我可以从 FBX SDK 的数据到我在游戏引擎中需要的数据有一个清晰的映射。

在这里插入图片描述

推荐:用 NSDT设计器 快速搭建可编程3D场景。

因为我认为没有人发布过关于如何将 FBX 文件转换为自定义格式的清晰而详尽的教程,所以我会这样做。 我希望这能帮助人们。 本教程将专门介绍游戏引擎。 基本上我会告诉读者如何获取他们的游戏引擎所需的数据。 对于诸如“如何初始化 FBX SDK”之类的内容,请自行查看示例代码,“ImportScene”示例在这方面非常有用。

如果你不了解骨骼动画如何工作以及需要哪些数据来使骨骼动画发生,请查看 Buckeye 的文章“使用矩阵的蒙皮网格动画”。 这会很有帮助。 链接在这里。

1、网格数据

你要做的第一件事是获取网格数据; 如果你可以将静态网格物体导入到引擎中,那感觉已经相当不错了。 为了使本节更加清晰,我选择首先向你展示如何遍历 FBX 文件中的网格。 这使我能够让你自上而下地了解收集网格数据需要做什么。

你不知道每个函数具体做什么,但应该知道我们正在遍历网格每个三角形上的 3 个顶点。 稍后我将回到每个功能。 请注意,有一些与动画混合信息相关的代码。 现在可以忽略它。 我们稍后会再讨论这个问题。

Exporter::ProcessMesh(FbxNode* inNode) 
{ 
	FbxMesh* currMesh = inNode->GetMesh(); 
	mTriangleCount = currMesh->GetPolygonCount(); 
	int vertexCounter = 0; 
	mTriangles.reserve(mTriangleCount); 
	
	for (unsigned int i = 0; i < mTriangleCount; ++i) 
	{ 
		XMFLOAT3 normal[3]; 
		XMFLOAT3 tangent[3]; 
		XMFLOAT3 binormal[3]; 
		XMFLOAT2 UV[3][2]; 
		Triangle currTriangle; 
		mTriangles.push_back(currTriangle); 
		
		for (unsigned int j = 0; j < 3; ++j) 
		{ 
			int ctrlPointIndex = currMesh->GetPolygonVertex(i, j); 
			CtrlPoint* currCtrlPoint = mControlPoints[ctrlPointIndex]; 
			ReadNormal(currMesh, ctrlPointIndex, vertexCounter, normal[j]); 
			
			// We only have diffuse texture 
			for (int k = 0; k < 1; ++k) 
			{ 
				ReadUV(currMesh, ctrlPointIndex, currMesh->GetTextureUVIndex(i, j), k, UV[j][k]); 
			} 
			
			PNTIWVertex temp; 
			temp.mPosition = currCtrlPoint->mPosition; 
			temp.mNormal = normal[j]; 
			temp.mUV = UV[j][0]; 
			
			// Copy the blending info from each control point 
			for(unsigned int i = 0; i < currCtrlPoint->mBlendingInfo.size(); ++i) 
			{ 
				VertexBlendingInfo currBlendingInfo; 
				currBlendingInfo.mBlendingIndex = currCtrlPoint->mBlendingInfo.mBlendingIndex; 
				currBlendingInfo.mBlendingWeight = currCtrlPoint->mBlendingInfo.mBlendingWeight; 
				temp.mVertexBlendingInfos.push_back(currBlendingInfo);
			} 
			
			// Sort the blending info so that later we can remove 
			// duplicated vertices 
			temp.SortBlendingInfoByWeight(); 
			mVertices.push_back(temp); 
			mTriangles.back().mIndices.push_back(vertexCounter); 
			++vertexCounter; 
		} 
	} 
	
	// Now mControlPoints has served its purpose 
	// We can free its memory 
	for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr) { delete itr->second; } 
	
	mControlPoints.clear(); 

首先请让我解释一下 FBX 如何存储有关网格的所有信息。 在 FBX 中,我们有术语控制点(Control Point),基本上控制点是一个物理顶点。 例如,你有一个立方体,那么你有 8 个顶点(Vertex)。 这8个顶点是FBX文件中仅有的8个“控制点”。 因此,如果需要,你可以互换使用“顶点”和“控制点”。 位置信息存储在控制点中。 以下代码将提供网格所有顶点的位置:

// inNode is the Node in this FBX Scene that contains the mesh 
// this is why I can use inNode->GetMesh() on it to get the mesh 
void FBXExporter::ProcessControlPoints(FbxNode* inNode) 
{ 
	FbxMesh* currMesh = inNode->GetMesh(); 
	unsigned int ctrlPointCount = currMesh->GetControlPointsCount(); 
	
	for(unsigned int i = 0; i < ctrlPointCount; ++i) 
	{ 
		CtrlPoint* currCtrlPoint = new CtrlPoint(); 
		XMFLOAT3 currPosition; 
		currPosition.x = static_cast(currMesh->GetControlPointAt(i).mData[0]); 
		currPosition.y = static_cast(currMesh->GetControlPointAt(i).mData[1]); 
		currPosition.z = static_cast(currMesh->GetControlPointAt(i).mData[2]); 
		currCtrlPoint->mPosition = currPosition; 
		mControlPoints = currCtrlPoint; 
	} 
} 

然后你会问“我如何获得 UV、法线、切线、副法线?” 好吧,请想象一下这样的网格:你有网格的主体,但这只是它的几何形状。 该物体没有任何关于其表面的信息。 换句话说,你有这个形状,但你没有任何关于这个形状的表面看起来如何的信息。 FBX 引入了这种层(Layer)的感觉,它覆盖了网格体的主体。

这就像你有一个盒子,然后用礼品纸包裹它。 这张礼品纸是FBX中网格的图层。 在图层中,你可以获取UV、法线、切线、副法线的信息。 然而,你可能已经问过我了。 如何将控制点与图层中的信息关联起来? 好吧,这是非常棘手的部分,请让我展示一些代码,然后逐行解释它。 不失一般性,我以 Binormal 为例:

在查看该函数之前,我们先回顾一下它的参数。

  • FbxMesh* inMesh:我们尝试导出的网格
  • int inCtrlPointIndex:控制点的索引。 我们需要这个,因为我们想要将图层信息与顶点(控制点)相关联
  • int inVertexCounter:这是我们正在处理的当前顶点的索引。
  • XMFLOAT3& outNormal:输出。 我们通过引用传递,以便我们可以在该函数内修改此变量并将其用作我们的输出看到这些参数后,你可能会问我“既然你说 控制带你基本上是 FBX SDK 中的顶点,为什么有 inCtrlPointIndex 和 inVertexCounter? 他们不是同一个东西吗?” 不,它们不一样。

正如我之前所解释的,控制点是几何体上的物理顶点。 让我们以四边形为例。 给定一个四边形(2 个三角形),有多少个控制点? 答案是 4。但是我们基于三角形的游戏引擎中有多少个顶点呢? 答案是 6,因为我们有 2 个三角形,每个三角形有 3 个顶点。 2 * 3 = 6 FBXSDK 的控制点和我们的顶点之间的主要区别在于,我们的顶点具有“每个三角形”的感觉,但 FBXSDK 的控制点没有。 我们将在下面的代码解释中回到这一点。 因此,如果你仍然对游戏引擎中 FBXSDK 的控制点和顶点没有非常清晰的了解,请不要担心。

要记住的一件事是,在该函数之外,我们使用循环来遍历该网格中所有三角形的所有顶点。 如果你感到困惑并且不知道“我们正在使用循环遍历此网格中所有三角形的所有顶点”是什么意思,请查看本文开头的内容。 这就是为什么我们可以有像 inCtrlPointIndex 和 inVertexCounter 这样的参数。

void FBXExporter::ReadNormal(FbxMesh* inMesh, int inCtrlPointIndex, int inVertexCounter, XMFLOAT3& outNormal) 
{ 
	if(inMesh->GetElementNormalCount() < 1) { throw std::exception("Invalid Normal Number"); } 
	
	FbxGeometryElementNormal* vertexNormal = inMesh->GetElementNormal(0); 
	
	switch(vertexNormal->GetMappingMode()) 
	{ 
		case FbxGeometryElement::eByControlPoint: 
			switch(vertexNormal->GetReferenceMode()) 
			{ 
				case FbxGeometryElement::eDirect: 
				{ 
					outNormal.x = static_cast(vertexNormal->GetDirectArray().GetAt(inCtrlPointIndex).mData[0]); 
					outNormal.y = static_cast(vertexNormal->GetDirectArray().GetAt(inCtrlPointIndex).mData[1]); 
					outNormal.z = static_cast(vertexNormal->GetDirectArray().GetAt(inCtrlPointIndex).mData[2]); 
				} 
				break; 
				
				case FbxGeometryElement::eIndexToDirect: 
				{ 
					int index = vertexNormal->GetIndexArray().GetAt(inCtrlPointIndex); 
					outNormal.x = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[0]); 
					outNormal.y = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[1]); 
					outNormal.z = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[2]); 
				} 
				break; 
				default: 
					throw std::exception("Invalid Reference"); 
			} 
			break; 
		case FbxGeometryElement::eByPolygonVertex: 
			switch(vertexNormal->GetReferenceMode()) 
			{ 
				case FbxGeometryElement::eDirect: 
				{ 
					outNormal.x = static_cast(vertexNormal->GetDirectArray().GetAt(inVertexCounter).mData[0]); 
					outNormal.y = static_cast(vertexNormal->GetDirectArray().GetAt(inVertexCounter).mData[1]); 
					outNormal.z = static_cast(vertexNormal->GetDirectArray().GetAt(inVertexCounter).mData[2]); 
				} 
				break; 
				case FbxGeometryElement::eIndexToDirect: 
				{ 
					int index = vertexNormal->GetIndexArray().GetAt(inVertexCounter); 
					outNormal.x = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[0]); 
					outNormal.y = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[1]); 
					outNormal.z = static_cast(vertexNormal->GetDirectArray().GetAt(index).mData[2]); 
				} 
				break; 
				default: throw std::exception("Invalid Reference"); 
			} 
			
			break; 
		} 
	} 

好吧,这很长,但请不要害怕。 其实很简单。 这为我们获取了图层中的法线信息 FbxGeometryElementNormal* vertexNormal = inMesh->GetElementNormal(0);

第一个 switch 语句是关于 MappingMode() 的。 对于游戏引擎,我认为我们只需要考虑 FbxGeometryElement::eByControlPoint 和 FbxGeometryElement::eByPolygonVertex 。 让我解释一下这两种模式。

正如我所说,控制点基本上就是顶点。 然而,有一个问题。 尽管立方体有 8 个控制点,但如果你希望立方体看起来正确,则其法线将超过 8 个。 原因是,如果你有锐利的边缘,我们必须为同一控制点分配多个法线,以保证锐利的感觉。 这就是我们的游戏引擎中的顶点概念出现的时候,因为即使立方体的顶点具有相同的位置,在游戏引擎中,你也很可能最终得到 3 个位置相同但 3 个顶点 不同的法线。

因此,当你没有锋利边缘的情况时, FbxGeometryElement::eByControlPoint 因此每个控制点只有一个法线。 FbxGeometryElement::eByPolygonVertex 是当你有锐边并且需要获取每个面上每个顶点的法线时,因为每个面都为同一控制点分配了不同的法线。

因此, FbxGeometryElement::eByControlPoint 意味着我们可以通过控制点的索引来精确定位控制点的法线,而 FbxGeometryElement::eByPolygonVertex 意味着我们可以通过顶点的索引来精确定位面上顶点的法线。

这是一个更具体、更深入的例子,说明了FBX SDK的控制点和顶点在游戏引擎中的区别以及为什么当我谈论这个函数的参数时,我说我们必须同时传入 inCtrlPointIndex和 inVertexCounter。 因为我们不知道需要哪一个来获取我们需要的信息,所以我们最好将两者都传入。

现在我们有另一个 switch 语句嵌套在里面,我们在 ReferenceMode() 上“切换”。 这是 FBX 正在做的某种优化,与计算机图形学中的索引缓冲区相同。 你不想多次使用相同的 Vector3; 相反,可以使用其索引来引用它。

FbxGeometryElement::eDirect 意味着你可以直接使用控制点索引或面顶点索引来引用我们的法线。

FbxGeometryElement::eIndexToDirect 意味着使用控制点索引或面顶点索引只会给我们一个指向我们想要的法线的索引,我们必须使用这个索引来找到实际的法线。 这行代码为我们提供了所需的索引 :

int index = vertexNormal->GetIndexArray().GetAt(inVertexCounter); 

这些是提取网格的位置和“层”信息的主要步骤。

现在我们转向动画,这是 FBX 导出的难点。

2、动画数据

因此,让我们考虑一下 FBX 需要什么才能使动画在我们的渲染器(游戏引擎)中正常工作。

  • 骨骼层次结构。 哪个关节是哪个关节的父关节
  • 对于每个顶点,我们需要 4 个 SkinningWeight-JointIndex 对
  • 每个关节的绑定姿势矩阵,用于计算全局绑定姿势的逆矩阵
  • 时间 t 时的变换矩阵,以便我们可以将网格变换为该姿势以实现动画

获得骨骼层次结构非常简单:基本上,我们从场景的根节点执行递归深度优先搜索,然后逐层深入。 节点是 FBX 场景的构建块。 FBX 文件中有许多节点,每种类型的节点都包含某种类型的信息。 如果一个节点是骨架类型,我们将它添加到我们的关节列表中,并且它的索引将只是列表的大小。

因此,我们可以保证父级的索引始终小于子级的索引。 如果你想要存储本地变换并手动计算子项在时间 t 的变换,这是必要的。 但如果你像我一样使用全局转换,你不一定需要这样。 注意:如果你不熟悉深度优先搜索的概念。

看看这个页面 和这个页面。

阅读这些页面后,你可能会问“为什么我们不需要跟踪访问过的节点?” 答案是:骨骼层次结构是一棵树,而不是一张图。

void FBXExporter::ProcessSkeletonHierarchy(FbxNode* inRootNode) 
{ 
	for (int childIndex = 0; childIndex < inRootNode->GetChildCount(); ++childIndex) 
	{ 
		FbxNode* currNode = inRootNode->GetChild(childIndex); 
		ProcessSkeletonHierarchyRecursively(currNode, 0, 0, -1); 
	} 
} 

// inDepth is not needed here, I used it for debug but forgot to remove it 
void FBXExporter::ProcessSkeletonHierarchyRecursively(FbxNode* inNode, int inDepth, int myIndex, int inParentIndex) 
{ 
	if(inNode->GetNodeAttribute() && inNode->GetNodeAttribute()->GetAttributeType() && inNode->GetNodeAttribute()->GetAttributeType() == FbxNodeAttribute::eSkeleton) 
	{ 
		Joint currJoint; 
		currJoint.mParentIndex = inParentIndex; 
		currJoint.mName = inNode->GetName(); 
		mSkeleton.mJoints.push_back(currJoint); 
	} 
	
	for (int i = 0; i < inNode->GetChildCount(); i++) 
	{ 
		ProcessSkeletonHierarchyRecursively(inNode->GetChild(i), inDepth + 1, mSkeleton.mJoints.size(), myIndex); 
	} 
} 

现在我们需要获取每个顶点的 SkinningWeight-JointIndex 对。 不幸的是,我的动画代码不是很干净,所以下面的函数同时执行步骤 2、3、4。 我将仔细检查代码,请不要失去耐心。 这主要是因为 FBX 存储信息的方式阻止我有效地在单独的函数中获取数据。 如果我想分离我的代码,我需要多次遍历相同的数据。

在看任何代码之前,请让我解释一下 FBX SDK 中使用的术语。 我认为这是大多数人感到困惑的部分,因为 FBX SDK 的关键字与我们(游戏开发人员)的关键字不匹配。 在FBX中,有一个叫做变形器(Deformer)的东西。 我将变形器视为使网格变形的一种方式。

在 Maya 中,你可以使用骨骼变形器,但也可以使用约束(Constraints)来使网格变形。 我认为你可以将“变形器”视为网格的整个骨架。 在每个“变形器”(我认为通常网格只有一个)内,都有簇(Cluster)。 每个簇既是又不是一个关节……你可以把一个簇看成一个关节,但实际上,每个簇内部都有一个“链接”。 这个“链接”实际上是真正的关节,它包含了我需要的有用信息。 现在我们深入研究代码:

void FBXExporter::ProcessJointsAndAnimations(FbxNode* inNode) 
{ 
    FbxMesh* currMesh = inNode->GetMesh(); 
    unsigned int numOfDeformers = currMesh->GetDeformerCount(); 
    
    // This geometry transform is something I cannot understand 
    // I think it is from MotionBuilder 
    // If you are using Maya for your models, 99% this is just an 
    // identity matrix 
    // But I am taking it into account anyways...... 
    FbxAMatrix geometryTransform = Utilities::GetGeometryTransformation(inNode); 
    
    // A deformer is a FBX thing, which contains some clusters 
    // A cluster contains a link, which is basically a joint 
    // Normally, there is only one deformer in a mesh 
    for (unsigned int deformerIndex = 0; deformerIndex < numOfDeformers; ++deformerIndex) 
    {
        // There are many types of deformers in Maya, 
        // We are using only skins, so we see if this is a skin 
        FbxSkin* currSkin = reinterpret_cast(currMesh->GetDeformer(deformerIndex, FbxDeformer::eSkin)); 
        
        if (!currSkin) { continue; } 
        
        unsigned int numOfClusters = currSkin->GetClusterCount(); 
        for (unsigned int clusterIndex = 0; clusterIndex < numOfClusters; ++clusterIndex) 
        { 
            FbxCluster* currCluster = currSkin->GetCluster(clusterIndex); 
            std::string currJointName = currCluster->GetLink()->GetName(); 
            unsigned int currJointIndex = FindJointIndexUsingName(currJointName); 
            FbxAMatrix transformMatrix; FbxAMatrix transformLinkMatrix; 
            FbxAMatrix globalBindposeInverseMatrix; 
            currCluster->GetTransformMatrix(transformMatrix); 
            
            // The transformation of the mesh at binding time 
            currCluster->GetTransformLinkMatrix(transformLinkMatrix); 
            
            // The transformation of the cluster(joint) at binding time from joint space to world space 
            globalBindposeInverseMatrix = transformLinkMatrix.Inverse() * transformMatrix * geometryTransform; 
            
            // Update the information in mSkeleton 
            mSkeleton.mJoints[currJointIndex].mGlobalBindposeInverse = globalBindposeInverseMatrix; 
            mSkeleton.mJoints[currJointIndex].mNode = currCluster->GetLink(); 
            
            // Associate each joint with the control points it affects 
            unsigned int numOfIndices = currCluster->GetControlPointIndicesCount(); 
            for (unsigned int i = 0; i < numOfIndices; ++i) 
            { 
                BlendingIndexWeightPair currBlendingIndexWeightPair; 
                currBlendingIndexWeightPair.mBlendingIndex = currJointIndex; 
                
                currBlendingIndexWeightPair.mBlendingWeight = currCluster->GetControlPointWeights(); 
                mControlPoints[currCluster->GetControlPointIndices()]->mBlendingInfo.push_back(currBlendingIndexWeightPair); 
            } 
            
            // Get animation information 
            // Now only supports one take 
            FbxAnimStack* currAnimStack = mFBXScene->GetSrcObject(0); 
            FbxString animStackName = currAnimStack->GetName(); 
            mAnimationName = animStackName.Buffer(); 
            FbxTakeInfo* takeInfo = mFBXScene->GetTakeInfo(animStackName); 
            FbxTime start = takeInfo->mLocalTimeSpan.GetStart(); 
            FbxTime end = takeInfo->mLocalTimeSpan.GetStop(); 
            mAnimationLength = end.GetFrameCount(FbxTime::eFrames24) - start.GetFrameCount(FbxTime::eFrames24) + 1; 
            Keyframe** currAnim = &mSkeleton.mJoints[currJointIndex].mAnimation; 
            
            for (FbxLongLong i = start.GetFrameCount(FbxTime::eFrames24); i <= end.GetFrameCount(FbxTime::eFrames24); ++i) 
            { 
                FbxTime currTime; 
                currTime.SetFrame(i, FbxTime::eFrames24);
                *currAnim = new Keyframe(); 
                (*currAnim)->mFrameNum = i; 
                FbxAMatrix currentTransformOffset = inNode->EvaluateGlobalTransform(currTime) * geometryTransform; 
                (*currAnim)->mGlobalTransform = currentTransformOffset.Inverse() * currCluster->GetLink()->EvaluateGlobalTransform(currTime); 
                currAnim = &((*currAnim)->mNext); 
            } 
        } 
    } 
    
    // Some of the control points only have less than 4 joints 
    // affecting them. 
    // For a normal renderer, there are usually 4 joints 
    // I am adding more dummy joints if there isn't enough 
    BlendingIndexWeightPair currBlendingIndexWeightPair; 
    currBlendingIndexWeightPair.mBlendingIndex = 0; 
    currBlendingIndexWeightPair.mBlendingWeight = 0; 
    for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr) 
    { 
        for(unsigned int i = itr->second->mBlendingInfo.size(); i <= 4; ++i) 
        { 
            itr->second->mBlendingInfo.push_back(currBlendingIndexWeightPair); 
        } 
    } 
} 

一开始我有这个:

// This geometry transform is something I cannot understand 
// I think it is from MotionBuilder 
// If you are using Maya for your models, 99% this is just an 
// identity matrix 
// But I am taking it into account anyways...... 
FbxAMatrix geometryTransform = Utilities::GetGeometryTransformation(inNode); 

嗯,这是我在 FBX SDK 论坛上看到的。 那里的官员告诉我们应该考虑“几何变换”。 但根据我的经验,大多数时候,这个“GeometricTransform”只是一个单位矩阵。 无论如何,要获得这个“GeometricTransform”,请使用以下函数:

FbxAMatrix Utilities::GetGeometryTransformation(FbxNode* inNode) 
{ 
    if (!inNode) 
    { 
        throw std::exception("Null for mesh geometry"); 
    } 
    
    const FbxVector4 lT = inNode->GetGeometricTranslation(FbxNode::eSourcePivot); 
    const FbxVector4 lR = inNode->GetGeometricRotation(FbxNode::eSourcePivot); 
    const FbxVector4 lS = inNode->GetGeometricScaling(FbxNode::eSourcePivot); 
    
    return FbxAMatrix(lT, lR, lS); 
} 

这段代码中最重要的事情是如何获得每个关节的全局绑定姿势的倒数。 这部分非常棘手并且搞砸了很多人。 我将详细解释这一点。

FbxAMatrix transformMatrix; 
FbxAMatrix transformLinkMatrix; 
FbxAMatrix globalBindposeInverseMatrix;
currCluster->GetTransformMatrix(transformMatrix); 

// The transformation of the mesh at binding time 
currCluster->GetTransformLinkMatrix(transformLinkMatrix); 

// The transformation of the cluster(joint) at binding time from joint space to world space 
globalBindposeInverseMatrix = transformLinkMatrix.Inverse() * transformMatrix * geometryTransform; 

// Update the information in mSkeleton 
mSkeleton.mJoints[currJointIndex].mGlobalBindposeInverse = globalBindposeInverseMatrix;  

那么我们就从这个 GetTransformMatrix开始吧。 TransformMatrix 实际上是一个遗留的东西。 它是整个网格在绑定时的全局变换,并且所有簇都具有完全相同的变换矩阵。 如果你的艺术家有良好的习惯,并且在装配模型之前,他们在模型的所有通道上“冻结变换”,则不需要此矩阵。 如果你的艺术家执行“冻结变换”,那么这个矩阵将只是一个单位矩阵。 现在我们继续 GetTransformLinkMatrix。 这就是动画导出代码的本质。 这是 Maya 中绑定时簇(关节)从关节空间到世界空间的变换。

现在我们已经准备好了,我们可以得到每个关节的全局绑定姿势的逆。 我们最终想要的是下面代码中的 InverseOfGlobalBindPoseMatrix:

VertexAtTimeT = TransformationOfPoseAtTimeT * InverseOfGlobalBindPoseMatrix * VertexAtBindingTime

为了得到这个,我们这样做:

transformLinkMatrix.Inverse() * transformMatrix * GeometryTransform

现在我们距离动画还有两步。 我们需要获取每个顶点的 SkinningWeight-JointIndex 对,并且仍然需要获取动画中不同时间的变换。让我们首先处理 SkinningWeight-JointIndex 对。 在我们的游戏引擎中,我们有这样的关系: Vertex -> 4 SkinningWeight-JointIndex 对。

然而,在 FBX SDK 中,这种关系是相反的。 每个簇都有一个其影响的所有控制点(顶点)及其影响程度的列表。 下面的代码以我们喜欢的格式获取关系,但请记住,当我处理控制点时,我根据控制点的索引将所有控制点存储到地图中。 这就是我们可以获利的地方。

有了这个映射,我们可以在 O(1) 内查找并更新集群影响的控制点。

// Associate each joint with the control points it affects 
unsigned int numOfIndices = currCluster->GetControlPointIndicesCount(); 
for (unsigned int i = 0; i < numOfIndices; ++i) 
{ 
    BlendingIndexWeightPair currBlendingIndexWeightPair; 
    currBlendingIndexWeightPair.mBlendingIndex = currJointIndex; 
    currBlendingIndexWeightPair.mBlendingWeight = currCluster->GetControlPointWeights(); 
    mControlPoints[currCluster->GetControlPointIndices()]->mBlendingInfo.push_back(currBlendingIndexWeightPair); 
} 

现在我们只需要拼图中的最后一块:动画中时间 t 的变换。 请注意,这部分是我做得不好的地方,我的方式不是很优化,因为我得到了每个关键帧。 理想情况下应该做的是获取密钥并在它们之间进行插值,但我想这是空间和速度之间的权衡。 另外,我没有认真研究 FBX 的动画层次结构。 实际上,FBX 文件中存储了一条动画曲线,通过一些工作,你可以访问它并获得所需的精简和干净。

// Now only supports one take 
FbxAnimStack* currAnimStack = mFBXScene->GetSrcObject(0); 
FbxString animStackName = currAnimStack->GetName(); 
mAnimationName = animStackName.Buffer(); 
FbxTakeInfo* takeInfo = mFBXScene->GetTakeInfo(animStackName); 
FbxTime start = takeInfo->mLocalTimeSpan.GetStart(); 
FbxTime end = takeInfo->mLocalTimeSpan.GetStop(); 
mAnimationLength = end.GetFrameCount(FbxTime::eFrames24) - start.GetFrameCount(FbxTime::eFrames24) + 1; 
Keyframe** currAnim = &mSkeleton.mJoints[currJointIndex].mAnimation; 

for (FbxLongLong i = start.GetFrameCount(FbxTime::eFrames24); i <= end.GetFrameCount(FbxTime::eFrames24); ++i) 
{ 
    FbxTime currTime; 
    currTime.SetFrame(i, FbxTime::eFrames24); 
    *currAnim = new Keyframe(); 
    (*currAnim)->mFrameNum = i; 
    FbxAMatrix currentTransformOffset = inNode->EvaluateGlobalTransform(currTime) * geometryTransform; 
    (*currAnim)->mGlobalTransform = currentTransformOffset.Inverse() * currCluster->GetLink()->EvaluateGlobalTransform(currTime); 
    currAnim = &((*currAnim)->mNext); 
} 

这部分非常简单 - 唯一需要注意的是 Maya 目前不支持多镜头动画(也许 MotionBuilder 支持)。 我会根据有多少人阅读这篇文章来决定是否写导出材料,但这很简单,可以通过“ImportScene”示例来学习

3、DirectX 和 OpenGL 转换

我对此 FBX 导出器的目标是提供一种从 FBX 文件中提取数据的方法,并以自定义格式输出数据,以便阅读器的渲染器可以获取数据并渲染它。 渲染器内部不需要任何转换,因为所有转换工作都落在导出器本身上。 在我说任何内容之前,我需要澄清一下,只有当你在 Maya 中制作模型/动画并使用其默认坐标系(X-Right、Y-Up、 Z-超出屏幕)。

如果你想将模型/动画导入OpenGL,那么更有可能你不需要执行任何额外的转换步骤,因为我认为默认情况下OpenGL具有与Maya相同的右手坐标系,即(X-Right, Y 向上,Z 超出屏幕)。 在FBX SDK的示例代码“ViewScene”中,没有对数据进行任何转换,它使用OpenGL作为渲染器,并使用OpenGL中的默认坐标系。 因此,如果你确实遇到麻烦,请查看该代码。

但是,如果你指定自己的坐标系,则可能需要进行一些转换。 现在是 DirectX 的时代了,我在网上看到大多数问题都来自于人们想要在 DirectX 中渲染 FBX 模型/动画的情况。 因此,如果你想将模型/动画导入 DirectX,很可能需要进行一些转换。

我只会解决左手“X-Right,Y-Up,Z-Into Screen”坐标系与背面剔除的情况,因为从我读到的帖子来看,大多数人在使用时都使用这个系统 DirectX。 这确实意味着任何一般性的事情; 这只是我的经验观察。 你需要执行以下操作将坐标从右手坐标系统“X-Right,Y-Up,Z-Out Of Screen”转换为左手“X-Right,Y-Up,Z-Into Screen”系统 :

  • 位置、法线、副法线、切线
  • 我们需要对 UV 向量的 Z 分量求反 ,使 V = 1.0f - V
  • 三角形的顶点顺序,从 Vertex0、Vertex1、Vertex2 更改为 Vertex0, Vertex2、Vertex1(基本上颠倒剔除顺序)矩阵
  • 获取矩阵的平移分量,对其 Z 分量取反
  • 获取矩阵的旋转分量,对其 X 和 Y 分量取反
  • 我认为如果你使用 XMMath 库,则不需要进行转置。

要使用我的转换方式,你需要分解矩阵并分别更改其平移、旋转和缩放。 幸运的是,FBX SDK 提供了分解矩阵的方法,只要你的矩阵是 FbxAMatrix(FBX 仿射矩阵)。 下面的示例代码向你展示了如何操作:

FbxAMatrix input; 

//Assume this matrix is the one to be converted. 
FbxVector4 translation = input.GetT(); 
FbxVector4 rotation = input.GetR(); 
translation.Set(translation.mData[0], translation.mData[1], -translation.mData[2]); 

// This negate Z of Translation Component of the matrix 
rotation.Set(-rotation.mData[0], -rotation.mData[1], rotation.mData[2]); 

// This negate X,Y of Rotation Component of the matrix 
// These 2 lines finally set "input" to the eventual converted result 
input.SetT(translation); 
input.SetR(rotation); 

如果你的动画有Scaling,你需要自己弄清楚需要做什么转换,因为我没有遇到过Scaling发生的情况。

4、局限性及超越

因此,本教程仅旨在帮助你开始使用 FBXSDK。 我自己是个菜鸟,所以我的很多技术可能效率很低。 在这里我把我认为存在的问题列出来。 在此过程中,读者可以自行决定是否使用我的技术以及需要注意什么。

  • 该转换方法仅适用于从 Maya 导出的具有 Maya 右手 X-Right、Y-Up、Z-Out 坐标系的模型/动画。 我的转换技术很可能不适用于其他建模软件(Blender、MotionBuilder、3ds Max)
  • 我提取动画的方式效率低下。 我需要在导出动画之前烘焙动画,然后以 24 帧/秒的速率获取所有关键帧。 这可能会导致巨大的内存消耗。 如果你知道如何使用关键帧而不是关键帧,请通过下面的评论告诉我。
  • 我的转换方法不处理动画中的缩放。 正如你从我的代码中看到的,当我提取动画时,我从不处理转换矩阵中的缩放分量。 因此,你需要自己弄清楚你的动画是否具有缩放功能。
  • 在本教程中,我没有包含删除重复顶点的代码,但实际上,如果你使用我的方法在不进行任何优化的情况下导出 FBX 文件,最终会得到很多重复顶点。 我做了一个比较,优化导出可以将文件大小减少 2/3…你会出现重复的原因是:如果你遍历网格中每个三角形的每个顶点,尽管相同的 Control 法线不同的点会很好处理,法线相同的同一个控制点会被计数1次以上!

原文链接:FBX SDK简明教程 — BimAnt

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/839202.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux操作系统~Linux基础知识相关题

整卷阅览&#xff1a; 想要获取试卷原版请点击以下链接下载&#xff1a; https://download.csdn.net/download/qq_53142796/88168132https://download.csdn.net/download/qq_53142796/88168132 解题过程&#xff1a; 选择题&#xff08;每小题2分&#xff0c;共30分&#xff…

【设计模式】-建造者模式

Java建造者模式&#xff1a;创建复杂对象的灵活构建者 在软件开发中&#xff0c;我们经常遇到需要创建一个复杂对象的情况。如果使用传统的构造函数进行对象创建&#xff0c;可能会导致构造函数参数过多&#xff0c;难以管理和维护。建造者模式&#xff08;Builder Pattern&am…

FFmpeg中硬解码后深度学习模型的图像处理dnn_processing(一)

ffmpeg 硬件解码 ffmpeg硬件解码可以使用最新的vulkan来做&#xff0c;基本上来说&#xff0c;不挑操作系统是比较重要的&#xff0c;如果直接使用cuda也是非常好的选择。 AVPixelFormat sourcepf AV_PIX_FMT_NV12;// AV_PIX_FMT_NV12;// AV_PIX_FMT_YUV420P;AVPixelFormat d…

Linux-centos花生壳实现内网穿透

Linux-centos花生壳实现内网穿透 官网教程 1.安装花生壳 下载网址 点击复制就可以复制下载命令了 wget "https://dl.oray.com/hsk/linux/phddns_5.2.0_amd64.rpm" -O phddns_5.2.0_amd64.rpm# 下载完成之后会多一个rpm文件 [rootlocalhost HuaSheng]# ls phddns_…

UE5+Paperzd问题

TopDown的2D游戏&#xff0c;遇到两个问题&#xff0c;第一问题是游戏一开始就会从tilemap上掉下去。第二个问题是没法和图层2上的物体做碰撞。 一、碰撞问题 1、创建的TileSet后&#xff0c;左侧选中一个tile后&#xff0c;一定要点击上边的Add Box&#xff0c;否则创建出来的…

jenkins的cicd操作

cicd概念 持续集成&#xff08; Continuous Integration&#xff09; 持续频繁的&#xff08;每天多次&#xff09;将本地代码“集成”到主干分支&#xff0c;并保证主干分支可用 持续交付&#xff08;Continuous Delivery&#xff09; 是持续集成的下一步&#xff0c;持续…

Julia 字典和集合

数组是一种集合&#xff0c;此外 Julia 也有其他类型的集合&#xff0c;比如字典和 set&#xff08;无序集合列表&#xff09;。 字典 字典是一种可变容器模型&#xff0c;且可存储任意类型对象。 字典的每个键值 key>value 对用 > 分割&#xff0c;每个键值对之间用逗…

STM32的电动自行车信息采集上报系统(学习)

摘要 针对电动自行车实时监管不便的问题&#xff0c;设计了一种基于STM32的电动自行车信息采集系统&#xff0c;通过获取电池、位置和行驶状态信息并上报到服务器中&#xff0c;实现实时监管。 通过多路串口请求电池、行驶状态和位置信息&#xff0c;以并发方式进行数据接收、…

亿欧智库:2023中国宠物行业新趋势洞察报告(附下载)

关于报告的所有内容&#xff0c;公众【营销人星球】获取下载查看 核心观点 户外赛道本质上迎合了全球共性需求的增长&#xff0c;从养宠意愿的转化到养宠生活的需求&#xff0c;多层次的需求推动行业发展新趋势 从需求端进行分析&#xff0c;可以将养宠意愿的转化分为三个层…

nginx部署以及反向代理多域名实现HTTPS访问

nginx部署以及反向代理多域名实现 1.nginx部署 1.1 编写nginx部署文件 docker-compose.yml version: 3 services: nginx:restart: always image: nginx:1.20container_name: nginx-mainports:- 80:80- 443:443volumes: # 基础配置- /opt/nginx_main/nginx-info/nginx.conf:/…

并查集模板-两个操作:合并集合和查询两个元素是否属于同一个集合

一、链接 836. 合并集合 二、题目 一共有 nn 个数&#xff0c;编号是 1∼n1∼n&#xff0c;最开始每个数各自在一个集合中。 现在要进行 mm 个操作&#xff0c;操作共有两种&#xff1a; M a b&#xff0c;将编号为 aa 和 bb 的两个数所在的集合合并&#xff0c;如果两个数…

在 aosp 中启用 Material You design

作者&#xff1a;Mr_万能胶 近期研究了一下如何在 aosp 中启用 Material You design&#xff0c;在把踩过的坑记录一下&#xff0c;方便后续有厂商可以快速集成。 本文基于 aosp 最新代码&#xff0c;版本号为 Android 13&#xff0c;并使用 Cuttlefish 快速验证。 Material …

消息队列项目(3)

Message 存储 由于 Message 以后会是一个比较庞大的数据, 放在数据库里并不合适, 因此我们要将它放在二进制文件中 因为 Message 和 Queue 是捆绑在一起的, 因此我们将目录名设置成 QueueName, 然后这些目录是存储在之前的 data 里 就像这样: 在 testQueue 中有包含两个文件…

LeetCode 0021. 合并两个有序链表

【LetMeFly】21.合并两个有序链表 力扣题目链接&#xff1a;https://leetcode.cn/problems/merge-two-sorted-lists/ 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1&#xff1a; 输入&#xff1a;l1 [1,2,4], l…

-bash: fork: retry: Resource temporarily unavailable 问题解决

错误提示&#xff1a; -bash: fork: retry: Resource temporarily unavailable 错误分析&#xff1a;之前已经出现过这种资源限制的报错提醒&#xff0c;然后整个系统可用的连接数就已经用完了&#xff0c;无法使用工具来获取系统信息&#xff0c;所以将运行的任务脚本kill后开…

Cesium引入vite + vue3

下载Cesium yarn add cesium下载cesium-vite 插件 yarn add vite-plugin-cesium使用 vite.config.js import { defineConfig } from vite import vue from vitejs/plugin-vue import WindiCSS from vite-plugin-windicss import cesium from vite-plugin-cesium; //引入插件…

Stephen Wolfram:嵌入的概念

The Concept of Embeddings 嵌入的概念 Neural nets—at least as they’re currently set up—are fundamentally based on numbers. So if we’re going to to use them to work on something like text we’ll need a way to represent our text with numbers. And certain…

如何制作网红小和尚视频

文章目录 写在前面需要用到的工具制作过程制作一张自己喜欢的底图制作爆款文案将文案转为语音使用底图和语音生成动画去除水印使用剪映生成视频 写在后面 写在前面 最近我尝试制作小和尚的视频&#xff0c;使用了一些AI技术&#xff0c;制作成本相对较低。这次经历让我感受到了…

工厂模式(C++)

定义 定义一个用于创建对象的接口&#xff0c;让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟(目的:解耦&#xff0c;手段:虚函数)到子类。 应用场景 在软件系统中&#xff0c;经常面临着创建对象的工作;由于需求的变化&#xff0c;需要创建的对象的具体类…

c语言——计算一串字符的长度

//计算一串字符的长度 //在main函数中输出一个字符&#xff0c;并且计算出该字符的长度。 #include<stdio.h> #include<stdlib.h> int length(char *s){int i0;while(*s!\0){i;s;}return i;} int main() {int len;char str[20];printf("输入字符串&#xff1a…