InfraredLightGuider.cs 3.3 KB

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