使用Vuforia ARCamera在Unity中如何获取图像目标的四个角的屏幕位置?

huangapple go评论113阅读模式
英文:

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

使用Vuforia ARCamera在Unity中如何获取图像目标的四个角的屏幕位置?

答案1

得分: 0

以下是代码部分的翻译:

  1. 自从你提到了`GetLocalCorners`,假设你有一个`RectTransform` => 首先通过[GetWorldCorners](https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html)获取其世界坐标,然后通过[WorldToScreenPoint](https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html)将其转换为屏幕坐标。
  2. 例如:
  3. ```csharp
  4. RectTransform rectTransform = YOUR_TARGET_RECT_TRANSFORM;
  5. var corners = new Vector3[4];
  6. rectTransform.GetWorldCorners(corners);
  7. var camera = Camera.main;
  8. for(var i = 0; i < corners.Length; i++)
  9. {
  10. corners[i] = camera.WorldToScreenPoint(corners[i]);
  11. }

现在corners中的坐标是以像素为单位的屏幕坐标。

如果目标实际上不是RectTransform,你需要首先自己在世界坐标中计算角点。可能看起来像:

  1. var position = YOUR_TARGET_Transform.position;
  2. // 假设缩放已经应用到目标上,以使其与目标尺寸匹配
  3. var size = YOUR_TARGET_Transform.lossyScale;
  4. // 假设目标默认在XZ平面上是平的
  5. // => 默认情况下,transform.up朝向天空
  6. // 如果目标实际上旋转了,你需要调整这些值
  7. var x = YOUR_TARGET_Transform.right * size.x * 0.5f;
  8. var z = YOUR_TARGET_Transform.forward * size.z * 0.5f;
  9. var corners = new Vector3[4];
  10. corners[0] = position - x - z;
  11. corners[1] = position - x + z;
  12. corners[2] = position + x + z;
  13. corners[3] = position + x - z;

这是一个小示例:

  1. public class TEST : MonoBehaviour
  2. {
  3. public RectTransform[] images;
  4. // 在每帧调用一次
  5. void Update()
  6. {
  7. var position = transform.position;
  8. // 假设缩放已经应用到目标上,以使其与目标尺寸匹配
  9. var size = transform.lossyScale;
  10. // 假设目标默认在XZ平面上是平的
  11. // => transform.up默认朝向天空
  12. // 如果目标实际上旋转了,你需要调整这些值
  13. var x = transform.right * size.x * 0.5f;
  14. var z = transform.forward * size.z * 0.5f;
  15. var corners = new Vector3[4];
  16. corners[0] = position - x - z;
  17. corners[1] = position - x + z;
  18. corners[2] = position + x + z;
  19. corners[3] = position + x - z;
  20. for (var i = 0; i < 4; i++)
  21. {
  22. corners[i] = Camera.main.WorldToScreenPoint(corners[i]);
  23. images[i].position = (Vector2)corners[i];
  24. }
  25. }
  26. }

使用Vuforia ARCamera在Unity中如何获取图像目标的四个角的屏幕位置?

  1. 这就是你提供的代码的翻译。
  2. <details>
  3. <summary>英文:</summary>
  4. Since you speak of `GetLocalCorners` assuming you have a `RectTransform` =&gt; 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)
  5. so e.g.
  6. RectTransform rectTransform = YOUR_TARGET_RECT_TRANSFORM;
  7. var corners = new Vector3[4];
  8. rectTransform.GetWorldCorners(corners);
  9. var camera = Camera.main;
  10. for(var i = 0; i &lt; corners.Length; i++)
  11. {
  12. corners[i] = camera.WorldToScreenPoint(corners[i]);
  13. }
  14. Now the `corners` are in pixel screen space.
  15. ---
  16. 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.
  17. var position = YOUR_TARGET_Transform.position;
  18. // assuming that scale is applied to the target so that it matches with the target dimensions
  19. var size = YOUR_TARGET_Transform.lossyScale;
  20. // assuming your target is by default flat on the XZ plane
  21. // =&gt; transform.up points towards the sky by default
  22. // if the target is actually rotated you need to adjust those
  23. var x = YOUR_TARGET_Transform.right * size.x * 0.5f;
  24. var z = YOUR_TARGET_Transform.forward * size.z * 0.5f;
  25. var corners = new Vector3[4];
  26. corners[0] = position - x - z;
  27. corners[1] = position - x + z;
  28. corners[2] = position + x + z;
  29. corners[3] = position + x - z;
  30. Little demo of that
  31. public class TEST : MonoBehaviour
  32. {
  33. public RectTransform[] images;
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. var position = transform.position;
  38. // assuming that scale is applied to the target so that it matches with the target dimensions
  39. var size = transform.lossyScale;
  40. // assuming your target is by default flat on the XZ plane
  41. // =&gt; transform.up points towards the sky by default
  42. // if the target is actually rotated you need to adjust those
  43. var x = transform.right * size.x * 0.5f;
  44. var z = transform.forward * size.z * 0.5f;
  45. var corners = new Vector3[4];
  46. corners[0] = position - x - z;
  47. corners[1] = position - x + z;
  48. corners[2] = position + x + z;
  49. corners[3] = position + x - z;
  50. for (var i = 0; i &lt; 4; i++)
  51. {
  52. corners[i] = Camera.main.WorldToScreenPoint(corners[i]);
  53. images[i].position = (Vector2)corners[i];
  54. }
  55. }
  56. }
  57. [![enter image description here][1]][1]
  58. [1]: https://i.stack.imgur.com/nTUPZ.gif
  59. </details>

huangapple
  • 本文由 发表于 2023年3月9日 16:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定