ScreenLocate.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. #define ENABLE_LOG
  2. using InfraredManager;
  3. using o0;
  4. using o0.Geometry2D.Float;
  5. using o0InfraredLocate.ZIM;
  6. using SixLabors.ImageSharp.PixelFormats;
  7. using SLAMUVC;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using UnityEngine;
  13. using UnityEngine.Experimental.AI;
  14. using UnityEngine.UI;
  15. using ZIM;
  16. using ZIM.Unity;
  17. using static SLAMUVC.UVCManager;
  18. using Color = UnityEngine.Color;
  19. using Time = UnityEngine.Time;
  20. [RequireComponent(typeof(Canvas))]
  21. public partial class ScreenLocate : o0InfraredCameraHandler
  22. {
  23. public InfraredCameraHelper InfraredCameraHelper;
  24. private const string TAG = "ScreenLocate#";
  25. enum Mode
  26. {
  27. InfraredLocate,
  28. ScreenMap,
  29. ScreenLocateManual
  30. }
  31. enum Platform
  32. {
  33. Window,
  34. Android
  35. }
  36. Platform mPlatform = Platform.Android;
  37. public enum ScreenIdentificationTag
  38. {
  39. // 屏幕定位的方式,手动、半自动、自动
  40. Manual,
  41. SemiAuto,
  42. Auto
  43. }
  44. // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  45. public InfraredSpot[] InfraredSpots
  46. {
  47. get
  48. {
  49. return infraredSpotBuffer;
  50. }
  51. }
  52. // 1个灯, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  53. public InfraredSpot InfraredSpotSingle
  54. {
  55. get
  56. {
  57. return infraredSpotBuffer[0];
  58. }
  59. }
  60. public InfraredSpot[] infraredSpotBuffer => infraredLocate.InfraredSpotBuffer;
  61. // 当前 应用/生效 的屏幕四边形数据(QuadrilateralInCamera类)
  62. public QuadrilateralInCamera CurrentScreenQuad => screenIdentification.Screen.QuadInCamera;
  63. /// <summary>
  64. /// 定位之后,可能有3种结果(手动、半自动、自动),从中选择一种作为最终识别到的屏幕。
  65. /// 如果选择的是null,即没有识别到屏幕,则返回false,否则返回true
  66. /// </summary>
  67. public bool SelectScreenAfterLocate(ScreenIdentificationTag tag) => ScreenIdentification.SelectScreenAfterLocate(tag);
  68. /// <summary>
  69. /// 定位之后,获取定位数据,返回null代表没有数据
  70. /// </summary>
  71. public QuadrilateralInCamera GetScreenAfterLocate(ScreenIdentificationTag tag) => ScreenIdentification.GetScreenAfterLocate(tag);
  72. /// 上一次屏幕定位的情况, 还未识别(或识别失败)的时候返回值是null
  73. public QuadrilateralInCamera LastQuadState(ScreenIdentificationTag tag)
  74. {
  75. QuadrilateralInCamera target = tag switch
  76. {
  77. ScreenLocate.ScreenIdentificationTag.Manual => ScreenIdentification.QuadManual,
  78. ScreenLocate.ScreenIdentificationTag.SemiAuto => ScreenIdentification.QuadSemiAuto,
  79. ScreenLocate.ScreenIdentificationTag.Auto => ScreenIdentification.QuadAuto,
  80. _ => null
  81. };
  82. return target;
  83. }
  84. /// <summary>
  85. /// 上一次半自动识别的情况, 还未识别的时候数组是null
  86. /// 通过索引获取布尔值,false代表这条边识别失败(回退应用了手动数据), 0-下、1-右、2-上、3-左
  87. /// </summary>
  88. public bool[] LastQuadSemiAutoState() => screenIdentification.LastQuadSemiAutoState;
  89. /// <summary>
  90. /// 获取算法执行过程中输出的纹理,0原图,1半自动识别到的全部线段,2屏幕黑白色差,3识别结果,4屏幕色差叠加识别结果,5半自动时的备选线段
  91. /// </summary>
  92. public Texture2D[] OutputTextures => outputTexture2D;
  93. /// <summary>
  94. /// CameraLocation 的偏移量
  95. /// </summary>
  96. public Vector2 CameraLocationOffset => infraredLocate.CameraLocationOffset;
  97. public Vector2 UVOffset => infraredLocate.UVOffset;
  98. //是否单点显示
  99. public bool bSinglePoint => infraredLocate.bSinglePoint;
  100. // public InfraredDemo InfraredDemoMain => FindObjectOfType<InfraredDemo>();
  101. #region UVC 处理的对象
  102. //public UVCManager mUVCManager;
  103. public CameraInfo mUVCCameraInfo;
  104. public bool getUVCCameraInfo => mUVCCameraInfo != null ? true : false;
  105. public Vector2 getUVCCameraInfoSize => getUVCCameraInfo ? mUVCCameraInfo.Size : new Vector2(320, 240);
  106. private Texture mUVCTexture;
  107. public Texture getUVCTexture => mUVCTexture;
  108. public Texture setUVCTexture
  109. {
  110. set
  111. {
  112. mUVCTexture = value;
  113. }
  114. }
  115. private Texture2D mUVCTexture2D;
  116. // [SerializeField] Texture2DArray mUVCOutArray;
  117. #endregion
  118. public Text Info;
  119. public List<RectTransform> CrosshairInCamera;
  120. public List<RectTransform> CrosshairInScreen;
  121. public RectTransform ScreenQuad;
  122. public Toggle SaveToggle;
  123. public Toggle FullScreenToggle;
  124. public LineGenerator UILineGenerator;
  125. public bool ShowScreenQuad = false;
  126. // 显示在demo上的rawImage
  127. public List<RawImage> outputRawImages;
  128. readonly Texture2D[] outputTexture2D = new Texture2D[8];
  129. public RawImage FullScreenImage;
  130. public PixelCheaker ScreenPixelCheaker;
  131. public List<Texture2D> DebugScreenImages = new List<Texture2D>();
  132. public bool DebugOnZIMDemo = false;
  133. // private SynchronizationContext mainContext;
  134. public float ReDoLocateCalibrationRatio { get; private set; } // 半自动定位时校准的距离比例,以手动的结果来校准,离手动太远的线段会被舍弃
  135. public void SetCameraSize(Vector size) => cameraSize = size;
  136. public override Vector CameraSize => cameraSize;
  137. // 记录算法中的CameraSize,红外识别和屏幕识别都会使用到
  138. Vector cameraSize;
  139. bool bIdentifyRed = true;//默认设备红色
  140. bool bIdentifyGreen = true;
  141. #region 性能检测相关
  142. public Text m_UITime;
  143. const float m_UIUpdateInterval = 0.1f;
  144. float m_UIUpdateTimer = 0.0f;
  145. List<float> m_History = new List<float>(100);
  146. int m_ValidHistoryFrames = 0;
  147. float m_AverageTime = float.NaN;
  148. float m_MedianTime = float.NaN;
  149. float m_MinTime = float.NaN;
  150. float m_MaxTime = float.NaN;
  151. public float updateInterval = 0.5F;
  152. private double lastInterval;
  153. private int frames = 0;
  154. private float fps;
  155. public Text m_FPS;
  156. #endregion
  157. #region PC部分参数
  158. //亮度
  159. public float pcBrightness { get; set; } = 0.0f;
  160. //对比度
  161. public float pcContrast { get; set; } = 0.0f;
  162. #endregion
  163. // 红外灯识别算法
  164. InfraredLocate infraredLocate;
  165. // 屏幕识别算法
  166. o0.Project.ScreenIdentification screenIdentification;
  167. public o0.Project.ScreenIdentification ScreenIdentification => screenIdentification;
  168. RectTransform canvas;
  169. Mode mode;
  170. //List<(Vector2 pos, GameObject go)> pointManual = new List<(Vector2, GameObject)>();
  171. //o0.Project.WebCam o0WebCam = null;
  172. /// <summary>
  173. /// 正在识别的状态,自动识别时候记录
  174. /// </summary>
  175. bool bAutomaticRecognition { get; set; } = false;//进行捕获时
  176. bool bAutomaticRecognitionStart { get; set; } = false;//是否进行捕获
  177. bool bAutomaticRecognitionEnd { get; set; } = false;//是否结束捕获
  178. [NonSerialized] public RectTransform BackQuad = null;
  179. static public ScreenLocate Main { get; private set; }
  180. static public void AutoLightPixels(Color[] pixels, int width, int height)
  181. {
  182. if (Main.DebugOnZIMDemo)
  183. {
  184. var newTex = pixels.zimAutoLightSimple(width, height);
  185. DebugTexture(7, newTex);
  186. try
  187. {
  188. Main.FullScreenImage.texture = newTex;
  189. }
  190. catch { }
  191. }
  192. }
  193. static public void DebugTexture(int index, Texture2D texture)
  194. {
  195. LateDestory(Main.outputTexture2D[index]);
  196. Main.outputTexture2D[index] = texture;
  197. try
  198. {
  199. Main.outputRawImages[index].texture = texture;
  200. }
  201. catch { }
  202. }
  203. static void LateDestory(UnityEngine.Object o) => Main.StartCoroutine(Main.LateDestoryIEnum(o));
  204. static public void SetScreen(UnityEngine.Color? color = null)
  205. {
  206. if (Main.BackQuad == null)
  207. {
  208. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  209. var background = canvas.Find("Background");
  210. Main.BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  211. }
  212. Main.BackQuad.parent.gameObject.SetActive(color != null);
  213. Main.BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  214. //Debug.Log("Set Screen " + color.GetColorName());
  215. }
  216. static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
  217. {
  218. if (Main.BackQuad == null)
  219. {
  220. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  221. var background = canvas.Find("Background");
  222. Main.BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  223. }
  224. Main.BackQuad.parent.gameObject.SetActive(color != null);
  225. Main.BackQuad.anchorMin = rect.min;
  226. Main.BackQuad.anchorMax = rect.max;
  227. Main.BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  228. //Debug.Log("Set Screen " + color.GetColorName());
  229. }
  230. static void DebugBackQuad(Rect? rect = null)
  231. {
  232. if (Main.BackQuad)
  233. {
  234. Main.BackQuad.parent.GetComponent<RawImage>().enabled = false;
  235. Main.BackQuad.GetComponent<RawImage>().color = Color.white;
  236. Main.BackQuad.parent.gameObject.SetActive(!Main.BackQuad.parent.gameObject.activeSelf);
  237. if (rect.HasValue)
  238. {
  239. Main.BackQuad.anchorMin = rect.Value.min;
  240. Main.BackQuad.anchorMax = rect.Value.max;
  241. }
  242. }
  243. }
  244. //public void ReSizeTexture(int width, int height)
  245. //{
  246. // Debug.Log("Cur mUVCTexture Size: [" + mUVCTexture.width + "," + mUVCTexture.height + "]");
  247. // if (mUVCTexture.width < width || mUVCTexture.height < height) // 如果当前分辨率太小,则重新new一个texture
  248. // {
  249. // Texture2D tex = new Texture2D(
  250. // width, height,
  251. // TextureFormat.ARGB32,
  252. // false, /* mipmap */
  253. // true /* linear */);
  254. // tex.filterMode = FilterMode.Point;
  255. // tex.Apply();
  256. // mUVCTexture = tex;
  257. // mUVCCameraInfo.previewTexture = tex;
  258. // var nativeTexPtr = mUVCCameraInfo.previewTexture.GetNativeTexturePtr();
  259. // }
  260. //}
  261. void Awake()
  262. {
  263. if (Main != null)
  264. throw new Exception("[ScreenLocaer] 不允许多个实例");
  265. Main = this;
  266. #if !UNITY_EDITOR_WIN
  267. DebugOnZIMDemo = false;
  268. #endif
  269. //if (mUVCDrawer)
  270. // mUVCDrawer.StartPreviewAction += UVCIsReady;
  271. }
  272. void OnDestroy()
  273. {
  274. //if (mUVCDrawer)
  275. // mUVCDrawer.StartPreviewAction -= UVCIsReady;
  276. }
  277. void Start()
  278. {
  279. //mainContext = SynchronizationContext.Current;
  280. canvas = transform.GetComponent<RectTransform>();
  281. mode = Mode.InfraredLocate;
  282. if (DebugScreenImages.Count != 0 && DebugOnZIMDemo)
  283. {
  284. screenIdentification = new o0.Project.ScreenIdentification(this);
  285. screenIdentification.LocateScreen();
  286. }
  287. ReDoLocateCalibrationRatio = 0.125f;
  288. #region 性能检测相关
  289. for (var i = 0; i < m_History.Capacity; ++i)
  290. {
  291. m_History.Add(0.0f);
  292. }
  293. lastInterval = Time.realtimeSinceStartup;
  294. frames = 0;
  295. #endregion
  296. }
  297. IEnumerator LateDestoryIEnum(UnityEngine.Object o)
  298. {
  299. if (o)
  300. {
  301. yield return new WaitForEndOfFrame();
  302. Destroy(o);
  303. }
  304. }
  305. //ZIMWebCamera场景使用
  306. public void WebCamIsReady(Texture texture)
  307. {
  308. mPlatform = Platform.Window;
  309. mUVCTexture = texture;
  310. mUVCCameraInfo = new CameraInfo(mUVCTexture);
  311. brightness = 0;
  312. //UVC准备好
  313. InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
  314. }
  315. /// <summary>
  316. /// UVCManager 创建初始化时候,更新此函数
  317. /// </summary>
  318. /// <param name="cameraInfo"></param>
  319. public void UVCIsReady(CameraInfo cameraInfo)
  320. {
  321. mPlatform = Platform.Android;
  322. mUVCTexture = cameraInfo.previewTexture;
  323. mUVCCameraInfo = cameraInfo;
  324. Debug.Log("UVCIsReady:" + mUVCCameraInfo);
  325. //UVC准备好
  326. InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
  327. }
  328. /// <summary>
  329. /// 获取新的 previewTexture
  330. /// </summary>
  331. public void UVCUpdate(bool bChange)
  332. {
  333. mUVCTexture = mUVCCameraInfo.previewTexture;
  334. Debug.Log("[ScreenLocate] UVCUpdate:" + mUVCCameraInfo + ",bChange:" + bChange);
  335. InfraredCameraHelper?.InvokeOnUVCIsUpdate();
  336. //这里判断是否进入自动识别?
  337. if (bAutomaticRecognitionStart)
  338. {
  339. bAutomaticRecognitionStart = false;
  340. Debug.Log("[ScreenLocate] UVCUpdate 开始自动识别 Capture:" + Capture + " ,Delay: " + Delay);
  341. screenIdentification.LocateScreen(Capture, Delay);
  342. }
  343. if (bAutomaticRecognitionEnd)
  344. {
  345. bAutomaticRecognitionEnd = false;
  346. Debug.Log("[ScreenLocate] UVCUpdate 结束捕获,当前摄像机分辨率为: " + mUVCCameraInfo.Size);
  347. bAutomaticRecognition = false;
  348. }
  349. }
  350. /// <summary>
  351. /// 选择模式后更新 quadUnityVectorList
  352. /// </summary>
  353. public void UpdateQuadUnityVectorList()
  354. {
  355. quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
  356. SaveScreenLocateVectorList();
  357. }
  358. int brightness = 0;
  359. /// <summary>
  360. /// 设置算法红外灯的亮度值
  361. /// </summary>
  362. /// <param name="value"></param>
  363. public void SetInfraredLocateBrightnessThreshold(float value)
  364. {
  365. if (infraredLocate != null)
  366. infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  367. }
  368. void Update()
  369. {
  370. //++frames;
  371. //float timeNow = Time.realtimeSinceStartup;
  372. //if (timeNow > lastInterval + updateInterval)
  373. //{
  374. // fps = (float)(frames / (timeNow - lastInterval));
  375. // frames = 0;
  376. // lastInterval = timeNow;
  377. //}
  378. //if (m_FPS != null)
  379. // m_FPS.text = "FPS:" + fps.ToString("f2");
  380. if (mUVCCameraInfo == null) return;
  381. if (screenIdentification == null)
  382. {
  383. screenIdentification = new o0.Project.ScreenIdentification(this);
  384. Debug.Log("[ScreenLocate] 初始化屏幕识别");
  385. //screenIdentification.OnLocateScreenEnter += OnLocateScreenEnter;
  386. screenIdentification.OnLocateScreenEnd += OnLocateScreenEnd;
  387. }
  388. if (infraredLocate == null)
  389. {
  390. infraredLocate = new InfraredLocate(this, screenIdentification.Screen);
  391. cameraSize = new Vector(getUVCCameraInfoSize.x, getUVCCameraInfoSize.y);
  392. InfraredSpot.RefreshMinVerifyLength(new o0.Geometry2D.Float.Vector(getUVCCameraInfoSize.x, getUVCCameraInfoSize.y));
  393. Debug.Log($"[ScreenLocate] 初始化红外灯识别, 当前相机分辨率: {CameraSize.x}×{CameraSize.y},红外算法追踪距离: {InfraredSpot.MinVerifyLength}");
  394. //InfraredDemo 初始化
  395. //float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
  396. //Debug.Log("Init Red filterValue:" + redfilterValue);
  397. //infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  398. }
  399. /* New*/
  400. //Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
  401. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing)
  402. {
  403. //if (bAutomaticRecognition)
  404. //{
  405. // //识别的过程使用的分辨率
  406. // //CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
  407. // if (log1)
  408. // {
  409. // log1 = false;
  410. // Debug.Log("[ScreenLocate] log1:[" + (int)getUVCCameraInfoSize.x + ", " + (int)getUVCCameraInfoSize.y + "]");
  411. // }
  412. //}
  413. //else
  414. //{
  415. // //自动识别完成后使用相机分辨率大小 getUVCCameraInfoSize
  416. // //CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
  417. // if (log2)
  418. // {
  419. // log2 = false;
  420. // Debug.Log("[ScreenLocate] log2:[" + (int)getUVCCameraInfoSize.x + ", " + (int)getUVCCameraInfoSize.y + "]");
  421. // }
  422. //}
  423. //如果是连接了蓝牙设备,并且不是9轴设备。不进行识别算法处理
  424. if (BluetoothAim.ins?.status == BluetoothStatusEnum.ConnectSuccess && AimHandler.ins && AimHandler.ins.bRuning9Axis()) return;
  425. //根据getUVCCameraInfoSize 分辨率渲染
  426. CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
  427. if (!screenIdentification.Update(mUVCTexture2D))
  428. {
  429. // 同步分辨率, 分辨率变化后还需同步到InfraredDemo
  430. if (RefreshCameraSize())
  431. {
  432. if (screenIdentification.Screen.QuadInCamera != null)
  433. {
  434. quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
  435. if (!ContainsNaN(quadUnityVectorList))
  436. {
  437. SaveScreenLocateVectorList();
  438. //SyncInfraredDemo();
  439. //SyncInfraredScreenPositioningView();
  440. InfraredCameraHelper?.InvokeOnUVCPosUpdate(quadUnityVectorList);
  441. Debug.Log("[ScreenLocate] RefreshCameraSize 屏幕size改变:[" + (int)getUVCCameraInfoSize.x + "," + (int)getUVCCameraInfoSize.y + "]");
  442. Debug.Log("[ScreenLocate] RefreshCameraSize 屏幕size改变,刷新quadUnityVectorList:" + PrintVector2List(quadUnityVectorList));
  443. }
  444. else
  445. {
  446. Debug.LogError("[ScreenLocate] RefreshCameraSize 屏幕size改变,存在NaN值,重新校准:" + PrintVector2List(quadUnityVectorList));
  447. }
  448. }
  449. if (DebugOnZIMDemo)
  450. Main.ShowScreen(screenIdentification.Screen.QuadInCamera);
  451. }
  452. if (CameraSize.x != mUVCTexture2D.width || CameraSize.y != mUVCTexture2D.height)
  453. {
  454. Debug.Log($"<color=red>[ScreenLocate] 分辨率不匹配,相机分辨率为: {getUVCCameraInfoSize}, mUVCTexture2D纹理尺寸: {mUVCTexture2D.width}×{mUVCTexture2D.height}</color>");
  455. return;
  456. }
  457. // 获取像素,用于后续操作
  458. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  459. AutoLightPixels(pixels, CameraWidth, CameraHeight);
  460. InfraredUpdate(pixels);
  461. if (mode == Mode.ScreenLocateManual)
  462. {
  463. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  464. {
  465. if (infraredSpotBuffer[i].CameraLocation != null)
  466. {
  467. // 检测到光点
  468. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(CameraSize, FullScreenImage.rectTransform.rect);
  469. CrosshairInCamera[i].gameObject.SetActive(true);
  470. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  471. }
  472. else
  473. CrosshairInCamera[i].gameObject.SetActive(false);
  474. }
  475. }
  476. else if(mode == Mode.InfraredLocate)
  477. {
  478. if (mPlatform == Platform.Window) //渲染ui上面的点。进入游戏可以隐藏
  479. {
  480. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  481. {
  482. if (infraredSpotBuffer[i].CameraLocation != null)
  483. {
  484. // 检测到光点
  485. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(CameraSize, outputRawImages[0].rectTransform.rect);
  486. CrosshairInCamera[i].gameObject.SetActive(true);
  487. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  488. }
  489. else
  490. CrosshairInCamera[i].gameObject.SetActive(false);
  491. }
  492. }
  493. //手机端使用 mPlatform == Platform.Android &&
  494. //通用,手机 和 PC
  495. if (infraredSpotBuffer.Length > 0)
  496. {
  497. int redIndex = 0;
  498. int greenIndex = 1;
  499. //仅仅第一个点显示(如果最大点出界了会闪烁)
  500. if (bSinglePoint)
  501. {
  502. redIndex = 0; //单点识别是,可以选择切换颜色
  503. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  504. {
  505. string str = "Single:";
  506. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  507. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  508. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  509. }
  510. }
  511. else
  512. {
  513. //雙點模式下選擇第一個點
  514. if (bIdentifyRed && !bIdentifyGreen)
  515. {
  516. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  517. {
  518. Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  519. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  520. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  521. }
  522. else
  523. {
  524. Info.text = "未检测到红色最大点!";
  525. }
  526. }
  527. else if (!bIdentifyRed && bIdentifyGreen)
  528. {
  529. if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  530. {
  531. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  532. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  533. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  534. }
  535. else
  536. {
  537. Info.text = "未检测到绿色点!";
  538. }
  539. }
  540. else
  541. {
  542. //两个不选择和两个全选都跑识别两个点
  543. //自動切換 检测到光点
  544. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  545. {
  546. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  547. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  548. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  549. }
  550. else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  551. {
  552. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  553. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  554. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  555. }
  556. else
  557. {
  558. Info.text = "未检测到点!";
  559. }
  560. }
  561. }
  562. }
  563. }
  564. else if (mode == Mode.ScreenMap && DebugOnZIMDemo)
  565. {
  566. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  567. {
  568. if (infraredSpotBuffer[i].ScreenUV != null)
  569. {
  570. // 检测到光点
  571. var posInCanvas = infraredSpotBuffer[i].ScreenUV.Value.pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
  572. CrosshairInScreen[i].gameObject.SetActive(true);
  573. CrosshairInScreen[i].anchoredPosition = posInCanvas;
  574. }
  575. else
  576. CrosshairInScreen[i].gameObject.SetActive(false);
  577. }
  578. if (Input.GetKeyDown(KeyCode.Escape))
  579. ToMode(Mode.InfraredLocate);
  580. }
  581. }
  582. }
  583. //var t1 = Time.realtimeSinceStartup;
  584. //var dt = t1 - t0;
  585. //m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  586. //++m_ValidHistoryFrames;
  587. //m_UIUpdateTimer += Time.deltaTime;
  588. //if (m_UIUpdateTimer >= m_UIUpdateInterval)
  589. //{
  590. // m_UIUpdateTimer = 0.0f;
  591. // if (m_ValidHistoryFrames >= m_History.Count)
  592. // {
  593. // m_ValidHistoryFrames = 0;
  594. // m_AverageTime = 0.0f;
  595. // m_MinTime = float.PositiveInfinity;
  596. // m_MaxTime = float.NegativeInfinity;
  597. // {
  598. // for (var i = 0; i < m_History.Count; i++)
  599. // {
  600. // var time = m_History[i];
  601. // m_AverageTime += time;
  602. // m_MinTime = Mathf.Min(m_MinTime, time);
  603. // m_MaxTime = Mathf.Max(m_MaxTime, time);
  604. // }
  605. // m_AverageTime /= m_History.Count;
  606. // }
  607. // {
  608. // m_History.Sort();
  609. // // Odd-length history?
  610. // if ((m_History.Count & 1) != 0)
  611. // {
  612. // m_MedianTime = m_History[m_History.Count / 2];
  613. // }
  614. // else
  615. // {
  616. // m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  617. // }
  618. // }
  619. // }
  620. // var statistics = $"{m_History.Count} 帧样本:\naverage: {m_AverageTime * 1000.0f:F2}ms\nmedian: {m_MedianTime * 1000.0f:F2}ms\nmin: {m_MinTime * 1000.0f:F2}ms\nmax: {m_MaxTime * 1000.0f:F2}ms\n";
  621. // //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  622. // if (m_UITime != null)
  623. // m_UITime.text = $"Cam: {mUVCCameraInfo.CurrentWidth}x{mUVCCameraInfo.CurrentHeight}{(mUVCTexture2D? ",T2D:" : "")}{(mUVCTexture2D? mUVCTexture2D.width+ "x" : "")}{(mUVCTexture2D ? mUVCTexture2D.height:"")} \nLast Frame: {dt * 1000.0f:F2}ms \n{statistics}";
  624. //}
  625. //UpdateInputs();
  626. if (DebugOnZIMDemo)
  627. {
  628. if (Input.GetKeyDown(KeyCode.Z))
  629. SelectScreenAfterLocate(ScreenIdentificationTag.Manual);
  630. if (Input.GetKeyDown(KeyCode.X))
  631. SelectScreenAfterLocate(ScreenIdentificationTag.SemiAuto);
  632. if (Input.GetKeyDown(KeyCode.C))
  633. SelectScreenAfterLocate(ScreenIdentificationTag.Auto);
  634. }
  635. }
  636. public void InfraredUpdate(Color[] cameraPixels)
  637. {
  638. infraredLocate.InfraredUpdate(cameraPixels);
  639. if (DebugOnZIMDemo)
  640. DebugPixelSpotArea(infraredLocate.DebugAreas);
  641. }
  642. private bool RefreshCameraSize()
  643. {
  644. var sizeNew = new Vector(getUVCCameraInfoSize.x, getUVCCameraInfoSize.y);
  645. var sizeNewFloat = getUVCCameraInfoSize.o0Vector();
  646. if (sizeNew != CameraSize || (screenIdentification?.Screen?.QuadInCamera != null && sizeNewFloat != screenIdentification.Screen.QuadInCamera.CameraSize))
  647. {
  648. Debug.Log($"<color=aqua>[ScreenLocate] 分辨率变化,刷新分辨率(from {CameraSize.x}×{CameraSize.y} to {sizeNew.x}×{sizeNew.y}), 是否有屏幕数据: {screenIdentification.Screen.QuadInCamera != null}, 是否有手动数据: {screenIdentification.QuadManual != null}</color>");
  649. // 同步相机分辨率
  650. cameraSize = sizeNew;
  651. screenIdentification.Screen.RefreshCameraSize(sizeNewFloat);
  652. screenIdentification.QuadAuto?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
  653. screenIdentification.QuadManual?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
  654. screenIdentification.QuadSemiAuto?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
  655. InfraredSpot.RefreshMinVerifyLength(sizeNewFloat);
  656. return true;
  657. }
  658. return false;
  659. }
  660. Vector2 targetPos = Vector2.zero;
  661. Vector2 movePos = Vector2.zero;
  662. int moveSpeed = 20;
  663. public float filterDis = 3.0f;
  664. void onFilterPos(Vector2 _vector2Pos)
  665. {
  666. //主要用于模拟九轴时候的
  667. //添加一个偏移量,使得最后输出的准心是指向正中心
  668. Vector2 np = new Vector2((_vector2Pos.x - UVOffset.x) * Screen.width, (_vector2Pos.y - UVOffset.y) * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
  669. if (Vector2.Distance(np, targetPos) >= filterDis)
  670. {
  671. targetPos = np;
  672. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
  673. //Vector2 np = new Vector2(uvCenterOffset.x * Screen.width, uvCenterOffset.y * Screen.height);
  674. //point -= np;
  675. InfraredCameraHelper?.InvokeOnPositionUpdate(targetPos);
  676. }
  677. //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
  678. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
  679. }
  680. Vector2[] _targetPoints2 = new Vector2[] { Vector2.zero, Vector2.zero };
  681. void onFilterPos2(Vector2 _vector2Pos, int index)
  682. {
  683. Vector2 np = new Vector2((_vector2Pos.x - UVOffset.x) * Screen.width, (_vector2Pos.y - UVOffset.y) * Screen.height);
  684. if (Vector2.Distance(np, _targetPoints2[index]) >= filterDis)
  685. {
  686. _targetPoints2[index] = np;
  687. InfraredCameraHelper.InvokeOnPositionUpdate2(_targetPoints2[index], index);
  688. }
  689. }
  690. public void DebugPixelSpotArea(List<PixelSpotArea> areas)
  691. {
  692. if (areas!=null)
  693. {
  694. Info.transform.GetChild(0).GetComponent<Text>().text = $"areas.Count: {areas.Count}";
  695. PixelSpotArea a0 = null; // 表示最大半径的区域
  696. PixelSpotArea a1 = null; // 表示第二大半径的区域
  697. foreach (var a in areas)
  698. {
  699. if (a0 == null || a.Radius > a0.Radius)
  700. {
  701. a1 = a0; // 更新第二大为之前最大
  702. a0 = a; // 更新最大为当前的
  703. }
  704. else if (a1 == null || a.Radius > a1.Radius)
  705. {
  706. a1 = a; // 更新第二大
  707. }
  708. }
  709. Texture2D texture = new Texture2D(CameraWidth, CameraHeight);
  710. Color[] blackPixels = new Color[texture.width * texture.height];
  711. for (int i = 0; i < blackPixels.Length; i++)
  712. blackPixels[i] = Color.black;
  713. texture.SetPixels(blackPixels);
  714. if (a0 != null)
  715. {
  716. foreach (var p in a0.Pixels0)
  717. texture.SetPixel((int)p.x, (int)p.y, Color.yellow);
  718. foreach (var p in a0.Pixels1)
  719. texture.SetPixel((int)p.x, (int)p.y, Color.white);
  720. }
  721. if (a1 != null)
  722. {
  723. foreach (var p in a1.Pixels0)
  724. texture.SetPixel((int)p.x, (int)p.y, Color.green);
  725. foreach (var p in a1.Pixels1)
  726. texture.SetPixel((int)p.x, (int)p.y, Color.blue);
  727. }
  728. texture.Apply();
  729. DebugTexture(6, texture);
  730. }
  731. }
  732. #region 自动识别
  733. int Capture = 30;
  734. int Delay = 30;
  735. Vector2 EnterResolution;
  736. // int DefaultResolutionIndex;
  737. // readonly public int HighScreenLocateResolutionIndex = 2; // 自动识别时,摄像机分辨率固定为1280 * 720 ( 对应索引是2 )
  738. public void BtnScreenLocate()
  739. {
  740. if (DebugScreenImages.Count != 0)
  741. {
  742. //screenIdentification = new o0.Project.ScreenIdentification();
  743. cameraSize = new Vector(DebugScreenImages[0].width, DebugScreenImages[0].height);
  744. WebCamIsReady(DebugScreenImages[0]);
  745. CreateUVCTexture2DIfNeeded();
  746. }
  747. //Debug.Log("BtnScreenLocate Capture:" + Capture + " ,Delay: " + Delay);
  748. //screenIdentification.LocateScreen(Capture, Delay);
  749. OnLocateScreenEnter();
  750. }
  751. // bool log1 = false, log2 = false;
  752. public void OnLocateScreenEnter()
  753. {
  754. bAutomaticRecognition = true;
  755. bAutomaticRecognitionStart = true;
  756. ResetScreenIdentification();
  757. //DefaultResolutionIndex = InfraredDemoMain?.ResolutionIndex ?? 0; // 记录一下进入前的分辨率(游戏场景的分辨率,比识别时更低)
  758. //HighScreenLocateResolutionIndex = InfraredDemoMain.getTextureToResolutionNewIndex(); // index = 0
  759. // Debug.Log("[ScreenLocate] 开始捕获 DefaultResolutionIndex:" + DefaultResolutionIndex + " ,HighScreenLocateResolutionIndex:" + HighScreenLocateResolutionIndex);
  760. // InfraredDemoMain?.SetResolutionNew(HighScreenLocateResolutionIndex);
  761. EnterResolution = mUVCCameraInfo.Size;// 记录一下进入前的分辨率(游戏场景的分辨率,比识别时更低)
  762. Vector2 _HighResolution = mUVCCameraInfo.CurrentCalibrationResolution; //最高的分辨率
  763. Resize((int)_HighResolution.x, (int)_HighResolution.y);
  764. //CreateUVCTexture2DIfNeeded();
  765. // log1 = true;
  766. // log2 = true;
  767. screenIdentification.LocateScreen(); // 自动识别开始的入口
  768. if (DebugOnZIMDemo)
  769. {
  770. var webCam = GetComponent<ZIMWebCamera>();
  771. webCam.AdjustResolution(1920, 1080);
  772. mUVCCameraInfo.SetSize(webCam.webCamTexture.width, webCam.webCamTexture.height);
  773. }
  774. }
  775. public void OnLocateScreenEnd()
  776. {
  777. bAutomaticRecognitionEnd = true;
  778. // 记录本次屏幕识别的分辨率(目前采用高分辨率做识别,识别结束后调回低分辨率)
  779. //InfraredDemoMain?.SetResolutionNew(DefaultResolutionIndex);
  780. Resize((int)EnterResolution.x, (int)EnterResolution.y);
  781. if (DebugOnZIMDemo)
  782. {
  783. var webCam = GetComponent<ZIMWebCamera>();
  784. GetComponent<ZIMWebCamera>().AdjustResolution((int)EnterResolution.x, (int)EnterResolution.y);
  785. mUVCCameraInfo.SetSize(webCam.webCamTexture.width, webCam.webCamTexture.height);
  786. }
  787. }
  788. /**
  789. * 修改相机的实际分辨率
  790. */
  791. public void Resize(int width, int height)
  792. {
  793. if (mUVCCameraInfo == null) return;
  794. #if UNITY_ANDROID
  795. //发送修改指令给相机实际分辨率
  796. mUVCCameraInfo.SetCameraSize(width, height);
  797. #endif
  798. #if UNITY_STANDALONE_WIN
  799. // pc todo 看看怎么处理
  800. // ResizePC(width, height);
  801. #endif
  802. //mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
  803. Debug.Log($"[ScreenLocate] 开始修改分辨率 mUVCCameraInfo origin:[{mUVCCameraInfo.CurrentWidth},{mUVCCameraInfo.CurrentHeight}]=>target:[{width},{height}]");
  804. // if (screenIdentification.isInitLocateScreen()) screenIdentification.bStartLocateScreen = true;
  805. }
  806. /// <summary>
  807. /// pc修改分辨率
  808. /// </summary>
  809. /// <param name="width"></param>
  810. /// <param name="height"></param>
  811. public void ResizePC(int width, int height)
  812. {
  813. if (mUVCCameraInfo == null) return;
  814. //if (screenIdentification.isInitLocateScreen()) screenIdentification.bStartLocateScreen = true;
  815. // PcWebCamera pcWebCamera = GetComponent<PcWebCamera>();
  816. // if(pcWebCamera.webCamTexture == null || !pcWebCamera.webCamTexture.isPlaying) return;
  817. //StartCoroutine(ResetWebCam(pcWebCamera, width, height));
  818. mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
  819. Debug.Log("[ScreenLocate] Resize mUVCCameraInfo.SetSize: [" + mUVCCameraInfo.CurrentWidth + "," + mUVCCameraInfo.CurrentHeight + "]");
  820. }
  821. private System.Collections.IEnumerator ResetWebCam(PcWebCamera pcWebCamera, int newWidth, int newHeight)
  822. {
  823. WebCamTexture _webCamTexture = pcWebCamera.webCamTexture;
  824. // Stop the current WebCamTexture
  825. _webCamTexture.Stop();
  826. // Trigger OnWebCamStopped event
  827. // OnWebCamStopped?.Invoke();
  828. // Wait for a short time to ensure resources are released
  829. yield return new WaitForSeconds(0.5f);
  830. // Create a new WebCamTexture with the new dimensions
  831. _webCamTexture = new WebCamTexture(newWidth, newHeight);
  832. pcWebCamera.webCamTexture = _webCamTexture;
  833. mUVCTexture = _webCamTexture;
  834. // Restart the camera
  835. yield return StartCoroutine(StartWebCam(pcWebCamera));
  836. }
  837. private System.Collections.IEnumerator StartWebCam(PcWebCamera pcWebCamera)
  838. {
  839. WebCamTexture _webCamTexture = pcWebCamera.webCamTexture;
  840. _webCamTexture.Play();
  841. // Wait until the WebCamTexture is playing
  842. while (!_webCamTexture.isPlaying)
  843. {
  844. yield return null;
  845. }
  846. // Trigger OnWebCamStarted event
  847. //OnWebCamStarted?.Invoke();
  848. mUVCCameraInfo.SetSize(_webCamTexture.width, _webCamTexture.height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
  849. Debug.Log("[ScreenLocate] ResizePc mUVCCameraInfo.SetSize: [" + mUVCCameraInfo.CurrentWidth + "," + mUVCCameraInfo.CurrentHeight + "]");
  850. // if(screenIdentification.isInitLocateScreen())screenIdentification.bStartLocateScreen = true;
  851. }
  852. #endregion
  853. public void BtnScreenMap()
  854. {
  855. ToMode(Mode.ScreenMap);
  856. }
  857. //进入手动定位屏幕
  858. public void BtnScreenLocateManual()
  859. {
  860. ToMode(Mode.ScreenLocateManual);
  861. }
  862. // 重置屏幕识别的数据
  863. public void ResetScreenIdentification()
  864. {
  865. screenIdentification.Screen.Active = false;
  866. }
  867. // threshold 的值是0-1,0代表最近,1代表最远
  868. public void SetReDoLocateCalibrationRatio(float threshold)
  869. {
  870. const float MIN = 0.02f;
  871. const float MAX = 0.32f;
  872. ReDoLocateCalibrationRatio = MIN + (MAX - MIN) * threshold;
  873. }
  874. /// <summary>
  875. /// 固定的顶点顺序: 左下,右下,左上,右上
  876. /// </summary>
  877. public static List<Vector2> quadUnityVectorList = new();
  878. /// <summary>
  879. /// 打印信息
  880. /// </summary>
  881. /// <param name="list">左下,右下,左上,右上</param>
  882. /// <returns></returns>
  883. public string PrintVector2List(List<Vector2> list)
  884. {
  885. if (screenIdentification == null || !screenIdentification.Screen.Active) return "[]";
  886. string result = "";
  887. if (list.Count == 4)
  888. {
  889. result = "左下" + list[0].ToString() + ",右下" + list[1].ToString() + ",左上" + list[2].ToString() + ",右上" + list[3].ToString();
  890. }
  891. else
  892. {
  893. result = "count != 4 error";
  894. }
  895. //foreach (Vector2 vector in list)
  896. //{
  897. // result += vector.ToString() + " ";
  898. //}
  899. //Debug.Log(result);
  900. return result;
  901. }
  902. /// <summary>
  903. /// 判断是否存在NaN
  904. /// </summary>
  905. /// <param name="vectors"></param>
  906. /// <returns></returns>
  907. public bool ContainsNaN(List<Vector2> vectors)
  908. {
  909. foreach (var v in vectors)
  910. {
  911. if (float.IsNaN(v.x) || float.IsNaN(v.y))
  912. {
  913. return true;
  914. }
  915. }
  916. return false;
  917. }
  918. // 标记屏幕的四个角, ScreenQuadObject 下挂了4个子节点用于标记
  919. public void ShowScreen(RectTransform ScreenQuadObject, QuadrilateralInCamera screen)
  920. {
  921. if (screen == null)
  922. {
  923. Info.text = "识别屏幕失败";
  924. return;
  925. }
  926. Info.text = "已识别到屏幕";
  927. //if (ScreenQuadObject && ScreenQuadObject.childCount >= 4)
  928. //{
  929. // ScreenQuadObject.gameObject.SetActive(true);
  930. // for (int i = 0; i < 4; i++)
  931. // {
  932. // if (DebugOnZIMDemo)
  933. // {
  934. // RectTransform t = ScreenQuadObject.GetChild(i) as RectTransform;
  935. // t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, ScreenQuadObject.rect);
  936. // }
  937. // }
  938. //}
  939. quadUnityVectorList = screen.GetUnityVertexNormalizedList(); // 记录四个点
  940. if (!ContainsNaN(quadUnityVectorList))
  941. {
  942. SaveScreenLocateVectorList();
  943. //SyncInfraredDemo();
  944. if (DebugOnZIMDemo)
  945. SyncInfraredScreenPositioningView();
  946. InfraredCameraHelper?.InvokeOnUVCPosUpdate(quadUnityVectorList);
  947. Debug.Log("[ScreenLocate] ShowScreen 已识别到屏幕,更新quadUnityVectorList:" + PrintVector2List(quadUnityVectorList));
  948. }
  949. else
  950. {
  951. Debug.LogError("[ScreenLocate] RefreshCameraSize 屏幕size改变,存在NaN值,重新校准:" + PrintVector2List(quadUnityVectorList));
  952. }
  953. }
  954. public void ShowScreen(QuadrilateralInCamera screen) => ShowScreen(ScreenQuad, screen);
  955. /// <summary>
  956. /// 校准点位置存储到本地
  957. /// </summary>
  958. static public void SaveScreenLocateVectorList()
  959. {
  960. string saveStr = string.Join(";", quadUnityVectorList.Select(v => $"{v.x},{v.y}")); //,{v.z}
  961. Debug.Log("SaveScreenLocateVectorList: " + saveStr);
  962. PlayerPrefs.SetString("ScreenLocateVectorList", saveStr);
  963. }
  964. /// <summary>
  965. /// 获取本地存储校准点位置
  966. /// </summary>
  967. static public bool GetScreenLocateVectorList()
  968. {
  969. string posListStr = PlayerPrefs.GetString("ScreenLocateVectorList", "");
  970. Debug.Log("GetScreenLocateVectorList:" + posListStr);
  971. if (!string.IsNullOrWhiteSpace(posListStr))
  972. {
  973. quadUnityVectorList.Clear();
  974. quadUnityVectorList = posListStr.Split(';')
  975. .Select(s =>
  976. {
  977. string[] parts = s.Split(',');
  978. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  979. })
  980. .ToList();
  981. return true;
  982. }
  983. else return false;
  984. }
  985. public void SetSinglePoint(bool value)=> infraredLocate.SetSinglePoint(value);
  986. public bool IsSinglePoint() => infraredLocate.IsSinglePoint();
  987. public Vector2 AdjustPointsOffset(Vector2 inputPoint, string type = "CameraLocation") => infraredLocate.GetCenterOffset(inputPoint, type);
  988. /// <summary>
  989. /// 重置偏移量
  990. /// </summary>
  991. public void ResetPointsOffest() => infraredLocate.ResetCenterOffset();
  992. /// <summary>
  993. /// 这里计算一个偏移后的cameraLocatoin位置
  994. /// </summary>
  995. /// <param name="cameraLocatoin"></param>
  996. /// <returns></returns>
  997. public Vector2 GetOffsetCameraLocation(Vector2 cameraLocatoin)
  998. {
  999. return cameraLocatoin - CameraLocationOffset;
  1000. }
  1001. void ToMode(Mode mode)
  1002. {
  1003. if (this.mode == mode)
  1004. return;
  1005. if (mode == Mode.ScreenMap)
  1006. {
  1007. if (!screenIdentification.Screen.Active)
  1008. {
  1009. Info.text = "先定位屏幕";
  1010. return;
  1011. }
  1012. Info.text = "按ESC退出";
  1013. SetScreen(Color.black);
  1014. //Info.transform.SetAsLastSibling();
  1015. this.mode = Mode.ScreenMap;
  1016. }
  1017. else if (mode == Mode.InfraredLocate)
  1018. {
  1019. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  1020. //Info.text = "已识别到屏幕";
  1021. SetScreen(null);
  1022. foreach (var i in CrosshairInScreen)
  1023. i.gameObject.SetActive(false);
  1024. FullScreenImage.gameObject.SetActive(false);
  1025. ScreenPixelCheaker?.HideImage();
  1026. //Info.transform.SetSiblingIndex(transform.childCount - 4);
  1027. this.mode = Mode.InfraredLocate;
  1028. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1029. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{screenIdentification.Screen.Active}");
  1030. #endif
  1031. }
  1032. else if (mode == Mode.ScreenLocateManual)
  1033. {
  1034. Info.text = "左键单击屏幕 左下角";
  1035. FullScreenImage.gameObject.SetActive(true);
  1036. ScreenPixelCheaker?.ShowImage();
  1037. //Info.transform.SetSiblingIndex(transform.childCount - 1);
  1038. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  1039. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  1040. CreateUVCTexture2DIfNeeded();
  1041. DebugTexture(7, mUVCTexture2D.zimAutoLight(brightness));
  1042. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  1043. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  1044. this.mode = Mode.ScreenLocateManual;
  1045. }
  1046. }
  1047. private Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
  1048. {
  1049. if (width == 0)
  1050. width = texture.width;
  1051. if (height == 0)
  1052. height = texture.height;
  1053. Texture2D _texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
  1054. RenderTexture currentRT = RenderTexture.active;
  1055. RenderTexture renderTexture = RenderTexture.GetTemporary(
  1056. width,
  1057. height,
  1058. 0,
  1059. RenderTextureFormat.ARGB32,
  1060. RenderTextureReadWrite.Linear);
  1061. Graphics.Blit(texture, renderTexture);
  1062. RenderTexture.active = renderTexture;
  1063. _texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  1064. _texture2D.Apply();
  1065. RenderTexture.active = currentRT;
  1066. RenderTexture.ReleaseTemporary(renderTexture);
  1067. return _texture2D;
  1068. }
  1069. //public void CreateUVCTexture2DFocusSizeIfNeeded(int width, int height)
  1070. //{
  1071. // if (mUVCTexture2D != null)
  1072. // Destroy(mUVCTexture2D);
  1073. // mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
  1074. //}
  1075. /// <summary>
  1076. /// 使用默认的mUVCTexture宽高
  1077. /// </summary>
  1078. private void CreateUVCTexture2DIfNeeded()
  1079. {
  1080. if (mUVCTexture2D != null)
  1081. Destroy(mUVCTexture2D);
  1082. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  1083. }
  1084. /// <summary>
  1085. /// 根据宽高调整mUVCTexture2D
  1086. /// </summary>
  1087. /// <param name="width"></param>
  1088. /// <param name="height"></param>
  1089. private void CreateUVCTexture2DIfNeeded(int width = 0, int height = 0)
  1090. {
  1091. if (mUVCTexture2D != null)
  1092. Destroy(mUVCTexture2D);
  1093. mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
  1094. }
  1095. #region DoubleButton
  1096. private DateTime m_firstTime;
  1097. private DateTime m_secondTime;
  1098. private void Press()
  1099. {
  1100. Debug.Log("进入手动定位");
  1101. BtnScreenLocateManual();
  1102. resetTime();
  1103. }
  1104. public void OnDoubleClick()
  1105. {
  1106. //超时重置
  1107. if (!m_firstTime.Equals(default(DateTime)))
  1108. {
  1109. var intervalTime = DateTime.Now - m_firstTime;
  1110. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  1111. if (milliSeconds >= 400)
  1112. resetTime();
  1113. }
  1114. // 按下按钮时对两次的时间进行记录
  1115. if (m_firstTime.Equals(default(DateTime)))
  1116. m_firstTime = DateTime.Now;
  1117. else
  1118. m_secondTime = DateTime.Now;
  1119. // 在第二次点击触发,时差小于400ms触发
  1120. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  1121. {
  1122. var intervalTime = m_secondTime - m_firstTime;
  1123. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  1124. if (milliSeconds < 400)
  1125. Press();
  1126. else
  1127. resetTime();
  1128. }
  1129. }
  1130. private void resetTime()
  1131. {
  1132. m_firstTime = default(DateTime);
  1133. m_secondTime = default(DateTime);
  1134. }
  1135. #endregion
  1136. #region 性能检测相关
  1137. void InvalidateTimings()
  1138. {
  1139. m_ValidHistoryFrames = 0;
  1140. m_AverageTime = float.NaN;
  1141. m_MedianTime = float.NaN;
  1142. m_MinTime = float.NaN;
  1143. m_MaxTime = float.NaN;
  1144. }
  1145. void UpdateInputs()
  1146. {
  1147. //重置
  1148. if (Input.GetKeyDown(KeyCode.UpArrow))
  1149. {
  1150. InvalidateTimings();
  1151. }
  1152. }
  1153. #endregion
  1154. }