英文:
How to get the screen position of the four corners of an image target using Vuforia ARCamera in Unity?
问题
"GetLocalCorners" 返回一个表示矩形形状角落的四个角的数组,但我需要图像角。
我想要获取黄点的屏幕位置(像素)。
英文:
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
答案1
得分: 0
以下是代码部分的翻译:
自从你提到了`GetLocalCorners`,假设你有一个`RectTransform` => 首先通过[GetWorldCorners](https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html)获取其世界坐标,然后通过[WorldToScreenPoint](https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html)将其转换为屏幕坐标。
例如:
```csharp
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]);
}
现在corners
中的坐标是以像素为单位的屏幕坐标。
如果目标实际上不是RectTransform
,你需要首先自己在世界坐标中计算角点。可能看起来像:
var position = YOUR_TARGET_Transform.position;
// 假设缩放已经应用到目标上,以使其与目标尺寸匹配
var size = YOUR_TARGET_Transform.lossyScale;
// 假设目标默认在XZ平面上是平的
// => 默认情况下,transform.up朝向天空
// 如果目标实际上旋转了,你需要调整这些值
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;
这是一个小示例:
public class TEST : MonoBehaviour
{
public RectTransform[] images;
// 在每帧调用一次
void Update()
{
var position = transform.position;
// 假设缩放已经应用到目标上,以使其与目标尺寸匹配
var size = transform.lossyScale;
// 假设目标默认在XZ平面上是平的
// => transform.up默认朝向天空
// 如果目标实际上旋转了,你需要调整这些值
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];
}
}
}
这就是你提供的代码的翻译。
<details>
<summary>英文:</summary>
Since you speak of `GetLocalCorners` assuming you have a `RectTransform` => You would first go through [GetWorldCorners](https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html) then pass those through [WorldToScreenPoint](https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html)
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];
}
}
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/nTUPZ.gif
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论