ScreenLocate.cs 43 KB

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