LoginSignup
7
10

More than 1 year has passed since last update.

[UE4][UE5] 画面上の座標(スクリーン座標)から三次元座標(ワールド座標)を取得する方法について(標準のDeproject Screen to World と 自作版 )

Last updated at Posted at 2022-07-27

はじめに

カメラに映っている範囲でActorを生成したいので、画面上の座標(スクリーン座標)から三次元座標(ワールド座標)を取得したい!」というお問い合わせを頂いた際に調べたことのメモです。

ちなみにこの処理ができるようになると、画面端にBlokingVolumeを配置してキャラを画面外に出さないようにするといったことができます。便利

スクリーン座標…?ワールド座標…?という方はこちらとか参考になると思います。座標系たのちい
座標系の考え方と生成・配置 : https://www.vcssl.org/ja-jp/doc/3d/coordinate

Deproject Screen to World ノードを使う方法

image.png
エンジン標準で用意されているのが、Deproject Screen to WorldノードUGameplayStatics::DeprojectScreenToWorld)です。これを使うことで、現在表示されている画面におけるスクリーン座標からワールド座標を取得することができます。

ただし壁や地面などの遮蔽物は考慮していないため、本当に画面に映っているのかは判定できません。なので、遮蔽物を考慮する際はLineTraceなどを使って判定をかけることになります(ちなみに、指定のActorが画面に映っているか否かは Was Recently Rendered ノードが有用です)。

Deproject Screen to World で簡単に実装できるのですが、Get Player Controller ノードの結果を渡す関係で現在表示されている画面での結果しか取れません。そのため、もし画面表示に使っていないCameraに対して同じ処理を行いたい場合は C++ で少しゴリゴリ書く必要があります。

Deproject Screen to World ノードを自作する方法

早速ですが、C++コードの紹介です。UGameplayStatics::DeprojectScreenToWorld 内で走っている処理を再現した感じになります。

MyBlueprintFunctionLibrary.h
#include "Camera/CameraComponent.h"

UFUNCTION(BlueprintCallable)
static bool DeprojectScreenToWorldWithCameraComponent(UCameraComponent* CameraComponent, float ViewRectX, float ViewRectY, const FVector2D& ScreenPosition, FVector& WorldPosition, FVector& WorldDirection);

MyBlueprintFunctionLibrary.cpp
bool UMyBlueprintFunctionLibrary::DeprojectScreenToWorldWithCameraComponent(UCameraComponent* CameraComponent, float ViewRectX, float ViewRectY, const FVector2D& ScreenPosition, FVector& WorldPosition, FVector& WorldDirection)
{
	if (!CameraComponent)
	{
		return false;
	}

	FMinimalViewInfo MinimalViewInfo;
	CameraComponent->GetCameraView(0.0f, MinimalViewInfo);

	FSceneViewProjectionData ProjectionData;
	ProjectionData.ViewOrigin = MinimalViewInfo.Location;
	ProjectionData.ProjectionMatrix = MinimalViewInfo.CalculateProjectionMatrix();

	// from UGameplayStatics::CalculateViewProjectionMatricesFromMinimalView
	FMatrix ViewRotationMatrix = FInverseRotationMatrix(MinimalViewInfo.Rotation) * FMatrix(
		FPlane(0, 0, 1, 0),
		FPlane(1, 0, 0, 0),
		FPlane(0, 1, 0, 0),
		FPlane(0, 0, 0, 1));
	ProjectionData.ViewRotationMatrix = ViewRotationMatrix;

	// スクリーン座標で指定する関係で、表示してないけど画面解像度を設定する必要がある
	// 手っ取り早く手動で設定するように(手抜き
	ProjectionData.SetConstrainedViewRectangle(FIntRect(0, 0, ViewRectX, ViewRectY));

	// from UGameplayStatics::DeprojectScreenToWorld
	FMatrix const InvViewProjMatrix = ProjectionData.ComputeViewProjectionMatrix().InverseFast();
	FSceneView::DeprojectScreenToWorld(ScreenPosition, ProjectionData.GetConstrainedViewRect(), InvViewProjMatrix, /*out*/ WorldPosition, /*out*/ WorldDirection);
	return true;
}

これでこうして
image.png
こうすると…
image.png
表示していないカメラでも、スクリーン座標からワールド座標の情報を取得できるようになります。
image.png
なお、Constrain Aspect Ratioを有効にして、Aspect Ratioを設定して、ノードに渡す解像度の比率を設定したAspect Ratioに合わせないと何らかの不具合が出るかと思います。
image.png

さいごに

今回はスクリーン座標からワールド座標でしたが、ワールド座標からスクリーン座標に変換することもできます。こちらもUGameplayStatics::ProjectWorldToScreen ノードで簡単にできますが、表示していないカメラの場合だと今回のようにC++コードで少し書く必要があるかと思います。その際にも上記コードが参考になれば幸いです。

おわり

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10