前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UE4投影矩阵[通俗易懂]

UE4投影矩阵[通俗易懂]

作者头像
全栈程序员站长
发布2022-11-10 15:23:42
1.1K0
发布2022-11-10 15:23:42
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

UE4投影矩阵

正交投影

代码语言:javascript
复制
class FOrthoMatrix
: public FMatrix
{ 

public:
/** * Constructor * * @param Width view space width * @param Height view space height * @param ZScale scale in the Z axis * @param ZOffset offset in the Z axis */
FOrthoMatrix(float Width,float Height,float ZScale,float ZOffset);
};
class FReversedZOrthoMatrix : public FMatrix
{ 

public:
FReversedZOrthoMatrix(float Width,float Height,float ZScale,float ZOffset);
};
FORCEINLINE FOrthoMatrix::FOrthoMatrix(float Width,float Height,float ZScale,float ZOffset)
: FMatrix(
FPlane((Width)? (1.0f / Width) : 1.0f,	0.0f,								0.0f,				0.0f),
FPlane(0.0f,							(Height)? (1.0f / Height) : 1.f,	0.0f,				0.0f),
FPlane(0.0f,							0.0f,								ZScale,				0.0f),
FPlane(0.0f,							0.0f,								ZOffset * ZScale,	1.0f)
)
{ 
 }
FORCEINLINE FReversedZOrthoMatrix::FReversedZOrthoMatrix(float Width,float Height,float ZScale,float ZOffset)
: FMatrix(
FPlane((Width)? (1.0f / Width) : 1.0f,	0.0f,								0.0f,					0.0f),
FPlane(0.0f,							(Height)? (1.0f / Height) : 1.f,	0.0f,					0.0f),
FPlane(0.0f,							0.0f,								-ZScale,				0.0f),
FPlane(0.0f,							0.0f,								1.0 - ZOffset * ZScale,	1.0f)
)
{ 
 }

透视投影

代码语言:javascript
复制
class FPerspectiveMatrix
: public FMatrix
{ 

public:
// Note: the value of this must match the mirror in Common.usf!
#define Z_PRECISION 0.0f
/** * Constructor * * @param HalfFOVX Half FOV in the X axis * @param HalfFOVY Half FOV in the Y axis * @param MultFOVX multiplier on the X axis * @param MultFOVY multiplier on the y axis * @param MinZ distance to the near Z plane * @param MaxZ distance to the far Z plane */
FPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ);
/** * Constructor * * @param HalfFOV half Field of View in the Y direction * @param Width view space width * @param Height view space height * @param MinZ distance to the near Z plane * @param MaxZ distance to the far Z plane * @note that the FOV you pass in is actually half the FOV, unlike most perspective matrix functions (D3DXMatrixPerspectiveFovLH). */
FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ);
/** * Constructor * * @param HalfFOV half Field of View in the Y direction * @param Width view space width * @param Height view space height * @param MinZ distance to the near Z plane * @note that the FOV you pass in is actually half the FOV, unlike most perspective matrix functions (D3DXMatrixPerspectiveFovLH). */
FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ);
};
class FReversedZPerspectiveMatrix : public FMatrix
{ 

public:
FReversedZPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ);
FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ);
FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ);
};
#if _MSC_VER
#pragma warning (push)
// Disable possible division by 0 warning
#pragma warning (disable : 4723)
#endif
FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ)
: FMatrix(
FPlane(MultFOVX / FMath::Tan(HalfFOVX),	0.0f,								0.0f,																	0.0f),
FPlane(0.0f,							MultFOVY / FMath::Tan(HalfFOVY),	0.0f,																	0.0f),
FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),			1.0f),
FPlane(0.0f,							0.0f,								-MinZ * ((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),	0.0f)
)
{ 
 }
FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ)
: FMatrix(
FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,							0.0f),
FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,							0.0f),
FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),			1.0f),
FPlane(0.0f,						0.0f,									-MinZ * ((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),	0.0f)
)
{ 
 }
FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ)
: FMatrix(
FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,							0.0f),
FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,							0.0f),
FPlane(0.0f,						0.0f,									(1.0f - Z_PRECISION),			1.0f),
FPlane(0.0f,						0.0f,									-MinZ * (1.0f - Z_PRECISION),	0.0f)
)
{ 
 }
FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ)
: FMatrix(
FPlane(MultFOVX / FMath::Tan(HalfFOVX),	0.0f,								0.0f,													0.0f),
FPlane(0.0f,							MultFOVY / FMath::Tan(HalfFOVY),	0.0f,													0.0f),
FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? 0.0f : MinZ / (MinZ - MaxZ)),			1.0f),
FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? MinZ : -MaxZ * MinZ / (MinZ - MaxZ)),	0.0f)
)
{ 
 }
FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ)
: FMatrix(
FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,													0.0f),
FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,													0.0f),
FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? 0.0f : MinZ / (MinZ - MaxZ)),			1.0f),
FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? MinZ : -MaxZ * MinZ / (MinZ - MaxZ)),	0.0f)
)
{ 
 }
FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ)
: FMatrix(
FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f, 0.0f),
FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f, 0.0f),
FPlane(0.0f,						0.0f,									0.0f, 1.0f),
FPlane(0.0f,						0.0f,									MinZ, 0.0f)
)
{ 
 }

使用

代码语言:javascript
复制
FMinimalViewInfo::CalculateProjectionMatrixGivenView(ControllingActorViewInfo, AspectRatioAxisConstraint, Viewport, /*inout*/ ViewInitOptions);
{ 

if (ViewInfo.ProjectionMode == ECameraProjectionMode::Orthographic)
{ 

const float YScale = 1.0f / ViewInfo.AspectRatio;
const float OrthoWidth = ViewInfo.OrthoWidth / 2.0f;
const float OrthoHeight = ViewInfo.OrthoWidth / 2.0f * YScale;
const float NearPlane = ViewInfo.OrthoNearClipPlane;
const float FarPlane = ViewInfo.OrthoFarClipPlane;
const float ZScale = 1.0f / (FarPlane - NearPlane);
const float ZOffset = -NearPlane;
InOutProjectionData.ProjectionMatrix = FReversedZOrthoMatrix(
OrthoWidth,
OrthoHeight,
ZScale,
ZOffset
);
}
else
{ 

// Avoid divide by zero in the projection matrix calculation by clamping FOV
InOutProjectionData.ProjectionMatrix = FReversedZPerspectiveMatrix(
FMath::Max(0.001f, ViewInfo.FOV) * (float)PI / 360.0f,
ViewInfo.AspectRatio,
1.0f,
GNearClippingPlane );
}
}

参考链接

UE4 投影矩阵

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185758.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月5日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • UE4投影矩阵
    • 正交投影
      • 透视投影
        • 使用
          • 参考链接
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档