How to find the screen position of the four corners of an image target in Vuforia for perspective transform?
"GetLocalCorners" returns an array of four corners that represent the corners of the rectangular shape. but I need image corners.
I want to get screen position(pixel) in yellow points
Since you speak of GetLocalCorners
assuming you have a RectTransform
=> You would first go through GetWorldCorners then pass those through WorldToScreenPoint
so e.g.
RectTransform rectTransform = YOUR_TARGET_RECT_TRANSFORM;
var corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
var camera = Camera.main;
for(var i = 0; i < corners.Length; i++)
{
corners[i] = camera.WorldToScreenPoint(corners[i]);
}
Now the corners
are in pixel screen space.
If the target is actually not a RectTransform
you would need to calculate the corners in worldspace yourself first. Might look somewhat like e.g.
var position = YOUR_TARGET_Transform.position;
// assuming that scale is applied to the target so that it matches with the target dimensions
var size = YOUR_TARGET_Transform.lossyScale;
// assuming your target is by default flat on the XZ plane
// => transform.up points towards the sky by default
// if the target is actually rotated you need to adjust those
var x = YOUR_TARGET_Transform.right * size.x * 0.5f;
var z = YOUR_TARGET_Transform.forward * size.z * 0.5f;
var corners = new Vector3[4];
corners[0] = position - x - z;
corners[1] = position - x + z;
corners[2] = position + x + z;
corners[3] = position + x - z;
Little demo of that
public class TEST : MonoBehaviour
{
public RectTransform[] images;
// Update is called once per frame
void Update()
{
var position = transform.position;
// assuming that scale is applied to the target so that it matches with the target dimensions
var size = transform.lossyScale;
// assuming your target is by default flat on the XZ plane
// => transform.up points towards the sky by default
// if the target is actually rotated you need to adjust those
var x = transform.right * size.x * 0.5f;
var z = transform.forward * size.z * 0.5f;
var corners = new Vector3[4];
corners[0] = position - x - z;
corners[1] = position - x + z;
corners[2] = position + x + z;
corners[3] = position + x - z;
for (var i = 0; i < 4; i++)
{
corners[i] = Camera.main.WorldToScreenPoint(corners[i]);
images[i].position = (Vector2)corners[i];
}
}
}