InfraredLightGuider2.cs 3.6 KB

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