Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jackbrookes
Created July 8, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackbrookes/5c4edc20a465a6d6043f319ef3538207 to your computer and use it in GitHub Desktop.
Save jackbrookes/5c4edc20a465a6d6043f319ef3538207 to your computer and use it in GitHub Desktop.
Unity method for calculating screen-space position a world space object. Useful for showing something on screen that is not visible in VR.
/// <summary>
/// Calculates screen-space position a world space object. Useful for showing something on screen that is not visible in VR.
/// For example, it can be used to update the position of a marker that highlights the gaze of the player, using eye tracking.
/// </summary>
/// <param name="camera">The camera used for VR rendering.</param>
/// <param name="worldPos">World position of a point.</param>
/// <returns>Screen position of a point.</returns>
static Vector2 WorldToScreenVR(Camera camera, Vector3 worldPos)
{
Vector3 screenPoint = camera.WorldToViewportPoint(worldPos);
float w = XRSettings.eyeTextureWidth;
float h = XRSettings.eyeTextureHeight;
float ar = w / h;
screenPoint.x = (screenPoint.x - 0.15f * XRSettings.eyeTextureWidth) / 0.7f;
screenPoint.y = (screenPoint.y - 0.15f * XRSettings.eyeTextureHeight) / 0.7f;
return screenPoint;
}
@Waffle1434
Copy link

Stores XRSettings.eyeTexture dimensions in variables
Doesn't use them

Guilty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment