- UE5 精度提高后,以前项目迁移,处理一下double->float 问题;
- 结构对应;
- 接口对应;
目录
参考文件
Engine\Source\Runtime\Core\Public\CoreFwd.h
Engine\Source\Runtime\Core\Public\Misc\LargeWorldCoordinates.h
Engine\Source\Runtime\Core\Public\Math\*
Engine\Source\Runtime\Core\Public\Math\TransformVectorized.h
Engine\Source\Runtime\Core\Public\Math\Rotator.h
Engine\Source\Runtime\Core\Public\Math\Plane.h
宏定义
// Args - TYPE, DIMENSION, [UE_TYPENAME], [COMPONENT_TYPE]. e.g. Vector, 3, FVector, double // LWC_TODO: Remove COMPONENT_TYPE
#define UE_DECLARE_LWC_TYPE(...) FORCE_EXPAND(UE_DECLARE_LWC_TYPE_SELECT(__VA_ARGS__)(__VA_ARGS__))
// Necessary to convince the MSVC preprocessor to play ball with variadic args - https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly
#define FORCE_EXPAND(X) X
#define UE_DECLARE_LWC_TYPE_SELECT(...) FORCE_EXPAND(UE_LWC_MACRO_SELECT(__VA_ARGS__, UE_DECLARE_LWC_TYPE_EX, UE_DECLARE_LWC_TYPE_3, UE_DECLARE_LWC_TYPE_2, UE_DECLARE_LWC_TYPE_1 ))
#define UE_LWC_MACRO_SELECT(PAD1, PAD2, PAD3, PAD4, MACRO, ...) MACRO
#define UE_DECLARE_LWC_TYPE_2(TYPE, DIM) UE_DECLARE_LWC_TYPE_3(TYPE, DIM, F##TYPE)
#define UE_DECLARE_LWC_TYPE_3(TYPE, DIM, UE_TYPENAME) UE_DECLARE_LWC_TYPE_EX(TYPE, DIM, UE_TYPENAME, double)
// Forward declaration of LWC supported core types
#define UE_DECLARE_LWC_TYPE_EX(TYPE, CC, DEFAULT_TYPENAME, COMPONENT_TYPE) \
namespace UE { namespace Math { template<typename T> struct T##TYPE; } } \
typedef UE::Math::T##TYPE<float> F##TYPE##CC##f; /* FVector3f */ \
typedef UE::Math::T##TYPE<double> F##TYPE##CC##d; /* FVector3d */ \
typedef UE::Math::T##TYPE<COMPONENT_TYPE> DEFAULT_TYPENAME; /* FVector */ \
namespace ispc { struct DEFAULT_TYPENAME; } /* ISPC forward declaration */
宏应用
UE_DECLARE_LWC_TYPE(Matrix, 44);
UE_DECLARE_LWC_TYPE(Plane, 4);
UE_DECLARE_LWC_TYPE(Quat, 4);
UE_DECLARE_LWC_TYPE(Rotator, 3);
UE_DECLARE_LWC_TYPE(Transform, 3);
UE_DECLARE_LWC_TYPE(Vector2,, FVector2D);
UE_DECLARE_LWC_TYPE(Vector, 3);
UE_DECLARE_LWC_TYPE(Vector4);
解析示例
//解析UE_DECLARE_LWC_TYPE(Vector, 3);
FORCE_EXPAND(UE_DECLARE_LWC_TYPE_SELECT(Vector, 3)(Vector, 3))
UE_DECLARE_LWC_TYPE_SELECT(Vector, 3)(Vector, 3)
FORCE_EXPAND(UE_LWC_MACRO_SELECT(Vector, 3, UE_DECLARE_LWC_TYPE_EX, UE_DECLARE_LWC_TYPE_3, UE_DECLARE_LWC_TYPE_2, UE_DECLARE_LWC_TYPE_1 ))(Vector, 3)
UE_LWC_MACRO_SELECT(Vector, 3, UE_DECLARE_LWC_TYPE_EX, UE_DECLARE_LWC_TYPE_3, UE_DECLARE_LWC_TYPE_2, UE_DECLARE_LWC_TYPE_1 )(Vector, 3)
UE_DECLARE_LWC_TYPE_2(Vector, 3)
UE_DECLARE_LWC_TYPE_3(Vector, 3, F##Vector)
UE_DECLARE_LWC_TYPE_EX(Vector, 3, FVector, double)
namespace UE
{
namespace Math
{
template<typename T> struct TVector;
}
}
typedef UE::Math::TVector<float> FVector3f; /* FVector3f */ \
typedef UE::Math::TVector<double> FVector3d; /* FVector3d */ \
typedef UE::Math::TVector<double> FVector; /* FVector */ \
namespace ispc { struct FVector; } /* ISPC forward declaration */
UE4<->UE5
# | UE4 | UE5 |
1 | FVector2D | UE::Math::TVector2<float> 或者 FVector2f |
2 | FVector | UE::Math::TVector<float> 或者 FVector3f |
3 | FTransform | UE::Math::TTransform<float> 或者 FTransform3f |
4 | FQuat | UE::Math::TQuat<float> 或者 FQuat4f |
5 | FVector4 | UE::Math::TVector4<float> 或者 FVector4f |
6 | FMatrix | UE::Math::TMatrix<float> 或者 FMatrix44f |
7 | FRotator | FRotator3f |
8 | FLookAtMatrix | UE::Math::TLookAtMatrix<float> |
9 | FPlane | FPlane4f |
10 | FRotationMatrix | FRotationMatrix44f |
11 | FQuatRotationMatrix | FQuatRotationMatrix44f |
UE4<->DX
# | DX | UE4 | |
1 | D3DXMatrixRotationAxis | FVector .RotateAngleAxis vecTarget = BaseDir.RotateAngleAxis (AngleDeg, Axis.GetSafeNormal ()); | 绕轴旋转(假设Axis. Size() == 1)。 |
FQuat.RotateVector //旋转后的向量 FVector vecTarget; FQuat RotatorQuat = FQuat(Axis, fAngleRad); vecTarget=RotatorQuat.RotateVector (vecSrc); | |||
D3DXMatrixRotationX | FTransform RotX(FRotator(0.0f, 0.0f, -90.0f));//Pitch-X轴,转-90度 matRotX = RotX.ToMatrixNoScale(); | 待确定 | |
D3DXMatrixRotationY | FMatrix RMatrix; FTransform RotY(FRotator(90.0f, 0.0f, 0.0f));//Pitch-Y轴,转90度 RMatrix = RotY.ToMatrixNoScale(); | 待确定 | |
D3DXVec3Cross | FVector::CrossProduct | 叉乘 | |
D3DXVec3Dot D3DXVec2Dot | FVector::DotProduct FVector2D::DotProduct | 点乘 | |
D3DXVec3Normalize D3DXVec2Normalize | FVector.GetSafeNormal() GetUnsafeNormal() GetSafeNormal2D GetUnsafeNormal2D() | 规范化 KINDA_SMALL_NUMBER | |
D3DXVec3TransformNormal |
|
| |
FQuat RotatorQuat = FQuat(任意旋转轴, 需旋转的弧度); retVec = RotatorQuat.RotateVector(基础方向); | 绕任意轴旋转 | ||
FQuat RotatorQuat = FQuat(AxisZ, 0.1745329); FMatrix RandomOrientation = FRotationMatrix::Make(RotatorQuat); FVector Rvec1 = RandomOrientation.TransformPosition(vec); | |||
D3DXVec3Length | FVector .Size() | 长度 | |
D3DXVec3LengthSq | FVector .SizeSquared() | 长度平方 | |
FVector .Size2D() | 2D长度 | ||
FVector .SizeSquared2D() | 2D长度平方 | ||
FVector .GetComponentForAxis | 获取指定轴分量 | ||
IsNearlyZero/IsZero() | 接近0 / ==0 | ||
IsUnit/IsNormalized() | 接近1 | ||
ToDirectionAndLength(FVector &OutDir, float &OutLength) | 转换为单位方向向量及其原长度。 | ||
FVector::PointsAreSame | 两点相同 | ||
FVector::Dist/ Distance/ DistXY/ Dist2D | 距离/2D距离 | ||
FVector::DistSquared/DistSquaredXY/DistSquared2D | 距离^2 | ||
FVector::Parallel/ Coincident/ Orthogonal | 两个法向量,平行/重合/垂直判断 | ||
FVector::Coplanar | 共面判断(法线几乎平行,且这些平面包含相同的点集) | ||
D3DXVECTOR4 | FVector4 | ||
D3DXCOLOR | FLinearColor | ||
D3DXVec3Lerp | FMath::Lerp | ||
D3DXVec3TransformCoord | vec4 = matrix.TransformVector(vec); vec41 = matrix.TransformPosition(vec); || vec5 = transf.TransformVector(vec); vec51 = transf.TransformPosition(vec); | ||
D3DXMatrixScaling | FMatrix ScaleMatrix( FPlane(sx, 0.0f, 0.0f, 0.0f), FPlane(0.0f, sy, 0.0f, 0.0f), FPlane(0.0f, 0.0f, sz, 0.0f), FPlane(0.0f, 0.0f, 0.0f, 1.0f) ); || FMatrix .ApplyScale | ||
D3DXQUATERNION | FQuat | ||
D3DXQuaternionSlerp | FQuat::SLerp | ||
D3DXQuaternionNormalize | FQuat.GetNormalized() | ||
D3DXQuaternionRotationYawPitchRoll ( _In_ FLOAT Yaw, _In_ FLOAT Pitch, _In_ FLOAT Roll ); | pOut = FQuat(Roll, Pitch, Yaw); | 待确定============ | |
D3DXQuaternionIdentity | FQuat::Identity | ||
D3DXMATRIX | FMatrix | ||
D3DXMatrixIdentity | FMatrix.SetIdentity(); | ||
D3DXMatrixMultiply | FMatrix.operator* | ||
D3DXMatrixRotationQuaternion | FQuatRotationMatrix::Make | 四元数,需标准化Normalize() | |
D3DXMatrixDecompose 将一般的三维变换矩阵分解成标量、旋转和平移分量。 | FMatrix.GetScaleVector(); | 返回3D scale vector | |
FMatrix.ToQuat(); | 将旋转矩阵转换为四元数。warning rotation part will need to be unit length for this to be right! | ||
FVector(m_Transform.M[3][0], m_Transform.M[3][1], m_Transform.M[3][2]) | |||
D3DXMatrixTranslation | FMatrix.ConcatTranslation (); | ||
D3DXMatrixLookAtLH | FLookAtMatrix | ||
D3DXMatrixOrthoLH | 2/w 0 0 0 0 2/h 0 0 0 0 1/(zf-zn) 0 0 0 zn/(zn-zf) 1 FMatrix( FPlane(2.0f/(Right-Left), 0, 0, 0 ), FPlane(0, 2.0f/(Top-Bottom), 0, 0 ), FPlane(0, 0, 1/(ZNear-ZFar), 0 ), FPlane(0, 0, ZNear/(ZNear-ZFar), 1 ) ); | ||
Note:Math
FMath | |
atan2f | FMath::Atan2() |
acosf | FMath::Acos |
tanf | FMath::Tan |
fabsf | FMath::Abs() |
问题
(1)"GenericDivide_FloatFloat": 不是 "UKismetMathLibrary" 的成员
处理:删除对 GeneratedCodeHelpers.h 文件的包含;
(2)byte
//S1.Windows引用前加
#ifdef _HAS_STD_BYTE
#undef _HAS_STD_BYTE
#endif
#define _HAS_STD_BYTE 0
//S2.除去对using namespace std;的使用;改为具体应用,如:
using std::string;
using std::stringstream;
using std::vector;
using std::map;
using std::make_pair;
using std::list;
using std::iterator;
(3)法线
//法线
T GetNormal()
{
T tNormal;
ZeroMemory(&tNormal, sizeof(T));
//float* pfDirection = (float*)&getDirection(), * pfNormal = (float*)&tNormal;
double* pfDirection = (double*)&getDirection(), * pfNormal = (double*)&tNormal;
int ubtSize = sizeof(T);
//int ubtCount = ubtSize / sizeof(float);
int ubtCount = ubtSize / sizeof(double);
pfNormal[0] = -1.0f * pfDirection[ubtCount - 1];
pfNormal[ubtCount - 1] = pfDirection[0];
return *(T*)pfNormal;
}