InfraredScreenPositioningView.cs 53 KB

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