InfraredScreenPositioningView.cs 58 KB

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