PixelCheaker.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using o0.Geometry2D.Float;
  2. using o0InfraredLocate.ZIM;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. // 用椭圆标记要识别的区域,区域外的像素会被剔除
  6. public class PixelCheaker : MonoBehaviour, IPixelCheaker
  7. {
  8. public Image Bordermage; // 椭圆的图片
  9. public RectTransform BorderArea; // 椭圆的大小区域,调整这个gameobject的scale作为椭圆的区域
  10. public void Start()
  11. {
  12. ellipseRadius = BorderArea.localScale / 2f;
  13. }
  14. private Vector2 ellipseRadius;
  15. //public bool OutArea2D(Vector2 pixelPosition, Vector<int> imageSize, out float f) => OutArea2D(new Vector2(pixelPosition.x / imageSize.x, pixelPosition.y / imageSize.y), out f);
  16. //public bool OutArea2D(Vector2 pixelPosition, Vector2 imageSize, out float f) => OutArea2D(pixelPosition / imageSize, out f);
  17. //public bool OutArea2D(o0.Geometry2D.Float.Vector pixelPosition, Vector<int> imageSize, out float f) => OutArea2D(new Vector2(pixelPosition.x / imageSize.x, pixelPosition.y / imageSize.y), out f);
  18. public bool OutArea2D(Vector2 pixelPosition, Vector2 imageSize) => OutArea2D(pixelPosition / imageSize);
  19. public bool OutArea2D(Vector pixelPosition, Vector imageSize)=> OutArea2D(new Vector2(pixelPosition.x / imageSize.x, pixelPosition.y / imageSize.y));
  20. // out 椭圆的距离系数,越大表示离椭圆越远
  21. //private bool OutArea2D(Vector2 pixelUV, out float f)
  22. //{
  23. // Vector2 center = new Vector2(0.5f, 0.5f); // 椭圆中心
  24. // float normalizedX = (pixelUV.x - center.x) / ellipseRadius.x;
  25. // float normalizedY = (pixelUV.y - center.y) / ellipseRadius.y;
  26. // float value = (normalizedX * normalizedX) + (normalizedY * normalizedY); // 使用椭圆方程判断
  27. // f = (float)Math.Sqrt(value);
  28. // return value > 1.0f;
  29. //}
  30. // 无out的版本
  31. private bool OutArea2D(Vector2 pixelUV)
  32. {
  33. Vector2 center = new Vector2(0.5f, 0.5f); // 椭圆中心
  34. float normalizedX = (pixelUV.x - center.x) / ellipseRadius.x;
  35. float normalizedY = (pixelUV.y - center.y) / ellipseRadius.y;
  36. float value = (normalizedX * normalizedX) + (normalizedY * normalizedY); // 使用椭圆方程判断
  37. return value > 1.0f;
  38. }
  39. public void ShowImage() => Bordermage.enabled = true;
  40. public void HideImage() => Bordermage.enabled = false;
  41. }