当前位置:网站首页>Ue5 Comment convertir les coordonnées de l'écran en coordonnées du monde et en direction du monde
Ue5 Comment convertir les coordonnées de l'écran en coordonnées du monde et en direction du monde
2022-06-10 13:59:00 【Maison humaine】
Bonjour.,Bonjour tout le monde,Partager un peu deUE5Comment convertir les coordonnées de l'écran en coordonnées du monde.

Les coordonnées de l'écran sont converties en coordonnées du monde
InPlayerControolerTrouvez cette fonction ci - dessous: DeprojectScreenPositionToWorld
bool APlayerController::DeprojectScreenPositionToWorld(float ScreenX, float ScreenY, FVector& WorldLocation, FVector& WorldDirection) const;
La position et l'orientation du monde en fonction de la position de l'écran peuvent être obtenues simplement en passant la position de l'écran.
Comment est - ce possible??
bool UGameplayStatics::DeprojectScreenToWorld(APlayerController const* Player, const FVector2D& ScreenPosition, FVector& WorldPosition, FVector& WorldDirection)
{
ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
if (LP && LP->ViewportClient)
{
// get the projection data
FSceneViewProjectionData ProjectionData;
if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
{
// Commencez à inverser
FMatrix const InvViewProjMatrix = ProjectionData.ComputeViewProjectionMatrix().InverseFast();
FSceneView::DeprojectScreenToWorld(ScreenPosition, ProjectionData.GetConstrainedViewRect(), InvViewProjMatrix, /*out*/ WorldPosition, /*out*/ WorldDirection);
return true;
}
}
// something went wrong, zero things and return false
WorldPosition = FVector::ZeroVector;
WorldDirection = FVector::ZeroVector;
return false;
}
Dont:FSceneViewProjectionData Est une description des données sur le viewport de la scène
// Projection data for a FSceneView
struct FSceneViewProjectionData
{
/** The view origin. */
FVector ViewOrigin;
/** Rotation matrix transforming from world space to view space. */
FMatrix ViewRotationMatrix;
/** UE4 projection matrix projects such that clip space Z=1 is the near plane, and Z=0 is the infinite far plane. */
FMatrix ProjectionMatrix;
protected:
//The unconstrained (no aspect ratio bars applied) view rectangle (also unscaled)
FIntRect ViewRect;
// The constrained view rectangle (identical to UnconstrainedUnscaledViewRect if aspect ratio is not constrained)
FIntRect ConstrainedViewRect;
};
ViewOrigin: C'est l'origine du viewport
ViewRotationMatrix:C'est une matrice de rotation Peut être compris commeViewMatrix Sauf qu'il n'y a pas de matrice de traduction
ProjectionMatrix:Matrice de projection
Ce qu'il faut distinguer ViewMatrix Est de transformer un objet du point du monde en coordonnées locales sous le viewport ,EtWorldMatrix Est de transformer les coordonnées locales en coordonnées du monde .
Dont: VP Inverse J'espère que les points sous le viewport seront convertis en coordonnées du monde par cette inversion
FMatrix ComputeViewProjectionMatrix() const
{
return FTranslationMatrix(-ViewOrigin) * ViewRotationMatrix * ProjectionMatrix;
}
FMatrix const InvViewProjMatrix = ProjectionData.ComputeViewProjectionMatrix().InverseFast();
Quand j'ai obtenu FSceneViewProjectionData Données structurelles,Ensuite, regardons DeprojectScreenToWorld
Nous savons que la formule du rayon est

Voyons maintenant. DeprojectScreenToWorld Cette fonction Qu'est - ce qu'il y a dedans .
Nous savons que si vous déplacez un point de modèle local de l'espace local à l'espace d'écran , Il a subi ces changements
a.Adoptionworldmatrix Transformer le point local du modèle en point mondial
b.Adoptionviewmatrix La matrice déplace les points de la position du monde sous le viewport
c.Adoptionprojematrix La matrice de projection convertit les points de vue en points sur le plan de coupure proche
d. Mapping to NDCEspace[-1.1]
e. En utilisant la technique semi - Lambert [-1.1]Mapping to[0,1]
f. Multipliez ensuite la largeur et la hauteur de l'écran pour obtenir la position de l'écran
Si vous voulez réaliser la cueillette , Ce serait un processus inverse , L'emplacement de l'écran est déjà connu , Comment trouver la position de cet écran World locationEtOrientation?
Voici un examen de la connaissance d'une matrice .
Si unworldmatrix Vous pouvez transformer des points de l'espace local en espace mondial , Alors, pour faire passer ce point de l'espace mondial à l'espace local ,C'est tout ce qu'il faut.worldmatrix L'inverse suffit , On peut comprendre que l'inversion est la Division d'une matrice .
Par exemple, Connu pour
,J'espère que ça passera500S'il te plaît. 100,C'est tout.
Et vous obtenez100.Parmi eux C'est5L'inverse de, La matrice est la même .
Voyons voir.UE5Comment faire:
1. Obtenez d'abord les coordonnées de l'écran
float PixelX = FMath::TruncToFloat(ScreenPos.X);
float PixelY = FMath::TruncToFloat(ScreenPos.Y);
2. Après avoir obtenu les coordonnées, cartographiez - les à 0-1Champ d'application
const float NormalizedX = (PixelX - ViewRect.Min.X) / ((float)ViewRect.Width());
const float NormalizedY = (PixelY - ViewRect.Min.Y) / ((float)ViewRect.Height());
3. Mapper les coordonnées à NDCEspace[-1,1]
const float ScreenSpaceX = (NormalizedX - 0.5f) * 2.0f;
const float ScreenSpaceY = ((1.0f - NormalizedY) - 0.5f) * 2.0f;
4. Le début et la fin du rayon dans l'espace projeté ,Parmi eux0.01 C'est près du plan de coupure ,1 C'est le plan de coupure
const FVector4 RayStartProjectionSpace = FVector4(ScreenSpaceX, ScreenSpaceY, 1.0f, 1.0f);
const FVector4 RayEndProjectionSpace = FVector4(ScreenSpaceX, ScreenSpaceY, 0.01f, 1.0f);
5.Par devantVPFonctionnement inverse de Convertir les points de l'espace de culture en espace mondial
const FVector4 HGRayStartWorldSpace = InvViewProjMatrix.TransformFVector4(RayStartProjectionSpace);
const FVector4 HGRayEndWorldSpace = InvViewProjMatrix.TransformFVector4(RayEndProjectionSpace);
FVector RayStartWorldSpace(HGRayStartWorldSpace.X, HGRayStartWorldSpace.Y, HGRayStartWorldSpace.Z);
FVector RayEndWorldSpace(HGRayEndWorldSpace.X, HGRayEndWorldSpace.Y, HGRayEndWorldSpace.Z);
6. Diviser le vecteur par W Pour annuler toute projection et obtenir 3-spaceCoordonnées( Vous pouvez comprendre le plan de coupure qui se rapproche du monde )
if (HGRayStartWorldSpace.W != 0.0f)
{
RayStartWorldSpace /= HGRayStartWorldSpace.W;
}
if (HGRayEndWorldSpace.W != 0.0f)
{
RayEndWorldSpace /= HGRayEndWorldSpace.W;
}
7. Prenez la direction
const FVector RayDirWorldSpace = (RayEndWorldSpace - RayStartWorldSpace).GetSafeNormal();
8. Et finalement obtenir la valeur requise
out_WorldOrigin = RayStartWorldSpace;
out_WorldDirection = RayDirWorldSpace;
On a l'occasion de parler des rayons
Nous allons maintenant appliquer cette idée à Dans un petit moteur auto - développé :

边栏推荐
- P3379 [template] nearest common ancestor (LCA)
- markdown设置字体为红色
- [note] about the problem of insufficient compilation mapping memory in keil
- net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
- 【笔记】C语言数组指针、结构体+二维数组指针小记
- Flutter 页面跳转 传参,TabBar学习总结5
- NC|王军/宋默识结合三代测序解析肠道菌群结构变异和功能
- 《软件体系结构原理、方法与实践》第二版期末考试复习总结
- How to build Haojing technology when the computing power network is brought into reality?
- 新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
猜你喜欢

Application analysis of key recording and playing of wt2003h4-16s voice chip

CentOS Linux 已死!Oracle Linux 可能是它的更好替代品
![buuctf [Discuz]wooyun-2010-080723](/img/b9/d63c9c638ff7c390c490d6fd5eafd1.png)
buuctf [Discuz]wooyun-2010-080723
![[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service](/img/ff/96a0a77795b271bef3a8ade2d646c3.png)
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service

C#多线程学习笔记二

5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
![[notes] notes on C language array pointer, structure + two-dimensional array pointer](/img/77/5923754b12ccfe0bcba7e46617de86.png)
[notes] notes on C language array pointer, structure + two-dimensional array pointer

【解决】每次加载已经训练好的模型,生成的向量会有不同

新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles

22.6.7成功使用doc2vec模型生成嵌入向量
随机推荐
解决win10虚拟机和主机不能互相粘贴复制的问题
Leetcode-57- insert interval
Gorm设置外键
win10虚拟机下载安装流程
【笔记】关于keil中的出现的编译映射内存不足的问题
C#多线程学习笔记四
5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
The shortcomings of the "big model" and the strengths of the "knowledge map"
What does the multi cloud management platform CMP mean? Who can explain it clearly
[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use
22.6.7成功使用doc2vec模型生成嵌入向量
New features mail GPU counter module adds GPU primitive processing and GPU shader cycles
Leetcode 2293. 极大极小游戏(可以.一次过)
[FAQ] résumé des problèmes courants et des solutions lors de l'utilisation de l'interface API rest du Service de santé sportive
odoo 权限管理(访问权限及记录规则)结合实用,升级角色管理
大厂面试上午10:00面试,10:09就出来了 ,问的实在是太...
C#多线程学习笔记三
net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
如何定位游戏发热问题
Leetcode 829. 连续整数求和