InfraredScreenPositioningView.cs 17 KB

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