using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using SLAMUVC; using UnityEngine.UI; using SmartBowSDK; using o0InfraredLocate.ZIM; using static ScreenLocate; namespace InfraredManager { public class InfraredCameraHelper { #region 单例 private static InfraredCameraHelper _Instance; public static InfraredCameraHelper GetInstance() { if (_Instance == null) _Instance = new(); return _Instance; } #endregion #region 创建销毁 private GameObject _managerObject; private ScreenLocate _screenLocate; private UVCManager _uvcManager; public static List InfraredCameraHelperRawImageList = new(); public void Create(int defWidth = 320, int defHeight = 240) { if (_managerObject) return; string prefabName = "WebCameraView"; var o = UnityEngine.Object.Instantiate(Resources.Load(prefabName)); UnityEngine.Object.DontDestroyOnLoad(o); o.name = prefabName; _screenLocate = o.GetComponent(); _screenLocate.InfraredCameraHelper = this; _uvcManager = o.GetComponent(); _uvcManager.startUVCManager.AddListener(_screenLocate.UVCIsReady); _uvcManager.updateUVCManager.AddListener(_screenLocate.UVCUpdate); _uvcManager.DefaultWidth = defWidth; _uvcManager.DefaultHeight = defHeight; Debug.Log("InitUVCManagerCamera 初始化相机,size:[" + _uvcManager.DefaultWidth + "," + _uvcManager.DefaultHeight + "]"); _managerObject = o; if (Application.platform == RuntimePlatform.Android) Application.targetFrameRate = 60; } /// /// 在pc里面创建此预制 /// public void CreateToPc() { if (_managerObject) return; string prefabName = "WebCameraView_Pc"; var o = UnityEngine.Object.Instantiate(Resources.Load(prefabName)); UnityEngine.Object.DontDestroyOnLoad(o); o.name = "WebCameraView"; _screenLocate = o.GetComponent(); _screenLocate.InfraredCameraHelper = this; _uvcManager = o.GetComponent(); _managerObject = o; } public void Dispose() { if (_managerObject) UnityEngine.Object.DestroyImmediate(_managerObject); if (_Instance == this) _Instance = null; } #endregion #region 画面接口 public void onStartPreview() { _uvcManager.onStartPreview(); } public void onStopPreview() { _uvcManager.onStopPreview(); } /// /// 获取摄像机对象CameraInfo /// public UVCManager.CameraInfo GetUVCCameraInfo() { return _screenLocate.mUVCCameraInfo; } /// /// 获取相机画面贴图 /// public Texture GetCameraTexture() { return _screenLocate.getUVCTexture; //_screenLocate.rawImage.texture; } /// /// 获取相机画面材质 /// public Material GetCameraMaterial() { return null;//_screenLocate.rawImage.material; } /// /// 设置亮度(-1~1) /// public void SetBrightness(float value) { //_screenLocate.rawImage.material.SetFloat("_Brightness", value); _screenLocate.pcBrightness = value; } /// /// 获取亮度(-1~1) /// public float GetBrightness() { return _screenLocate.pcBrightness;// _screenLocate.rawImage.material.GetFloat("_Brightness"); } /// /// 设置对比度(-1~1) /// public void SetContrast(float value) { // _screenLocate.rawImage.material.SetFloat("_Contrast", value); _screenLocate.pcContrast = value; } /// /// 获取对比度(-1~1) /// public float GetContrast() { return _screenLocate.pcContrast;//_screenLocate.rawImage.material.GetFloat("_Contrast"); } /// /// 设置相机画面分辨率 /// public void SetCameraResolution(int width, int height) { if (_uvcManager) { _uvcManager.DefaultWidth = width; _uvcManager.DefaultHeight = height; } Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolution,width:" + width + " ,height:" + height); } /// /// 设置相机画面分辨率 /// public void SetCameraResolutionNew(int width, int height) { Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolutionNew,width:" + width + " ,height:" + height); _screenLocate.Resize(width, height); } /// /// 设置default分辨率 /// public void SetHighCameraResolution() { Vector2 _highResolution = _screenLocate.mUVCCameraInfo.CurrentHighResolution; _screenLocate.Resize((int)_highResolution.x, (int)_highResolution.y); } /// /// 设置Low分辨率 /// public void SetLowCameraResolution() { Vector2 _lowResolution = _screenLocate.mUVCCameraInfo.CurrentLowResolution; _screenLocate.Resize((int)_lowResolution.x, (int)_lowResolution.y); } #endregion #region 操作接口 /// /// 获取screenLocate 对象 /// /// ScreenLocate public ScreenLocate getScreenLocate() { return _screenLocate; } /// /// 单点模式开关 /// public void SetSinglePoint(bool value) => _screenLocate.SetSinglePoint(value); /// /// 是否为单点模式 /// public bool IsSinglePoint() => _screenLocate.IsSinglePoint(); /// /// 设置抖动过滤值 /// public void SetShakeFilterValue(float value) { _screenLocate.filterDis = value; } /// /// 获取抖动过滤值 /// public float GetShakeFilterValue() { return _screenLocate.filterDis; } /// /// 设置亮度过滤阈值 /// public void SetInfraredLocateBrightnessThreshold(float value) { _screenLocate.SetInfraredLocateBrightnessThreshold(value); } /// /// 是否已经定位 /// public bool IsScreenLoateOK() { return _screenLocate.IsScreenLoateOK(); } /// /// 是否正在手动定位 /// public bool IsScreenLocateManualDoing() { return _screenLocate.IsScreenLocateManualDoing(); } /// /// 进入手动定位 /// /// 相机画面截图 public Texture2D EnterScreenLocateManual() { return _screenLocate.EnterScreenLocateManual(); } /// /// 退出/完成手动定位 /// /// 定位点(左下、右下、右上、左上) public void QuitScreenLocateManual(List points) { _screenLocate.QuitScreenLocateManual(points); } /// /// 初始化中心点偏移量,用于有记录数据时候初始化 /// /// /// CameraLocation , ScreenUV /// public Vector2 SetCenterOffset(Vector2 inputPointOffset, string type = "CameraLocation") { // 获取偏移量 return _screenLocate.SetPointsOffset(inputPointOffset, type); } /// /// 获取中心点偏移量,双点情况下不会自动存储。需要再调用 SetCenterOffsets /// /// /// CameraLocation , ScreenUV /// public Vector2 GetCenterOffset(Vector2 inputPoint, string type = "CameraLocation") { // 获取偏移量 return _screenLocate.AdjustPointsOffset(inputPoint, type); } /// /// 重置中心点的偏移量 /// public void ResetCenterOffset() { _screenLocate.ResetPointsOffest(); } /// /// 撤销回退中心点的偏移量操作。 /// public void RevokeCenterOffset() { _screenLocate.RevokePointsOffest(); } /// /// 获取中心点偏移量,双点情况下不会自动存储。需要再调用 SetCenterOffsets /// 左右屏时候调用 /// /// /// CameraLocation , ScreenUV /// public Vector2 GetCenterOffset(Vector2 inputPoint, bool isLeft, string type = "CameraLocation") { // 获取偏移量 return _screenLocate.AdjustPointsOffset(inputPoint, type, isLeft); } /// /// 设置偏移量--双点 /// /// 玩家类型 /// 偏移点 /// CameraLocation 或者 UV /// public Vector2[] SetCenterOffsets(PlayerType playerType, Vector2 inputPointOffset, string type = "CameraLocation") { // 获取偏移量 return _screenLocate.SetPointsOffsets(playerType, inputPointOffset, type); } /// /// 重置偏移点--双点 /// public void RevokeCenterOffsets() { _screenLocate.RevokePointsOffests(); } /// /// 重置中心点的偏移量--双点 /// public void ResetCenterOffsets() { _screenLocate.ResetPointsOffests(); } /// /// 清除定位数据 /// public void ClearInfraredPositioningData(bool bSyncLocal = false) { //重置识别点 ScreenLocate.Main.ScreenIdentification.ClearQuadCache(); //清除一下记录的点 ScreenLocate.quadUnityVectorList.Clear(); //是否需要保存清除后的数据 if (bSyncLocal) ScreenLocate.SaveScreenLocateVectorList(); } /// /// 初始化一次本地的记录点 /// public void InitScreenLocateManual() { //获取本地记录的点 if (ScreenLocate.GetScreenLocateVectorList()) { Debug.Log("[初始化本地坐标]:" + _screenLocate.PrintVector2List(ScreenLocate.quadUnityVectorList)); //使用算法顺序点来设置屏幕四边形,定位点(左下,右下,左上,右上) _screenLocate.QuadUnityVectorListToScreenQuad(ScreenLocate.quadUnityVectorList); // _screenLocate.SyncInfraredDemo(); InvokeOnUVCPosUpdate(ScreenLocate.quadUnityVectorList); } } /// /// 进入自动定位 /// public void EnterScreenLocateManualAuto() { _screenLocate.EnterScreenLocateManualAuto(); } /// /// 修改 Capture ,建议值30 /// public void SetCapture(int value) { _screenLocate.SetCapture(value); } /// /// 修改delay,建议值30 /// public void SetDelay(int value) { _screenLocate.SetDelay(value); } #endregion #region 监听事件 /// /// ScreenLocate准备事件 /// 初始化 screenIdentification 和 infraredLocate后回调 /// public OnScreenLocateIsReadyEvent OnScreenLocateIsReady; public delegate void OnScreenLocateIsReadyEvent(); public void InvokeOnScreenLocateIsReady() { try { OnScreenLocateIsReady?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// UVC 相机准备事件 /// public OnUVCIsReadyEvent OnUVCIsReady; public delegate void OnUVCIsReadyEvent(UVCManager.CameraInfo camera); public void InvokeOnUVCIsReady(UVCManager.CameraInfo camera) { try { OnUVCIsReady?.Invoke(camera); } catch (Exception e) { Debug.LogError(e); } } /// /// UVC 相机更新事件 /// public OnUVCIsUpdateEvent OnUVCIsUpdate; public delegate void OnUVCIsUpdateEvent(); public void InvokeOnUVCIsUpdate() { try { OnUVCIsUpdate?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// UVC 定位点更新事件 /// public OnUVCPosUpdateEvent OnUVCPosUpdate; public delegate void OnUVCPosUpdateEvent(List list); public void InvokeOnUVCPosUpdate(List list) { try { OnUVCPosUpdate?.Invoke(list); } catch (Exception e) { Debug.LogError(e); } } /// /// 单点更新事件 /// public OnPositionUpdateEvent OnPositionUpdate; public delegate void OnPositionUpdateEvent(Vector2 position); public void InvokeOnPositionUpdate(Vector2 position) { try { OnPositionUpdate?.Invoke(position); } catch (Exception e) { Debug.LogError(e); } } /// /// 双点更新事件 /// public OnPositionUpdate2Event OnPositionUpdate2; public delegate void OnPositionUpdate2Event(Vector2 position, int index); public void InvokeOnPositionUpdate2(Vector2 position, int index) { try { OnPositionUpdate2?.Invoke(position, index); } catch (Exception e) { Debug.LogError(e); } } #endregion } }