InfraredScreenPositioningView.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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. using Color = UnityEngine.Color;
  10. using InfraredManager;
  11. using Org.BouncyCastle.Asn1.Crmf;
  12. using ZIM.Unity;
  13. using o0.Project;
  14. using UnityEngine.SceneManagement;
  15. public enum ScreenPositioningStep {
  16. None, //
  17. Start,//开始界面
  18. AutoEnd,//自动定位结束后界面
  19. Marker,//进行标记界面
  20. Manual,//手动界面
  21. LightCtrl,//灯光调整界面
  22. Successful,//结果选择界面
  23. }
  24. public class LinePosition
  25. {
  26. public int index;
  27. public List<Vector3> pos;
  28. }
  29. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  30. {
  31. [SerializeField]
  32. RectTransform canvasRectTransform;
  33. [SerializeField]
  34. RectTransform draggableParent;
  35. [SerializeField]
  36. GameObject cameraLight;
  37. [SerializeField]
  38. RectTransform pointsParent;
  39. [SerializeField]
  40. RectTransform pos1;
  41. [SerializeField]
  42. RectTransform pos2;
  43. [SerializeField]
  44. RectTransform pos3;
  45. [SerializeField]
  46. RectTransform pos4;
  47. //画线时候的点偏移量
  48. float offset = 10;
  49. [SerializeField]
  50. Line line;
  51. List<LinePosition> oldLinePosition;
  52. Vector3 beginPos;
  53. Vector3 endPos;
  54. //相机感光部分
  55. [SerializeField]
  56. Slider slider;
  57. [SerializeField]
  58. GameObject textTip1;
  59. [SerializeField]
  60. GameObject textTip2;
  61. [SerializeField]
  62. Button btnAuto;
  63. [SerializeField]
  64. Button btnHandMovement;
  65. [SerializeField]
  66. BtnRecordInfrared btnRecordInfrared;
  67. bool bAuto = true;
  68. [SerializeField] Color normalColor = Color.white;
  69. [SerializeField] Color highlightedColor = Color.green;
  70. [SerializeField] Color normalTextColor = Color.black;
  71. [SerializeField] Color highlightedTextColor = Color.white;
  72. [Header("Common Layout Group")]
  73. [SerializeField]
  74. GameObject mask;
  75. [SerializeField]
  76. Line maskLine;
  77. [SerializeField]
  78. RawImage rawImage;
  79. [Tooltip("Demo测试时候显示")]
  80. [SerializeField]
  81. RectTransform crosshair;
  82. [SerializeField]
  83. RectTransform crosshairRed;
  84. [Tooltip("存在校准数据时候显示")]
  85. [SerializeField] LineGenerator CurrentUILineGenerator;//开始显示屏幕线条
  86. [Header("Start Layout Group")]
  87. [SerializeField]
  88. GameObject LayoutStart;
  89. [SerializeField]
  90. GameObject StartTextTip1;
  91. [SerializeField]
  92. GameObject StartTextTipHasData;
  93. [Tooltip("没有存在校准数据时候显示")]
  94. [SerializeField]
  95. GameObject BottomAutoBtn;
  96. [Tooltip("存在校准数据时候显示")]
  97. [SerializeField]
  98. GameObject BottomConfirmBtn;
  99. [Header("AutoEnd Layout Group")]
  100. [SerializeField]
  101. GameObject LayoutAutoEnd;
  102. [Header("LightCtrl Layout Group")]
  103. [SerializeField]
  104. GameObject LayoutLightCtrl;
  105. [SerializeField] Slider sliderContrast;
  106. //亮度
  107. [SerializeField] Slider sliderBrightness;
  108. [Header("Marker Layout Group")]
  109. [SerializeField]
  110. GameObject LayoutMarker;
  111. [SerializeField]
  112. RectTransform crosshairSmall;
  113. [Tooltip("标记页面标题提示")]
  114. [SerializeField] TextAutoLanguage2 markerTextAutoLanguage2;
  115. [SerializeField]
  116. RectTransform markerPointsParent;
  117. [SerializeField]
  118. List<RectTransform> markerPosList;
  119. [SerializeField]
  120. Line markerPointsLine;
  121. [Header("Manual Layout Group")]
  122. [SerializeField]
  123. GameObject LayoutManual;
  124. [Header("Successful Layout Group")]
  125. [SerializeField]
  126. GameObject LayoutSuccessful;
  127. [Tooltip("选择框的Line")]
  128. [SerializeField]
  129. Sprite[] ResultLines;// 0: 选中状态, 1: 未选中状态
  130. [Tooltip("选择框图片")]
  131. [SerializeField]
  132. Image[] ResultImages;
  133. [Tooltip("选择框按钮")]
  134. [SerializeField]
  135. Button[] ResultImagesButtons;
  136. [SerializeField]
  137. Button[] ResultButtons; // 结果选择按钮
  138. [SerializeField]
  139. RawImage CameraFirstImage; //第一个结果屏幕
  140. [SerializeField]
  141. RawImage CameraSecondImage; //第二个结果屏幕
  142. [SerializeField] LineGenerator FirstUILineGenerator;//第一个结果屏幕线条
  143. [SerializeField] LineGenerator SecondUILineGenerator;//第二个结果屏幕线条
  144. bool bMarkerFinish = false;
  145. ScreenPositioningStep perStepView = ScreenPositioningStep.None;//最后一次进入LayoutSuccessful的页面
  146. //标记当前页面情况
  147. ScreenPositioningStep curStepView = ScreenPositioningStep.Start;//设置一个开始页面
  148. InfraredDemo infraredDemo;
  149. bool doLocateAuto;
  150. int DefaultResolutionIndex;
  151. private void Awake()
  152. {
  153. //设置一次显示相机的image 的颜色
  154. rawImage.color = Color.white;
  155. offset = line.MyThickness;
  156. }
  157. void Start()
  158. {
  159. //获取InfraredDemo
  160. infraredDemo = InfraredDemo._ins;
  161. doLocateAuto = false;
  162. textTip1.SetActive(true);
  163. //设置btnAuto 高亮
  164. //OnButtonClick(btnAuto);
  165. textTip2.SetActive(false);
  166. //动态设置marker的提示语
  167. //if (BluetoothAim.ins!=null && markerTextAutoLanguage2 != null)
  168. //{
  169. // string name = "";
  170. // bool switchValue = false;
  171. // if (BluetoothAim.ins.isMainConnectToHOUYIPRO())
  172. // {
  173. // name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-HOUYIPro");
  174. // }
  175. // else if (BluetoothAim.ins.isMainConnectToARTEMISPRO())
  176. // {
  177. // name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-ArtemisPro");
  178. // }
  179. // else if (BluetoothAim.ins.isMainConnectToGun())
  180. // {
  181. // //name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-M9");
  182. // switchValue = true;
  183. // }
  184. // if (switchValue)
  185. // {
  186. // markerTextAutoLanguage2.SetTextKey("TitleTipMarker-M9");
  187. // }
  188. // else {
  189. // markerTextAutoLanguage2.textFormatArgs = new object[] { name };
  190. // }
  191. //}
  192. //手动不高亮
  193. //ResetButton(btnHandMovement);
  194. //quadUnityVectorList.Clear();
  195. //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
  196. //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
  197. //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
  198. //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
  199. Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
  200. if (ScreenLocate.quadUnityVectorList.Count == 0)
  201. {
  202. Debug.Log("GetLocalPos");
  203. GetLocalPos();
  204. }
  205. else
  206. {
  207. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  208. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  209. //自动识别时候四个点
  210. SyncQuadUnityVectorListToPos();
  211. //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  212. //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  213. //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  214. //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  215. }
  216. //更新start相关ui
  217. UpdateStartUI();
  218. //记录操作的位置信息
  219. oldLinePosition = new List<LinePosition>();
  220. SetLinePos();
  221. //相机感光度
  222. if (infraredDemo)
  223. {
  224. ////重置偏移量
  225. //infraredDemo.ResetCenterOffset();
  226. //////重置识别点
  227. ////ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  228. //////清除一下记录的点
  229. ////ScreenLocate.quadUnityVectorList.Clear();
  230. //infraredDemo.ResetPositioningData();
  231. slider.onValueChanged.AddListener((value) =>
  232. {
  233. infraredDemo.onSliderEvent(value);
  234. });
  235. infraredDemo.onSetSliderValue(slider);
  236. //对比度
  237. sliderContrast.onValueChanged.AddListener((value) =>
  238. {
  239. infraredDemo.onSliderCustomEvent(value, -20.0f, 20.0f);
  240. });
  241. infraredDemo.onSetSliderCustomValue(sliderContrast, -20.0f, 20.0f);
  242. //亮度
  243. sliderBrightness.onValueChanged.AddListener((value) =>
  244. {
  245. infraredDemo.onSliderCustomBrightnessEvent(value, -20.0f, 20.0f);
  246. });
  247. infraredDemo.onSetSliderCustomBrightnessValue(sliderBrightness, -20.0f, 20.0f);
  248. offset = line.MyThickness = markerPointsLine.MyThickness = infraredDemo.lineWidth.Get();
  249. //LineGenerator 比 line 宽少了大概2倍
  250. CurrentUILineGenerator.LineThickness = infraredDemo.lineWidth.Get() * 2;
  251. //扇形宽
  252. markerPointsLine.MyFanWidth = line.MyFanWidth = infraredDemo.fanWidth.Get();
  253. ZIM.Unity.QuadrilateralInCamera screen = ScreenLocate.Main.ScreenIdentification.Screen.QuadInCamera;
  254. CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screen, ScreenLocate.Main.getUVCCameraInfoSize);
  255. }
  256. else {
  257. //编辑器使用
  258. if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
  259. slider.onValueChanged.AddListener((value) =>
  260. {
  261. double originalMin = 0.0;
  262. double originalMax = 10.0;
  263. double targetMin = -1;
  264. double targetMax = 1;
  265. // 计算转换后的值
  266. double result = InfraredDemo.MapValue(value, originalMin, originalMax, targetMin, targetMax);
  267. //int _current = (int)(result);
  268. Debug.Log("_current:" + value + " , result:" + result);
  269. ScreenLocate.Main.pcContrast = (float)result;
  270. });
  271. float v = ScreenLocate.Main.pcContrast;
  272. // 目标区间 [0, 10] 的边界值
  273. double targetMin = 0.0;
  274. double targetMax = 10.0;
  275. double originalMin = -1;
  276. double originalMax = 1;
  277. // 计算转换后的值
  278. double v2 = InfraredDemo.MapValue(v, originalMin, originalMax, targetMin, targetMax);
  279. Debug.Log("PC获取相机的感光度:" + v + " = " + v2);
  280. slider.SetValueWithoutNotify((float)v2);
  281. // 对 sliderContrast 和 sliderBrightness 调用统一的处理函数
  282. HandleSliderValueChanged(sliderContrast, value => ScreenLocate.Main.pcContrast = value, ScreenLocate.Main.pcContrast);
  283. HandleSliderValueChanged(sliderBrightness, value => ScreenLocate.Main.pcBrightness = value, ScreenLocate.Main.pcBrightness);
  284. }
  285. }
  286. initSelectInfo();
  287. //if (ScreenLocate.Main.getUVCTexture)
  288. //{
  289. // //渲染相机画面
  290. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  291. // //Debug.Log("rawImage.texture:" + rawImage.texture.name);
  292. //}
  293. //slider.onValueChanged.AddListener((value) => {
  294. // //onSliderEvent(value);
  295. // infraredDemo.SetBrightness(value);
  296. //});
  297. //slider.value = infraredDemo.brightness.Get();
  298. //修改分辨率。是否清晰一点?
  299. //DefaultResolutionIndex = infraredDemo?.ResolutionIndex ?? 0;
  300. //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
  301. //infraredDemo?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
  302. //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  303. //{
  304. // SB_EventSystem.ins.AwakenSimulateMouse();
  305. // // Debug.Log("simulate-mouse-close");
  306. //}
  307. }
  308. // 两个滑块值处理
  309. void HandleSliderValueChanged(UnityEngine.UI.Slider slider, System.Action<float> setValue, float pcValue)
  310. {
  311. double targetMin2 = -20.0;
  312. double targetMax2 = 20.0;
  313. double originalMin2 = -1;
  314. double originalMax2 = 1;
  315. slider.onValueChanged.AddListener((value) =>
  316. {
  317. double result = InfraredDemo.MapValue(value, targetMin2, targetMax2, originalMin2, originalMax2);
  318. Debug.Log($"{slider.name}_current: {value}, result: {result}");
  319. setValue((float)result);
  320. });
  321. double mappedValue = InfraredDemo.MapValue(pcValue, originalMin2, originalMax2, targetMin2, targetMax2);
  322. Debug.Log($"{slider.name} PC获取相机的感光度: {pcValue} = {mappedValue}");
  323. slider.SetValueWithoutNotify((float)mappedValue);
  324. }
  325. private void OnDestroy()
  326. {
  327. //修改回进入手动调节页面时候的分辨率
  328. //infraredDemo?.SetResolutionNew(DefaultResolutionIndex);
  329. Debug.Log("OnDestroy*********************************AwakenSimulateMouse:" + SB_EventSystem.ins.simulateMouseIsAwaked);
  330. if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked)
  331. {
  332. SB_EventSystem.ins.AwakenSimulateMouse();
  333. }
  334. }
  335. public RawImage Bg => rawImage;
  336. void Update()
  337. {
  338. if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnZIMDemo) {
  339. // ZimWebCamera场景测试
  340. rawImage.texture = ScreenLocate.Main.outputRawImages[7].texture;
  341. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  342. {
  343. // 检测到光点
  344. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  345. crosshair.gameObject.SetActive(true);
  346. crosshair.anchoredPosition = posInCanvas;
  347. }
  348. else
  349. crosshair.gameObject.SetActive(false);
  350. //渲染相机画面
  351. CameraSecondImage.texture = rawImage.texture;
  352. CameraFirstImage.texture = rawImage.texture;
  353. }
  354. else if (InfraredDemo.running)
  355. {
  356. //渲染相机画面
  357. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  358. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  359. {
  360. rawImage.texture = texture;
  361. //渲染相机画面
  362. CameraSecondImage.texture = rawImage.texture;
  363. CameraFirstImage.texture = rawImage.texture;
  364. }
  365. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  366. {
  367. // 检测到光点
  368. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  369. crosshairSmall.gameObject.SetActive(true);
  370. crosshairSmall.anchoredPosition = posInCanvas;
  371. }
  372. else
  373. crosshairSmall.gameObject.SetActive(false);
  374. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  375. //可见光调节页面出现红十字光标
  376. if (curStepView == ScreenPositioningStep.LightCtrl)
  377. {
  378. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  379. {
  380. // 检测到光点
  381. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  382. crosshairRed.gameObject.SetActive(true);
  383. crosshairRed.anchoredPosition = posInCanvas;
  384. }
  385. else
  386. crosshairRed.gameObject.SetActive(false);
  387. }
  388. else if (crosshairRed.gameObject.activeSelf) {
  389. crosshairRed.gameObject.SetActive(false);
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// 根据enum操作ui
  395. /// </summary>
  396. /// <param name="step"></param>
  397. public void SetScreenPositioningStepState(ScreenPositioningStep step = ScreenPositioningStep.None)
  398. {
  399. curStepView = step;
  400. AllScreenPositioningStepFalse();
  401. switch (step)
  402. {
  403. case ScreenPositioningStep.Start:
  404. maskLine.SetDrawMask(true);
  405. LayoutStart.SetActive(true);
  406. UpdateStartUI();
  407. break;
  408. case ScreenPositioningStep.AutoEnd:
  409. maskLine.SetDrawMask(true);
  410. LayoutAutoEnd.SetActive(true);
  411. pointsParent.gameObject.SetActive(false);
  412. CurrentUILineGenerator.enabled = true;
  413. break;
  414. case ScreenPositioningStep.LightCtrl:
  415. maskLine.SetDrawMask(false);
  416. LayoutLightCtrl.SetActive(true);
  417. CurrentUILineGenerator.enabled = true;
  418. pointsParent.gameObject.SetActive(false);
  419. //setPointsLocation(ScreenLocate.quadUnityVectorList, pointsParent.gameObject, true);
  420. break;
  421. case ScreenPositioningStep.Marker:
  422. perStepView = ScreenPositioningStep.Marker;
  423. bMarkerFinish = false;
  424. maskLine.SetDrawMask(true);
  425. LayoutMarker.SetActive(true);
  426. CurrentUILineGenerator.enabled = false;
  427. pointsParent.gameObject.SetActive(false);
  428. SyncQuadUnityVectorListToMarkerPointesPos();
  429. //setPointsLocation(ScreenLocate.quadUnityVectorList, pointsParent.gameObject, true);
  430. break;
  431. case ScreenPositioningStep.Manual:
  432. SaveStartOldLinePosition();
  433. perStepView = ScreenPositioningStep.Manual;
  434. bMarkerFinish = false;
  435. LayoutManual.SetActive(true);
  436. bAuto = false;
  437. doLocateAuto = false;
  438. draggableParent.gameObject.SetActive(true);
  439. pointsParent.gameObject.SetActive(false);
  440. break;
  441. case ScreenPositioningStep.Successful:
  442. LayoutSuccessful.SetActive(true);
  443. pointsParent.gameObject.SetActive(false);
  444. CurrentUILineGenerator.enabled = false;
  445. break;
  446. }
  447. }
  448. void AllScreenPositioningStepFalse()
  449. {
  450. LayoutStart.SetActive(false);
  451. LayoutMarker.SetActive(false);
  452. LayoutManual.SetActive(false);
  453. LayoutAutoEnd.SetActive(false);
  454. LayoutLightCtrl.SetActive(false);
  455. LayoutSuccessful.SetActive(false);
  456. }
  457. /// <summary>
  458. /// 开始页面时候ui
  459. /// </summary>
  460. void UpdateStartUI() {
  461. pointsParent.gameObject.SetActive(false);
  462. CurrentUILineGenerator.enabled = true;
  463. if (ScreenLocate.quadUnityVectorList.Count == 0)
  464. {
  465. BottomConfirmBtn.SetActive(false);
  466. StartTextTip1.SetActive(true);
  467. StartTextTipHasData.SetActive(false);
  468. }
  469. else
  470. {
  471. //存在数据,显示确认按钮,确认按钮直接跳转到游戏
  472. BottomConfirmBtn.SetActive(true);
  473. StartTextTip1.SetActive(false);
  474. StartTextTipHasData.SetActive(true);
  475. }
  476. }
  477. public void OnClick_Back()
  478. {
  479. AudioMgr.ins.PlayBtn();
  480. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  481. }
  482. /// <summary>
  483. /// 返回最开始页面
  484. /// </summary>
  485. public void OnClick_BackLayoutStart()
  486. {
  487. //重置中心点0
  488. infraredDemo.ResetCenterOffset();
  489. //清除白线数据
  490. infraredDemo.ResetPositioningData();
  491. SetScreenPositioningStepState(ScreenPositioningStep.Start);
  492. }
  493. /// <summary>
  494. /// 进入环境光测试
  495. /// </summary>
  496. public void OnClick_EnterLightCtrl() {
  497. SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
  498. }
  499. /// <summary>
  500. /// 进入屏幕标记
  501. /// </summary>
  502. public void OnClick_EnterMarker() {
  503. SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  504. }
  505. /// <summary>
  506. /// successful 返回页面时候,根据ScreenPositioningStep区分
  507. /// </summary>
  508. public void OnClick_BackByScreenPositioningStep()
  509. {
  510. SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  511. //if(perStepView == ScreenPositioningStep.Manual)
  512. // SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  513. //else
  514. // SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  515. }
  516. /// <summary>
  517. /// 直接进入游戏流程
  518. /// </summary>
  519. public void OnClick_EnterGame()
  520. {
  521. //对比确认效果。直接进入游戏引导页面
  522. EnterGame();
  523. }
  524. /// <summary>
  525. /// 进入自动流程校准操作
  526. /// </summary>
  527. public void OnClick_EnterAuto()
  528. {
  529. Debug.LogError("OnClick_EnterAuto");
  530. //自动按钮时候进入之前的校准流程
  531. //重置偏移量
  532. infraredDemo.ResetCenterOffset();
  533. infraredDemo.ResetPositioningData();
  534. OnClick_Auto();
  535. }
  536. /// <summary>
  537. /// 自动识别
  538. /// </summary>
  539. public void OnClick_Auto()
  540. {
  541. bAuto = true;
  542. doLocateAuto = true;
  543. //textTip1.SetActive(false);
  544. //textTip2.SetActive(true);
  545. //ResetButton(btnAuto);
  546. if (enterFromZimWebCamera)
  547. {
  548. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  549. _screenLocate.EnterScreenLocateManualAuto();
  550. return;
  551. }
  552. if (InfraredDemo.running)
  553. {
  554. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  555. }
  556. }
  557. /// <summary>
  558. /// 进入手动界面
  559. /// </summary>
  560. public void OnClick_EnterManual()
  561. {
  562. SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  563. }
  564. /// <summary>
  565. /// 切换成手动方式
  566. /// </summary>
  567. public void onHandMovement()
  568. {
  569. bAuto = false;
  570. doLocateAuto = false;
  571. draggableParent.gameObject.SetActive(true);
  572. pointsParent.gameObject.SetActive(false);
  573. mask.SetActive(false);
  574. cameraLight.SetActive(true);
  575. }
  576. /// <summary>
  577. /// 自动识别后同步
  578. /// </summary>
  579. public void SyncScreenPosition()
  580. {
  581. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  582. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  583. SyncQuadUnityVectorListToPos();
  584. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  585. SyncQuadUnityVectorListToMarkerPointesPos();
  586. if (curStepView == ScreenPositioningStep.Start || curStepView == ScreenPositioningStep.AutoEnd) {
  587. //如果是开始页面进行自动定位的
  588. if(curStepView != ScreenPositioningStep.AutoEnd) SetScreenPositioningStepState(ScreenPositioningStep.AutoEnd);
  589. }
  590. else if (curStepView == ScreenPositioningStep.Marker || curStepView == ScreenPositioningStep.Manual) {
  591. //显示最后结果
  592. if(bMarkerFinish) SetScreenPositioningStepState(ScreenPositioningStep.Successful);
  593. }
  594. //屏幕变化情况下。同步修改对应的line
  595. //设置两个线段
  596. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  597. QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
  598. if (screenAuto != null)
  599. {
  600. Debug.Log("[校准流程]Successful自动识别screenAuto信息 ------------ ");
  601. CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  602. FirstUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  603. }
  604. else
  605. {
  606. Debug.LogError("screenAuto 不存在!");
  607. }
  608. if (screenSemiAuto != null)
  609. {
  610. Debug.Log("[校准流程]Successful半自动识别screenSemiAuto信息 ------------ ");
  611. SecondUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenSemiAuto, texSize);
  612. }
  613. else
  614. {
  615. Debug.LogError("screenSemiAuto 不存在!");
  616. }
  617. }
  618. #region 绘制线段部分
  619. //点击拖拽的开始位置
  620. public void onBeginPos(int index, Vector3 pos)
  621. {
  622. Debug.Log("pos begin: " + pos);
  623. beginPos = pos;
  624. }
  625. public void onDragPos(int index, Vector3 pos)
  626. {
  627. //设置线段的点
  628. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  629. }
  630. //点击拖拽的结束位置
  631. public void onEndPos(int index, Vector3 pos)
  632. {
  633. Debug.Log("pos end: " + pos);
  634. endPos = pos;
  635. if (beginPos == endPos) return;
  636. //Debug.Log(index+",最后的点:" + pos);
  637. //再记录一次最后的点
  638. SetLinePos();
  639. }
  640. //同步设置图片对应的位置到line
  641. public void SetLinePos()
  642. {
  643. //记录一个操作点的操作位置
  644. AddOldLinePosition();
  645. //设置线段的点
  646. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  647. }
  648. void AddOldLinePosition()
  649. {
  650. Vector3[] v = new Vector3[4];
  651. pos1.GetWorldCorners(v);
  652. Vector3[] v1 = new Vector3[4];
  653. pos1.GetLocalCorners(v1);
  654. //for (int i = 0; i < 4; i++)
  655. //{
  656. // Debug.Log(i + " =1= " + v[i]);
  657. // Debug.Log(i + " =2= " + v1[i]);
  658. //}
  659. //记录一个操作点的操作位置
  660. List<Vector3> screenPositions = new List<Vector3>();
  661. screenPositions.Add(pos1.localPosition);
  662. screenPositions.Add(pos2.localPosition);
  663. screenPositions.Add(pos3.localPosition);
  664. screenPositions.Add(pos4.localPosition);
  665. LinePosition linePosition = new LinePosition();
  666. linePosition.index = oldLinePosition.Count;
  667. linePosition.pos = screenPositions;
  668. oldLinePosition.Add(linePosition);
  669. }
  670. /// <summary>
  671. /// 记录一个开始位置
  672. /// </summary>
  673. void SaveStartOldLinePosition() {
  674. oldLinePosition.Clear();
  675. //记录一个操作点的操作位置
  676. List<Vector3> screenPositions = new List<Vector3>();
  677. screenPositions.Add(pos1.localPosition);
  678. screenPositions.Add(pos2.localPosition);
  679. screenPositions.Add(pos3.localPosition);
  680. screenPositions.Add(pos4.localPosition);
  681. LinePosition linePosition = new LinePosition();
  682. linePosition.index = 1;
  683. linePosition.pos = screenPositions;
  684. oldLinePosition.Add(linePosition);
  685. }
  686. //转换绘制线段的点
  687. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  688. {
  689. List<Vector2> _screenPositions = new List<Vector2>();
  690. //点为负数的增大 offset,正数减少 offset
  691. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  692. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  693. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  694. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  695. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  696. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  697. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  698. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  699. _screenPositions.Add(startPos1);
  700. _screenPositions.Add(startPos2);
  701. _screenPositions.Add(startPos3);
  702. _screenPositions.Add(startPos4);
  703. return _screenPositions;
  704. }
  705. void SetRectanglePoints(List<Vector2> screenPositions)
  706. {
  707. line.SetLine(screenPositions);
  708. }
  709. //撤回上一个元素
  710. public void onRecall()
  711. {
  712. // 获取并删除最后一个元素,并且保留一个元素
  713. if (oldLinePosition.Count > 1) // 确保列表不为空
  714. {
  715. // 获取回退的那个元素点
  716. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  717. // 获取最后一个元素
  718. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  719. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  720. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  721. pos1.localPosition = lastElement_second.pos[0];
  722. pos2.localPosition = lastElement_second.pos[1];
  723. pos3.localPosition = lastElement_second.pos[2];
  724. pos4.localPosition = lastElement_second.pos[3];
  725. //设置线段的点
  726. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  727. }
  728. }
  729. //是不是从Demo界面进入该页面的
  730. public bool enterFromInfraredDemo { get; set; } = false;
  731. //是否从测试场景进入
  732. public bool enterFromZimWebCamera { get; set; } = false;
  733. //确认修改
  734. public void onConfirmation()
  735. {
  736. if (enterFromZimWebCamera)
  737. {
  738. ConfirmScreenLocateManualTest();
  739. if (oldLinePosition.Count > 1) // 确保列表不为空
  740. {
  741. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  742. oldLinePosition.Clear();
  743. oldLinePosition.Add(lastElement);
  744. }
  745. SaveLocalPos();
  746. AudioMgr.ins.PlayBtn();
  747. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  748. return;
  749. }
  750. if (!ConfirmScreenLocateManual()) return;
  751. if (oldLinePosition.Count > 1) // 确保列表不为空
  752. {
  753. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  754. oldLinePosition.Clear();
  755. //oldLinePosition.Add(lastElement);
  756. }
  757. if (InfraredDemo.running)
  758. {
  759. //跳转入界面
  760. AudioMgr.ins.PlayBtn();
  761. if (!enterFromInfraredDemo)
  762. {
  763. //每次初始化重置一下引导
  764. infraredDemo.resetInfraredPlayerPrefs();
  765. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  766. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  767. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  768. {
  769. //如果是红外连接成功,记录一个tag
  770. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  771. }
  772. onEnterInfrared();
  773. }
  774. else {
  775. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  776. }
  777. }
  778. //存储一次节点
  779. SaveLocalPos();
  780. }
  781. //重置位置
  782. public void onReset()
  783. {
  784. oldLinePosition.Clear();
  785. // 获取屏幕的四个角的像素坐标
  786. Vector2 bottomLeft = new Vector2(0, 0);
  787. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  788. Vector2 localBottomLeft;
  789. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  790. // 打印结果
  791. Debug.Log("Local Bottom Left: " + localBottomLeft);
  792. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  793. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  794. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  795. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  796. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  797. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  798. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  799. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  800. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  801. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  802. //设置一次位置
  803. SetLinePos();
  804. btnRecordInfrared.Reset();
  805. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  806. if (enterFromZimWebCamera)
  807. {
  808. ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
  809. ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
  810. }
  811. }
  812. /// <summary>
  813. /// 标准四个点
  814. /// </summary>
  815. /// <param name="index"></param>
  816. /// <param name="pos"></param>
  817. public void onManualNewPos(int index, Vector3 pos)
  818. {
  819. Debug.Log("pos end: " + pos);
  820. if (index == 0)
  821. pos1.localPosition = pos;
  822. else if (index == 1)
  823. pos2.localPosition = pos;
  824. else if (index == 2)
  825. pos3.localPosition = pos;
  826. else if (index == 3)
  827. pos4.localPosition = pos;
  828. //再记录一次最后的点
  829. SetLinePos();
  830. }
  831. /// <summary>
  832. /// 处理新流程,先记录手动,然后到自动识别处理
  833. /// </summary>
  834. public void onManualToAutomatic() {
  835. if (enterFromZimWebCamera)
  836. {
  837. ConfirmScreenLocateManualTest();
  838. if (oldLinePosition.Count > 1) // 确保列表不为空
  839. {
  840. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  841. oldLinePosition.Clear();
  842. oldLinePosition.Add(lastElement);
  843. }
  844. SaveLocalPos();
  845. //到自动
  846. OnClick_Auto();
  847. return;
  848. }
  849. if (!ConfirmScreenLocateManual()) {
  850. Debug.LogError("ConfirmScreenLocateManual is false!");
  851. return;
  852. }
  853. if (oldLinePosition.Count > 1) // 确保列表不为空
  854. {
  855. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  856. oldLinePosition.Clear();
  857. oldLinePosition.Add(lastElement);
  858. }
  859. //存储一次节点
  860. SaveLocalPos();
  861. Debug.Log("[校准流程]进入自动校准数据,调用EnterScreenLocateManualAuto");
  862. bAuto = true;
  863. doLocateAuto = true;
  864. //自动校准
  865. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  866. }
  867. /// <summary>
  868. /// PointMarker 调用
  869. /// </summary>
  870. public void onFinishManualToAutomatic() {
  871. bMarkerFinish = true;
  872. onManualToAutomatic();
  873. }
  874. #endregion
  875. List<Vector2> _locatePointList = new();
  876. float _texWidth;
  877. float _texHeight;
  878. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  879. {
  880. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  881. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  882. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  883. _locatePointList.Add(pos);
  884. }
  885. bool ConfirmScreenLocateManual()
  886. {
  887. if (InfraredDemo.running)
  888. {
  889. //渲染截图
  890. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  891. if (texture2D == null)
  892. {
  893. Debug.Log("EnterScreenLocateManual = null");
  894. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  895. return false;
  896. }
  897. _locatePointList.Clear();
  898. _texWidth = texture2D.width;
  899. _texHeight = texture2D.height;
  900. RecordLocatePoint(pos1, new Vector2(0, 0));
  901. RecordLocatePoint(pos2, new Vector2(1, 0));
  902. RecordLocatePoint(pos3, new Vector2(1, 1));
  903. RecordLocatePoint(pos4, new Vector2(0, 1));
  904. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  905. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  906. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  907. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  908. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  909. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  910. //同步数据
  911. ScreenLocate.quadUnityVectorList.Clear();
  912. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  913. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  914. //两个点切换,顺序不一样
  915. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  916. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  917. ScreenLocate.SaveScreenLocateVectorList();
  918. Debug.Log("[校准流程]设置QuitScreenLocateManual()手动数据,保存quadUnityVectorList");
  919. return true;
  920. }
  921. return false;
  922. }
  923. void ConfirmScreenLocateManualTest()
  924. {
  925. //渲染截图
  926. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  927. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  928. if (texture2D == null)
  929. {
  930. _screenLocate.QuitScreenLocateManual(null);
  931. return;
  932. }
  933. _locatePointList.Clear();
  934. _texWidth = texture2D.width;
  935. _texHeight = texture2D.height;
  936. RecordLocatePoint(pos1, new Vector2(0, 0));
  937. RecordLocatePoint(pos2, new Vector2(1, 0));
  938. RecordLocatePoint(pos3, new Vector2(1, 1));
  939. RecordLocatePoint(pos4, new Vector2(0, 1));
  940. _screenLocate.QuitScreenLocateManual(_locatePointList);
  941. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  942. setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
  943. if (!doLocateAuto) // 设置手动定位数据
  944. setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  945. //同步数据
  946. ScreenLocate.quadUnityVectorList.Clear();
  947. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  948. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  949. //两个点切换,顺序不一样
  950. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  951. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  952. ScreenLocate.SaveScreenLocateVectorList();
  953. }
  954. void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  955. {
  956. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  957. pointsTF2.SetActive(active);
  958. if (pointsTF2.transform.childCount == targetList.Count)
  959. {
  960. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  961. {
  962. Transform pointTF = pointsTF2.transform.GetChild(i);
  963. Vector2 pos = targetList[i];
  964. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  965. pointTF.gameObject.SetActive(true);
  966. }
  967. }
  968. }
  969. void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  970. {
  971. setPointsLocation(targetList, pointsTF2, active);
  972. var lo = new Vector2(-0.5f, -0.5f);
  973. ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
  974. 2 * (targetList[0] + lo),
  975. 2 * (targetList[1] + lo),
  976. 2 * (targetList[2] + lo),
  977. 2 * (targetList[3] + lo) };
  978. }
  979. void SaveLocalPos()
  980. {
  981. List<Vector3> screenPositions = new List<Vector3>();
  982. screenPositions.Add(pos1.anchoredPosition);
  983. screenPositions.Add(pos2.anchoredPosition);
  984. screenPositions.Add(pos3.anchoredPosition);
  985. screenPositions.Add(pos4.anchoredPosition);
  986. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  987. Debug.Log("Local UI Position: " + saveStr);
  988. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  989. }
  990. void GetLocalPos()
  991. {
  992. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  993. if (!string.IsNullOrWhiteSpace(posListStr))
  994. {
  995. List<Vector2> posList = posListStr.Split(';')
  996. .Select(s =>
  997. {
  998. string[] parts = s.Split(',');
  999. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  1000. })
  1001. .ToList();
  1002. pos1.anchoredPosition = posList[0];
  1003. pos2.anchoredPosition = posList[1];
  1004. pos3.anchoredPosition = posList[2];
  1005. pos4.anchoredPosition = posList[3];
  1006. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  1007. }
  1008. }
  1009. #region 按钮颜色切换
  1010. private void OnButtonClick(Button button)
  1011. {
  1012. // 切换按钮颜色
  1013. ColorBlock colors = button.colors;
  1014. colors.normalColor = highlightedColor;
  1015. colors.highlightedColor = highlightedColor;
  1016. button.colors = colors;
  1017. // 切换字体颜色
  1018. Text buttonText = button.GetComponentInChildren<Text>();
  1019. buttonText.color = highlightedTextColor;
  1020. }
  1021. private void ResetButton(Button button)
  1022. {
  1023. // 重置按钮颜色
  1024. ColorBlock colors = button.colors;
  1025. colors.normalColor = normalColor;
  1026. colors.highlightedColor = normalColor;
  1027. button.colors = colors;
  1028. // 重置字体颜色
  1029. Text buttonText = button.GetComponentInChildren<Text>();
  1030. buttonText.color = normalTextColor;
  1031. }
  1032. #endregion
  1033. #region 标定完成之后进入游戏界面
  1034. void onEnterInfrared()
  1035. {
  1036. //添加进入射箭场景
  1037. if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
  1038. {
  1039. Debug.Log("-----进入射箭场景!");
  1040. NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
  1041. newUserGuiderManager.curConfigKey = "开始-红外调整";
  1042. //b端都是红外设备流程
  1043. if (CommonConfig.StandaloneModeOrPlatformB)
  1044. {
  1045. newUserGuiderManager.isNewModule = true;
  1046. }
  1047. else {
  1048. newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
  1049. }
  1050. //进入射箭场景
  1051. GlobalData.pkMatchType = PKMatchType.None;
  1052. GameMgr.gameType = 1;
  1053. //射一箭回到连接页面,Device.view
  1054. GameMgr.bNavBack = true;
  1055. GameMgr.bShowDistance = false;
  1056. if (!CommonConfig.StandaloneModeOrPlatformB)
  1057. {
  1058. AimHandler.ins.bInitOne = false;//true
  1059. }
  1060. //关闭计时器
  1061. GameMgr.turnOffTimer = true;
  1062. //关闭左边靶子和底部速度栏
  1063. GameMgr.HideTargetView = true;
  1064. GameMgr.HideBillboard = true;
  1065. GameMgr.ButtonCount = 0;
  1066. UnityEngine.SceneManagement.SceneManager.LoadScene(
  1067. "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
  1068. PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
  1069. }
  1070. else
  1071. {
  1072. Debug.Log("-----跳转回连接界面!");
  1073. //如不是第一次,则应该跳转回连接界面
  1074. ViewMgr.Instance.DestroyView<SmartArcheryView>();
  1075. ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  1076. }
  1077. }
  1078. #endregion
  1079. #region 最后选择算法处理UI
  1080. int selected = -1;
  1081. void initSelectInfo() {
  1082. for (int i = 0; i < 2; i++)
  1083. {
  1084. int currentIndex = i; // 将 i 赋值给局部变量
  1085. ResultImagesButtons[currentIndex].onClick.AddListener(() => SelectImage(currentIndex));
  1086. //ResultButtons[currentIndex].onClick.AddListener(() =>
  1087. //{
  1088. // if (currentIndex == 0)
  1089. // {
  1090. // OnFirstResult();
  1091. // }
  1092. // else if (currentIndex == 1)
  1093. // {
  1094. // OnSecondResult();
  1095. // }
  1096. //});
  1097. }
  1098. //默认选择1 自动
  1099. SelectImage(0);
  1100. }
  1101. // 添加一个选择函数
  1102. void SelectImage(int selectedIndex)
  1103. {
  1104. //如果重复选中则跳转
  1105. if (selected == selectedIndex) {
  1106. if (selected == 0)
  1107. {
  1108. OnFirstResult();
  1109. }
  1110. else if (selected == 1)
  1111. {
  1112. OnSecondResult();
  1113. }
  1114. return;
  1115. }
  1116. // 遍历所有图片
  1117. selected = selectedIndex;
  1118. //for (int i = 0; i < ResultImages.Length; i++)
  1119. //{
  1120. // // 如果是选中的 index,赋值为 ResultLines[0],否则赋值为 ResultLines[1]
  1121. // if (i == selectedIndex)
  1122. // {
  1123. // selected = i;
  1124. // ResultImages[i].sprite = ResultLines[0];
  1125. // ResultButtons[i].interactable = true;
  1126. // }
  1127. // else
  1128. // {
  1129. // ResultImages[i].sprite = ResultLines[1];
  1130. // ResultButtons[i].interactable = false;
  1131. // }
  1132. //}
  1133. }
  1134. //选择全自动结果后进入游戏
  1135. void OnFirstResult()
  1136. {
  1137. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
  1138. UpdateQuadUnityVectorList();
  1139. onCompelete();
  1140. }
  1141. //选择半自动结果
  1142. void OnSecondResult()
  1143. {
  1144. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
  1145. UpdateQuadUnityVectorList();
  1146. onCompelete();
  1147. }
  1148. public void onCompelete()
  1149. {
  1150. if (enterFromZimWebCamera)
  1151. {
  1152. SaveLocalPos();
  1153. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1154. return;
  1155. }
  1156. EnterGame();
  1157. //存储一次节点
  1158. SaveLocalPos();
  1159. }
  1160. void EnterGame() {
  1161. if (InfraredDemo.running)
  1162. {
  1163. //跳转入界面
  1164. AudioMgr.ins.PlayBtn();
  1165. if (!enterFromInfraredDemo)
  1166. {
  1167. //每次初始化重置一下引导
  1168. infraredDemo.resetInfraredPlayerPrefs();
  1169. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  1170. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  1171. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  1172. {
  1173. //如果是红外连接成功,记录一个tag
  1174. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  1175. }
  1176. onEnterInfrared();
  1177. }
  1178. else
  1179. {
  1180. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1181. }
  1182. }
  1183. }
  1184. /// <summary>
  1185. /// 选择模式后更新 quadUnityVectorList
  1186. /// </summary>
  1187. void UpdateQuadUnityVectorList() {
  1188. ScreenLocate.Main.UpdateQuadUnityVectorList();
  1189. SyncQuadUnityVectorListToPos();
  1190. infraredDemo?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1191. _locatePointList.Clear();
  1192. }
  1193. void SyncQuadUnityVectorListToPos() {
  1194. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  1195. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1196. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1197. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1198. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1199. }
  1200. //这个脚本存在时候。任何切换操作都直接处理删除
  1201. //void OnSceneUnloaded(Scene scene)
  1202. //{
  1203. // ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  1204. // ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1205. //}
  1206. //void OnEnable()
  1207. //{
  1208. // SceneManager.sceneUnloaded += OnSceneUnloaded;
  1209. //}
  1210. //void OnDisable()
  1211. //{
  1212. // SceneManager.sceneUnloaded -= OnSceneUnloaded;
  1213. //}
  1214. #endregion
  1215. #region 判断点是否在 quad 内部
  1216. //判断maskline
  1217. public bool IsPointInMaskLine(Vector2 point) {
  1218. return IsPointInQuad(point, maskLine.ScreenPositions);
  1219. }
  1220. //实现基于射线法,通过数交点的奇偶性来判断点是否在多边形内
  1221. bool IsPointInQuad(Vector2 point, List<Vector2> quadVertices)
  1222. {
  1223. if (quadVertices.Count != 4)
  1224. {
  1225. Debug.LogError("Quad must have exactly 4 vertices.");
  1226. return false;
  1227. }
  1228. bool isInside = false;
  1229. int vertexCount = quadVertices.Count;
  1230. for (int i = 0, j = vertexCount - 1; i < vertexCount; j = i++)
  1231. {
  1232. Vector2 vi = quadVertices[i];
  1233. Vector2 vj = quadVertices[j];
  1234. // 检查射线是否与边相交
  1235. if (((vi.y > point.y) != (vj.y > point.y)) &&
  1236. (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x))
  1237. {
  1238. isInside = !isInside;
  1239. }
  1240. }
  1241. return isInside;
  1242. }
  1243. /// <summary>
  1244. /// 判断四个点是否能够形成有效的四边形,并根据面积阈值过滤小四边形
  1245. /// </summary>
  1246. /// <param name="points">四个点</param>
  1247. /// <param name="minArea">面积阈值,用于过滤面积太小的四边形</param>
  1248. /// <returns>是否是有效的四边形</returns>
  1249. public bool IsValidQuadrilateral(Vector2[] points, float minArea = 100000.0f)
  1250. {
  1251. if (points.Length != 4)
  1252. {
  1253. Debug.Log("四个点数不足,无法形成四边形。");
  1254. return false; // 如果点数不是4,直接返回false
  1255. }
  1256. // 计算四边形的面积
  1257. float area = CalculateArea(points);
  1258. Debug.Log($"四边形面积: {area}");
  1259. if (area < minArea)
  1260. {
  1261. Debug.Log($"面积小于阈值: {minArea},返回 false。");
  1262. return false; // 面积太小,返回 false
  1263. }
  1264. // 检查是否有三点共线
  1265. for (int i = 0; i < points.Length; i++)
  1266. {
  1267. Vector2 a = points[i];
  1268. Vector2 b = points[(i + 1) % points.Length];
  1269. Vector2 c = points[(i + 2) % points.Length];
  1270. if (ArePointsCollinear(a, b, c))
  1271. {
  1272. Debug.Log($"点 {a}, {b}, {c} 共线,返回 false。");
  1273. return false; // 存在共线点,返回 false
  1274. }
  1275. }
  1276. // 检查是否为矩形或接近矩形
  1277. if (!IsRectangle(points))
  1278. {
  1279. // 如果不是矩形,检查是否是梯形
  1280. if (!IsTrapezoid(points))
  1281. {
  1282. Debug.Log("既不是矩形,也不是梯形,返回 false。");
  1283. return false; // 不是矩形也不是梯形,返回 false
  1284. }
  1285. else
  1286. {
  1287. Debug.Log("是梯形。");
  1288. }
  1289. }
  1290. else
  1291. {
  1292. Debug.Log("是矩形。");
  1293. }
  1294. // 检查对角线是否相交
  1295. if (DoLinesIntersect(points[0], points[1], points[2], points[3]) ||
  1296. DoLinesIntersect(points[1], points[2], points[3], points[0]))
  1297. {
  1298. Debug.Log("对角线相交,返回 false。");
  1299. return false; // 对角线相交,返回 false
  1300. }
  1301. // 检查点的排列顺序,确保是顺时针或逆时针
  1302. if (!ArePointsClockwise(points) && !ArePointsClockwise(points.Reverse().ToArray()))
  1303. {
  1304. Debug.Log("点的排列顺序不正确,返回 false。");
  1305. return false; // 点的排列顺序不正确
  1306. }
  1307. Debug.Log("四边形有效,返回 true。");
  1308. return true; // 通过所有检查,返回 true
  1309. }
  1310. /// <summary>
  1311. /// 判断是否是矩形(近似90度角)
  1312. /// </summary>
  1313. private bool IsRectangle(Vector2[] points)
  1314. {
  1315. // 角度容差范围,允许夹角有小的偏差
  1316. const float dotProductThreshold = 0.1f;
  1317. for (int i = 0; i < points.Length; i++)
  1318. {
  1319. Vector2 a = points[i];
  1320. Vector2 b = points[(i + 1) % points.Length];
  1321. Vector2 c = points[(i + 2) % points.Length];
  1322. // 计算向量 AB 和 BC 的内积
  1323. Vector2 ab = b - a;
  1324. Vector2 bc = c - b;
  1325. float dotProduct = Vector2.Dot(ab.normalized, bc.normalized);
  1326. // 如果内积接近 0,表示夹角接近 90 度
  1327. if (Mathf.Abs(dotProduct) > dotProductThreshold)
  1328. {
  1329. Debug.Log($"点 {a}, {b}, {c} 的夹角不接近 90 度,返回 false。");
  1330. return false; // 角度不接近 90 度
  1331. }
  1332. }
  1333. return true;
  1334. }
  1335. /// <summary>
  1336. /// 判断是否是梯形
  1337. /// </summary>
  1338. private bool IsTrapezoid(Vector2[] points)
  1339. {
  1340. // 斜率容差范围,允许斜率差异较小
  1341. const float slopeThreshold = 0.2f;
  1342. // 计算对角线的斜率
  1343. float slope1 = (points[1].y - points[0].y) / (points[1].x - points[0].x);
  1344. float slope2 = (points[3].y - points[2].y) / (points[3].x - points[2].x);
  1345. // 如果对角线斜率差异小于容差范围,则认为是梯形
  1346. if (Mathf.Abs(slope1 - slope2) < slopeThreshold)
  1347. {
  1348. Debug.Log("对角线平行,判断为梯形。");
  1349. return true; // 对角线平行,返回 true
  1350. }
  1351. else
  1352. {
  1353. Debug.Log($"对角线斜率差异过大: {slope1} vs {slope2},不是梯形,返回 false。");
  1354. return false; // 斜率差异过大,不是梯形
  1355. }
  1356. }
  1357. //计算四边形面积
  1358. float CalculateArea(Vector2[] points)
  1359. {
  1360. float area = 0f;
  1361. int n = points.Length;
  1362. for (int i = 0; i < n; i++)
  1363. {
  1364. Vector2 current = points[i];
  1365. Vector2 next = points[(i + 1) % n];
  1366. area += current.x * next.y - current.y * next.x;
  1367. }
  1368. return Mathf.Abs(area) / 2f;
  1369. }
  1370. //确保点不共线
  1371. //判断点是否在同一条直线上(即共线),如果共线则无法形成四边形。可以通过计算任意三点的面积是否为 0 来验证是否共线:
  1372. bool ArePointsCollinear(Vector2 a, Vector2 b, Vector2 c)
  1373. {
  1374. float area = Mathf.Abs(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2.0f;
  1375. return Mathf.Approximately(area, 0f);
  1376. }
  1377. //确保边不相交
  1378. bool DoLinesIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
  1379. {
  1380. float cross(Vector2 p1, Vector2 p2, Vector2 p3)
  1381. {
  1382. return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
  1383. }
  1384. float d1 = cross(a, b, c);
  1385. float d2 = cross(a, b, d);
  1386. float d3 = cross(c, d, a);
  1387. float d4 = cross(c, d, b);
  1388. return d1 * d2 < 0 && d3 * d4 < 0;
  1389. }
  1390. //顺时针或逆时针排列顶点
  1391. bool ArePointsClockwise(Vector2[] points)
  1392. {
  1393. float sum = 0;
  1394. for (int i = 0; i < points.Length; i++)
  1395. {
  1396. Vector2 current = points[i];
  1397. Vector2 next = points[(i + 1) % points.Length];
  1398. sum += (next.x - current.x) * (next.y + current.y);
  1399. }
  1400. return sum < 0; // 小于0为顺时针,大于0为逆时针
  1401. }
  1402. #endregion
  1403. #region marker line 部分渲染
  1404. /// <summary>
  1405. /// maker 界面渲染
  1406. /// </summary>
  1407. void SyncQuadUnityVectorListToMarkerPointesPos()
  1408. {
  1409. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToMarkerPointesPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  1410. markerPosList[0].anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1411. markerPosList[1].anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1412. markerPosList[2].anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1413. markerPosList[3].anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1414. SetMarkerRectanglePoints(linePosConversion(markerPosList[0].localPosition, markerPosList[1].localPosition, markerPosList[2].localPosition, markerPosList[3].localPosition));
  1415. }
  1416. void SetMarkerRectanglePoints(List<Vector2> screenPositions)
  1417. {
  1418. markerPointsLine.SetLine(screenPositions);
  1419. }
  1420. #endregion
  1421. }