UE C++ 相机视口变换(World与相机互转)

2024-03-14 12:20

本文主要是介绍UE C++ 相机视口变换(World与相机互转),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

UE C++ 相机视口变换(World与相机互转)

FUNCTION(BlueprintCallable, BlueprintPure)static void ProjectSceneCaptureToWorld(const class USceneCaptureComponent2D* SceneCaptureComponent2D,const FVector2D& SceneCapturePosition, FVector& WorldPosition,FVector& WorldDirection){if (!IsValid(SceneCaptureComponent2D)){return;}// 视口矩阵const FTransform& ViewTransform = SceneCaptureComponent2D->GetComponentToWorld();FMatrix ViewMatrix = ViewTransform.ToInverseMatrixWithScale();ViewMatrix = ViewMatrix * FMatrix(FPlane(0, 0, 1, 0), FPlane(1, 0, 0, 0), FPlane(0, 1, 0, 0),FPlane(0, 0, 0, 1));const float FOV = SceneCaptureComponent2D->FOVAngle * (float)PI / 360.0f;const FIntPoint CaptureSize(SceneCaptureComponent2D->TextureTarget->GetSurfaceWidth(),SceneCaptureComponent2D->TextureTarget->GetSurfaceHeight());float XAxisMultiplier;float YAxisMultiplier;if (CaptureSize.X > CaptureSize.Y){XAxisMultiplier = 1.0f;YAxisMultiplier = CaptureSize.X / static_cast<float>(CaptureSize.Y);}else{XAxisMultiplier = CaptureSize.Y / static_cast<float>(CaptureSize.X);YAxisMultiplier = 1.0f;}// 投影矩阵const FMatrix ProjectionMatrix = FReversedZPerspectiveMatrix(FOV, FOV, XAxisMultiplier, YAxisMultiplier,GNearClippingPlane, GNearClippingPlane);//视口矩阵与投影矩阵的逆矩阵                                                           const FMatrix InverseViewMatrix = ViewMatrix.InverseFast();const FMatrix InverseProjectionMatrix = ProjectionMatrix.Inverse();const FIntRect ViewRect = FIntRect(0, 0, CaptureSize.X, CaptureSize.Y);FSceneView::DeprojectScreenToWorld(SceneCapturePosition, ViewRect, InverseViewMatrix, InverseProjectionMatrix,WorldPosition, WorldDirection);}
	UFUNCTION(BlueprintCallable, BlueprintPure)static bool ProjectWorldToSceneCapture(const FVector& WorldPosition,const class USceneCaptureComponent2D* SceneCaptureComponent2D,FVector2D& SceneCapturePosition){if (!IsValid(SceneCaptureComponent2D)){return false;}//视口矩阵const FTransform& ViewTransform = SceneCaptureComponent2D->GetComponentToWorld();FMatrix ViewMatrix = ViewTransform.ToInverseMatrixWithScale();ViewMatrix = ViewMatrix * FMatrix(FPlane(0, 0, 1, 0), FPlane(1, 0, 0, 0), FPlane(0, 1, 0, 0),FPlane(0, 0, 0, 1));const float FOV = SceneCaptureComponent2D->FOVAngle * (float)PI / 360.0f;const FIntPoint CaptureSize(SceneCaptureComponent2D->TextureTarget->GetSurfaceWidth(),SceneCaptureComponent2D->TextureTarget->GetSurfaceHeight());float XAxisMultiplier;float YAxisMultiplier;if (CaptureSize.X > CaptureSize.Y){XAxisMultiplier = 1.0f;YAxisMultiplier = CaptureSize.X / static_cast<float>(CaptureSize.Y);}else{XAxisMultiplier = CaptureSize.Y / static_cast<float>(CaptureSize.X);YAxisMultiplier = 1.0f;}// 投影矩阵const FMatrix ProjectionMatrix = FReversedZPerspectiveMatrix(FOV, FOV, XAxisMultiplier, YAxisMultiplier,GNearClippingPlane, GNearClippingPlane);const FMatrix ViewProjectionMatrix = ViewMatrix * ProjectionMatrix;const FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(WorldPosition, 1.f));const FIntRect viewRect = FIntRect(0, 0, CaptureSize.X, CaptureSize.Y);if (Result.W > 0.0f){// the result of this will be x and y coords in -1..1 projection spaceconst float RHW = 1.0f / Result.W;const FPlane PosInScreenSpace = FPlane(Result.X * RHW, Result.Y * RHW, Result.Z * RHW, Result.W);// Move from projection space to normalized 0..1 UI spaceconst float NormalizedX = (PosInScreenSpace.X / 2.f) + 0.5f;const float NormalizedY = 1.f - (PosInScreenSpace.Y / 2.f) - 0.5f;const FVector2D RayStartViewRectSpace((NormalizedX * static_cast<float>(viewRect.Width())),(NormalizedY * static_cast<float>(viewRect.Height())));SceneCapturePosition = RayStartViewRectSpace + FVector2D(static_cast<float>(viewRect.Min.X),static_cast<float>(viewRect.Min.Y));return true;}return false;}

这篇关于UE C++ 相机视口变换(World与相机互转)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/808417

相关文章

利用c++判断水仙花数并输出示例代码

《利用c++判断水仙花数并输出示例代码》水仙花数是指一个三位数,其各位数字的立方和恰好等于该数本身,:本文主要介绍利用c++判断水仙花数并输出的相关资料,文中通过代码介绍的非常详细,需要的朋友可以... 以下是使用C++实现的相同逻辑代码:#include <IOStream>#include <vec

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.