軌跡記錄器更改成Raw Input
This commit is contained in:
1 parent
14140797ba
commit
ec0708d05b
17 files changed
+605
-316
No files matched your search
@@ -1,6 +1,5 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class Movement : MonoBehaviour
|
||||
{
|
||||
@@ -17,22 +16,16 @@ public class Movement : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Mouse.current != null)
|
||||
{
|
||||
Vector2 currentDelta = Mouse.current.delta.ReadValue();
|
||||
// 跟遊戲內視角讀同一份 Raw Input 的位移
|
||||
Vector2 currentDelta = RawMouseInput.FrameDelta;
|
||||
|
||||
if (smoothedDelta == Vector2.zero)
|
||||
{
|
||||
smoothedDelta = currentDelta;
|
||||
}
|
||||
else
|
||||
{
|
||||
smoothedDelta = (alpha * currentDelta) + ((1.0f - alpha) * smoothedDelta);
|
||||
}
|
||||
if (smoothedDelta == Vector2.zero)
|
||||
{
|
||||
smoothedDelta = currentDelta;
|
||||
}
|
||||
else
|
||||
{
|
||||
smoothedDelta = Vector2.zero;
|
||||
smoothedDelta = (alpha * currentDelta) + ((1.0f - alpha) * smoothedDelta);
|
||||
}
|
||||
|
||||
displayTimer += Time.unscaledDeltaTime;
|
||||
|
||||
@@ -1,172 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
|
||||
// 滑鼠回報率 : 用 Raw Input 每一筆回報的真實時間戳算出來 (跟遊戲中記錄的是同一份資料)
|
||||
// 同時也是 Raw Input 有沒有正常運作的檢查
|
||||
public class Polling : MonoBehaviour
|
||||
{
|
||||
[Header("UI 參照")]
|
||||
[Tooltip("Text (TMP) ")]
|
||||
public TextMeshProUGUI pollingDisplay;
|
||||
|
||||
private const int WH_MOUSE_LL = 14;
|
||||
private const int WM_MOUSEMOVE = 0x0200;
|
||||
|
||||
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
private static LowLevelMouseProc _proc;
|
||||
private static IntPtr _hookID = IntPtr.Zero;
|
||||
|
||||
private static Stopwatch stopwatch = new Stopwatch();
|
||||
private static long lastTicks = 0;
|
||||
private static List<double> intervals = new List<double>();
|
||||
private static readonly object lockObj = new object();
|
||||
|
||||
// EMA
|
||||
private double smoothedMs = 0;
|
||||
private const double alpha = 0.25;
|
||||
|
||||
// 無動作計時
|
||||
private List<int> recentRates = new List<int>();
|
||||
private float timeSinceLastEvent = 0f;
|
||||
private const float idleTimeout = 2f; // 超過 2 秒沒動就歸零
|
||||
|
||||
[Header("顯示")]
|
||||
[Tooltip("文字更新間隔 (秒),偵測仍然每幀進行")]
|
||||
[Tooltip("文字更新間隔 (秒)")]
|
||||
public float updateInterval = 0.5f;
|
||||
private float displayTimer = 0f;
|
||||
private int shownHz = -1; // 上次顯示的數值,沒變就不重設文字
|
||||
|
||||
// 與 hook 交換用的暫存 List,避免每幀 new 一個新的
|
||||
private List<double> currentBatch = new List<double>();
|
||||
[Tooltip("超過這麼久沒移動就顯示 0 (秒)")]
|
||||
public float idleTimeout = 2f;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
stopwatch.Start();
|
||||
_proc = HookCallback;
|
||||
_hookID = SetHook(_proc);
|
||||
}
|
||||
// 間隔超過這個就算是停下來了,不拿來算回報率 (秒)
|
||||
private const double MaxGap = 0.02;
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
UnhookWindowsHookEx(_hookID);
|
||||
}
|
||||
|
||||
private IntPtr SetHook(LowLevelMouseProc proc)
|
||||
{
|
||||
using (Process curProcess = Process.GetCurrentProcess())
|
||||
using (ProcessModule curModule = curProcess.MainModule)
|
||||
{
|
||||
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode >= 0 && wParam == (IntPtr)WM_MOUSEMOVE)
|
||||
{
|
||||
long currentTicks = stopwatch.ElapsedTicks;
|
||||
if (lastTicks != 0)
|
||||
{
|
||||
double ms = (double)(currentTicks - lastTicks) / Stopwatch.Frequency * 1000.0;
|
||||
|
||||
lock (lockObj)
|
||||
{
|
||||
if (ms > 0.001 && ms < 100.0)
|
||||
{
|
||||
intervals.Add(ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
lastTicks = currentTicks;
|
||||
}
|
||||
return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
||||
}
|
||||
private readonly List<double> gaps = new List<double>();
|
||||
private double lastEventTime;
|
||||
private float displayTimer;
|
||||
private float idleTimer;
|
||||
private int shownHz = -1;
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 把 hook 收集的資料換過來,hook 改寫進清空的那一份
|
||||
currentBatch.Clear();
|
||||
lock (lockObj)
|
||||
if (!RawMouseInput.IsRunning)
|
||||
{
|
||||
List<double> temp = intervals;
|
||||
intervals = currentBatch;
|
||||
currentBatch = temp;
|
||||
SetText("滑鼠回報率: 無法偵測 (" + RawMouseInput.Status + ")", -2);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentBatch.Count > 0)
|
||||
foreach (RawMouseInput.Event e in RawMouseInput.FrameEvents)
|
||||
{
|
||||
timeSinceLastEvent = 0f;
|
||||
|
||||
double sum = 0;
|
||||
foreach (var val in currentBatch) sum += val;
|
||||
double batchAvgMs = sum / currentBatch.Count;
|
||||
|
||||
if (smoothedMs == 0)
|
||||
{
|
||||
smoothedMs = batchAvgMs;
|
||||
}
|
||||
else
|
||||
{
|
||||
smoothedMs = (alpha * batchAvgMs) + ((1.0 - alpha) * smoothedMs);
|
||||
}
|
||||
|
||||
if (smoothedMs > 0)
|
||||
{
|
||||
// 直接用實測值,不對齊到 250 / 500 / 1000... 等標準值
|
||||
recentRates.Add((int)Math.Round(1000.0 / smoothedMs));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
timeSinceLastEvent += Time.unscaledDeltaTime;
|
||||
if (e.dx == 0 && e.dy == 0) continue; // 只有按鍵變化的回報不算
|
||||
double gap = e.t - lastEventTime;
|
||||
if (lastEventTime > 0 && gap > 0 && gap < MaxGap) gaps.Add(gap);
|
||||
lastEventTime = e.t;
|
||||
idleTimer = 0f;
|
||||
}
|
||||
idleTimer += Time.unscaledDeltaTime;
|
||||
|
||||
if (timeSinceLastEvent >= idleTimeout)
|
||||
{
|
||||
recentRates.Clear();
|
||||
smoothedMs = 0;
|
||||
}
|
||||
|
||||
int maxHz = 0;
|
||||
if (recentRates.Count > 0)
|
||||
{
|
||||
foreach (int rate in recentRates)
|
||||
{
|
||||
if (rate >= maxHz) maxHz = rate;
|
||||
}
|
||||
|
||||
if (recentRates.Count > 60)
|
||||
{
|
||||
recentRates.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
// 每 updateInterval 秒才更新一次文字,數值沒變就不重設
|
||||
displayTimer += Time.unscaledDeltaTime;
|
||||
if (displayTimer < updateInterval) return;
|
||||
displayTimer = 0f;
|
||||
|
||||
if (pollingDisplay != null && maxHz != shownHz)
|
||||
// 取間隔的中位數 : 滑鼠慢慢動時偶爾會少送幾筆,中位數比平均穩定
|
||||
if (gaps.Count >= 10)
|
||||
{
|
||||
pollingDisplay.text = $"滑鼠回報率: {maxHz} Hz";
|
||||
shownHz = maxHz;
|
||||
gaps.Sort();
|
||||
int hz = Mathf.RoundToInt((float)(1.0 / gaps[gaps.Count / 2]));
|
||||
SetText($"滑鼠回報率: {hz} Hz", hz);
|
||||
}
|
||||
else if (idleTimer > idleTimeout)
|
||||
{
|
||||
SetText("滑鼠回報率: 0 Hz", 0);
|
||||
}
|
||||
gaps.Clear();
|
||||
}
|
||||
|
||||
#region Win32 API 匯入
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool UnhookWindowsHookEx(IntPtr hk);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr CallNextHookEx(IntPtr hk, int nCode, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
#endregion
|
||||
}
|
||||
void SetText(string text, int code)
|
||||
{
|
||||
if (pollingDisplay == null || code == shownHz) return;
|
||||
shownHz = code;
|
||||
pollingDisplay.text = text;
|
||||
}
|
||||
}
|
||||
@@ -92,17 +92,23 @@ public class SettingsSaver : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// 要勾同意書,而且 Raw Input 正常運作 (沒有它就拿不到每一筆的真實時間,資料不能用) ; 測試不受影響
|
||||
void OnConsentChanged(bool isOn)
|
||||
{
|
||||
if (startButton != null)
|
||||
{
|
||||
startButton.interactable = isOn;
|
||||
startButton.interactable = isOn && RawMouseInput.IsRunning;
|
||||
}
|
||||
}
|
||||
|
||||
void OnStartClicked()
|
||||
{
|
||||
if (consentToggle != null && !consentToggle.isOn) return;
|
||||
if (!RawMouseInput.IsRunning)
|
||||
{
|
||||
Debug.LogWarning($"Raw Input 沒有運作 ({RawMouseInput.Status}),無法開始收集資料");
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectPanel != null)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user