InfraredLightGuider2.cs 3.7 KB

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