ScreenLocate.cs 61 KB

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