InfraredLightGuider2.cs 3.3 KB

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