InfraredLightGuider.cs 3.3 KB

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