InfraredLightGuider2.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //对比度
  26. slider.onValueChanged.AddListener((value) =>
  27. {
  28. InfraredDemo._ins.onSliderCustomEvent(value,-20.0f,20.0f);
  29. });
  30. InfraredDemo._ins.onSetSliderCustomValue(slider,-20.0f, 20.0f);
  31. //亮度
  32. sliderBrightness.onValueChanged.AddListener((value) =>
  33. {
  34. InfraredDemo._ins.onSliderCustomBrightnessEvent(value, -20.0f, 20.0f);
  35. });
  36. InfraredDemo._ins.onSetSliderCustomBrightnessValue(sliderBrightness, -20.0f, 20.0f);
  37. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList,1,1);
  38. }
  39. Color currentColor = rawImage.color;
  40. currentColor.a = Mathf.Clamp01(1); // Clamp the alpha between 0 and 1
  41. rawImage.color = currentColor;
  42. }
  43. void Update()
  44. {
  45. if (InfraredDemo.running)
  46. {
  47. //渲染相机画面
  48. //渲染相机画面
  49. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  50. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  51. rawImage.texture = texture;
  52. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  53. //在相机画面显示准心
  54. if (ScreenLocate.Main)
  55. {
  56. var _sl = ScreenLocate.Main;
  57. var buffer = _sl.infraredSpotBuffer;
  58. if (buffer != null)
  59. {
  60. for (int i = 0; i < buffer.Length; i++)
  61. {
  62. if (buffer[i].CameraLocation != null)
  63. {
  64. //添加一个偏移量,使得最后输出的准心是指向正中心
  65. Vector2 newPoint2 = _sl.GetOffsetCameraLocation(buffer[i].CameraLocation.Value);
  66. // 检测到光点
  67. var pos = newPoint2.pixelToLocalPosition_AnchorCenter(_sl.mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  68. _crosshairsInCamera[i].gameObject.SetActive(true);
  69. _crosshairsInCamera[i].anchoredPosition = pos;
  70. }
  71. else
  72. _crosshairsInCamera[i].gameObject.SetActive(false);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. public void SetLocatePointsToCameraRender(List<Vector2> points, float w, float h)
  79. {
  80. Transform pointsTF2 = rawImage.transform.Find("Points");
  81. if (pointsTF2.childCount == points.Count)
  82. {
  83. Vector2 texSize = new Vector2(w, h);
  84. for (int i = 0; i < pointsTF2.childCount; i++)
  85. {
  86. Transform pointTF = pointsTF2.GetChild(i);
  87. Vector2 pos = points[i];
  88. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, rawImage.rectTransform.rect);
  89. pointTF.gameObject.SetActive(true);
  90. }
  91. }
  92. }
  93. public void Close() {
  94. Destroy(gameObject);
  95. }
  96. }