InfraredLightGuider2.cs 3.1 KB

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