UE4投影矩阵[通俗易懂]

UE4投影矩阵[通俗易懂]UE4投影矩阵正交投影classFOrthoMatrix :publicFMatrix{public: /** *Constructor * *@paramWidthviewspacewidth *@paramHeightviewspaceheight *@paramZScalescaleintheZaxis *@paramZOffsetoffsetintheZaxis */ FOrthoMatrix(flo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

UE4投影矩阵

正交投影

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)
	)
{ 
    }

透视投影

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)
	)
{ 
    }

使用

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.net/185758.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Nginx配置Https并进行Http强制跳转Https

    Nginx配置Https并进行Http强制跳转Https

    2021年5月29日
    157
  • 如何在pycharm运行python_pycharm怎么运行部分代码

    如何在pycharm运行python_pycharm怎么运行部分代码这篇文章主要介绍了Pycharm如何运行.py文件的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧!Pycharm时一个非常好用的IDE,但是一开始的时候甚至会因为.py文件不能运行而束手无策。。。这里需要说明一下Pycharm的作用,Pycharm并不是一个像matlab那样什么都自己提供的软件,它更像一个非常棒的…

    2022年8月25日
    5
  • Vue Router Tab「建议收藏」

    Vue Router Tab「建议收藏」介绍VueRouterTab是基于Vue.js和VueRouter的路由页签组件,用来实现多页签页面的管理。官网演示包含的功能✅响应路由变化来打开或切换页签✅页签过多鼠标滚轮滚动✅页签拖拽排序✅支持页签打开、切换、关闭、刷新、重置等操作✅Iframe页签嵌入外部网站✅组件个性化设置:过渡效果、自定义插槽、页签右键菜单✅多语言支持✅缓存控制:页签规则、页签是否缓存、最大缓存数、是否复用组件等✅动态页签信息:标题、图标、提示✅初始页签数据,进入页

    2022年7月27日
    13
  • 推荐15个月 Node.js 开发工具

    推荐15个月 Node.js 开发工具

    2022年1月17日
    45
  • mybatis(pagehelper) dataTables实现分页功能

    mybatis(pagehelper) dataTables实现分页功能

    2021年5月12日
    175
  • ubuntu 卸载命令_强制卸载电脑软件

    ubuntu 卸载命令_强制卸载电脑软件Ubuntu命令卸载软件_李柏林的博客-CSDN博客_ubuntu卸载程序1.打开一个终端,输入dpkg–list,按下Enter键,终端输出以下内容,显示的是你电脑上安装的所有软件。2.在终端中找到你需要卸载的软件的名称,列表是按照首字母排序的。3.在终端上输入命令sudoapt-get–purgeremove包名(–purge是可选项,写上这个属性是将软件及其配置文件一并删除,如不需要删除配置文件,可执行sudoapt-getr…https://blog.csdn.net/

    2025年10月11日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号