ScreenLocate.cs 51 KB

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