InfraredScreenPositioningView.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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. //上面应该清空了 ScreenLocate.Main.ScreenIdentification.QuadAuto
  492. onClearCurrentUILineGenerator();
  493. SetScreenPositioningStepState(ScreenPositioningStep.Start);
  494. }
  495. /// <summary>
  496. /// 进入环境光测试
  497. /// </summary>
  498. public void OnClick_EnterLightCtrl() {
  499. SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
  500. }
  501. /// <summary>
  502. /// 进入屏幕标记
  503. /// </summary>
  504. public void OnClick_EnterMarker() {
  505. SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  506. }
  507. /// <summary>
  508. /// successful 返回页面时候,根据ScreenPositioningStep区分
  509. /// </summary>
  510. public void OnClick_BackByScreenPositioningStep()
  511. {
  512. SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
  513. //清空两个绘制线
  514. onClearSuccessfullLineGenerator();
  515. //if(perStepView == ScreenPositioningStep.Manual)
  516. // SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  517. //else
  518. // SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  519. }
  520. /// <summary>
  521. /// 直接进入游戏流程
  522. /// </summary>
  523. public void OnClick_EnterGame()
  524. {
  525. //对比确认效果。直接进入游戏引导页面
  526. EnterGame();
  527. }
  528. /// <summary>
  529. /// 进入自动流程校准操作
  530. /// </summary>
  531. public void OnClick_EnterAuto()
  532. {
  533. Debug.LogError("OnClick_EnterAuto");
  534. //自动按钮时候进入之前的校准流程
  535. //重置偏移量
  536. infraredDemo.ResetCenterOffset();
  537. infraredDemo.ResetPositioningData();
  538. OnClick_Auto();
  539. }
  540. /// <summary>
  541. /// 自动识别
  542. /// </summary>
  543. public void OnClick_Auto()
  544. {
  545. bAuto = true;
  546. doLocateAuto = true;
  547. //textTip1.SetActive(false);
  548. //textTip2.SetActive(true);
  549. //ResetButton(btnAuto);
  550. if (enterFromZimWebCamera)
  551. {
  552. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  553. _screenLocate.EnterScreenLocateManualAuto();
  554. return;
  555. }
  556. if (InfraredDemo.running)
  557. {
  558. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  559. }
  560. }
  561. /// <summary>
  562. /// 进入手动界面
  563. /// </summary>
  564. public void OnClick_EnterManual()
  565. {
  566. SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  567. }
  568. /// <summary>
  569. /// 切换成手动方式
  570. /// </summary>
  571. public void onHandMovement()
  572. {
  573. bAuto = false;
  574. doLocateAuto = false;
  575. draggableParent.gameObject.SetActive(true);
  576. pointsParent.gameObject.SetActive(false);
  577. mask.SetActive(false);
  578. cameraLight.SetActive(true);
  579. }
  580. /// <summary>
  581. /// 自动识别后同步
  582. /// </summary>
  583. public void SyncScreenPosition()
  584. {
  585. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  586. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  587. SyncQuadUnityVectorListToPos();
  588. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  589. SyncQuadUnityVectorListToMarkerPointesPos();
  590. if (curStepView == ScreenPositioningStep.Start || curStepView == ScreenPositioningStep.AutoEnd) {
  591. //如果是开始页面进行自动定位的
  592. if(curStepView != ScreenPositioningStep.AutoEnd) SetScreenPositioningStepState(ScreenPositioningStep.AutoEnd);
  593. }
  594. else if (curStepView == ScreenPositioningStep.Marker || curStepView == ScreenPositioningStep.Manual) {
  595. //显示最后结果
  596. if(bMarkerFinish) SetScreenPositioningStepState(ScreenPositioningStep.Successful);
  597. }
  598. //屏幕变化情况下。同步修改对应的line
  599. //设置两个线段
  600. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  601. QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
  602. if (screenAuto != null)
  603. {
  604. Debug.Log("[校准流程]Successful自动识别screenAuto信息 ------------ ");
  605. CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  606. FirstUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  607. }
  608. else
  609. {
  610. Debug.LogError("screenAuto 不存在!");
  611. }
  612. if (screenSemiAuto != null)
  613. {
  614. Debug.Log("[校准流程]Successful半自动识别screenSemiAuto信息 ------------ ");
  615. SecondUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenSemiAuto, texSize);
  616. }
  617. else
  618. {
  619. Debug.LogError("screenSemiAuto 不存在!");
  620. }
  621. }
  622. /// <summary>
  623. /// 清空白线数据
  624. /// </summary>
  625. void onClearCurrentUILineGenerator() {
  626. if (CurrentUILineGenerator.Points.Length != 0) CurrentUILineGenerator.Points = new Vector2[0];//System.Array.Clear(CurrentUILineGenerator.Points, 0, CurrentUILineGenerator.Points.Length);
  627. }
  628. /// <summary>
  629. /// 清空最后识别的两个自动和半自动数据线
  630. /// </summary>
  631. void onClearSuccessfullLineGenerator()
  632. {
  633. if(FirstUILineGenerator.Points.Length !=0) FirstUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(FirstUILineGenerator.Points, 0, FirstUILineGenerator.Points.Length);
  634. if (SecondUILineGenerator.Points.Length !=0) SecondUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(SecondUILineGenerator.Points, 0, SecondUILineGenerator.Points.Length);
  635. }
  636. #region 绘制线段部分
  637. //点击拖拽的开始位置
  638. public void onBeginPos(int index, Vector3 pos)
  639. {
  640. Debug.Log("pos begin: " + pos);
  641. beginPos = pos;
  642. }
  643. public void onDragPos(int index, Vector3 pos)
  644. {
  645. //设置线段的点
  646. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  647. }
  648. //点击拖拽的结束位置
  649. public void onEndPos(int index, Vector3 pos)
  650. {
  651. Debug.Log("pos end: " + pos);
  652. endPos = pos;
  653. if (beginPos == endPos) return;
  654. //Debug.Log(index+",最后的点:" + pos);
  655. //再记录一次最后的点
  656. SetLinePos();
  657. }
  658. //同步设置图片对应的位置到line
  659. public void SetLinePos()
  660. {
  661. //记录一个操作点的操作位置
  662. AddOldLinePosition();
  663. //设置线段的点
  664. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  665. }
  666. void AddOldLinePosition()
  667. {
  668. Vector3[] v = new Vector3[4];
  669. pos1.GetWorldCorners(v);
  670. Vector3[] v1 = new Vector3[4];
  671. pos1.GetLocalCorners(v1);
  672. //for (int i = 0; i < 4; i++)
  673. //{
  674. // Debug.Log(i + " =1= " + v[i]);
  675. // Debug.Log(i + " =2= " + v1[i]);
  676. //}
  677. //记录一个操作点的操作位置
  678. List<Vector3> screenPositions = new List<Vector3>();
  679. screenPositions.Add(pos1.localPosition);
  680. screenPositions.Add(pos2.localPosition);
  681. screenPositions.Add(pos3.localPosition);
  682. screenPositions.Add(pos4.localPosition);
  683. LinePosition linePosition = new LinePosition();
  684. linePosition.index = oldLinePosition.Count;
  685. linePosition.pos = screenPositions;
  686. oldLinePosition.Add(linePosition);
  687. }
  688. /// <summary>
  689. /// 记录一个开始位置
  690. /// </summary>
  691. void SaveStartOldLinePosition() {
  692. oldLinePosition.Clear();
  693. //记录一个操作点的操作位置
  694. List<Vector3> screenPositions = new List<Vector3>();
  695. screenPositions.Add(pos1.localPosition);
  696. screenPositions.Add(pos2.localPosition);
  697. screenPositions.Add(pos3.localPosition);
  698. screenPositions.Add(pos4.localPosition);
  699. LinePosition linePosition = new LinePosition();
  700. linePosition.index = 1;
  701. linePosition.pos = screenPositions;
  702. oldLinePosition.Add(linePosition);
  703. }
  704. //转换绘制线段的点
  705. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  706. {
  707. List<Vector2> _screenPositions = new List<Vector2>();
  708. //点为负数的增大 offset,正数减少 offset
  709. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  710. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  711. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  712. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  713. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  714. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  715. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  716. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  717. _screenPositions.Add(startPos1);
  718. _screenPositions.Add(startPos2);
  719. _screenPositions.Add(startPos3);
  720. _screenPositions.Add(startPos4);
  721. return _screenPositions;
  722. }
  723. void SetRectanglePoints(List<Vector2> screenPositions)
  724. {
  725. line.SetLine(screenPositions);
  726. }
  727. //撤回上一个元素
  728. public void onRecall()
  729. {
  730. // 获取并删除最后一个元素,并且保留一个元素
  731. if (oldLinePosition.Count > 1) // 确保列表不为空
  732. {
  733. // 获取回退的那个元素点
  734. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  735. // 获取最后一个元素
  736. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  737. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  738. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  739. pos1.localPosition = lastElement_second.pos[0];
  740. pos2.localPosition = lastElement_second.pos[1];
  741. pos3.localPosition = lastElement_second.pos[2];
  742. pos4.localPosition = lastElement_second.pos[3];
  743. //设置线段的点
  744. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  745. }
  746. }
  747. //是不是从Demo界面进入该页面的
  748. public bool enterFromInfraredDemo { get; set; } = false;
  749. //是否从测试场景进入
  750. public bool enterFromZimWebCamera { get; set; } = false;
  751. //确认修改
  752. public void onConfirmation()
  753. {
  754. if (enterFromZimWebCamera)
  755. {
  756. ConfirmScreenLocateManualTest();
  757. if (oldLinePosition.Count > 1) // 确保列表不为空
  758. {
  759. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  760. oldLinePosition.Clear();
  761. oldLinePosition.Add(lastElement);
  762. }
  763. SaveLocalPos();
  764. AudioMgr.ins.PlayBtn();
  765. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  766. return;
  767. }
  768. if (!ConfirmScreenLocateManual()) return;
  769. if (oldLinePosition.Count > 1) // 确保列表不为空
  770. {
  771. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  772. oldLinePosition.Clear();
  773. //oldLinePosition.Add(lastElement);
  774. }
  775. if (InfraredDemo.running)
  776. {
  777. //跳转入界面
  778. AudioMgr.ins.PlayBtn();
  779. if (!enterFromInfraredDemo)
  780. {
  781. //每次初始化重置一下引导
  782. infraredDemo.resetInfraredPlayerPrefs();
  783. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  784. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  785. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  786. {
  787. //如果是红外连接成功,记录一个tag
  788. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  789. }
  790. onEnterInfrared();
  791. }
  792. else {
  793. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  794. }
  795. }
  796. //存储一次节点
  797. SaveLocalPos();
  798. }
  799. //重置位置
  800. public void onReset()
  801. {
  802. oldLinePosition.Clear();
  803. // 获取屏幕的四个角的像素坐标
  804. Vector2 bottomLeft = new Vector2(0, 0);
  805. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  806. Vector2 localBottomLeft;
  807. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  808. // 打印结果
  809. Debug.Log("Local Bottom Left: " + localBottomLeft);
  810. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  811. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  812. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  813. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  814. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  815. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  816. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  817. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  818. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  819. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  820. //设置一次位置
  821. SetLinePos();
  822. btnRecordInfrared.Reset();
  823. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  824. if (enterFromZimWebCamera)
  825. {
  826. ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
  827. ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
  828. }
  829. }
  830. /// <summary>
  831. /// 标准四个点
  832. /// </summary>
  833. /// <param name="index"></param>
  834. /// <param name="pos"></param>
  835. public void onManualNewPos(int index, Vector3 pos)
  836. {
  837. Debug.Log("pos end: " + pos);
  838. if (index == 0)
  839. pos1.localPosition = pos;
  840. else if (index == 1)
  841. pos2.localPosition = pos;
  842. else if (index == 2)
  843. pos3.localPosition = pos;
  844. else if (index == 3)
  845. pos4.localPosition = pos;
  846. //再记录一次最后的点
  847. SetLinePos();
  848. }
  849. /// <summary>
  850. /// 处理新流程,先记录手动,然后到自动识别处理
  851. /// </summary>
  852. public void onManualToAutomatic() {
  853. if (enterFromZimWebCamera)
  854. {
  855. ConfirmScreenLocateManualTest();
  856. if (oldLinePosition.Count > 1) // 确保列表不为空
  857. {
  858. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  859. oldLinePosition.Clear();
  860. oldLinePosition.Add(lastElement);
  861. }
  862. SaveLocalPos();
  863. //到自动
  864. OnClick_Auto();
  865. return;
  866. }
  867. if (!ConfirmScreenLocateManual()) {
  868. Debug.LogError("ConfirmScreenLocateManual is false!");
  869. return;
  870. }
  871. if (oldLinePosition.Count > 1) // 确保列表不为空
  872. {
  873. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  874. oldLinePosition.Clear();
  875. oldLinePosition.Add(lastElement);
  876. }
  877. //存储一次节点
  878. SaveLocalPos();
  879. Debug.Log("[校准流程]进入自动校准数据,调用EnterScreenLocateManualAuto");
  880. bAuto = true;
  881. doLocateAuto = true;
  882. //自动校准
  883. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  884. }
  885. /// <summary>
  886. /// PointMarker 调用
  887. /// </summary>
  888. public void onFinishManualToAutomatic() {
  889. bMarkerFinish = true;
  890. onManualToAutomatic();
  891. }
  892. #endregion
  893. List<Vector2> _locatePointList = new();
  894. float _texWidth;
  895. float _texHeight;
  896. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  897. {
  898. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  899. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  900. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  901. _locatePointList.Add(pos);
  902. }
  903. bool ConfirmScreenLocateManual()
  904. {
  905. if (InfraredDemo.running)
  906. {
  907. //渲染截图
  908. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  909. if (texture2D == null)
  910. {
  911. Debug.Log("EnterScreenLocateManual = null");
  912. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  913. return false;
  914. }
  915. _locatePointList.Clear();
  916. _texWidth = texture2D.width;
  917. _texHeight = texture2D.height;
  918. RecordLocatePoint(pos1, new Vector2(0, 0));
  919. RecordLocatePoint(pos2, new Vector2(1, 0));
  920. RecordLocatePoint(pos3, new Vector2(1, 1));
  921. RecordLocatePoint(pos4, new Vector2(0, 1));
  922. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  923. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  924. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  925. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  926. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  927. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  928. //同步数据
  929. ScreenLocate.quadUnityVectorList.Clear();
  930. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  931. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  932. //两个点切换,顺序不一样
  933. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  934. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  935. ScreenLocate.SaveScreenLocateVectorList();
  936. Debug.Log("[校准流程]设置QuitScreenLocateManual()手动数据,保存quadUnityVectorList");
  937. return true;
  938. }
  939. return false;
  940. }
  941. void ConfirmScreenLocateManualTest()
  942. {
  943. //渲染截图
  944. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  945. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  946. if (texture2D == null)
  947. {
  948. _screenLocate.QuitScreenLocateManual(null);
  949. return;
  950. }
  951. _locatePointList.Clear();
  952. _texWidth = texture2D.width;
  953. _texHeight = texture2D.height;
  954. RecordLocatePoint(pos1, new Vector2(0, 0));
  955. RecordLocatePoint(pos2, new Vector2(1, 0));
  956. RecordLocatePoint(pos3, new Vector2(1, 1));
  957. RecordLocatePoint(pos4, new Vector2(0, 1));
  958. _screenLocate.QuitScreenLocateManual(_locatePointList);
  959. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  960. setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
  961. if (!doLocateAuto) // 设置手动定位数据
  962. setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  963. //同步数据
  964. ScreenLocate.quadUnityVectorList.Clear();
  965. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  966. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  967. //两个点切换,顺序不一样
  968. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  969. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  970. ScreenLocate.SaveScreenLocateVectorList();
  971. }
  972. void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  973. {
  974. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  975. pointsTF2.SetActive(active);
  976. if (pointsTF2.transform.childCount == targetList.Count)
  977. {
  978. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  979. {
  980. Transform pointTF = pointsTF2.transform.GetChild(i);
  981. Vector2 pos = targetList[i];
  982. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  983. pointTF.gameObject.SetActive(true);
  984. }
  985. }
  986. }
  987. void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  988. {
  989. setPointsLocation(targetList, pointsTF2, active);
  990. var lo = new Vector2(-0.5f, -0.5f);
  991. ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
  992. 2 * (targetList[0] + lo),
  993. 2 * (targetList[1] + lo),
  994. 2 * (targetList[2] + lo),
  995. 2 * (targetList[3] + lo) };
  996. }
  997. void SaveLocalPos()
  998. {
  999. List<Vector3> screenPositions = new List<Vector3>();
  1000. screenPositions.Add(pos1.anchoredPosition);
  1001. screenPositions.Add(pos2.anchoredPosition);
  1002. screenPositions.Add(pos3.anchoredPosition);
  1003. screenPositions.Add(pos4.anchoredPosition);
  1004. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  1005. Debug.Log("Local UI Position: " + saveStr);
  1006. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  1007. }
  1008. void GetLocalPos()
  1009. {
  1010. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  1011. if (!string.IsNullOrWhiteSpace(posListStr))
  1012. {
  1013. List<Vector2> posList = posListStr.Split(';')
  1014. .Select(s =>
  1015. {
  1016. string[] parts = s.Split(',');
  1017. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  1018. })
  1019. .ToList();
  1020. pos1.anchoredPosition = posList[0];
  1021. pos2.anchoredPosition = posList[1];
  1022. pos3.anchoredPosition = posList[2];
  1023. pos4.anchoredPosition = posList[3];
  1024. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  1025. }
  1026. }
  1027. #region 按钮颜色切换
  1028. private void OnButtonClick(Button button)
  1029. {
  1030. // 切换按钮颜色
  1031. ColorBlock colors = button.colors;
  1032. colors.normalColor = highlightedColor;
  1033. colors.highlightedColor = highlightedColor;
  1034. button.colors = colors;
  1035. // 切换字体颜色
  1036. Text buttonText = button.GetComponentInChildren<Text>();
  1037. buttonText.color = highlightedTextColor;
  1038. }
  1039. private void ResetButton(Button button)
  1040. {
  1041. // 重置按钮颜色
  1042. ColorBlock colors = button.colors;
  1043. colors.normalColor = normalColor;
  1044. colors.highlightedColor = normalColor;
  1045. button.colors = colors;
  1046. // 重置字体颜色
  1047. Text buttonText = button.GetComponentInChildren<Text>();
  1048. buttonText.color = normalTextColor;
  1049. }
  1050. #endregion
  1051. #region 标定完成之后进入游戏界面
  1052. void onEnterInfrared()
  1053. {
  1054. //添加进入射箭场景
  1055. if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
  1056. {
  1057. Debug.Log("-----进入射箭场景!");
  1058. NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
  1059. newUserGuiderManager.curConfigKey = "开始-红外调整";
  1060. //b端都是红外设备流程
  1061. if (CommonConfig.StandaloneModeOrPlatformB)
  1062. {
  1063. newUserGuiderManager.isNewModule = true;
  1064. }
  1065. else {
  1066. newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
  1067. }
  1068. //进入射箭场景
  1069. GlobalData.pkMatchType = PKMatchType.None;
  1070. GameMgr.gameType = 1;
  1071. //射一箭回到连接页面,Device.view
  1072. GameMgr.bNavBack = true;
  1073. GameMgr.bShowDistance = false;
  1074. if (!CommonConfig.StandaloneModeOrPlatformB)
  1075. {
  1076. AimHandler.ins.bInitOne = false;//true
  1077. }
  1078. //关闭计时器
  1079. GameMgr.turnOffTimer = true;
  1080. //关闭左边靶子和底部速度栏
  1081. GameMgr.HideTargetView = true;
  1082. GameMgr.HideBillboard = true;
  1083. GameMgr.ButtonCount = 0;
  1084. UnityEngine.SceneManagement.SceneManager.LoadScene(
  1085. "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
  1086. PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
  1087. }
  1088. else
  1089. {
  1090. Debug.Log("-----跳转回连接界面!");
  1091. //如不是第一次,则应该跳转回连接界面
  1092. ViewMgr.Instance.DestroyView<SmartArcheryView>();
  1093. ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  1094. }
  1095. }
  1096. #endregion
  1097. #region 最后选择算法处理UI
  1098. int selected = -1;
  1099. void initSelectInfo() {
  1100. for (int i = 0; i < 2; i++)
  1101. {
  1102. int currentIndex = i; // 将 i 赋值给局部变量
  1103. ResultImagesButtons[currentIndex].onClick.AddListener(() => SelectImage(currentIndex));
  1104. //ResultButtons[currentIndex].onClick.AddListener(() =>
  1105. //{
  1106. // if (currentIndex == 0)
  1107. // {
  1108. // OnFirstResult();
  1109. // }
  1110. // else if (currentIndex == 1)
  1111. // {
  1112. // OnSecondResult();
  1113. // }
  1114. //});
  1115. }
  1116. //默认选择1 自动
  1117. SelectImage(0);
  1118. }
  1119. // 添加一个选择函数
  1120. void SelectImage(int selectedIndex)
  1121. {
  1122. //如果重复选中则跳转
  1123. if (selected == selectedIndex) {
  1124. if (selected == 0)
  1125. {
  1126. OnFirstResult();
  1127. }
  1128. else if (selected == 1)
  1129. {
  1130. OnSecondResult();
  1131. }
  1132. return;
  1133. }
  1134. // 遍历所有图片
  1135. selected = selectedIndex;
  1136. //for (int i = 0; i < ResultImages.Length; i++)
  1137. //{
  1138. // // 如果是选中的 index,赋值为 ResultLines[0],否则赋值为 ResultLines[1]
  1139. // if (i == selectedIndex)
  1140. // {
  1141. // selected = i;
  1142. // ResultImages[i].sprite = ResultLines[0];
  1143. // ResultButtons[i].interactable = true;
  1144. // }
  1145. // else
  1146. // {
  1147. // ResultImages[i].sprite = ResultLines[1];
  1148. // ResultButtons[i].interactable = false;
  1149. // }
  1150. //}
  1151. }
  1152. //选择全自动结果后进入游戏
  1153. void OnFirstResult()
  1154. {
  1155. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
  1156. UpdateQuadUnityVectorList();
  1157. onCompelete();
  1158. }
  1159. //选择半自动结果
  1160. void OnSecondResult()
  1161. {
  1162. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
  1163. UpdateQuadUnityVectorList();
  1164. onCompelete();
  1165. }
  1166. public void onCompelete()
  1167. {
  1168. if (enterFromZimWebCamera)
  1169. {
  1170. SaveLocalPos();
  1171. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1172. return;
  1173. }
  1174. EnterGame();
  1175. //存储一次节点
  1176. SaveLocalPos();
  1177. }
  1178. void EnterGame() {
  1179. if (InfraredDemo.running)
  1180. {
  1181. //跳转入界面
  1182. AudioMgr.ins.PlayBtn();
  1183. if (!enterFromInfraredDemo)
  1184. {
  1185. //每次初始化重置一下引导
  1186. infraredDemo.resetInfraredPlayerPrefs();
  1187. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  1188. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  1189. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  1190. {
  1191. //如果是红外连接成功,记录一个tag
  1192. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  1193. }
  1194. onEnterInfrared();
  1195. }
  1196. else
  1197. {
  1198. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1199. }
  1200. }
  1201. }
  1202. /// <summary>
  1203. /// 选择模式后更新 quadUnityVectorList
  1204. /// </summary>
  1205. void UpdateQuadUnityVectorList() {
  1206. ScreenLocate.Main.UpdateQuadUnityVectorList();
  1207. SyncQuadUnityVectorListToPos();
  1208. infraredDemo?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1209. _locatePointList.Clear();
  1210. }
  1211. void SyncQuadUnityVectorListToPos() {
  1212. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  1213. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1214. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1215. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1216. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1217. }
  1218. //这个脚本存在时候。任何切换操作都直接处理删除
  1219. //void OnSceneUnloaded(Scene scene)
  1220. //{
  1221. // ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  1222. // ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  1223. //}
  1224. //void OnEnable()
  1225. //{
  1226. // SceneManager.sceneUnloaded += OnSceneUnloaded;
  1227. //}
  1228. //void OnDisable()
  1229. //{
  1230. // SceneManager.sceneUnloaded -= OnSceneUnloaded;
  1231. //}
  1232. #endregion
  1233. #region 判断点是否在 quad 内部
  1234. //判断maskline
  1235. public bool IsPointInMaskLine(Vector2 point) {
  1236. return IsPointInQuad(point, maskLine.ScreenPositions);
  1237. }
  1238. //实现基于射线法,通过数交点的奇偶性来判断点是否在多边形内
  1239. bool IsPointInQuad(Vector2 point, List<Vector2> quadVertices)
  1240. {
  1241. if (quadVertices.Count != 4)
  1242. {
  1243. Debug.LogError("Quad must have exactly 4 vertices.");
  1244. return false;
  1245. }
  1246. bool isInside = false;
  1247. int vertexCount = quadVertices.Count;
  1248. for (int i = 0, j = vertexCount - 1; i < vertexCount; j = i++)
  1249. {
  1250. Vector2 vi = quadVertices[i];
  1251. Vector2 vj = quadVertices[j];
  1252. // 检查射线是否与边相交
  1253. if (((vi.y > point.y) != (vj.y > point.y)) &&
  1254. (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x))
  1255. {
  1256. isInside = !isInside;
  1257. }
  1258. }
  1259. return isInside;
  1260. }
  1261. /// <summary>
  1262. /// 判断四个点是否能够形成有效的四边形,并根据面积阈值过滤小四边形
  1263. /// </summary>
  1264. /// <param name="points">四个点</param>
  1265. /// <param name="minArea">面积阈值,用于过滤面积太小的四边形</param>
  1266. /// <returns>是否是有效的四边形</returns>
  1267. public bool IsValidQuadrilateral(Vector2[] points, float minArea = 100000.0f)
  1268. {
  1269. if (points.Length != 4)
  1270. {
  1271. Debug.Log("四个点数不足,无法形成四边形。");
  1272. return false; // 如果点数不是4,直接返回false
  1273. }
  1274. // 计算四边形的面积
  1275. float area = CalculateArea(points);
  1276. Debug.Log($"四边形面积: {area}");
  1277. if (area < minArea)
  1278. {
  1279. Debug.Log($"面积小于阈值: {minArea},返回 false。");
  1280. return false; // 面积太小,返回 false
  1281. }
  1282. // 检查是否有三点共线
  1283. for (int i = 0; i < points.Length; i++)
  1284. {
  1285. Vector2 a = points[i];
  1286. Vector2 b = points[(i + 1) % points.Length];
  1287. Vector2 c = points[(i + 2) % points.Length];
  1288. if (ArePointsCollinear(a, b, c))
  1289. {
  1290. Debug.Log($"点 {a}, {b}, {c} 共线,返回 false。");
  1291. return false; // 存在共线点,返回 false
  1292. }
  1293. }
  1294. // 检查是否为矩形或接近矩形
  1295. if (!IsRectangle(points))
  1296. {
  1297. // 如果不是矩形,检查是否是梯形
  1298. if (!IsTrapezoid(points))
  1299. {
  1300. Debug.Log("既不是矩形,也不是梯形,返回 false。");
  1301. return false; // 不是矩形也不是梯形,返回 false
  1302. }
  1303. else
  1304. {
  1305. Debug.Log("是梯形。");
  1306. }
  1307. }
  1308. else
  1309. {
  1310. Debug.Log("是矩形。");
  1311. }
  1312. // 检查对角线是否相交
  1313. if (DoLinesIntersect(points[0], points[1], points[2], points[3]) ||
  1314. DoLinesIntersect(points[1], points[2], points[3], points[0]))
  1315. {
  1316. Debug.Log("对角线相交,返回 false。");
  1317. return false; // 对角线相交,返回 false
  1318. }
  1319. // 检查点的排列顺序,确保是顺时针或逆时针
  1320. if (!ArePointsClockwise(points) && !ArePointsClockwise(points.Reverse().ToArray()))
  1321. {
  1322. Debug.Log("点的排列顺序不正确,返回 false。");
  1323. return false; // 点的排列顺序不正确
  1324. }
  1325. Debug.Log("四边形有效,返回 true。");
  1326. return true; // 通过所有检查,返回 true
  1327. }
  1328. /// <summary>
  1329. /// 判断是否是矩形(近似90度角)
  1330. /// </summary>
  1331. private bool IsRectangle(Vector2[] points)
  1332. {
  1333. // 角度容差范围,允许夹角有小的偏差
  1334. const float dotProductThreshold = 0.1f;
  1335. for (int i = 0; i < points.Length; i++)
  1336. {
  1337. Vector2 a = points[i];
  1338. Vector2 b = points[(i + 1) % points.Length];
  1339. Vector2 c = points[(i + 2) % points.Length];
  1340. // 计算向量 AB 和 BC 的内积
  1341. Vector2 ab = b - a;
  1342. Vector2 bc = c - b;
  1343. float dotProduct = Vector2.Dot(ab.normalized, bc.normalized);
  1344. // 如果内积接近 0,表示夹角接近 90 度
  1345. if (Mathf.Abs(dotProduct) > dotProductThreshold)
  1346. {
  1347. Debug.Log($"点 {a}, {b}, {c} 的夹角不接近 90 度,返回 false。");
  1348. return false; // 角度不接近 90 度
  1349. }
  1350. }
  1351. return true;
  1352. }
  1353. /// <summary>
  1354. /// 判断是否是梯形
  1355. /// </summary>
  1356. private bool IsTrapezoid(Vector2[] points)
  1357. {
  1358. // 斜率容差范围,允许斜率差异较小
  1359. const float slopeThreshold = 0.2f;
  1360. // 计算对角线的斜率
  1361. float slope1 = (points[1].y - points[0].y) / (points[1].x - points[0].x);
  1362. float slope2 = (points[3].y - points[2].y) / (points[3].x - points[2].x);
  1363. // 如果对角线斜率差异小于容差范围,则认为是梯形
  1364. if (Mathf.Abs(slope1 - slope2) < slopeThreshold)
  1365. {
  1366. Debug.Log("对角线平行,判断为梯形。");
  1367. return true; // 对角线平行,返回 true
  1368. }
  1369. else
  1370. {
  1371. Debug.Log($"对角线斜率差异过大: {slope1} vs {slope2},不是梯形,返回 false。");
  1372. return false; // 斜率差异过大,不是梯形
  1373. }
  1374. }
  1375. //计算四边形面积
  1376. float CalculateArea(Vector2[] points)
  1377. {
  1378. float area = 0f;
  1379. int n = points.Length;
  1380. for (int i = 0; i < n; i++)
  1381. {
  1382. Vector2 current = points[i];
  1383. Vector2 next = points[(i + 1) % n];
  1384. area += current.x * next.y - current.y * next.x;
  1385. }
  1386. return Mathf.Abs(area) / 2f;
  1387. }
  1388. //确保点不共线
  1389. //判断点是否在同一条直线上(即共线),如果共线则无法形成四边形。可以通过计算任意三点的面积是否为 0 来验证是否共线:
  1390. bool ArePointsCollinear(Vector2 a, Vector2 b, Vector2 c)
  1391. {
  1392. float area = Mathf.Abs(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2.0f;
  1393. return Mathf.Approximately(area, 0f);
  1394. }
  1395. //确保边不相交
  1396. bool DoLinesIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
  1397. {
  1398. float cross(Vector2 p1, Vector2 p2, Vector2 p3)
  1399. {
  1400. return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
  1401. }
  1402. float d1 = cross(a, b, c);
  1403. float d2 = cross(a, b, d);
  1404. float d3 = cross(c, d, a);
  1405. float d4 = cross(c, d, b);
  1406. return d1 * d2 < 0 && d3 * d4 < 0;
  1407. }
  1408. //顺时针或逆时针排列顶点
  1409. bool ArePointsClockwise(Vector2[] points)
  1410. {
  1411. float sum = 0;
  1412. for (int i = 0; i < points.Length; i++)
  1413. {
  1414. Vector2 current = points[i];
  1415. Vector2 next = points[(i + 1) % points.Length];
  1416. sum += (next.x - current.x) * (next.y + current.y);
  1417. }
  1418. return sum < 0; // 小于0为顺时针,大于0为逆时针
  1419. }
  1420. #endregion
  1421. #region marker line 部分渲染
  1422. /// <summary>
  1423. /// maker 界面渲染
  1424. /// </summary>
  1425. void SyncQuadUnityVectorListToMarkerPointesPos()
  1426. {
  1427. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToMarkerPointesPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  1428. markerPosList[0].anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1429. markerPosList[1].anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1430. markerPosList[2].anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1431. markerPosList[3].anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  1432. SetMarkerRectanglePoints(linePosConversion(markerPosList[0].localPosition, markerPosList[1].localPosition, markerPosList[2].localPosition, markerPosList[3].localPosition));
  1433. }
  1434. void SetMarkerRectanglePoints(List<Vector2> screenPositions)
  1435. {
  1436. markerPointsLine.SetLine(screenPositions);
  1437. }
  1438. #endregion
  1439. }