ScreenLocate.cs 49 KB

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