InfraredScreenPositioningView.cs 18 KB

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