Created
July 8, 2021 10:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Guilty