Click or drag to resize
AB4D logo

CameraUtilsGetPerspectiveScreenSize Method

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

Namespace: Ab3d.Utilities
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.1.8864.1045
Syntax
C#
public static Size GetPerspectiveScreenSize(
	Size worldSize,
	double lookDirectionDistance,
	double fieldOfView,
	Size viewport3DSize
)

Parameters

worldSize  Size
Size in 3D space
lookDirectionDistance  Double
distance from camera in the camera's look direction (see remarks for more info)
fieldOfView  Double
camera's field of view
viewport3DSize  Size
Viewport3D's size

Return Value

Size
size on screen
Remarks

GetPerspectiveScreenSize calculates a size on screen (in same units as size of Viewport3D.Width - without DPI scale) of a Size in 3D space (worldSize) that is at distanceFromCamera and shown with perspective camera with fieldOfView and in Viewport3D with viewport3DSize.

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 Point3D(x,y,z);
var cameraPosition = Camera1.GetCameraPosition();

var distanceVector = targetPosition - cameraPosition;

var lookDirection = Camera1.LookDirection;
lookDirection.Normalize();

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

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