| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LineUI;
- using UnityEngine.UI;
- using System.Linq;
- using ZIM;
- using o0;
- using Color = UnityEngine.Color;
- using InfraredManager;
- using Org.BouncyCastle.Asn1.Crmf;
- using ZIM.Unity;
- using o0.Project;
- using UnityEngine.SceneManagement;
- public enum ScreenPositioningStep {
- None, //
- Start,//开始界面
- AutoEnd,//自动定位结束后界面
- Marker,//进行标记界面
- Manual,//手动界面
- LightCtrl,//灯光调整界面
- Successful,//结果选择界面
- }
- public class LinePosition
- {
- public int index;
- public List<Vector3> pos;
- }
- public class InfraredScreenPositioningView : JCUnityLib.ViewBase
- {
- [SerializeField]
- RectTransform canvasRectTransform;
- [SerializeField]
- RectTransform draggableParent;
- [SerializeField]
- GameObject cameraLight;
- [SerializeField]
- RectTransform pointsParent;
- [SerializeField]
- RectTransform pos1;
- [SerializeField]
- RectTransform pos2;
- [SerializeField]
- RectTransform pos3;
- [SerializeField]
- RectTransform pos4;
- //画线时候的点偏移量
- float offset = 10;
- [SerializeField]
- Line line;
- List<LinePosition> oldLinePosition;
- Vector3 beginPos;
- Vector3 endPos;
- //相机感光部分
- [SerializeField]
- Slider slider;
- [SerializeField]
- GameObject textTip1;
- [SerializeField]
- GameObject textTip2;
- [SerializeField]
- Button btnAuto;
- [SerializeField]
- Button btnHandMovement;
- [SerializeField]
- BtnRecordInfrared btnRecordInfrared;
- bool bAuto = true;
- [SerializeField] Color normalColor = Color.white;
- [SerializeField] Color highlightedColor = Color.green;
- [SerializeField] Color normalTextColor = Color.black;
- [SerializeField] Color highlightedTextColor = Color.white;
- [Header("Common Layout Group")]
- [SerializeField]
- GameObject mask;
- [SerializeField]
- Line maskLine;
- [SerializeField]
- RawImage rawImage;
- [Tooltip("Demo测试时候显示")]
- [SerializeField]
- RectTransform crosshair;
- [SerializeField]
- RectTransform crosshairRed;
- [Tooltip("存在校准数据时候显示")]
- [SerializeField] LineGenerator CurrentUILineGenerator;//开始显示屏幕线条
- [Header("Start Layout Group")]
- [SerializeField]
- GameObject LayoutStart;
- [SerializeField]
- GameObject StartTextTip1;
- [SerializeField]
- GameObject StartTextTipHasData;
- [Tooltip("没有存在校准数据时候显示")]
- [SerializeField]
- GameObject BottomAutoBtn;
- [Tooltip("存在校准数据时候显示")]
- [SerializeField]
- GameObject BottomConfirmBtn;
- [Header("AutoEnd Layout Group")]
- [SerializeField]
- GameObject LayoutAutoEnd;
- [SerializeField] Button AutoEndConfirmBtn;
- [Header("LightCtrl Layout Group")]
- [SerializeField]
- GameObject LayoutLightCtrl;
- [SerializeField] Slider sliderContrast;
- //亮度
- [SerializeField] Slider sliderBrightness;
- [Header("Marker Layout Group")]
- [SerializeField]
- GameObject LayoutMarker;
- [SerializeField]
- RectTransform crosshairSmall;
- [Tooltip("标记页面标题提示")]
- [SerializeField] TextAutoLanguage2 markerTextAutoLanguage2;
- [SerializeField]
- RectTransform markerPointsParent;
- [SerializeField]
- List<RectTransform> markerPosList;
- [SerializeField]
- Line markerPointsLine;
- [SerializeField]
- PointMarker pointMarker;
- [Header("Manual Layout Group")]
- [SerializeField]
- GameObject LayoutManual;
- [Header("Successful Layout Group")]
- [SerializeField]
- GameObject LayoutSuccessful;
- [Tooltip("选择框的Line")]
- [SerializeField]
- Sprite[] ResultLines;// 0: 选中状态, 1: 未选中状态
- [Tooltip("选择框图片")]
- [SerializeField]
- Image[] ResultImages;
- [Tooltip("选择框按钮")]
- [SerializeField]
- Button[] ResultImagesButtons;
- [SerializeField]
- Button[] ResultButtons; // 结果选择按钮
- [SerializeField]
- RawImage CameraFirstImage; //第一个结果屏幕
- [SerializeField]
- RawImage CameraSecondImage; //第二个结果屏幕
- [SerializeField] LineGenerator FirstUILineGenerator;//第一个结果屏幕线条
- [SerializeField] LineGenerator SecondUILineGenerator;//第二个结果屏幕线条
- bool bMarkerFinish = false;
- ScreenPositioningStep perStepView = ScreenPositioningStep.None;//最后一次进入LayoutSuccessful的页面
- //标记当前页面情况
- ScreenPositioningStep curStepView = ScreenPositioningStep.Start;//设置一个开始页面
- InfraredDemo infraredDemo;
- bool doLocateAuto;
- int DefaultResolutionIndex;
- private void Awake()
- {
- //设置一次显示相机的image 的颜色
- rawImage.color = Color.white;
- offset = line.MyThickness;
- }
- void Start()
- {
- //获取InfraredDemo
- infraredDemo = InfraredDemo._ins;
- doLocateAuto = false;
- textTip1.SetActive(true);
- //设置btnAuto 高亮
- //OnButtonClick(btnAuto);
- textTip2.SetActive(false);
- //动态设置marker的提示语
- //if (BluetoothAim.ins!=null && markerTextAutoLanguage2 != null)
- //{
- // string name = "";
- // bool switchValue = false;
- // if (BluetoothAim.ins.isMainConnectToHOUYIPRO())
- // {
- // name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-HOUYIPro");
- // }
- // else if (BluetoothAim.ins.isMainConnectToARTEMISPRO())
- // {
- // name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-ArtemisPro");
- // }
- // else if (BluetoothAim.ins.isMainConnectToGun())
- // {
- // //name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-M9");
- // switchValue = true;
- // }
- // if (switchValue)
- // {
- // markerTextAutoLanguage2.SetTextKey("TitleTipMarker-M9");
- // }
- // else {
- // markerTextAutoLanguage2.textFormatArgs = new object[] { name };
- // }
- //}
- //手动不高亮
- //ResetButton(btnHandMovement);
- //quadUnityVectorList.Clear();
- //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
- //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
- //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
- //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
- Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
- if (ScreenLocate.quadUnityVectorList.Count == 0)
- {
- Debug.Log("GetLocalPos");
- GetLocalPos();
- }
- else
- {
- Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
- Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
- //自动识别时候四个点
- SyncQuadUnityVectorListToPos();
- //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- }
- //更新start相关ui
- UpdateStartUI();
- //记录操作的位置信息
- oldLinePosition = new List<LinePosition>();
- SetLinePos();
- //相机感光度
- if (infraredDemo)
- {
- ////重置偏移量
- //infraredDemo.ResetCenterOffset();
- //////重置识别点
- ////ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
- //////清除一下记录的点
- ////ScreenLocate.quadUnityVectorList.Clear();
- //infraredDemo.ResetPositioningData();
- slider.onValueChanged.AddListener((value) =>
- {
- infraredDemo.onSliderEvent(value);
- });
- infraredDemo.onSetSliderValue(slider);
- //对比度
- sliderContrast.onValueChanged.AddListener((value) =>
- {
- infraredDemo.onSliderCustomEvent(value, -20.0f, 20.0f);
- });
- infraredDemo.onSetSliderCustomValue(sliderContrast, -20.0f, 20.0f);
- //亮度
- sliderBrightness.onValueChanged.AddListener((value) =>
- {
- infraredDemo.onSliderCustomBrightnessEvent(value, -20.0f, 20.0f);
- });
- infraredDemo.onSetSliderCustomBrightnessValue(sliderBrightness, -20.0f, 20.0f);
- offset = line.MyThickness = markerPointsLine.MyThickness = infraredDemo.lineWidth.Get();
- //LineGenerator 比 line 宽少了大概2倍
- CurrentUILineGenerator.LineThickness = infraredDemo.lineWidth.Get() * 2;
- //扇形宽
- markerPointsLine.MyFanWidth = line.MyFanWidth = infraredDemo.fanWidth.Get();
- ZIM.Unity.QuadrilateralInCamera screen = ScreenLocate.Main.ScreenIdentification.Screen.QuadInCamera;
- CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screen, ScreenLocate.Main.getUVCCameraInfoSize);
- }
- else {
- //编辑器使用
- if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
- slider.onValueChanged.AddListener((value) =>
- {
- double originalMin = 0.0;
- double originalMax = 10.0;
- double targetMin = -1;
- double targetMax = 1;
- // 计算转换后的值
- double result = InfraredDemo.MapValue(value, originalMin, originalMax, targetMin, targetMax);
- //int _current = (int)(result);
- Debug.Log("_current:" + value + " , result:" + result);
- ScreenLocate.Main.pcContrast = (float)result;
- });
- float v = ScreenLocate.Main.pcContrast;
- // 目标区间 [0, 10] 的边界值
- double targetMin = 0.0;
- double targetMax = 10.0;
- double originalMin = -1;
- double originalMax = 1;
- // 计算转换后的值
- double v2 = InfraredDemo.MapValue(v, originalMin, originalMax, targetMin, targetMax);
- Debug.Log("PC获取相机的感光度:" + v + " = " + v2);
- slider.SetValueWithoutNotify((float)v2);
- // 对 sliderContrast 和 sliderBrightness 调用统一的处理函数
- HandleSliderValueChanged(sliderContrast, value => ScreenLocate.Main.pcContrast = value, ScreenLocate.Main.pcContrast);
- HandleSliderValueChanged(sliderBrightness, value => ScreenLocate.Main.pcBrightness = value, ScreenLocate.Main.pcBrightness);
- }
- }
- initSelectInfo();
- //if (ScreenLocate.Main.getUVCTexture)
- //{
- // //渲染相机画面
- // rawImage.texture = ScreenLocate.Main.getUVCTexture;
- // //Debug.Log("rawImage.texture:" + rawImage.texture.name);
- //}
- //slider.onValueChanged.AddListener((value) => {
- // //onSliderEvent(value);
- // infraredDemo.SetBrightness(value);
- //});
- //slider.value = infraredDemo.brightness.Get();
- //修改分辨率。是否清晰一点?
- //DefaultResolutionIndex = infraredDemo?.ResolutionIndex ?? 0;
- //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
- //infraredDemo?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
- //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
- //{
- // SB_EventSystem.ins.AwakenSimulateMouse();
- // // Debug.Log("simulate-mouse-close");
- //}
-
- }
- // 两个滑块值处理
- void HandleSliderValueChanged(UnityEngine.UI.Slider slider, System.Action<float> setValue, float pcValue)
- {
- double targetMin2 = -20.0;
- double targetMax2 = 20.0;
- double originalMin2 = -1;
- double originalMax2 = 1;
- slider.onValueChanged.AddListener((value) =>
- {
- double result = InfraredDemo.MapValue(value, targetMin2, targetMax2, originalMin2, originalMax2);
- Debug.Log($"{slider.name}_current: {value}, result: {result}");
- setValue((float)result);
- });
- double mappedValue = InfraredDemo.MapValue(pcValue, originalMin2, originalMax2, targetMin2, targetMax2);
- Debug.Log($"{slider.name} PC获取相机的感光度: {pcValue} = {mappedValue}");
- slider.SetValueWithoutNotify((float)mappedValue);
- }
- private void OnDestroy()
- {
- //修改回进入手动调节页面时候的分辨率
- //infraredDemo?.SetResolutionNew(DefaultResolutionIndex);
- Debug.Log("OnDestroy*********************************AwakenSimulateMouse:" + SB_EventSystem.ins.simulateMouseIsAwaked);
- if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked)
- {
- SB_EventSystem.ins.AwakenSimulateMouse();
- }
- }
- public RawImage Bg => rawImage;
- void Update()
- {
- if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnZIMDemo) {
- // ZimWebCamera场景测试
- rawImage.texture = ScreenLocate.Main.outputRawImages[7].texture;
- if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
- {
- // 检测到光点
- var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
- crosshair.gameObject.SetActive(true);
- crosshair.anchoredPosition = posInCanvas;
- }
- else
- crosshair.gameObject.SetActive(false);
- //渲染相机画面
- CameraSecondImage.texture = rawImage.texture;
- CameraFirstImage.texture = rawImage.texture;
- }
- else if (InfraredDemo.running)
- {
- //渲染相机画面
- Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
- if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
- {
- rawImage.texture = texture;
- //渲染相机画面
- CameraSecondImage.texture = rawImage.texture;
- CameraFirstImage.texture = rawImage.texture;
- }
- if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
- {
- // 检测到光点
- var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
- crosshairSmall.gameObject.SetActive(true);
- crosshairSmall.anchoredPosition = posInCanvas;
- }
- else
- crosshairSmall.gameObject.SetActive(false);
- //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
- //可见光调节页面出现红十字光标
- if (curStepView == ScreenPositioningStep.LightCtrl)
- {
- if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
- {
- // 检测到光点
- var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
- crosshairRed.gameObject.SetActive(true);
- crosshairRed.anchoredPosition = posInCanvas;
- }
- else
- crosshairRed.gameObject.SetActive(false);
- }
- else if (crosshairRed.gameObject.activeSelf) {
- crosshairRed.gameObject.SetActive(false);
- }
- }
- }
- /// <summary>
- /// 根据enum操作ui
- /// </summary>
- /// <param name="step"></param>
- public void SetScreenPositioningStepState(ScreenPositioningStep step = ScreenPositioningStep.None)
- {
- curStepView = step;
- AllScreenPositioningStepFalse();
- switch (step)
- {
- case ScreenPositioningStep.Start:
- maskLine.SetDrawMask(true);
- LayoutStart.SetActive(true);
- UpdateStartUI();
- break;
- case ScreenPositioningStep.AutoEnd:
- maskLine.SetDrawMask(true);
- LayoutAutoEnd.SetActive(true);
- pointsParent.gameObject.SetActive(false);
- CurrentUILineGenerator.enabled = true;
- QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
- if (screenAuto == null)
- {
- AutoEndConfirmBtn.interactable = false;
- }
- else
- {
- //存在数据,显示确认按钮
- AutoEndConfirmBtn.interactable = true;
- }
- break;
- case ScreenPositioningStep.LightCtrl:
- maskLine.SetDrawMask(false);
- LayoutLightCtrl.SetActive(true);
- CurrentUILineGenerator.enabled = true;
- pointsParent.gameObject.SetActive(false);
- //setPointsLocation(ScreenLocate.quadUnityVectorList, pointsParent.gameObject, true);
- break;
- case ScreenPositioningStep.Marker:
- //显示提示
- pointMarker.ShowHintImageParent(true);
- perStepView = ScreenPositioningStep.Marker;
- bMarkerFinish = false;
- maskLine.SetDrawMask(true);
- LayoutMarker.SetActive(true);
- CurrentUILineGenerator.enabled = false;
- pointsParent.gameObject.SetActive(false);
- SyncQuadUnityVectorListToMarkerPointesPos();
- //setPointsLocation(ScreenLocate.quadUnityVectorList, pointsParent.gameObject, true);
- break;
- case ScreenPositioningStep.Manual:
- SaveStartOldLinePosition();
- perStepView = ScreenPositioningStep.Manual;
- bMarkerFinish = false;
- LayoutManual.SetActive(true);
- bAuto = false;
- doLocateAuto = false;
- draggableParent.gameObject.SetActive(true);
- pointsParent.gameObject.SetActive(false);
- break;
-
- case ScreenPositioningStep.Successful:
- LayoutSuccessful.SetActive(true);
- pointsParent.gameObject.SetActive(false);
- CurrentUILineGenerator.enabled = false;
- break;
- }
- }
-
- void AllScreenPositioningStepFalse()
- {
- LayoutStart.SetActive(false);
- LayoutMarker.SetActive(false);
- LayoutManual.SetActive(false);
- LayoutAutoEnd.SetActive(false);
- LayoutLightCtrl.SetActive(false);
- LayoutSuccessful.SetActive(false);
- }
- /// <summary>
- /// 开始页面时候ui
- /// </summary>
- void UpdateStartUI() {
- pointsParent.gameObject.SetActive(false);
- CurrentUILineGenerator.enabled = true;
- if (ScreenLocate.quadUnityVectorList.Count == 0)
- {
- BottomConfirmBtn.SetActive(false);
- StartTextTip1.SetActive(true);
- StartTextTipHasData.SetActive(false);
- }
- else
- {
- //存在数据,显示确认按钮,确认按钮直接跳转到游戏
- BottomConfirmBtn.SetActive(true);
- StartTextTip1.SetActive(false);
- StartTextTipHasData.SetActive(true);
- }
- }
- public void OnClick_Back()
- {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
- }
- /// <summary>
- /// 返回最开始页面
- /// </summary>
- public void OnClick_BackLayoutStart()
- {
- ClearPointsData();
- SetScreenPositioningStepState(ScreenPositioningStep.Start);
- }
- void ClearPointsData() {
- //重置中心点0
- infraredDemo.ResetCenterOffset();
- //清除白线数据
- infraredDemo.ResetPositioningData();
- //上面应该清空了 ScreenLocate.Main.ScreenIdentification.QuadAuto
- onClearCurrentUILineGenerator();
- }
- /// <summary>
- /// 进入环境光测试
- /// </summary>
- public void OnClick_EnterLightCtrl() {
- SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
- }
- /// <summary>
- /// 进入屏幕标记
- /// </summary>
- public void OnClick_EnterMarker() {
- SetScreenPositioningStepState(ScreenPositioningStep.Marker);
- }
- /// <summary>
- /// successful 返回页面时候,根据ScreenPositioningStep区分
- /// </summary>
- public void OnClick_BackByScreenPositioningStep()
- {
- SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
- //清空两个绘制线
- onClearSuccessfullLineGenerator();
- //if(perStepView == ScreenPositioningStep.Manual)
- // SetScreenPositioningStepState(ScreenPositioningStep.Manual);
- //else
- // SetScreenPositioningStepState(ScreenPositioningStep.Marker);
- }
- /// <summary>
- /// 直接进入游戏流程
- /// </summary>
- public void OnClick_EnterGame()
- {
- //对比确认效果。直接进入游戏引导页面
- EnterGame();
- }
- /// <summary>
- /// 进入自动流程校准操作
- /// </summary>
- public void OnClick_EnterAuto()
- {
- Debug.Log("*********** OnClick_EnterAuto ************");
- //自动按钮时候进入之前的校准流程
- ClearPointsData();
- OnClick_Auto();
- }
- /// <summary>
- /// 进入标记页面自动流程校准操作
- /// </summary>
- public void OnClick_EnterAutoByMarker()
- {
- Debug.Log("*********** OnClick_EnterAutoByMarker ************");
- //自动按钮时候进入之前的校准流程
- ClearPointsData();
- markerPointsLine.SetLine(new List<Vector2>());
- //隐藏提示
- pointMarker.ShowHintImageParent(false);
- OnClick_Auto();
- }
- /// <summary>
- /// 自动识别
- /// </summary>
- public void OnClick_Auto()
- {
- bAuto = true;
- doLocateAuto = true;
- //textTip1.SetActive(false);
- //textTip2.SetActive(true);
- //ResetButton(btnAuto);
- if (enterFromZimWebCamera)
- {
- ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
- _screenLocate.EnterScreenLocateManualAuto();
- return;
- }
- if (InfraredDemo.running)
- {
- InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
- }
-
- }
- /// <summary>
- /// 进入手动界面
- /// </summary>
- public void OnClick_EnterManual()
- {
- SetScreenPositioningStepState(ScreenPositioningStep.Manual);
- }
- /// <summary>
- /// 切换成手动方式
- /// </summary>
- public void onHandMovement()
- {
- bAuto = false;
- doLocateAuto = false;
- draggableParent.gameObject.SetActive(true);
- pointsParent.gameObject.SetActive(false);
- mask.SetActive(false);
- cameraLight.SetActive(true);
- }
- /// <summary>
- /// 自动识别后同步
- /// </summary>
- public void SyncScreenPosition()
- {
- Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
- Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
- SyncQuadUnityVectorListToPos();
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- SyncQuadUnityVectorListToMarkerPointesPos();
- //显示提示
- pointMarker.ShowHintImageParent(true);
- AutoEndConfirmBtn.interactable = true;
- if (curStepView == ScreenPositioningStep.Start || curStepView == ScreenPositioningStep.AutoEnd) {
- //如果是开始页面进行自动定位的
- if (curStepView != ScreenPositioningStep.AutoEnd) {
- SetScreenPositioningStepState(ScreenPositioningStep.AutoEnd);
- }
- }
- else if (curStepView == ScreenPositioningStep.Marker || curStepView == ScreenPositioningStep.Manual) {
- //显示最后结果
- if(bMarkerFinish) SetScreenPositioningStepState(ScreenPositioningStep.Successful);
- }
- //屏幕变化情况下。同步修改对应的line
- //设置两个线段
- QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
- QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
- if (screenAuto != null)
- {
- Debug.Log("[校准流程]Successful自动识别screenAuto信息 ------------ ");
- CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
- FirstUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
- }
- else
- {
- Debug.LogError("screenAuto 不存在!");
- }
- if (screenSemiAuto != null)
- {
- Debug.Log("[校准流程]Successful半自动识别screenSemiAuto信息 ------------ ");
- SecondUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenSemiAuto, texSize);
- }
- else
- {
- Debug.LogError("screenSemiAuto 不存在!");
- }
- }
- /// <summary>
- /// 清空白线数据
- /// </summary>
- void onClearCurrentUILineGenerator() {
- if (CurrentUILineGenerator.Points.Length != 0) CurrentUILineGenerator.Points = new Vector2[0];//System.Array.Clear(CurrentUILineGenerator.Points, 0, CurrentUILineGenerator.Points.Length);
- }
- /// <summary>
- /// 清空最后识别的两个自动和半自动数据线
- /// </summary>
- void onClearSuccessfullLineGenerator()
- {
- if(FirstUILineGenerator.Points.Length !=0) FirstUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(FirstUILineGenerator.Points, 0, FirstUILineGenerator.Points.Length);
- if (SecondUILineGenerator.Points.Length !=0) SecondUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(SecondUILineGenerator.Points, 0, SecondUILineGenerator.Points.Length);
- }
- #region 绘制线段部分
- //点击拖拽的开始位置
- public void onBeginPos(int index, Vector3 pos)
- {
- Debug.Log("pos begin: " + pos);
- beginPos = pos;
- }
- public void onDragPos(int index, Vector3 pos)
- {
- //设置线段的点
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- //点击拖拽的结束位置
- public void onEndPos(int index, Vector3 pos)
- {
- Debug.Log("pos end: " + pos);
- endPos = pos;
- if (beginPos == endPos) return;
- //Debug.Log(index+",最后的点:" + pos);
- //再记录一次最后的点
- SetLinePos();
- }
- //同步设置图片对应的位置到line
- public void SetLinePos()
- {
- //记录一个操作点的操作位置
- AddOldLinePosition();
- //设置线段的点
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- void AddOldLinePosition()
- {
- Vector3[] v = new Vector3[4];
- pos1.GetWorldCorners(v);
- Vector3[] v1 = new Vector3[4];
- pos1.GetLocalCorners(v1);
- //for (int i = 0; i < 4; i++)
- //{
- // Debug.Log(i + " =1= " + v[i]);
- // Debug.Log(i + " =2= " + v1[i]);
- //}
- //记录一个操作点的操作位置
- List<Vector3> screenPositions = new List<Vector3>();
- screenPositions.Add(pos1.localPosition);
- screenPositions.Add(pos2.localPosition);
- screenPositions.Add(pos3.localPosition);
- screenPositions.Add(pos4.localPosition);
- LinePosition linePosition = new LinePosition();
- linePosition.index = oldLinePosition.Count;
- linePosition.pos = screenPositions;
- oldLinePosition.Add(linePosition);
- }
- /// <summary>
- /// 记录一个开始位置
- /// </summary>
- void SaveStartOldLinePosition() {
- oldLinePosition.Clear();
- //记录一个操作点的操作位置
- List<Vector3> screenPositions = new List<Vector3>();
- screenPositions.Add(pos1.localPosition);
- screenPositions.Add(pos2.localPosition);
- screenPositions.Add(pos3.localPosition);
- screenPositions.Add(pos4.localPosition);
- LinePosition linePosition = new LinePosition();
- linePosition.index = 1;
- linePosition.pos = screenPositions;
- oldLinePosition.Add(linePosition);
- }
- //转换绘制线段的点
- List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
- {
- List<Vector2> _screenPositions = new List<Vector2>();
- //点为负数的增大 offset,正数减少 offset
- //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
- //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
- //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
- //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
- Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
- Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
- Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
- Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
- _screenPositions.Add(startPos1);
- _screenPositions.Add(startPos2);
- _screenPositions.Add(startPos3);
- _screenPositions.Add(startPos4);
- return _screenPositions;
- }
- void SetRectanglePoints(List<Vector2> screenPositions)
- {
- line.SetLine(screenPositions);
- }
- //撤回上一个元素
- public void onRecall()
- {
- // 获取并删除最后一个元素,并且保留一个元素
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- // 获取回退的那个元素点
- LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
- // 获取最后一个元素
- //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
- oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
- pos1.localPosition = lastElement_second.pos[0];
- pos2.localPosition = lastElement_second.pos[1];
- pos3.localPosition = lastElement_second.pos[2];
- pos4.localPosition = lastElement_second.pos[3];
- //设置线段的点
- SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
- }
- }
- //是不是从Demo界面进入该页面的
- public bool enterFromInfraredDemo { get; set; } = false;
- //是否从测试场景进入
- public bool enterFromZimWebCamera { get; set; } = false;
- //确认修改
- public void onConfirmation()
- {
- if (enterFromZimWebCamera)
- {
- ConfirmScreenLocateManualTest();
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- SaveLocalPos();
- AudioMgr.ins.PlayBtn();
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- return;
- }
- if (!ConfirmScreenLocateManual()) return;
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- //oldLinePosition.Add(lastElement);
- }
- if (InfraredDemo.running)
- {
- //跳转入界面
- AudioMgr.ins.PlayBtn();
- if (!enterFromInfraredDemo)
- {
- //每次初始化重置一下引导
- infraredDemo.resetInfraredPlayerPrefs();
- //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
- //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
- if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
- {
- //如果是红外连接成功,记录一个tag
- PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
- }
- onEnterInfrared();
- }
- else {
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- }
-
- }
- //存储一次节点
- SaveLocalPos();
- }
- //重置位置
- public void onReset()
- {
- oldLinePosition.Clear();
- // 获取屏幕的四个角的像素坐标
- Vector2 bottomLeft = new Vector2(0, 0);
- // 将屏幕像素坐标转换为 Canvas 的局部坐标
- Vector2 localBottomLeft;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
- // 打印结果
- Debug.Log("Local Bottom Left: " + localBottomLeft);
- //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
- int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
- pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
- pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
- pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
- pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
- //pos1.anchoredPosition = new Vector3(_x, _y, 0);
- //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
- //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
- //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
- //设置一次位置
- SetLinePos();
- btnRecordInfrared.Reset();
- ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
- if (enterFromZimWebCamera)
- {
- ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
- ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
- }
- if (curStepView == ScreenPositioningStep.Start || curStepView == ScreenPositioningStep.AutoEnd)
- {
- //如果是开始页面进行自动定位的失败的
- if (curStepView != ScreenPositioningStep.AutoEnd) {
- SetScreenPositioningStepState(ScreenPositioningStep.AutoEnd);
- }
- AutoEndConfirmBtn.interactable = false;
- }
- }
- /// <summary>
- /// 标准四个点
- /// </summary>
- /// <param name="index"></param>
- /// <param name="pos"></param>
- public void onManualNewPos(int index, Vector3 pos)
- {
- Debug.Log("pos end: " + pos);
- if (index == 0)
- pos1.localPosition = pos;
- else if (index == 1)
- pos2.localPosition = pos;
- else if (index == 2)
- pos3.localPosition = pos;
- else if (index == 3)
- pos4.localPosition = pos;
- //再记录一次最后的点
- SetLinePos();
- }
- /// <summary>
- /// 处理新流程,先记录手动,然后到自动识别处理
- /// </summary>
- public void onManualToAutomatic() {
- if (enterFromZimWebCamera)
- {
- ConfirmScreenLocateManualTest();
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- SaveLocalPos();
- //到自动
- OnClick_Auto();
- return;
- }
- if (!ConfirmScreenLocateManual()) {
- Debug.LogError("ConfirmScreenLocateManual is false!");
- return;
- }
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- //存储一次节点
- SaveLocalPos();
- Debug.Log("[校准流程]进入自动校准数据,调用EnterScreenLocateManualAuto");
- bAuto = true;
- doLocateAuto = true;
- //自动校准
- InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
- }
- /// <summary>
- /// PointMarker 调用
- /// </summary>
- public void onFinishManualToAutomatic() {
- bMarkerFinish = true;
- onManualToAutomatic();
- }
- #endregion
- List<Vector2> _locatePointList = new();
- float _texWidth;
- float _texHeight;
- void RecordLocatePoint(RectTransform p, Vector2 pivot)
- {
- Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
- pos.x = Mathf.Clamp01(pos.x / Screen.width);
- pos.y = Mathf.Clamp01(pos.y / Screen.height);
- _locatePointList.Add(pos);
- }
- bool ConfirmScreenLocateManual()
- {
- if (InfraredDemo.running)
- {
- //渲染截图
- Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
- if (texture2D == null)
- {
- Debug.Log("EnterScreenLocateManual = null");
- InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
- return false;
- }
- _locatePointList.Clear();
- _texWidth = texture2D.width;
- _texHeight = texture2D.height;
- RecordLocatePoint(pos1, new Vector2(0, 0));
- RecordLocatePoint(pos2, new Vector2(1, 0));
- RecordLocatePoint(pos3, new Vector2(1, 1));
- RecordLocatePoint(pos4, new Vector2(0, 1));
- //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
- InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
- FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
- //同步数据
- ScreenLocate.quadUnityVectorList.Clear();
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
- //两个点切换,顺序不一样
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
- ScreenLocate.SaveScreenLocateVectorList();
- Debug.Log("[校准流程]设置QuitScreenLocateManual()手动数据,保存quadUnityVectorList");
- return true;
- }
- return false;
- }
- void ConfirmScreenLocateManualTest()
- {
- //渲染截图
- ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
- Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
- if (texture2D == null)
- {
- _screenLocate.QuitScreenLocateManual(null);
- return;
- }
- _locatePointList.Clear();
- _texWidth = texture2D.width;
- _texHeight = texture2D.height;
- RecordLocatePoint(pos1, new Vector2(0, 0));
- RecordLocatePoint(pos2, new Vector2(1, 0));
- RecordLocatePoint(pos3, new Vector2(1, 1));
- RecordLocatePoint(pos4, new Vector2(0, 1));
- _screenLocate.QuitScreenLocateManual(_locatePointList);
- //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
- setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
- if (!doLocateAuto) // 设置手动定位数据
- setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
- //同步数据
- ScreenLocate.quadUnityVectorList.Clear();
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
- //两个点切换,顺序不一样
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
- ScreenLocate.SaveScreenLocateVectorList();
- }
- void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
- {
- //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
- pointsTF2.SetActive(active);
- if (pointsTF2.transform.childCount == targetList.Count)
- {
- for (int i = 0; i < pointsTF2.transform.childCount; i++)
- {
- Transform pointTF = pointsTF2.transform.GetChild(i);
- Vector2 pos = targetList[i];
- pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
- pointTF.gameObject.SetActive(true);
- }
- }
-
- }
- void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
- {
- setPointsLocation(targetList, pointsTF2, active);
- var lo = new Vector2(-0.5f, -0.5f);
- ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
- 2 * (targetList[0] + lo),
- 2 * (targetList[1] + lo),
- 2 * (targetList[2] + lo),
- 2 * (targetList[3] + lo) };
- }
- void SaveLocalPos()
- {
- List<Vector3> screenPositions = new List<Vector3>();
- screenPositions.Add(pos1.anchoredPosition);
- screenPositions.Add(pos2.anchoredPosition);
- screenPositions.Add(pos3.anchoredPosition);
- screenPositions.Add(pos4.anchoredPosition);
- string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
- Debug.Log("Local UI Position: " + saveStr);
- PlayerPrefs.SetString("ScreenPositioningView", saveStr);
- }
- void GetLocalPos()
- {
- string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
- if (!string.IsNullOrWhiteSpace(posListStr))
- {
- List<Vector2> posList = posListStr.Split(';')
- .Select(s =>
- {
- string[] parts = s.Split(',');
- return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
- })
- .ToList();
- pos1.anchoredPosition = posList[0];
- pos2.anchoredPosition = posList[1];
- pos3.anchoredPosition = posList[2];
- pos4.anchoredPosition = posList[3];
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- }
- #region 按钮颜色切换
- private void OnButtonClick(Button button)
- {
- // 切换按钮颜色
- ColorBlock colors = button.colors;
- colors.normalColor = highlightedColor;
- colors.highlightedColor = highlightedColor;
- button.colors = colors;
- // 切换字体颜色
- Text buttonText = button.GetComponentInChildren<Text>();
- buttonText.color = highlightedTextColor;
- }
- private void ResetButton(Button button)
- {
- // 重置按钮颜色
- ColorBlock colors = button.colors;
- colors.normalColor = normalColor;
- colors.highlightedColor = normalColor;
- button.colors = colors;
- // 重置字体颜色
- Text buttonText = button.GetComponentInChildren<Text>();
- buttonText.color = normalTextColor;
- }
- #endregion
- #region 标定完成之后进入游戏界面
- void onEnterInfrared()
- {
- //添加进入射箭场景
- if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
- {
- Debug.Log("-----进入射箭场景!");
- NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
- newUserGuiderManager.curConfigKey = "开始-红外调整";
- //b端都是红外设备流程
- if (CommonConfig.StandaloneModeOrPlatformB)
- {
- newUserGuiderManager.isNewModule = true;
- }
- else {
- newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
- }
- //进入射箭场景
- GlobalData.pkMatchType = PKMatchType.None;
- GameMgr.gameType = 1;
- //射一箭回到连接页面,Device.view
- GameMgr.bNavBack = true;
- GameMgr.bShowDistance = false;
- if (!CommonConfig.StandaloneModeOrPlatformB)
- {
- AimHandler.ins.bInitOne = false;//true
- }
- //关闭计时器
- GameMgr.turnOffTimer = true;
- //关闭左边靶子和底部速度栏
- GameMgr.HideTargetView = true;
- GameMgr.HideBillboard = true;
- GameMgr.ButtonCount = 0;
- UnityEngine.SceneManagement.SceneManager.LoadScene(
- "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
- PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
- }
- else
- {
- Debug.Log("-----跳转回连接界面!");
- //如不是第一次,则应该跳转回连接界面
- ViewMgr.Instance.DestroyView<SmartArcheryView>();
- ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
- }
- }
- #endregion
- #region 最后选择算法处理UI
- int selected = -1;
- void initSelectInfo() {
- for (int i = 0; i < 2; i++)
- {
- int currentIndex = i; // 将 i 赋值给局部变量
- ResultImagesButtons[currentIndex].onClick.AddListener(() => SelectImage(currentIndex));
- //ResultButtons[currentIndex].onClick.AddListener(() =>
- //{
- // if (currentIndex == 0)
- // {
- // OnFirstResult();
- // }
- // else if (currentIndex == 1)
- // {
- // OnSecondResult();
- // }
- //});
- }
- //默认选择1 自动
- SelectImage(0);
- }
- // 添加一个选择函数
- void SelectImage(int selectedIndex)
- {
- //如果重复选中则跳转
- if (selected == selectedIndex) {
- if (selected == 0)
- {
- OnFirstResult();
- }
- else if (selected == 1)
- {
- OnSecondResult();
- }
- return;
- }
- // 遍历所有图片
- selected = selectedIndex;
- //for (int i = 0; i < ResultImages.Length; i++)
- //{
- // // 如果是选中的 index,赋值为 ResultLines[0],否则赋值为 ResultLines[1]
- // if (i == selectedIndex)
- // {
- // selected = i;
- // ResultImages[i].sprite = ResultLines[0];
- // ResultButtons[i].interactable = true;
- // }
- // else
- // {
- // ResultImages[i].sprite = ResultLines[1];
- // ResultButtons[i].interactable = false;
- // }
- //}
- }
- //选择全自动结果后进入游戏
- void OnFirstResult()
- {
- ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
- UpdateQuadUnityVectorList();
- onCompelete();
- }
- //选择半自动结果
- void OnSecondResult()
- {
- ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
- UpdateQuadUnityVectorList();
- onCompelete();
- }
- public void onCompelete()
- {
- if (enterFromZimWebCamera)
- {
- SaveLocalPos();
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- return;
- }
- EnterGame();
- //存储一次节点
- SaveLocalPos();
- }
- void EnterGame() {
- if (InfraredDemo.running)
- {
- //跳转入界面
- AudioMgr.ins.PlayBtn();
- if (!enterFromInfraredDemo)
- {
- //每次初始化重置一下引导
- infraredDemo.resetInfraredPlayerPrefs();
- //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
- //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
- if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
- {
- //如果是红外连接成功,记录一个tag
- PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
- }
- onEnterInfrared();
- }
- else
- {
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- }
- }
- }
- /// <summary>
- /// 选择模式后更新 quadUnityVectorList
- /// </summary>
- void UpdateQuadUnityVectorList() {
- ScreenLocate.Main.UpdateQuadUnityVectorList();
- SyncQuadUnityVectorListToPos();
- infraredDemo?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
- _locatePointList.Clear();
-
- }
- void SyncQuadUnityVectorListToPos() {
- Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
- pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- }
- //这个脚本存在时候。任何切换操作都直接处理删除
- //void OnSceneUnloaded(Scene scene)
- //{
- // ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
- // ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- //}
- //void OnEnable()
- //{
- // SceneManager.sceneUnloaded += OnSceneUnloaded;
- //}
- //void OnDisable()
- //{
- // SceneManager.sceneUnloaded -= OnSceneUnloaded;
- //}
- #endregion
- #region 判断点是否在 quad 内部
- //判断maskline
- public bool IsPointInMaskLine(Vector2 point) {
- return IsPointInQuad(point, maskLine.ScreenPositions);
- }
- //实现基于射线法,通过数交点的奇偶性来判断点是否在多边形内
- bool IsPointInQuad(Vector2 point, List<Vector2> quadVertices)
- {
- if (quadVertices.Count != 4)
- {
- Debug.LogError("Quad must have exactly 4 vertices.");
- return false;
- }
- bool isInside = false;
- int vertexCount = quadVertices.Count;
- for (int i = 0, j = vertexCount - 1; i < vertexCount; j = i++)
- {
- Vector2 vi = quadVertices[i];
- Vector2 vj = quadVertices[j];
- // 检查射线是否与边相交
- if (((vi.y > point.y) != (vj.y > point.y)) &&
- (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x))
- {
- isInside = !isInside;
- }
- }
- return isInside;
- }
- /// <summary>
- /// 判断四个点是否能够形成有效的四边形,并根据面积阈值过滤小四边形
- /// </summary>
- /// <param name="points">四个点</param>
- /// <param name="minArea">面积阈值,用于过滤面积太小的四边形</param>
- /// <returns>是否是有效的四边形</returns>
- public bool IsValidQuadrilateral(Vector2[] points, float minArea = 100000.0f)
- {
- if (points.Length != 4)
- {
- Debug.Log("四个点数不足,无法形成四边形。");
- return false; // 如果点数不是4,直接返回false
- }
- // 计算四边形的面积
- float area = CalculateArea(points);
- Debug.Log($"四边形面积: {area}");
- if (area < minArea)
- {
- Debug.Log($"面积小于阈值: {minArea},返回 false。");
- return false; // 面积太小,返回 false
- }
- // 检查是否有三点共线
- for (int i = 0; i < points.Length; i++)
- {
- Vector2 a = points[i];
- Vector2 b = points[(i + 1) % points.Length];
- Vector2 c = points[(i + 2) % points.Length];
- if (ArePointsCollinear(a, b, c))
- {
- Debug.Log($"点 {a}, {b}, {c} 共线,返回 false。");
- return false; // 存在共线点,返回 false
- }
- }
- // 检查是否为矩形或接近矩形
- if (!IsRectangle(points))
- {
- // 如果不是矩形,检查是否是梯形
- if (!IsTrapezoid(points))
- {
- Debug.Log("既不是矩形,也不是梯形,返回 false。");
- return false; // 不是矩形也不是梯形,返回 false
- }
- else
- {
- Debug.Log("是梯形。");
- }
- }
- else
- {
- Debug.Log("是矩形。");
- }
- // 检查对角线是否相交
- if (DoLinesIntersect(points[0], points[1], points[2], points[3]) ||
- DoLinesIntersect(points[1], points[2], points[3], points[0]))
- {
- Debug.Log("对角线相交,返回 false。");
- return false; // 对角线相交,返回 false
- }
- // 检查点的排列顺序,确保是顺时针或逆时针
- if (!ArePointsClockwise(points) && !ArePointsClockwise(points.Reverse().ToArray()))
- {
- Debug.Log("点的排列顺序不正确,返回 false。");
- return false; // 点的排列顺序不正确
- }
- Debug.Log("四边形有效,返回 true。");
- return true; // 通过所有检查,返回 true
- }
- /// <summary>
- /// 判断是否是矩形(近似90度角)
- /// </summary>
- private bool IsRectangle(Vector2[] points)
- {
- // 角度容差范围,允许夹角有小的偏差
- const float dotProductThreshold = 0.1f;
- for (int i = 0; i < points.Length; i++)
- {
- Vector2 a = points[i];
- Vector2 b = points[(i + 1) % points.Length];
- Vector2 c = points[(i + 2) % points.Length];
- // 计算向量 AB 和 BC 的内积
- Vector2 ab = b - a;
- Vector2 bc = c - b;
- float dotProduct = Vector2.Dot(ab.normalized, bc.normalized);
- // 如果内积接近 0,表示夹角接近 90 度
- if (Mathf.Abs(dotProduct) > dotProductThreshold)
- {
- Debug.Log($"点 {a}, {b}, {c} 的夹角不接近 90 度,返回 false。");
- return false; // 角度不接近 90 度
- }
- }
- return true;
- }
- /// <summary>
- /// 判断是否是梯形
- /// </summary>
- private bool IsTrapezoid(Vector2[] points)
- {
- // 斜率容差范围,允许斜率差异较小
- const float slopeThreshold = 0.2f;
- // 计算对角线的斜率
- float slope1 = (points[1].y - points[0].y) / (points[1].x - points[0].x);
- float slope2 = (points[3].y - points[2].y) / (points[3].x - points[2].x);
- // 如果对角线斜率差异小于容差范围,则认为是梯形
- if (Mathf.Abs(slope1 - slope2) < slopeThreshold)
- {
- Debug.Log("对角线平行,判断为梯形。");
- return true; // 对角线平行,返回 true
- }
- else
- {
- Debug.Log($"对角线斜率差异过大: {slope1} vs {slope2},不是梯形,返回 false。");
- return false; // 斜率差异过大,不是梯形
- }
- }
- //计算四边形面积
- float CalculateArea(Vector2[] points)
- {
- float area = 0f;
- int n = points.Length;
- for (int i = 0; i < n; i++)
- {
- Vector2 current = points[i];
- Vector2 next = points[(i + 1) % n];
- area += current.x * next.y - current.y * next.x;
- }
- return Mathf.Abs(area) / 2f;
- }
- //确保点不共线
- //判断点是否在同一条直线上(即共线),如果共线则无法形成四边形。可以通过计算任意三点的面积是否为 0 来验证是否共线:
- bool ArePointsCollinear(Vector2 a, Vector2 b, Vector2 c)
- {
- float area = Mathf.Abs(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2.0f;
- return Mathf.Approximately(area, 0f);
- }
- //确保边不相交
- bool DoLinesIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
- {
- float cross(Vector2 p1, Vector2 p2, Vector2 p3)
- {
- return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
- }
- float d1 = cross(a, b, c);
- float d2 = cross(a, b, d);
- float d3 = cross(c, d, a);
- float d4 = cross(c, d, b);
- return d1 * d2 < 0 && d3 * d4 < 0;
- }
- //顺时针或逆时针排列顶点
- bool ArePointsClockwise(Vector2[] points)
- {
- float sum = 0;
- for (int i = 0; i < points.Length; i++)
- {
- Vector2 current = points[i];
- Vector2 next = points[(i + 1) % points.Length];
- sum += (next.x - current.x) * (next.y + current.y);
- }
- return sum < 0; // 小于0为顺时针,大于0为逆时针
- }
- #endregion
- #region marker line 部分渲染
- /// <summary>
- /// maker 界面渲染
- /// </summary>
- void SyncQuadUnityVectorListToMarkerPointesPos()
- {
- if (ScreenLocate.quadUnityVectorList.Count == 4)
- {
- Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToMarkerPointesPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
- markerPosList[0].anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- markerPosList[1].anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- markerPosList[2].anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- markerPosList[3].anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- SetMarkerRectanglePoints(linePosConversion(markerPosList[0].localPosition, markerPosList[1].localPosition, markerPosList[2].localPosition, markerPosList[3].localPosition));
- }
-
- }
- void SetMarkerRectanglePoints(List<Vector2> screenPositions)
- {
- markerPointsLine.SetLine(screenPositions);
- }
- #endregion
- }
|