InfraredScreenPositioningView.cs 17 KB

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