- Home /
How to get size in pixels of an object using SteamVR camera
I am designing an application in Unity for the HTC Vive in which Objects are children of the Eye Camera so that they move in the same way as the user does.
Now I want to get the absolute Pixel width and height of a rendered object on the Vive screen. Normally, in a non-Vive-project, it works somehow like this:
public Vector2 getAbsolutePxSize(GameObject go)
{
Bounds goBounds = go.GetComponent<Renderer>().bounds;
List<Vector3> minMaxValues = new List<Vector3>();
minMaxValues.Add(new Vector3(goBounds.center.x - goBounds.extents.x, goBounds.center.y + goBounds.extents.y, goBounds.center.z - goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x - goBounds.extents.x, goBounds.center.y + goBounds.extents.y, goBounds.center.z + goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x - goBounds.extents.x, goBounds.center.y - goBounds.extents.y, goBounds.center.z - goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x - goBounds.extents.x, goBounds.center.y - goBounds.extents.y, goBounds.center.z + goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x + goBounds.extents.x, goBounds.center.y + goBounds.extents.y, goBounds.center.z - goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x + goBounds.extents.x, goBounds.center.y + goBounds.extents.y, goBounds.center.z + goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x + goBounds.extents.x, goBounds.center.y - goBounds.extents.y, goBounds.center.z - goBounds.extents.z));
minMaxValues.Add(new Vector3(goBounds.center.x + goBounds.extents.x, goBounds.center.y - goBounds.extents.y, goBounds.center.z + goBounds.extents.z));
float maxX = 0, maxY = 0, minX = Screen.width, minY = Screen.height;
foreach (Vector3 x in minMaxValues)
{
// ViveCamera is the "Camera (eye)" object attached to the script
Vector3 screenPoint = ViveCamera.WorldToScreenPoint(x);
if (screenPoint.x < minX) minX = screenPoint.x;
if (screenPoint.x > maxX) maxX = screenPoint.x;
if (screenPoint.y < minY) minY = screenPoint.y;
if (screenPoint.y > maxY) maxY = screenPoint.y;
}
return new Vector2(maxX-minX,maxY-minY);
}
But in this scenario, the result of this function varies depending on the position and rotation of the Headset, although the object doesn't change the user's view.
What am I doing wrong? I just can't wrap my head around this. Appreciate any help.
Comment