ScreenLocate.cs 51 KB

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