Click or drag to resize
Ab4d.SharpEngine logo

CameraUtilsGetPerspectiveScreenSize Method

GetPerspectiveScreenSize calculates a size on screen (in same units as viewSize - without DPI scale) of a size in 3D space (worldSize) that is at lookDirectionDistance and shown with perspective camera with fieldOfView and in SceneView with viewSize.

Namespace: Ab4d.SharpEngine.Utilities
Assembly: Ab4d.SharpEngine (in Ab4d.SharpEngine.dll) Version: 2.0.8956+4c7684e186ca1be74e7a284fbe739d9a1b843d3c
Syntax
C#
public static Vector2 GetPerspectiveScreenSize(
	Vector2 worldSize,
	float lookDirectionDistance,
	float fieldOfView,
	Vector2 viewSize
)

Parameters

worldSize  Vector2
size in 3D space as Vector2
lookDirectionDistance  Single
distance from camera in the camera's look direction (see remarks for more info)
fieldOfView  Single
camera's field of view
viewSize  Vector2
SceneView's size as Vector2

Return Value

Vector2
size on screen as Vector2
Remarks

GetPerspectiveScreenSize calculates a size on screen (in same units as viewSize - without DPI scale) of a size in 3D space (worldSize) that is at lookDirectionDistance and shown with perspective camera with fieldOfView and in SceneView with viewSize.

To get the most accurate results the lookDirectionDistance must not be the direct distance from the camera but the distance in the camera's look direction. The lookDirectionDistance can be calculated with the following code:

C#
var targetPosition = new Vector3(x,y,z);
var cameraPosition = Camera1.GetCameraPosition();

var distanceVector = targetPosition - cameraPosition;

var lookDirection = Vector3.Normalize(Camera1.GetLookDirection());

// To get look direction distance we project the distanceVector to the look direction vector
var lookDirectionDistance = Vector3.Dot(distanceVector, lookDirection);

var screenSize = GetPerspectiveScreenSize(worldSize, lookDirectionDistance, Camera1.FieldOfView, viewSize)
See Also