Skip to content

Commit 6eab22e

Browse files
UniversalLanguageAlgorithmOkDankeveleigh
authored
Add option for framerate independent movement in Input Simulator (#1011)
* Add option for framerate independent movement speed in Input Simulator (FR #1005) Added checkbox in the InputSimulator inspector * Update MRTKInputSimulator.prefab * framerate -> frameRate * Add a few missing docs * Remove unused field --------- Co-authored-by: OkDan <danzlo.okulov@proton.me> Co-authored-by: Kurtis <keveleig@qti.qualcomm.com>
1 parent f52cded commit 6eab22e

5 files changed

Lines changed: 51 additions & 7 deletions

File tree

org.mixedrealitytoolkit.input/Editor/Inspectors/CameraSimulationSettingsDrawer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CameraSimulationSettingsDrawer : PropertyDrawer
1818

1919
private readonly GUIContent moveSpeedContent = new GUIContent("Speed");
2020
private readonly GUIContent moveSmoothingContent = new GUIContent("Smoothed");
21+
private readonly GUIContent frameRateIndependentContent = new GUIContent("Frame Rate Independent");
2122
private readonly GUIContent moveDepthContent = new GUIContent("Depth");
2223
private readonly GUIContent moveHorizontalContent = new GUIContent("Horizontal");
2324
private readonly GUIContent moveVerticalContent = new GUIContent("Vertical");
@@ -78,6 +79,7 @@ public override void OnGUI(
7879

7980
SerializedProperty moveSpeed = property.FindPropertyRelative("moveSpeed");
8081
SerializedProperty moveSmoothing = property.FindPropertyRelative("isMovementSmoothed");
82+
SerializedProperty frameRateIndependent = property.FindPropertyRelative("isFrameRateIndependent");
8183
SerializedProperty moveDepth = property.FindPropertyRelative("moveDepth");
8284
SerializedProperty moveHorizontal = property.FindPropertyRelative("moveHorizontal");
8385
SerializedProperty moveVertical = property.FindPropertyRelative("moveVertical");
@@ -97,6 +99,13 @@ public override void OnGUI(
9799
++rowMultiplier,
98100
PropertyDrawerUtilities.Height),
99101
moveSpeed, moveSpeedContent);
102+
EditorGUI.PropertyField(
103+
PropertyDrawerUtilities.GetPosition(
104+
position,
105+
PropertyDrawerUtilities.VerticalSpacing,
106+
++rowMultiplier,
107+
PropertyDrawerUtilities.Height),
108+
frameRateIndependent, frameRateIndependentContent);
100109
EditorGUI.PropertyField(
101110
PropertyDrawerUtilities.GetPosition(
102111
position,

org.mixedrealitytoolkit.input/Simulation/Devices/CameraSimulationSettings.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ public bool IsMovementSmoothed
5858
set => isMovementSmoothed = value;
5959
}
6060

61+
[SerializeField]
62+
[Tooltip("Multiply resulting speed with deltaTime?")]
63+
private bool isFrameRateIndependent = true;
64+
65+
/// <summary>
66+
/// Multiply resulting speed with deltaTime?
67+
/// </summary>
68+
public bool IsFrameRateIndependent
69+
{
70+
get => isFrameRateIndependent;
71+
set => isFrameRateIndependent = value;
72+
}
73+
6174
[SerializeField]
6275
[Tooltip("The input action used to move the camera along the depth axis.")]
6376
private InputActionReference moveDepth;

org.mixedrealitytoolkit.input/Simulation/Devices/SimulatedHMD.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,50 @@ public void ResetToOrigin()
116116
/// <param name="isSmoothed">Should smoothing be applied to camera movement?</param>
117117
/// <param name="moveSpeed">Multiplier used to adjust how fast the camera moves.</param>
118118
/// <param name="rotationSensitivity">Multiplier used to adjust the sensitivity of the camera rotation.</param>
119+
/// <param name="frameRateIndependent">Multiply resulting speed with deltaTime?</param>
119120
public void Update(
120121
Vector3 moveDelta,
121122
Vector3 rotationDelta,
122123
bool isSmoothed = true,
123124
float moveSpeed = 1f,
124-
float rotationSensitivity = 1f)
125+
float rotationSensitivity = 1f,
126+
bool frameRateIndependent = true)
125127
{
126128
using (UpdatePerfMarker.Auto())
127129
{
128130
if (simulatedHmd == null) { return; }
129131

130132
// Perform smoothing on move delta.
133+
if (frameRateIndependent)
134+
{
135+
smoothedMoveDelta = isSmoothed ?
136+
Smoothing.SmoothTo(smoothedMoveDelta, moveDelta, moveSmoothingTime, Time.deltaTime) :
137+
100f * Time.deltaTime * moveDelta;
138+
}
131139
// Yes; this isn't framerate independent. However; there's currently a Unity editor bug
132140
// wherein the *polling rate* of your mouse can cause severe lagspikes in the
133141
// editor; this causes deltaTimes to go all over the place while moving your mouse.
134142
// Using fixedDeltaTime is a good workaround until this bug is resolved.
135-
smoothedMoveDelta = isSmoothed ?
136-
Smoothing.SmoothTo(smoothedMoveDelta, moveDelta, moveSmoothingTime, Time.fixedDeltaTime) :
137-
moveDelta;
138-
143+
else
144+
{
145+
smoothedMoveDelta = isSmoothed ?
146+
Smoothing.SmoothTo(smoothedMoveDelta, moveDelta, moveSmoothingTime, Time.fixedDeltaTime) :
147+
moveDelta;
148+
}
149+
139150
simulatedHmdState.trackingState = (int)(InputTrackingState.Position | InputTrackingState.Rotation);
140151

141152
// HMD poses are relative to the camera floor offset.
142153
Transform origin = PlayspaceUtilities.XROrigin.CameraFloorOffsetObject.transform;
143-
Vector3 cameraPosition = simulatedHmdState.centerEyePosition + Quaternion.Inverse(origin.rotation) * Camera.main.transform.rotation * (smoothedMoveDelta * moveSpeed);
154+
Vector3 cameraPosition = simulatedHmdState.centerEyePosition;
155+
if (frameRateIndependent)
156+
{
157+
cameraPosition += Quaternion.Inverse(origin.rotation) * Camera.main.transform.rotation * (1000f * moveSpeed * Time.deltaTime * smoothedMoveDelta);
158+
}
159+
else
160+
{
161+
cameraPosition += Quaternion.Inverse(origin.rotation) * Camera.main.transform.rotation * (smoothedMoveDelta * moveSpeed);
162+
}
144163

145164
// Update camera rotation
146165
cameraRotation += rotationDelta * rotationSensitivity;

org.mixedrealitytoolkit.input/Simulation/InputSimulator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ private void UpdateSimulatedHMD()
203203
rotationDelta,
204204
CameraSettings.IsMovementSmoothed,
205205
CameraSettings.MoveSpeed,
206-
CameraSettings.RotationSensitivity);
206+
CameraSettings.RotationSensitivity,
207+
CameraSettings.IsFrameRateIndependent);
207208
}
208209
}
209210

org.mixedrealitytoolkit.input/Simulation/Prefabs/MRTKInputSimulator.prefab

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ MonoBehaviour:
5050
simulationEnabled: 1
5151
originOffset: {x: 0, y: 0, z: 0}
5252
isMovementSmoothed: 1
53+
isFrameRateIndependent: 1
5354
moveDepth: {fileID: 7299012687604269993, guid: 70b361cf3c037d54896482d1ffd9f999, type: 3}
5455
moveHorizontal: {fileID: 8718149717473967433, guid: 70b361cf3c037d54896482d1ffd9f999, type: 3}
5556
moveVertical: {fileID: 8557015737124631664, guid: 70b361cf3c037d54896482d1ffd9f999, type: 3}
@@ -61,6 +62,7 @@ MonoBehaviour:
6162
invertPitch: 0
6263
eyeGazeSettings:
6364
simulationEnabled: 1
65+
isTracked: 1
6466
eyeOriginOffset: {x: 0, y: 0, z: 0}
6567
lookHorizontal: {fileID: -7345170528461143681, guid: 70b361cf3c037d54896482d1ffd9f999, type: 3}
6668
lookVertical: {fileID: -4223466768389085971, guid: 70b361cf3c037d54896482d1ffd9f999, type: 3}

0 commit comments

Comments
 (0)