|
|
@@ -74,6 +74,12 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
/// </summary>
|
|
|
public bool SelectScreenAfterLocate(ScreenIdentificationTag tag) => ScreenIdentification.SelectScreenAfterLocate(tag);
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 上一次半自动识别的情况, 还未识别的时候数组是null
|
|
|
+ /// 通过索引获取布尔值,false代表这条边识别失败(回退应用了手动数据), 0-下、1-右、2-上、3-左
|
|
|
+ /// </summary>
|
|
|
+ public bool[] LastQuadSemiAutoState() => screenIdentification.LastQuadSemiAutoState;
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取算法执行过程中输出的纹理,0原图,1半自动识别到的全部线段,2屏幕黑白色差,3识别结果,4屏幕色差叠加识别结果,5半自动时的备选线段
|
|
|
/// </summary>
|
|
|
@@ -97,8 +103,10 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
private Texture mUVCTexture;
|
|
|
public Texture getUVCTexture => mUVCTexture;
|
|
|
|
|
|
- public Texture setUVCTexture {
|
|
|
- set {
|
|
|
+ public Texture setUVCTexture
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
mUVCTexture = value;
|
|
|
}
|
|
|
}
|
|
|
@@ -190,7 +198,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
|
|
|
[NonSerialized] public RectTransform BackQuad = null;
|
|
|
|
|
|
- static public ScreenLocate Main;
|
|
|
+ static public ScreenLocate Main { get; private set; }
|
|
|
|
|
|
static public void AutoLightPixels(Color[] pixels, int width, int height)
|
|
|
{
|
|
|
@@ -280,6 +288,8 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
|
|
|
void Awake()
|
|
|
{
|
|
|
+ if (Main != null)
|
|
|
+ throw new Exception("[ScreenLocaer] 不允许多个实例");
|
|
|
Main = this;
|
|
|
|
|
|
#if !UNITY_EDITOR_WIN
|
|
|
@@ -363,23 +373,34 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
public void UVCUpdate(bool bChange)
|
|
|
{
|
|
|
mUVCTexture = mUVCCameraInfo.previewTexture;
|
|
|
- Debug.Log("[ScreenLocate] UVCUpdate:" + mUVCCameraInfo + ",bChange:"+bChange);
|
|
|
+ Debug.Log("[ScreenLocate] UVCUpdate:" + mUVCCameraInfo + ",bChange:" + bChange);
|
|
|
InfraredCameraHelper?.InvokeOnUVCIsUpdate();
|
|
|
|
|
|
//这里判断是否进入自动识别?
|
|
|
- if (bAutomaticRecognitionStart) {
|
|
|
+ if (bAutomaticRecognitionStart)
|
|
|
+ {
|
|
|
bAutomaticRecognitionStart = false;
|
|
|
Debug.Log("[ScreenLocate] UVCUpdate 开始自动识别 Capture:" + Capture + " ,Delay: " + Delay);
|
|
|
screenIdentification.LocateScreen(Capture, Delay);
|
|
|
}
|
|
|
|
|
|
- if (bAutomaticRecognitionEnd) {
|
|
|
+ if (bAutomaticRecognitionEnd)
|
|
|
+ {
|
|
|
bAutomaticRecognitionEnd = false;
|
|
|
Debug.Log("[ScreenLocate] UVCUpdate 结束捕获,当前摄像机分辨率为: " + mUVCCameraInfo.Size);
|
|
|
bAutomaticRecognition = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 选择模式后更新 quadUnityVectorList
|
|
|
+ /// </summary>
|
|
|
+ public void UpdateQuadUnityVectorList()
|
|
|
+ {
|
|
|
+ quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
|
|
|
+ SaveScreenLocateVectorList();
|
|
|
+ }
|
|
|
+
|
|
|
int brightness = 0;
|
|
|
/// <summary>
|
|
|
/// 设置算法红外灯的亮度值
|
|
|
@@ -394,14 +415,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
|
|
|
}
|
|
|
}
|
|
|
- /// <summary>
|
|
|
- /// 选择模式后更新 quadUnityVectorList
|
|
|
- /// </summary>
|
|
|
- public void UpdateQuadUnityVectorList()
|
|
|
- {
|
|
|
- quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
|
|
|
- SaveScreenLocateVectorList();
|
|
|
- }
|
|
|
+
|
|
|
void Update()
|
|
|
{
|
|
|
//++frames;
|
|
|
@@ -432,39 +446,16 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
if (infraredLocate == null)
|
|
|
{
|
|
|
infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings, ScreenPixelCheaker);
|
|
|
+ InfraredSpot.RefreshMinVerifyLength(new o0.Geometry2D.Float.Vector(getUVCCameraInfoSize.x, getUVCCameraInfoSize.y));
|
|
|
//InfraredDemo 初始化
|
|
|
//float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
|
|
|
//Debug.Log("Init Red filterValue:" + redfilterValue);
|
|
|
//infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
|
|
|
}
|
|
|
|
|
|
- if (RefreshCameraSize()) // 同步分辨率, 分辨率变化后还需同步到InfraredDemo
|
|
|
- {
|
|
|
- quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
|
|
|
-
|
|
|
- if (!ContainsNaN(quadUnityVectorList))
|
|
|
- {
|
|
|
- SaveScreenLocateVectorList();
|
|
|
- //SyncInfraredDemo();
|
|
|
- //SyncInfraredScreenPositioningView();
|
|
|
- InfraredCameraHelper?.InvokeOnUVCPosUpdate(quadUnityVectorList);
|
|
|
- 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 (DebugOnZIMDemo)
|
|
|
- Main.ShowScreen(screenIdentification.Screen.QuadInCamera);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //var t0 = Time.realtimeSinceStartup;
|
|
|
-
|
|
|
/* New*/
|
|
|
//Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
|
|
|
- if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing) // 成功定位屏幕后才做红外识别
|
|
|
+ if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing)
|
|
|
{
|
|
|
//if (bAutomaticRecognition)
|
|
|
//{
|
|
|
@@ -494,13 +485,36 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
CreateUVCTexture2DIfNeeded((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
|
|
|
if (!screenIdentification.Update(mUVCTexture2D))
|
|
|
{
|
|
|
- CameraSize = new o0.Geometry2D.Vector<int>(mUVCTexture2D.width, mUVCTexture2D.height);
|
|
|
+ // 同步分辨率, 分辨率变化后还需同步到InfraredDemo
|
|
|
+ if (RefreshCameraSize())
|
|
|
+ {
|
|
|
+ if (screenIdentification.Screen.QuadInCamera != null)
|
|
|
+ {
|
|
|
+ quadUnityVectorList = screenIdentification.Screen.QuadInCamera.GetUnityVertexNormalizedList();
|
|
|
+
|
|
|
+ if (!ContainsNaN(quadUnityVectorList))
|
|
|
+ {
|
|
|
+ SaveScreenLocateVectorList();
|
|
|
+ //SyncInfraredDemo();
|
|
|
+ //SyncInfraredScreenPositioningView();
|
|
|
+ InfraredCameraHelper?.InvokeOnUVCPosUpdate(quadUnityVectorList);
|
|
|
+ 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 (DebugOnZIMDemo)
|
|
|
+ Main.ShowScreen(screenIdentification.Screen.QuadInCamera);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取像素,用于后续操作
|
|
|
var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
|
|
|
|
|
|
AutoLightPixels(pixels, CameraSize.x, CameraSize.y);
|
|
|
- //return;
|
|
|
- //InfraredSpots = infraredLocate.Update(pixels);
|
|
|
-
|
|
|
if (bSinglePoint)
|
|
|
infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
|
|
|
else
|
|
|
@@ -638,8 +652,6 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
ToMode(Mode.InfraredLocate);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//var t1 = Time.realtimeSinceStartup;
|
|
|
@@ -702,13 +714,25 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
|
|
|
private bool RefreshCameraSize()
|
|
|
{
|
|
|
- var sizeNew = getUVCCameraInfoSize.o0Vector();
|
|
|
- screenIdentification.QuadAuto?.ReSize(sizeNew, ScreenMap.ViewAspectRatioSetting);
|
|
|
- screenIdentification.QuadManual?.ReSize(sizeNew, ScreenMap.ViewAspectRatioSetting);
|
|
|
- screenIdentification.QuadSemiAuto?.ReSize(sizeNew, ScreenMap.ViewAspectRatioSetting);
|
|
|
- return screenIdentification.Screen.RefreshCameraSize(getUVCCameraInfoSize);
|
|
|
+ var sizeNew = new o0.Geometry2D.Vector<int>((int)getUVCCameraInfoSize.x, (int)getUVCCameraInfoSize.y);
|
|
|
+ if (sizeNew != CameraSize)
|
|
|
+ {
|
|
|
+ Debug.Log("[ScreenLocate] 分辨率变化,刷新分辨率");
|
|
|
+ // 同步相机分辨率
|
|
|
+ CameraSize = sizeNew;
|
|
|
+ var sizeNewFloat = getUVCCameraInfoSize.o0Vector();
|
|
|
+ screenIdentification.Screen.RefreshCameraSize(sizeNewFloat);
|
|
|
+ screenIdentification.QuadAuto?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
|
|
|
+ screenIdentification.QuadManual?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
|
|
|
+ screenIdentification.QuadSemiAuto?.ReSize(sizeNewFloat, ScreenMap.ViewAspectRatioSetting);
|
|
|
+
|
|
|
+ InfraredSpot.RefreshMinVerifyLength(sizeNewFloat);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
Vector2 targetPos = Vector2.zero;
|
|
|
Vector2 movePos = Vector2.zero;
|
|
|
int moveSpeed = 20;
|
|
|
@@ -748,7 +772,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
int Capture = 30;
|
|
|
int Delay = 30;
|
|
|
Vector2 EnterResolution;
|
|
|
- // int DefaultResolutionIndex;
|
|
|
+ // int DefaultResolutionIndex;
|
|
|
|
|
|
// readonly public int HighScreenLocateResolutionIndex = 2; // 自动识别时,摄像机分辨率固定为1280 * 720 ( 对应索引是2 )
|
|
|
|
|
|
@@ -767,7 +791,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
//screenIdentification.LocateScreen(Capture, Delay);
|
|
|
OnLocateScreenEnter();
|
|
|
}
|
|
|
- // bool log1 = false, log2 = false;
|
|
|
+ // bool log1 = false, log2 = false;
|
|
|
public void OnLocateScreenEnter()
|
|
|
{
|
|
|
bAutomaticRecognition = true;
|
|
|
@@ -781,12 +805,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
Vector2 _HighResolution = mUVCCameraInfo.CurrentCalibrationResolution; //最高的分辨率
|
|
|
Resize((int)_HighResolution.x, (int)_HighResolution.y);
|
|
|
|
|
|
- if (DebugOnZIMDemo)
|
|
|
- screenIdentification.LocateScreen();
|
|
|
-
|
|
|
-#if UNITY_EDITOR
|
|
|
- UVCUpdate(false);
|
|
|
-#endif
|
|
|
+ screenIdentification.LocateScreen();
|
|
|
|
|
|
//CreateUVCTexture2DIfNeeded();
|
|
|
// log1 = true;
|
|
|
@@ -814,14 +833,14 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
#endif
|
|
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
- // pc todo 看看怎么处理
|
|
|
- // ResizePC(width, height);
|
|
|
+ // pc todo 看看怎么处理
|
|
|
+ // ResizePC(width, height);
|
|
|
#endif
|
|
|
|
|
|
//mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
|
|
|
- Debug.Log($"[ScreenLocate] 开始修改分辨率 mUVCCameraInfo origin:[{ mUVCCameraInfo.CurrentWidth },{ mUVCCameraInfo.CurrentHeight }]=>target:[{ width },{ height }]");
|
|
|
+ Debug.Log($"[ScreenLocate] 开始修改分辨率 mUVCCameraInfo origin:[{mUVCCameraInfo.CurrentWidth},{mUVCCameraInfo.CurrentHeight}]=>target:[{width},{height}]");
|
|
|
|
|
|
- // if (screenIdentification.isInitLocateScreen()) screenIdentification.bStartLocateScreen = true;
|
|
|
+ // if (screenIdentification.isInitLocateScreen()) screenIdentification.bStartLocateScreen = true;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -849,7 +868,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
// Stop the current WebCamTexture
|
|
|
_webCamTexture.Stop();
|
|
|
// Trigger OnWebCamStopped event
|
|
|
- // OnWebCamStopped?.Invoke();
|
|
|
+ // OnWebCamStopped?.Invoke();
|
|
|
// Wait for a short time to ensure resources are released
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
// Create a new WebCamTexture with the new dimensions
|
|
|
@@ -873,7 +892,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
mUVCCameraInfo.SetSize(_webCamTexture.width, _webCamTexture.height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
|
|
|
Debug.Log("[ScreenLocate] ResizePc mUVCCameraInfo.SetSize: [" + mUVCCameraInfo.CurrentWidth + "," + mUVCCameraInfo.CurrentHeight + "]");
|
|
|
|
|
|
- // if(screenIdentification.isInitLocateScreen())screenIdentification.bStartLocateScreen = true;
|
|
|
+ // if(screenIdentification.isInitLocateScreen())screenIdentification.bStartLocateScreen = true;
|
|
|
}
|
|
|
#endregion
|
|
|
public void BtnScreenMap()
|
|
|
@@ -1002,7 +1021,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
static public bool GetScreenLocateVectorList()
|
|
|
{
|
|
|
string posListStr = PlayerPrefs.GetString("ScreenLocateVectorList", "");
|
|
|
- Debug.Log("GetScreenLocateVectorList:"+ posListStr);
|
|
|
+ Debug.Log("GetScreenLocateVectorList:" + posListStr);
|
|
|
if (!string.IsNullOrWhiteSpace(posListStr))
|
|
|
{
|
|
|
quadUnityVectorList.Clear();
|
|
|
@@ -1020,7 +1039,7 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
|
|
|
}
|
|
|
|
|
|
- public Vector2 AdjustPointsOffset(Vector2 inputPoint,string type = "CameraLocation")
|
|
|
+ public Vector2 AdjustPointsOffset(Vector2 inputPoint, string type = "CameraLocation")
|
|
|
{
|
|
|
// 计算从原始中心到输入点的偏移量
|
|
|
if (type == "CameraLocation")
|
|
|
@@ -1028,7 +1047,8 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
CameraLocationOffset = inputPoint - screenIdentification.Screen.TransformToCamera(new Vector2(0.5f, 0.5f) * screenIdentification.Screen.UVSize);
|
|
|
return CameraLocationOffset;
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
//ScreenUV
|
|
|
UVOffset = inputPoint - new Vector2(0.5f, 0.5f);
|
|
|
return UVOffset;
|
|
|
@@ -1037,7 +1057,8 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
/// <summary>
|
|
|
/// 重置偏移量
|
|
|
/// </summary>
|
|
|
- public void ResetPointsOffest() {
|
|
|
+ public void ResetPointsOffest()
|
|
|
+ {
|
|
|
CameraLocationOffset = Vector2.zero;
|
|
|
UVOffset = Vector2.zero;
|
|
|
}
|
|
|
@@ -1047,7 +1068,8 @@ public partial class ScreenLocate : MonoBehaviour
|
|
|
/// </summary>
|
|
|
/// <param name="cameraLocatoin"></param>
|
|
|
/// <returns></returns>
|
|
|
- public Vector2 GetOffsetCameraLocation(Vector2 cameraLocatoin) {
|
|
|
+ public Vector2 GetOffsetCameraLocation(Vector2 cameraLocatoin)
|
|
|
+ {
|
|
|
return cameraLocatoin - CameraLocationOffset;
|
|
|
}
|
|
|
|