Camera |
public static Vector2 GetPerspectiveWorldSize( Vector2 desiredScreenSize, float lookDirectionDistance, float fieldOfView, Vector2 viewport3DSize )
GetPerspectiveWorldSize calculates a size in 3D world from a screen Size (screenSize in screen coordinates) 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:
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)