InfraredScreenPositioningView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LineUI;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. using ZIM;
  8. using o0;
  9. public class LinePosition
  10. {
  11. public int index;
  12. public List<Vector3> pos;
  13. }
  14. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  15. {
  16. [SerializeField]
  17. RectTransform canvasRectTransform;
  18. [SerializeField]
  19. RectTransform draggableParent;
  20. [SerializeField]
  21. RectTransform pos1;
  22. [SerializeField]
  23. RectTransform pos2;
  24. [SerializeField]
  25. RectTransform pos3;
  26. [SerializeField]
  27. RectTransform pos4;
  28. //画线时候的点偏移量
  29. float offset = 20;
  30. [SerializeField]
  31. Line line;
  32. List<LinePosition> oldLinePosition;
  33. Vector3 beginPos;
  34. Vector3 endPos;
  35. //相机感光部分
  36. [SerializeField]
  37. Slider slider;
  38. [SerializeField]
  39. RawImage rawImage;
  40. [SerializeField]
  41. GameObject textTip1;
  42. [SerializeField]
  43. GameObject textTip2;
  44. int DefaultResolutionIndex;
  45. private void Awake()
  46. {
  47. offset = line.MyThickness;
  48. }
  49. void Start()
  50. {
  51. textTip1.SetActive(true);
  52. textTip2.SetActive(false);
  53. //quadUnityVectorList.Clear();
  54. //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
  55. //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
  56. //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
  57. //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
  58. Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
  59. if (ScreenLocate.quadUnityVectorList.Count == 0)
  60. {
  61. Debug.Log("GetLocalPos");
  62. GetLocalPos();
  63. }
  64. else
  65. {
  66. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  67. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  68. //自动识别时候四个点
  69. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  70. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  71. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  72. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  73. }
  74. //记录操作的位置信息
  75. oldLinePosition = new List<LinePosition>();
  76. SetLinePos();
  77. //相机感光度
  78. if (InfraredDemo._ins)
  79. {
  80. slider.onValueChanged.AddListener((value) =>
  81. {
  82. InfraredDemo._ins.onSliderEvent(value);
  83. });
  84. InfraredDemo._ins.onSetSliderValue(slider);
  85. offset = line.MyThickness = InfraredDemo._ins.lineWidth.Get();
  86. }
  87. if (ScreenLocate.Main.getUVCTexture)
  88. {
  89. //渲染相机画面
  90. rawImage.texture = ScreenLocate.Main.getUVCTexture;
  91. //Debug.Log("rawImage.texture:" + rawImage.texture.name);
  92. }
  93. //slider.onValueChanged.AddListener((value) => {
  94. // //onSliderEvent(value);
  95. // InfraredDemo._ins.SetBrightness(value);
  96. //});
  97. //slider.value = InfraredDemo._ins.brightness.Get();
  98. //修改分辨率。是否清晰一点?
  99. //DefaultResolutionIndex = InfraredDemo._ins?.ResolutionIndex ?? 0;
  100. //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
  101. //InfraredDemo._ins?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
  102. }
  103. private void OnDestroy()
  104. {
  105. //修改回进入手动调节页面时候的分辨率
  106. //InfraredDemo._ins?.SetResolutionNew(DefaultResolutionIndex);
  107. }
  108. void Update()
  109. {
  110. //if (InfraredDemo.running)
  111. //{
  112. // //渲染相机画面
  113. // //rawImage.texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  114. // //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  115. //}
  116. }
  117. public void OnClick_Back()
  118. {
  119. AudioMgr.ins.PlayBtn();
  120. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  121. }
  122. public void OnClick_Auto()
  123. {
  124. textTip1.SetActive(false);
  125. textTip2.SetActive(true);
  126. if (enterFromZimWebCamera)
  127. {
  128. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  129. _screenLocate.EnterScreenLocateManualAuto();
  130. return;
  131. }
  132. if (InfraredDemo.running)
  133. {
  134. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  135. }
  136. }
  137. public void SyncScreenPosition()
  138. {
  139. //Debug.Log("quadUnityVectorList[i]:" + quadUnityVectorList[i]);
  140. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  141. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  142. //自动识别时候四个点
  143. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  144. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  145. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  146. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  147. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  148. }
  149. #region 绘制线段部分
  150. //点击拖拽的开始位置
  151. public void onBeginPos(int index, Vector3 pos)
  152. {
  153. beginPos = pos;
  154. }
  155. public void onDragPos(int index, Vector3 pos)
  156. {
  157. //设置线段的点
  158. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  159. }
  160. //点击拖拽的结束位置
  161. public void onEndPos(int index, Vector3 pos)
  162. {
  163. endPos = pos;
  164. if (beginPos == endPos) return;
  165. //Debug.Log(index+",最后的点:" + pos);
  166. //再记录一次最后的点
  167. SetLinePos();
  168. }
  169. //同步设置图片对应的位置到line
  170. public void SetLinePos()
  171. {
  172. //记录一个操作点的操作位置
  173. AddOldLinePosition();
  174. //设置线段的点
  175. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  176. }
  177. void AddOldLinePosition()
  178. {
  179. Vector3[] v = new Vector3[4];
  180. pos1.GetWorldCorners(v);
  181. Vector3[] v1 = new Vector3[4];
  182. pos1.GetLocalCorners(v1);
  183. //for (int i = 0; i < 4; i++)
  184. //{
  185. // Debug.Log(i + " =1= " + v[i]);
  186. // Debug.Log(i + " =2= " + v1[i]);
  187. //}
  188. //记录一个操作点的操作位置
  189. List<Vector3> screenPositions = new List<Vector3>();
  190. screenPositions.Add(pos1.localPosition);
  191. screenPositions.Add(pos2.localPosition);
  192. screenPositions.Add(pos3.localPosition);
  193. screenPositions.Add(pos4.localPosition);
  194. LinePosition linePosition = new LinePosition();
  195. linePosition.index = oldLinePosition.Count;
  196. linePosition.pos = screenPositions;
  197. oldLinePosition.Add(linePosition);
  198. }
  199. //转换绘制线段的点
  200. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  201. {
  202. List<Vector2> _screenPositions = new List<Vector2>();
  203. //点为负数的增大 offset,正数减少 offset
  204. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  205. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  206. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  207. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  208. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  209. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  210. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  211. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  212. _screenPositions.Add(startPos1);
  213. _screenPositions.Add(startPos2);
  214. _screenPositions.Add(startPos3);
  215. _screenPositions.Add(startPos4);
  216. return _screenPositions;
  217. }
  218. void SetRectanglePoints(List<Vector2> screenPositions)
  219. {
  220. line.SetLine(screenPositions);
  221. }
  222. //撤回上一个元素
  223. public void onRecall()
  224. {
  225. // 获取并删除最后一个元素,并且保留一个元素
  226. if (oldLinePosition.Count > 1) // 确保列表不为空
  227. {
  228. // 获取回退的那个元素点
  229. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  230. // 获取最后一个元素
  231. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  232. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  233. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  234. pos1.localPosition = lastElement_second.pos[0];
  235. pos2.localPosition = lastElement_second.pos[1];
  236. pos3.localPosition = lastElement_second.pos[2];
  237. pos4.localPosition = lastElement_second.pos[3];
  238. //设置线段的点
  239. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  240. }
  241. }
  242. //是不是从Demo界面进入该页面的
  243. public bool enterFromInfraredDemo { get; set; } = false;
  244. //是否从测试场景进入
  245. public bool enterFromZimWebCamera { get; set; } = false;
  246. //确认修改
  247. public void onConfirmation()
  248. {
  249. if (enterFromZimWebCamera)
  250. {
  251. ConfirmScreenLocateManualTest();
  252. if (oldLinePosition.Count > 1) // 确保列表不为空
  253. {
  254. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  255. oldLinePosition.Clear();
  256. oldLinePosition.Add(lastElement);
  257. }
  258. SaveLocalPos();
  259. AudioMgr.ins.PlayBtn();
  260. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  261. return;
  262. }
  263. if (!ConfirmScreenLocateManual()) return;
  264. if (oldLinePosition.Count > 1) // 确保列表不为空
  265. {
  266. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  267. oldLinePosition.Clear();
  268. oldLinePosition.Add(lastElement);
  269. }
  270. if (InfraredDemo.running)
  271. {
  272. //跳转入界面
  273. AudioMgr.ins.PlayBtn();
  274. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  275. if (!enterFromInfraredDemo) {
  276. //每次初始化重置一下引导
  277. InfraredDemo._ins.resetInfraredPlayerPrefs();
  278. GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  279. connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  280. }
  281. }
  282. //存储一次节点
  283. SaveLocalPos();
  284. }
  285. //设置位置
  286. public void onReset()
  287. {
  288. oldLinePosition.Clear();
  289. // 获取屏幕的四个角的像素坐标
  290. Vector2 bottomLeft = new Vector2(0, 0);
  291. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  292. Vector2 localBottomLeft;
  293. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  294. // 打印结果
  295. Debug.Log("Local Bottom Left: " + localBottomLeft);
  296. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  297. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  298. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  299. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  300. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  301. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  302. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  303. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  304. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  305. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  306. //设置一次位置
  307. SetLinePos();
  308. }
  309. #endregion
  310. List<Vector2> _locatePointList = new();
  311. float _texWidth;
  312. float _texHeight;
  313. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  314. {
  315. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  316. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  317. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  318. _locatePointList.Add(pos);
  319. }
  320. bool ConfirmScreenLocateManual()
  321. {
  322. if (InfraredDemo.running)
  323. {
  324. //渲染截图
  325. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  326. if (texture2D == null)
  327. {
  328. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  329. return false;
  330. }
  331. _locatePointList.Clear();
  332. _texWidth = texture2D.width;
  333. _texHeight = texture2D.height;
  334. RecordLocatePoint(pos1, new Vector2(0, 0));
  335. RecordLocatePoint(pos2, new Vector2(1, 0));
  336. RecordLocatePoint(pos3, new Vector2(1, 1));
  337. RecordLocatePoint(pos4, new Vector2(0, 1));
  338. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  339. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  340. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  341. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  342. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  343. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  344. //同步数据
  345. ScreenLocate.quadUnityVectorList.Clear();
  346. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  347. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  348. //两个点切换,顺序不一样
  349. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  350. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  351. ScreenLocate.SaveScreenLocateVectorList();
  352. return true;
  353. }
  354. return false;
  355. }
  356. void ConfirmScreenLocateManualTest()
  357. {
  358. //渲染截图
  359. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  360. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  361. if (texture2D == null)
  362. {
  363. _screenLocate.QuitScreenLocateManual(null);
  364. return;
  365. }
  366. _locatePointList.Clear();
  367. _texWidth = texture2D.width;
  368. _texHeight = texture2D.height;
  369. RecordLocatePoint(pos1, new Vector2(0, 0));
  370. RecordLocatePoint(pos2, new Vector2(1, 0));
  371. RecordLocatePoint(pos3, new Vector2(1, 1));
  372. RecordLocatePoint(pos4, new Vector2(0, 1));
  373. _screenLocate.QuitScreenLocateManual(_locatePointList);
  374. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  375. GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  376. pointsTF2.SetActive(true);
  377. if (pointsTF2.transform.childCount == _locatePointList.Count)
  378. {
  379. Vector2 texSize = new Vector2(_texWidth, _texHeight);
  380. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  381. {
  382. Transform pointTF = pointsTF2.transform.GetChild(i);
  383. Vector2 pos = _locatePointList[i];
  384. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  385. pointTF.gameObject.SetActive(true);
  386. }
  387. }
  388. //同步数据
  389. ScreenLocate.quadUnityVectorList.Clear();
  390. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  391. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  392. //两个点切换,顺序不一样
  393. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  394. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  395. ScreenLocate.SaveScreenLocateVectorList();
  396. }
  397. void SaveLocalPos()
  398. {
  399. List<Vector3> screenPositions = new List<Vector3>();
  400. screenPositions.Add(pos1.anchoredPosition);
  401. screenPositions.Add(pos2.anchoredPosition);
  402. screenPositions.Add(pos3.anchoredPosition);
  403. screenPositions.Add(pos4.anchoredPosition);
  404. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  405. Debug.Log("Local UI Position: " + saveStr);
  406. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  407. }
  408. void GetLocalPos()
  409. {
  410. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  411. if (!string.IsNullOrWhiteSpace(posListStr))
  412. {
  413. List<Vector2> posList = posListStr.Split(';')
  414. .Select(s =>
  415. {
  416. string[] parts = s.Split(',');
  417. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  418. })
  419. .ToList();
  420. pos1.anchoredPosition = posList[0];
  421. pos2.anchoredPosition = posList[1];
  422. pos3.anchoredPosition = posList[2];
  423. pos4.anchoredPosition = posList[3];
  424. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  425. }
  426. }
  427. }