InfraredLightGuider2.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using InfraredManager;
  2. using o0InfraredLocate.ZIM;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using ZIM;
  8. using ZIM.Unity;
  9. //处理光源检测部分
  10. public class InfraredLightGuider : MonoBehaviour
  11. {
  12. [SerializeField]
  13. RawImage rawImage;
  14. //相机感光部分
  15. [SerializeField]
  16. Slider slider;
  17. [SerializeField] List<RectTransform> _crosshairsInCamera;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. if (InfraredDemo._ins)
  22. {
  23. slider.onValueChanged.AddListener((value) =>
  24. {
  25. InfraredDemo._ins.onSliderCustomEvent(value,-20.0f,20.0f);
  26. });
  27. InfraredDemo._ins.onSetSliderCustomValue(slider,- 20.0f, 20.0f);
  28. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList,1,1);
  29. }
  30. Color currentColor = rawImage.color;
  31. currentColor.a = Mathf.Clamp01(1); // Clamp the alpha between 0 and 1
  32. rawImage.color = currentColor;
  33. }
  34. void Update()
  35. {
  36. if (InfraredDemo.running)
  37. {
  38. //渲染相机画面
  39. //渲染相机画面
  40. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  41. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  42. rawImage.texture = texture;
  43. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  44. //在相机画面显示准心
  45. if (ScreenLocate.Main)
  46. {
  47. var _sl = ScreenLocate.Main;
  48. var buffer = _sl.infraredSpotBuffer;
  49. if (buffer != null)
  50. {
  51. for (int i = 0; i < buffer.Length; i++)
  52. {
  53. if (buffer[i].CameraLocation != null)
  54. {
  55. //添加一个偏移量,使得最后输出的准心是指向正中心
  56. Vector2 newPoint2 = _sl.GetOffsetCameraLocation(buffer[i].CameraLocation.Value);
  57. // 检测到光点
  58. var pos = newPoint2.pixelToLocalPosition_AnchorCenter(_sl.mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  59. _crosshairsInCamera[i].gameObject.SetActive(true);
  60. _crosshairsInCamera[i].anchoredPosition = pos;
  61. }
  62. else
  63. _crosshairsInCamera[i].gameObject.SetActive(false);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. public void SetLocatePointsToCameraRender(List<Vector2> points, float w, float h)
  70. {
  71. Transform pointsTF2 = rawImage.transform.Find("Points");
  72. if (pointsTF2.childCount == points.Count)
  73. {
  74. Vector2 texSize = new Vector2(w, h);
  75. for (int i = 0; i < pointsTF2.childCount; i++)
  76. {
  77. Transform pointTF = pointsTF2.GetChild(i);
  78. Vector2 pos = points[i];
  79. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, rawImage.rectTransform.rect);
  80. pointTF.gameObject.SetActive(true);
  81. }
  82. }
  83. }
  84. public void Close() {
  85. Destroy(gameObject);
  86. }
  87. }