| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- #define ENABLE_LOG
- using InfraredManager;
- using o0;
- using Serenegiant.UVC;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.UI;
- using ZIM;
- using ZIM.Unity;
- using static Serenegiant.UVC.UVCManager;
- using Color = UnityEngine.Color;
- [RequireComponent(typeof(Canvas))]
- public partial class ScreenLocate : MonoBehaviour
- {
- public InfraredCameraHelper InfraredCameraHelper;
- private const string TAG = "ScreenLocate#";
- enum Mode
- {
- InfraredLocate,
- ScreenMap,
- ScreenLocateManual
- }
- enum InfraredCount
- {
- Single,
- Double
- }
- enum Platform
- {
- Window,
- Android
- }
- Platform mPlatform = Platform.Android;
- // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
- public InfraredSpot[] InfraredSpots
- {
- get
- {
- infraredCount = InfraredCount.Double;
- return infraredSpotBuffer;
- }
- }
- // 1个灯, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
- public InfraredSpot InfraredSpotSingle
- {
- get
- {
- infraredCount = InfraredCount.Single;
- return infraredSpotBuffer[0];
- }
- }
- public InfraredSpot[] infraredSpotBuffer;
- InfraredCount infraredCount;
- public string GetInfraredCount() { return infraredCount.ToString(); }
- public InfraredDemo InfraredDemoMain => FindObjectOfType<InfraredDemo>();
- #region UVC 处理的对象
- public UVCManager mUVCManager;
- public UVCDrawer mUVCDrawer;
- public CameraInfo mUVCCameraInfo;
- public bool getUVCCameraInfo => mUVCCameraInfo != null ? true : false;
- public Vector2 getUVCCameraInfoSize => getUVCCameraInfo ? mUVCCameraInfo.Size : new Vector2(320, 240);
- private Texture mUVCTexture;
- public Texture getUVCTexture => mUVCTexture;
- private Texture2D mUVCTexture2D;
- // [SerializeField] Texture2DArray mUVCOutArray;
- #endregion
- public Text Info;
- public List<RectTransform> CrosshairInCamera;
- public List<RectTransform> CrosshairInScreen;
- public RectTransform ScreenQuad;
- public Toggle SaveToggle;
- public Vector2 ScreenLocateCameraSize; // 屏幕识别需要的目标分辨率,摄像机分辨率变化时该分辨率也会跟着调整
- public bool ShowScreenQuad = false;
- public RawImage rawImage;
- public RawImage rawImage1;
- public RawImage rawImage2;
- public RawImage rawImage3;
- public RawImage rawImage4;
- public RawImage rawImage5;
- public RawImage FullScreenImage;
- public InfraredSpotSettings InfraredSpotSettings;
- //public ZIMWebCamera zimWebCamera => GetComponent<ZIMWebCamera>();
- public Texture2D DebugScreenImage;
- public bool DebugOnEditorWin = false;
- // private SynchronizationContext mainContext;
- //是否单点显示
- public bool bSinglePoint = true;//默认单点识别
- bool bIdentifyRed = true;//默认设备红色
- bool bIdentifyGreen = true;
- #region 性能检测相关
- public Text m_UITime;
- const float m_UIUpdateInterval = 0.1f;
- float m_UIUpdateTimer = 0.0f;
- List<float> m_History = new List<float>(100);
- int m_ValidHistoryFrames = 0;
- float m_AverageTime = float.NaN;
- float m_MedianTime = float.NaN;
- float m_MinTime = float.NaN;
- float m_MaxTime = float.NaN;
- public float updateInterval = 0.5F;
- private double lastInterval;
- private int frames = 0;
- private float fps;
- public Text m_FPS;
- #endregion
- InfraredLocate infraredLocate;
- RectTransform canvas;
- Mode mode;
- List<Vector2> pointManual = new List<Vector2>();
- //o0.Project.WebCam o0WebCam = null;
- o0.Project.ScreenIdentification screenIdentification;
- public o0.Project.ScreenIdentification getScreenIdentification => screenIdentification;
- /// <summary>
- /// 正在识别的状态,自动识别时候记录
- /// </summary>
- bool bAutomaticRecognition = false;
- static public ScreenLocate Main;
- static public List<RawImage> DebugImage = new List<RawImage>();
- static public RectTransform BackQuad = null;
- static public void DebugTexture(int index, Texture texture)
- {
- Destroy(DebugImage[index].texture);
- DebugImage[index].texture = texture;
- }
- static public void SetScreen(UnityEngine.Color? color = null)
- {
- if (BackQuad == null)
- {
- var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
- var background = canvas.Find("Background");
- BackQuad = background.GetChild(0).GetComponent<RectTransform>();
- }
- BackQuad.parent.gameObject.SetActive(color != null);
- BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
- //Debug.Log("Set Screen " + color.GetColorName());
- }
- static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
- {
- if (BackQuad == null)
- {
- var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
- var background = canvas.Find("Background");
- BackQuad = background.GetChild(0).GetComponent<RectTransform>();
- }
- BackQuad.parent.gameObject.SetActive(color != null);
- BackQuad.anchorMin = rect.min;
- BackQuad.anchorMax = rect.max;
- BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
- //Debug.Log("Set Screen " + color.GetColorName());
- }
- static void DebugBackQuad(Rect? rect = null)
- {
- if (BackQuad)
- {
- BackQuad.parent.GetComponent<RawImage>().enabled = false;
- BackQuad.GetComponent<RawImage>().color = Color.white;
- BackQuad.parent.gameObject.SetActive(!BackQuad.parent.gameObject.activeSelf);
- if (rect.HasValue)
- {
- BackQuad.anchorMin = rect.Value.min;
- BackQuad.anchorMax = rect.Value.max;
- }
- }
- }
- public void ReSizeTexture(int width, int height)
- {
- Debug.Log("Cur mUVCTexture Size: [" + mUVCTexture.width + "," + mUVCTexture.height + "]");
- return;
- if (mUVCTexture.width < width || mUVCTexture.height < height) // 如果当前分辨率太小,则重新new一个texture
- {
- Texture2D tex = new Texture2D(
- width, height,
- TextureFormat.ARGB32,
- false, /* mipmap */
- true /* linear */);
- tex.filterMode = FilterMode.Point;
- tex.Apply();
- mUVCTexture = tex;
- mUVCCameraInfo.previewTexture = tex;
- var nativeTexPtr = mUVCCameraInfo.previewTexture.GetNativeTexturePtr();
- }
- }
- void Awake()
- {
- Main = this;
- #if !UNITY_EDITOR_WIN
- DebugOnEditorWin = false;
- #endif
- //if (mUVCDrawer)
- // mUVCDrawer.StartPreviewAction += UVCIsReady;
- }
- void OnDestroy()
- {
- //if (mUVCDrawer)
- // mUVCDrawer.StartPreviewAction -= UVCIsReady;
- }
- void Start()
- {
- //mainContext = SynchronizationContext.Current;
- DebugImage.Add(rawImage);
- DebugImage.Add(rawImage1);
- DebugImage.Add(rawImage2);
- DebugImage.Add(rawImage3);
- DebugImage.Add(rawImage4);
- DebugImage.Add(rawImage5);
- DebugImage.Add(FullScreenImage);
- canvas = transform.GetComponent<RectTransform>();
- mode = Mode.InfraredLocate;
- if (DebugScreenImage && DebugOnEditorWin)
- {
- screenIdentification = new o0.Project.ScreenIdentification();
- screenIdentification.LocateScreen();
- }
- infraredCount = InfraredCount.Single;
- #region 性能检测相关
- for (var i = 0; i < m_History.Capacity; ++i)
- {
- m_History.Add(0.0f);
- }
- lastInterval = Time.realtimeSinceStartup;
- frames = 0;
- #endregion
- }
- //ZIMWebCamera场景使用
- public void WebCamIsReady(Texture texture)
- {
- mPlatform = Platform.Window;
- mUVCTexture = texture;
- mUVCCameraInfo = new CameraInfo(mUVCTexture);
- brightness = 0;
- //UVC准备好
- InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
- }
- //手机端UVCCamra使用
- public void UVCIsReady(Texture texture)
- {
- mPlatform = Platform.Android;
- //this.startUVCBtn.interactable = true;
- mUVCTexture = texture;
- //ARGB32
- //mUVCTexture2D = Texture2D.CreateExternalTexture(texture.width, texture.height, TextureFormat.ARGB32, false, true, texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
- //Debug.Log("mUVCTexture2D isReable:" + mUVCTexture2D.isReadable);
- //Debug.Log("mUVCTexture2D mipmapCount:" + (mUVCTexture2D.mipmapCount > 1));
- List<CameraInfo> cameraInfos = mUVCManager.GetAttachedDevices();
- mUVCCameraInfo = cameraInfos[cameraInfos.Count - 1];
- Debug.Log("mUVCCameraInfo InvokeOnUVCIsReady:" + mUVCCameraInfo);
- //UVC准备好
- InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
- }
- public void startUVC()
- {
- }
- int brightness = 0;
- //public Text brightnessText;
- //public void SliderBrightness(Slider slider)
- //{
- // var _value = slider.value;
- // brightness = (int)_value;
- // brightnessText.text = (2 + brightness) + "";
- //}
- /// <summary>
- /// 设置算法红外灯的亮度值
- /// </summary>
- /// <param name="value"></param>
- public void SetInfraredLocateBrightnessThreshold(float value)
- {
- if (infraredLocate != null)
- {
- if (value >= 0 && value <= 1)
- infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
- }
- }
- void Update()
- {
- //++frames;
- //float timeNow = Time.realtimeSinceStartup;
- //if (timeNow > lastInterval + updateInterval)
- //{
- // fps = (float)(frames / (timeNow - lastInterval));
- // frames = 0;
- // lastInterval = timeNow;
- //}
- //if (m_FPS != null)
- // m_FPS.text = "FPS:" + fps.ToString("f2");
- if (mUVCCameraInfo == null) return;
- if (screenIdentification == null)
- {
- screenIdentification = new o0.Project.ScreenIdentification();
- screenIdentification.OnLocateScreenEnter += OnLocateScreenEnter;
- screenIdentification.OnLocateScreenEnd += OnLocateScreenEnd;
- }
- if (infraredLocate == null)
- {
- infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
- //InfraredDemo 初始化
- //float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
- //Debug.Log("Init Red filterValue:" + redfilterValue);
- //infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
- }
- if (screenIdentification.Screen.RefreshCameraSize(getUVCCameraInfoSize)) // 同步分辨率, 分辨率变化后还需同步到InfraredDemo
- {
- quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexList();
- if (!ContainsNaN(quadUnityVectorList))
- {
- SaveScreenLocateVectorList();
- SyncInfraredDemo();
- SyncInfraredScreenPositioningView();
- Debug.Log("[ScreenLocate] RefreshCameraSize 屏幕size改变:[" + (int)getUVCCameraInfoSize.x + "," + (int)getUVCCameraInfoSize.y + "]");
- Debug.Log("[ScreenLocate] RefreshCameraSize 屏幕size改变,刷新quadUnityVectorList:" + PrintVector2List(quadUnityVectorList));
- }
- else {
- Debug.LogError("[ScreenLocate] RefreshCameraSize 屏幕size改变,存在NaN值,重新校准:" + PrintVector2List(quadUnityVectorList));
- }
-
- if (DebugOnEditorWin)
- Main.ShowScreen(Main.ScreenQuad, screenIdentification.Screen.QuadInCamera);
- }
- if (mode == Mode.ScreenLocateManual)
- {
- //if (Input.GetMouseButtonDown(0))
- //{
- // var mouse = Input.mousePosition;
- // var u = mouse.x / Screen.width;
- // var v = mouse.y / Screen.height;
- // u = Math.Clamp(u, 0, 1);
- // v = Math.Clamp(v, 0, 1);
- // pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
- // var obj = Instantiate(Resources.Load("Point")) as GameObject;
- // obj.transform.SetParent(FullScreenImage.transform);
- // obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
- // if (pointManual.Count == 1)
- // Info.text = "左键单击屏幕 右下角";
- // else if (pointManual.Count == 2)
- // Info.text = "左键单击屏幕 右上角";
- // else if (pointManual.Count == 3)
- // Info.text = "左键单击屏幕 左上角";
- // else if (pointManual.Count == 4)
- // {
- // screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
- // pointManual.Clear();
- // ShowScreen(screenIdentification.Screen.Quad);
- // foreach (Transform i in FullScreenImage.transform)
- // Destroy(i.gameObject);
- // ToMode(Mode.InfraredLocate);
- // }
- //}
- return;
- }
- //var t0 = Time.realtimeSinceStartup;
- /* New*/
- //Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
- if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing) // 成功定位屏幕后才做红外识别
- {
- if (bAutomaticRecognition)
- {
- //识别的过程使用的分辨率
- //CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
- if (log1)
- {
- log1 = false;
- Debug.Log("[ScreenLocate] log1:[" + (int)getUVCCameraInfoSize.x + ", " + (int)getUVCCameraInfoSize.y + "]");
- }
- }
- else
- {
- //自动识别完成后使用相机分辨率大小 getUVCCameraInfoSize
- //CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
- if (log2)
- {
- log2 = false;
- Debug.Log("[ScreenLocate] log2:[" + (int)getUVCCameraInfoSize.x + ", " + (int)getUVCCameraInfoSize.y + "]");
- }
- }
- //如果是连接了蓝牙设备,并且不是9轴设备。不进行识别算法处理
- if (BluetoothAim.ins?.status == BluetoothStatusEnum.ConnectSuccess && AimHandler.ins && AimHandler.ins.bRuning9Axis()) return;
- //根据getUVCCameraInfoSize 分辨率渲染
- CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
- if (!screenIdentification.Update(mUVCTexture2D))
- {
- if (!screenIdentification.Screen.Active)
- {
- //DebugTexture(1, mUVCTexture2D.zimAutoLightSimple());
- return;
- }
- //if (mUVCCameraInfo.Size != ScreenLocateCameraSize) // 摄像机分辨率发生改变时执行
- //{
- // RefreshScreenQuad(mUVCCameraInfo.Size);
- // return;
- //}
- if (mode == Mode.InfraredLocate)
- {
- //0,0, cameraTexture2D.width, cameraTexture2D.height,0
- var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
- //InfraredSpots = infraredLocate.Update(pixels);
- if (bSinglePoint)
- infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
- else
- infraredSpotBuffer = infraredLocate.Update(pixels);
- if (mPlatform == Platform.Window) //渲染ui上面的点。进入游戏可以隐藏
- {
- for (int i = 0; i < infraredSpotBuffer.Length; i++)
- {
- if (infraredSpotBuffer[i].CameraLocation != null)
- {
- // 检测到光点
- var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
- CrosshairInCamera[i].gameObject.SetActive(true);
- CrosshairInCamera[i].anchoredPosition = posInCanvas;
- }
- else
- CrosshairInCamera[i].gameObject.SetActive(false);
- }
- }
- //手机端使用
- if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
- {
- int redIndex = 0;
- int greenIndex = 1;
- //仅仅第一个点显示(如果最大点出界了会闪烁)
- if (bSinglePoint)
- {
- redIndex = 0; //单点识别是,可以选择切换颜色
- if (infraredSpotBuffer[redIndex].ScreenUV != null)
- {
- string str = "Single:";
- Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
- onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
- }
- }
- else
- {
- //雙點模式下選擇第一個點
- if (bIdentifyRed && !bIdentifyGreen)
- {
- if (infraredSpotBuffer[redIndex].ScreenUV != null)
- {
- Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
- onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
- }
- else
- {
- Info.text = "未检测到红色最大点!";
- }
- }
- else if (!bIdentifyRed && bIdentifyGreen)
- {
- if (infraredSpotBuffer[greenIndex].ScreenUV != null)
- {
- Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
- onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
- }
- else
- {
- Info.text = "未检测到绿色点!";
- }
- }
- else
- {
- //两个不选择和两个全选都跑识别两个点
- //自動切換 检测到光点
- if (infraredSpotBuffer[redIndex].ScreenUV != null)
- {
- Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
- onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
- }
- else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
- {
- Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
- onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
- }
- else
- {
- Info.text = "未检测到点!";
- }
- }
- }
- }
- }
- else if (mode == Mode.ScreenMap && DebugOnEditorWin)
- {
- var pixels = mUVCTexture2D.GetPixels();
- if (infraredCount == InfraredCount.Single)
- infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
- else if (infraredCount == InfraredCount.Double)
- infraredSpotBuffer = infraredLocate.Update(pixels);
- for (int i = 0; i < infraredSpotBuffer.Length; i++)
- {
- if (infraredSpotBuffer[i].ScreenUV != null)
- {
- // 检测到光点
- var posInCanvas = infraredSpotBuffer[i].ScreenUV.Value.pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
- CrosshairInScreen[i].gameObject.SetActive(true);
- CrosshairInScreen[i].anchoredPosition = posInCanvas;
- }
- else
- CrosshairInScreen[i].gameObject.SetActive(false);
- }
- if (Input.GetKeyDown(KeyCode.Escape))
- ToMode(Mode.InfraredLocate);
- }
- }
- }
- //var t1 = Time.realtimeSinceStartup;
- //var dt = t1 - t0;
- //m_History[m_ValidHistoryFrames % m_History.Count] = dt;
- //++m_ValidHistoryFrames;
- //m_UIUpdateTimer += Time.deltaTime;
- //if (m_UIUpdateTimer >= m_UIUpdateInterval)
- //{
- // m_UIUpdateTimer = 0.0f;
- // if (m_ValidHistoryFrames >= m_History.Count)
- // {
- // m_ValidHistoryFrames = 0;
- // m_AverageTime = 0.0f;
- // m_MinTime = float.PositiveInfinity;
- // m_MaxTime = float.NegativeInfinity;
- // {
- // for (var i = 0; i < m_History.Count; i++)
- // {
- // var time = m_History[i];
- // m_AverageTime += time;
- // m_MinTime = Mathf.Min(m_MinTime, time);
- // m_MaxTime = Mathf.Max(m_MaxTime, time);
- // }
- // m_AverageTime /= m_History.Count;
- // }
- // {
- // m_History.Sort();
- // // Odd-length history?
- // if ((m_History.Count & 1) != 0)
- // {
- // m_MedianTime = m_History[m_History.Count / 2];
- // }
- // else
- // {
- // m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
- // }
- // }
- // }
- // 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";
- // //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
- // if (m_UITime != null)
- // 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}";
- //}
- //UpdateInputs();
- }
- Vector2 targetPos = Vector2.zero;
- Vector2 movePos = Vector2.zero;
- int moveSpeed = 20;
- public float filterDis = 3.0f;
- void onFilterPos(Vector2 _vector2Pos)
- {
- Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
- if (Vector2.Distance(np, targetPos) >= filterDis)
- {
- targetPos = np;
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
- InfraredCameraHelper.InvokeOnPositionUpdate(targetPos);
- }
- //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
- //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
- }
- Vector2[] _targetPoints2 = new Vector2[] { Vector2.zero, Vector2.zero };
- void onFilterPos2(Vector2 _vector2Pos, int index)
- {
- Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height);
- if (Vector2.Distance(np, _targetPoints2[index]) >= filterDis)
- {
- _targetPoints2[index] = np;
- InfraredCameraHelper.InvokeOnPositionUpdate2(_targetPoints2[index], index);
- }
- }
- #region 自动识别
- int Capture = 30;
- int Delay = 30;
- int DefaultResolutionIndex;
- readonly public int HighScreenLocateResolutionIndex = 2; // 自动识别时,摄像机分辨率固定为1280 * 720 ( 对应索引是2 )
- public void BtnScreenLocate()
- {
- if (DebugScreenImage)
- {
- screenIdentification = new o0.Project.ScreenIdentification();
- WebCamIsReady(DebugScreenImage);
- CreateUVCTexture2DIfNeeded();
- }
- Debug.Log("BtnScreenLocate Capture:" + Capture + " ,Delay: " + Delay);
- screenIdentification.LocateScreen(Capture, Delay);
- }
- bool log1 = false, log2 = false;
- public void OnLocateScreenEnter()
- {
- bAutomaticRecognition = true;
- screenIdentification.Screen.QuadInCamera = null;
- DefaultResolutionIndex = InfraredDemoMain?.ResolutionIndex ?? 0; // 记录一下进入前的分辨率(游戏场景的分辨率,比识别时更低)
- //HighScreenLocateResolutionIndex = InfraredDemoMain.getTextureToResolutionNewIndex(); // index = 0
- Debug.Log("[ScreenLocate] 开始捕获 DefaultResolutionIndex:" + DefaultResolutionIndex + " ,HighScreenLocateResolutionIndex:" + HighScreenLocateResolutionIndex);
- InfraredDemoMain?.SetResolutionNew(HighScreenLocateResolutionIndex);
- //CreateUVCTexture2DIfNeeded();
- log1 = true;
- log2 = true;
- }
- public void OnLocateScreenEnd()
- {
- Debug.Log("[ScreenLocate] 结束捕获,当前摄像机分辨率为: " + mUVCCameraInfo.Size);
- ScreenLocateCameraSize = mUVCCameraInfo.Size; // 记录本次屏幕识别的分辨率(目前采用高分辨率做识别,识别结束后调回低分辨率)
- InfraredDemoMain?.SetResolutionNew(DefaultResolutionIndex);
- bAutomaticRecognition = false;
- }
- /**
- * 与UVC设备协商时
- * H.264是否优先协商
- * 仅安卓实机有效
- * true: H.264 > MJPEG > YUV
- * false: MJPEG > H.264 > YUV
- */
- public bool PreferH264 = false;
- private const int FRAME_TYPE_MJPEG = 0x000007;
- private const int FRAME_TYPE_H264 = 0x000014;
- public void Resize(int width, int height)
- {
- if (mUVCCameraInfo == null) return;
- bool PreferH264 = mUVCManager.PreferH264;
- int[] frameTypes = {
- PreferH264 ? FRAME_TYPE_H264 : FRAME_TYPE_MJPEG,
- PreferH264 ? FRAME_TYPE_MJPEG : FRAME_TYPE_H264,
- };
- foreach (var frameType in frameTypes)
- {
- // Debug.Log("Resize frameType:" + frameType + " = " + width + " : " + height);
- if (UVCManager.onResize(mUVCCameraInfo.device.id, frameType, width, height) == 0)
- {
- // Debug.Log("frameType:" + frameType);
- break;
- }
- }
- //ReSizeTexture(width, height);
- mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
- Debug.Log("[ScreenLocate] Resize mUVCCameraInfo.SetSize: [" + mUVCCameraInfo.CurrentWidth + "," + mUVCCameraInfo.CurrentHeight + "]");
- }
- #endregion
- public void BtnScreenMap()
- {
- ToMode(Mode.ScreenMap);
- }
- //进入手动定位屏幕
- public void BtnScreenLocateManual()
- {
- ToMode(Mode.ScreenLocateManual);
- }
- /// <summary>
- /// 固定的顶点顺序: 左下,右下,左上,右上
- /// </summary>
- public static List<Vector2> quadUnityVectorList = new();
- /// <summary>
- /// 打印信息
- /// </summary>
- /// <param name="list">左下,右下,左上,右上</param>
- /// <returns></returns>
- public string PrintVector2List(List<Vector2> list)
- {
- if (screenIdentification == null || !screenIdentification.Screen.Active) return "[]";
- string result = "";
- if (list.Count == 4)
- {
- result = "左下" + list[0].ToString() + ",右下" + list[1].ToString() + ",左上" + list[2].ToString() + ",右上" + list[3].ToString();
- }
- else
- {
- result = "count != 4 error";
- }
- //foreach (Vector2 vector in list)
- //{
- // result += vector.ToString() + " ";
- //}
- //Debug.Log(result);
- return result;
- }
- /// <summary>
- /// 判断是否存在NaN
- /// </summary>
- /// <param name="vectors"></param>
- /// <returns></returns>
- public bool ContainsNaN(List<Vector2> vectors)
- {
- foreach (var v in vectors)
- {
- if (float.IsNaN(v.x) || float.IsNaN(v.y))
- {
- return true;
- }
- }
- return false;
- }
- // 标记屏幕的四个角, ScreenQuadObject 下挂了4个子节点用于标记
- public void ShowScreen(RectTransform ScreenQuadObject, QuadrilateralInCamera screen)
- {
- if (screen == null)
- {
- Info.text = "识别屏幕失败";
- return;
- }
- Info.text = "已识别到屏幕";
- if (ScreenQuadObject && ScreenQuadObject.childCount >= 4)
- {
- ScreenQuadObject.gameObject.SetActive(true);
- for (int i = 0; i < 4; i++)
- {
- if (DebugOnEditorWin)
- {
- RectTransform t = ScreenQuadObject.GetChild(i) as RectTransform;
- t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, ScreenQuadObject.rect);
- }
- }
- }
- quadUnityVectorList = screen.GetUnityVertexList(); // 记录四个点
- if (!ContainsNaN(quadUnityVectorList))
- {
- SaveScreenLocateVectorList();
- SyncInfraredDemo();
- SyncInfraredScreenPositioningView();
- Debug.Log("[ScreenLocate] ShowScreen 已识别到屏幕,更新quadUnityVectorList:" + PrintVector2List(quadUnityVectorList));
- }
- else
- {
- Debug.LogError("[ScreenLocate] RefreshCameraSize 屏幕size改变,存在NaN值,重新校准:" + PrintVector2List(quadUnityVectorList));
- }
- }
- public void ShowScreen(QuadrilateralInCamera screen) => ShowScreen(ScreenQuad, screen);
- /// <summary>
- /// 校准点位置存储到本地
- /// </summary>
- static public void SaveScreenLocateVectorList()
- {
- string saveStr = string.Join(";", quadUnityVectorList.Select(v => $"{v.x},{v.y}")); //,{v.z}
- Debug.Log("SaveScreenLocateVectorList: " + saveStr);
- PlayerPrefs.SetString("ScreenLocateVectorList", saveStr);
- }
- /// <summary>
- /// 获取本地存储校准点位置
- /// </summary>
- static public bool GetScreenLocateVectorList()
- {
- string posListStr = PlayerPrefs.GetString("ScreenLocateVectorList", "");
- Debug.Log("GetScreenLocateVectorList:"+ posListStr);
- if (!string.IsNullOrWhiteSpace(posListStr))
- {
- quadUnityVectorList.Clear();
- quadUnityVectorList = posListStr.Split(';')
- .Select(s =>
- {
- string[] parts = s.Split(',');
- return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
- })
- .ToList();
- return true;
- }
- else return false;
- }
- /// <summary>
- /// 校准点位置同步到[调试面板]
- /// </summary>
- public void SyncInfraredDemo()
- {
- if (quadUnityVectorList.Count == 0) return;
- Vector2 texSize = getUVCCameraInfoSize;
- Debug.Log("[ScreenLocate] SyncInfraredDemo : " + PrintVector2List(quadUnityVectorList));
- //同步到infaredDemo
- FindObjectOfType<InfraredDemo>()?.SetLocatePointsToCameraRender(
- quadUnityVectorList,
- 1,
- 1);
- }
- /// <summary>
- /// 校准点位置同步到[手动校准页面]
- /// </summary>
- public void SyncInfraredScreenPositioningView()
- {
- if (quadUnityVectorList.Count == 0) return;
- //同步位置
- FindObjectOfType<InfraredScreenPositioningView>()?.SyncScreenPosition();
- }
- void ToMode(Mode mode)
- {
- if (this.mode == mode)
- return;
- if (mode == Mode.ScreenMap)
- {
- if (!screenIdentification.Screen.Active)
- {
- Info.text = "先定位屏幕";
- return;
- }
- Info.text = "按ESC退出";
- SetScreen(Color.black);
- Info.transform.SetAsLastSibling();
- this.mode = Mode.ScreenMap;
- }
- else if (mode == Mode.InfraredLocate)
- {
- Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
- //Info.text = "已识别到屏幕";
- SetScreen(null);
- foreach (var i in CrosshairInScreen)
- i.gameObject.SetActive(false);
- FullScreenImage.gameObject.SetActive(false);
- Info.transform.SetSiblingIndex(transform.childCount - 4);
- this.mode = Mode.InfraredLocate;
- DebugTexture(6, null);
- //DebugTexture(1, null); //null
- // rawImage1.texture = null;
- #if (!NDEBUG && DEBUG && ENABLE_LOG)
- Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{screenIdentification.Screen.Active}");
- #endif
- }
- else if (mode == Mode.ScreenLocateManual)
- {
- Info.text = "左键单击屏幕 左下角";
- FullScreenImage.gameObject.SetActive(true);
- Info.transform.SetSiblingIndex(transform.childCount - 1);
- // var newTex = WebCamera.webCamTexture.AutoLight(10);
- //DebugTexture(1, TextureToTexture2D(rawImage.texture));
- CreateUVCTexture2DIfNeeded();
- DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
- //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
- //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
- this.mode = Mode.ScreenLocateManual;
- }
- }
- private Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
- {
- if (width == 0)
- width = texture.width;
- if (height == 0)
- height = texture.height;
- Texture2D _texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
- RenderTexture currentRT = RenderTexture.active;
- RenderTexture renderTexture = RenderTexture.GetTemporary(
- width,
- height,
- 0,
- RenderTextureFormat.ARGB32,
- RenderTextureReadWrite.Linear);
- Graphics.Blit(texture, renderTexture);
- RenderTexture.active = renderTexture;
- _texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
- _texture2D.Apply();
- RenderTexture.active = currentRT;
- RenderTexture.ReleaseTemporary(renderTexture);
- return _texture2D;
- }
- //public void CreateUVCTexture2DFocusSizeIfNeeded(int width, int height)
- //{
- // if (mUVCTexture2D != null)
- // Destroy(mUVCTexture2D);
- // mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
- //}
- /// <summary>
- /// 使用默认的mUVCTexture宽高
- /// </summary>
- private void CreateUVCTexture2DIfNeeded()
- {
- if (mUVCTexture2D != null)
- Destroy(mUVCTexture2D);
- mUVCTexture2D = TextureToTexture2D(mUVCTexture);
- }
- /// <summary>
- /// 根据宽高调整mUVCTexture2D
- /// </summary>
- /// <param name="width"></param>
- /// <param name="height"></param>
- private void CreateUVCTexture2DIfNeeded(int width = 0, int height = 0)
- {
- if (mUVCTexture2D != null)
- Destroy(mUVCTexture2D);
- mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
- }
- #region DoubleButton
- private DateTime m_firstTime;
- private DateTime m_secondTime;
- private void Press()
- {
- Debug.Log("进入手动定位");
- BtnScreenLocateManual();
- resetTime();
- }
- public void OnDoubleClick()
- {
- //超时重置
- if (!m_firstTime.Equals(default(DateTime)))
- {
- var intervalTime = DateTime.Now - m_firstTime;
- float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
- if (milliSeconds >= 400)
- resetTime();
- }
- // 按下按钮时对两次的时间进行记录
- if (m_firstTime.Equals(default(DateTime)))
- m_firstTime = DateTime.Now;
- else
- m_secondTime = DateTime.Now;
- // 在第二次点击触发,时差小于400ms触发
- if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
- {
- var intervalTime = m_secondTime - m_firstTime;
- float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
- if (milliSeconds < 400)
- Press();
- else
- resetTime();
- }
- }
- private void resetTime()
- {
- m_firstTime = default(DateTime);
- m_secondTime = default(DateTime);
- }
- #endregion
- #region 性能检测相关
- void InvalidateTimings()
- {
- m_ValidHistoryFrames = 0;
- m_AverageTime = float.NaN;
- m_MedianTime = float.NaN;
- m_MinTime = float.NaN;
- m_MaxTime = float.NaN;
- }
- void UpdateInputs()
- {
- //重置
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- InvalidateTimings();
- }
- }
- #endregion
- }
|