MAVRIC-2127: Updated guesture calculation based on Unity' XRI AR touch gestures

This commit is contained in:
Brown, Caleb M 2023-10-30 10:03:26 -04:00
parent 319ab2a11c
commit 4dee95e110
4 changed files with 257 additions and 166 deletions

View File

@ -210,35 +210,7 @@ MonoBehaviour:
m_Calls:
- m_Target: {fileID: 8943117819789651820}
m_TargetAssemblyTypeName: TwistContent, Assembly-CSharp
m_MethodName: OnDeltaTwistRotation
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: 1de7c2f1-e194-4530-8421-61e7edde47b2
m_ActionName: Player/DeltaTwistRotation
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2515947263796688530}
m_TargetAssemblyTypeName: ARContent.RotateContent, Assembly-CSharp
m_MethodName: OnFingerCount
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 8943117819789651820}
m_TargetAssemblyTypeName: TwistContent, Assembly-CSharp
m_MethodName: OnFingerCount
m_MethodName: OnTouchPrimary
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
@ -250,7 +222,7 @@ MonoBehaviour:
m_CallState: 2
- m_Target: {fileID: 6917086848295834418}
m_TargetAssemblyTypeName: PinchContent, Assembly-CSharp
m_MethodName: OnFingerCount
m_MethodName: OnPrimaryTouch
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
@ -260,8 +232,52 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: 4cc6c379-bcc6-4b0a-b544-f2bfa86d0779
m_ActionName: Player/FIngerCount
m_ActionId: ad5858a6-12a5-4b9f-99d2-1c63e3a4dbac
m_ActionName: Player/TouchPrimary[/Device Simulator Touchscreen/primaryTouch]
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 8943117819789651820}
m_TargetAssemblyTypeName: TwistContent, Assembly-CSharp
m_MethodName: OnTouchSecondary
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 6917086848295834418}
m_TargetAssemblyTypeName: PinchContent, Assembly-CSharp
m_MethodName: OnSecondaryTouch
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: e4462204-50dc-4f1d-93a7-150be1a5438b
m_ActionName: Player/TouchSecondary[/Device Simulator Touchscreen/touch1]
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 8943117819789651820}
m_TargetAssemblyTypeName: TwistContent, Assembly-CSharp
m_MethodName: OnTwist
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: 08d08992-8fcc-4776-86a9-6641a3bae7d4
m_ActionName: Player/TwistMouse[/Mouse/scroll/y]
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 6917086848295834418}
@ -276,8 +292,8 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: 662e4120-3501-488a-82ea-fcd73f5b38b2
m_ActionName: Player/New action
m_ActionId: 31b461b0-6bfe-4875-aeca-f94c3b6fb55a
m_ActionName: Player/Pinch[/Keyboard/shift,/Mouse/scroll/y]
m_NeverAutoSwitchControlSchemes: 0
m_DefaultControlScheme:
m_DefaultActionMap: Player
@ -307,15 +323,15 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6966010940186050953}
m_Enabled: 0
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7897338c349a8e541a96b33b0b2f289f, type: 3}
m_Name:
m_EditorClassIdentifier:
inputDeadzone: 0.001
speed: 10
twistDelta: 0
fingerCount: 0
mouseInput: 0
--- !u!114 &6917086848295834418
MonoBehaviour:
m_ObjectHideFlags: 0
@ -323,7 +339,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6966010940186050953}
m_Enabled: 0
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9522cb591cd668044b208224fdec61a3, type: 3}
m_Name:

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public class PinchContent : MonoBehaviour
{
@ -14,10 +15,45 @@ public class PinchContent : MonoBehaviour
[SerializeField, ReadOnly] private float input;
[SerializeField, ReadOnly] private int fingerCount;
private TouchState touch1;
private TouchState touch2;
private void Update()
{
Pinch(input);
var delta = input;
CalculateTouchPinch(ref delta);
Pinch(delta);
}
/// <summary>
///
/// </summary>
/// <param name="delta"></param>
/// <seealso cref="UnityEngine.XR.Interaction.Toolkit.AR.PinchGesture"/>
private void CalculateTouchPinch(ref float delta)
{
if (fingerCount != 2) return;
if (touch1.delta.sqrMagnitude <= inputDeadzone &&
touch2.delta.sqrMagnitude <= inputDeadzone) return;
var startPosition1 = touch1.startPosition;
var startPosition2 = touch2.startPosition;
Vector3 firstToSecondDirection = (startPosition1 - startPosition2).normalized;
var dot1 = Vector3.Dot(touch1.delta.normalized, -firstToSecondDirection);
var dot2 = Vector3.Dot(touch2.delta.normalized, firstToSecondDirection);
var dotThreshold = Mathf.Cos(inputDeadzone * Mathf.Deg2Rad);
// Check angle of motion for the first touch.
if (touch1.delta != Vector2.zero && Mathf.Abs(dot1) < dotThreshold) return;
// Check angle of motion for the second touch.
if (touch2.delta != Vector2.zero && Mathf.Abs(dot2) < dotThreshold) return;
delta = (touch1.position - touch2.position).magnitude;
}
private Vector3 ClampScale(Vector3 value, float min, float max)
@ -44,15 +80,34 @@ public class PinchContent : MonoBehaviour
public void OnPinch(InputAction.CallbackContext context)
{
return;
input = context.ReadValue<float>();
Debug.Log($"Pinch delta: {input}");
}
public void OnFingerCount(InputAction.CallbackContext context)
public void OnPrimaryTouch(InputAction.CallbackContext context)
{
fingerCount = context.ReadValue<int>();
if (context.phase == InputActionPhase.Started)
{
fingerCount++;
}
else if (context.phase == InputActionPhase.Canceled)
{
fingerCount--;
}
touch1 = context.ReadValue<TouchState>();
}
public void OnSecondaryTouch(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Started)
{
fingerCount++;
}
else if (context.phase == InputActionPhase.Canceled)
{
fingerCount--;
}
touch2 = context.ReadValue<TouchState>();
}
}

View File

@ -1,42 +1,108 @@
using System;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public class TwistContent : MonoBehaviour
{
[SerializeField] private float inputDeadzone = 0.001f;
[SerializeField] private float speed = 10f;
[SerializeField, ReadOnly] private float twistDelta;
[SerializeField, ReadOnly] private int fingerCount;
[SerializeField, ReadOnly] private float mouseInput;
private TouchState touch1;
private TouchState touch2;
private Vector2 previousPosition1, previousPosition2;
private float deltaRotation;
private void Update()
{
Twist(twistDelta);
deltaRotation = mouseInput;
CalculateTouchTwist(ref deltaRotation);
Twist(deltaRotation);
deltaRotation = 0f;
}
public void Twist(float input)
private void CalculateTouchTwist(ref float delta)
{
if (Mathf.Abs(input) <= inputDeadzone) return;
if (fingerCount != 2) return;
if (touch1.delta.sqrMagnitude <= inputDeadzone &&
touch2.delta.sqrMagnitude <= inputDeadzone) return;
// Determine the amount of rotation to apply
float rotationAmount = input * speed * Time.deltaTime;
delta = CalculateDeltaRotation(
touch1.position,
touch2.position,
previousPosition1,
previousPosition2);
// Apply the rotation
transform.Rotate(Vector3.forward, rotationAmount, Space.Self);
previousPosition1 = touch1.position;
previousPosition2 = touch2.position;
}
/// <summary>
/// Calculates a signed angle for how much to twist according to the movement of two touch positions.
/// </summary>
/// <param name="currentPosition1">Current position of the first touch.</param>
/// <param name="currentPosition2">Current position of the second touch.</param>
/// <param name="previousPosition1">Previous position of the first touch.</param>
/// <param name="previousPosition2">Previous position of the second touch.</param>
/// <returns>A signed angle, in degrees, representing how much to rotate an interactable by according to the changes in the two positions passed in.</returns>
/// <seealso cref="UnityEngine.XR.Interaction.Toolkit.AR.TwistGesture"/>
protected static float CalculateDeltaRotation(
Vector2 currentPosition1,
Vector2 currentPosition2,
Vector2 previousPosition1,
Vector2 previousPosition2)
{
var currentDirection = (currentPosition1 - currentPosition2).normalized;
var previousDirection = (previousPosition1 - previousPosition2).normalized;
var sign = Mathf.Sign((previousDirection.x * currentDirection.y) -
(previousDirection.y * currentDirection.x));
return Vector2.Angle(currentDirection, previousDirection) * sign;
}
public void OnDeltaTwistRotation(InputAction.CallbackContext context)
public void Twist(float delta)
{
return;
twistDelta = context.ReadValue<float>();
Debug.Log($"Twist delta: {twistDelta}");
transform.Rotate(Vector3.forward, delta * speed * Time.deltaTime);
}
public void OnFingerCount(InputAction.CallbackContext context)
public void OnTwist(InputAction.CallbackContext context)
{
fingerCount = context.ReadValue<int>();
mouseInput = context.ReadValue<float>();
}
public void OnTouchPrimary(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Started)
{
fingerCount++;
}
else if (context.phase == InputActionPhase.Canceled)
{
fingerCount--;
}
touch1 = context.ReadValue<TouchState>();
}
public void OnTouchSecondary(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Started)
{
fingerCount++;
}
else if (context.phase == InputActionPhase.Canceled)
{
fingerCount--;
}
touch2 = context.ReadValue<TouchState>();
}
}

View File

@ -23,15 +23,6 @@
"interactions": "",
"initialStateCheck": true
},
{
"name": "FIngerCount",
"type": "Value",
"id": "4cc6c379-bcc6-4b0a-b544-f2bfa86d0779",
"expectedControlType": "Integer",
"processors": "",
"interactions": "",
"initialStateCheck": true
},
{
"name": "RotateXY",
"type": "PassThrough",
@ -42,22 +33,40 @@
"initialStateCheck": false
},
{
"name": "DeltaTwistRotation",
"name": "TouchPrimary",
"type": "Value",
"id": "1de7c2f1-e194-4530-8421-61e7edde47b2",
"expectedControlType": "Axis",
"id": "ad5858a6-12a5-4b9f-99d2-1c63e3a4dbac",
"expectedControlType": "Touch",
"processors": "",
"interactions": "",
"initialStateCheck": true
},
{
"name": "DeltaPinch",
"name": "TouchSecondary",
"type": "Value",
"id": "662e4120-3501-488a-82ea-fcd73f5b38b2",
"expectedControlType": "Vector2",
"id": "e4462204-50dc-4f1d-93a7-150be1a5438b",
"expectedControlType": "Touch",
"processors": "",
"interactions": "",
"initialStateCheck": true
},
{
"name": "Twist",
"type": "PassThrough",
"id": "08d08992-8fcc-4776-86a9-6641a3bae7d4",
"expectedControlType": "Axis",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{
"name": "Pinch",
"type": "PassThrough",
"id": "31b461b0-6bfe-4875-aeca-f94c3b6fb55a",
"expectedControlType": "Axis",
"processors": "",
"interactions": "",
"initialStateCheck": false
}
],
"bindings": [
@ -215,148 +224,93 @@
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "",
"id": "d8b34392-805b-4ad9-8c9a-e196a30c3fa3",
"path": "<Touchscreen>/primaryTouch",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "TouchPrimary",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "0fc1db42-6303-4d3c-a708-41ed2379eb89",
"path": "<Touchscreen>/touch1",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "TouchSecondary",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "Mouse",
"id": "53d94f79-69da-46c7-9ae2-76f2c00f8ff2",
"id": "444f291b-3958-4a35-9e2f-3b53eaebe739",
"path": "OneModifier",
"interactions": "",
"processors": "",
"groups": "",
"action": "DeltaTwistRotation",
"action": "Twist",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "modifier",
"id": "33986f48-87aa-4795-b2bf-2a9a649b95f2",
"id": "c056ecf1-c3a4-448b-9279-a0a508e362d6",
"path": "<Mouse>/rightButton",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "DeltaTwistRotation",
"action": "Twist",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "binding",
"id": "9c700b13-1ae6-4a33-8e5f-ddad987f710b",
"id": "1f8a6f66-51e5-4ef8-b96b-20b2b501d7e0",
"path": "<Mouse>/scroll/y",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "DeltaTwistRotation",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "Touch",
"id": "1c1d05c7-0ba0-4fab-af40-fe1e184e75eb",
"path": "OneModifier",
"interactions": "",
"processors": "",
"groups": "",
"action": "DeltaTwistRotation",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "modifier",
"id": "f67c1ebb-46da-4622-b3e9-ad3ade32401d",
"path": "<Touchscreen>/touch*/Press",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "DeltaTwistRotation",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "binding",
"id": "646a179f-d96c-4e6a-909f-4887ae22b7bc",
"path": "<TouchscreenGestureInputController>/twistDeltaRotation",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "DeltaTwistRotation",
"action": "Twist",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "Mouse",
"id": "c29f7796-8ff6-4a11-9870-dc73aa62caac",
"id": "fb3e64a9-bc45-4c2e-8ca4-e4531f890b9a",
"path": "OneModifier",
"interactions": "",
"processors": "",
"groups": "",
"action": "DeltaPinch",
"action": "Pinch",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "modifier",
"id": "b8a26aa8-e014-4510-8e19-c1c11514db07",
"path": "<Mouse>/leftButton",
"id": "03ecf8db-946c-4ec6-9b41-f5e44b7db8af",
"path": "<Keyboard>/shift",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "DeltaPinch",
"action": "Pinch",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "binding",
"id": "95832974-77ef-4056-a92d-645ca43c9d3c",
"id": "d4553403-a86d-4199-8ffc-663fa9cc6c16",
"path": "<Mouse>/scroll/y",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "DeltaPinch",
"action": "Pinch",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "Touch",
"id": "b61a9a31-410d-42a9-8cc0-f480f11af1dc",
"path": "OneModifier",
"interactions": "",
"processors": "",
"groups": "",
"action": "DeltaPinch",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "modifier",
"id": "4c884337-3664-4b59-b131-2be138e35b2d",
"path": "<Touchscreen>/touch*/Press",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "DeltaPinch",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "binding",
"id": "3cbd2f60-5c45-44e4-9619-95399f17dfe1",
"path": "<TouchscreenGestureInputController>/pinchGapDelta",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "DeltaPinch",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "",
"id": "a36cc4fd-87f9-4e36-acff-a170c4ad8aec",
"path": "<TouchscreenGestureInputController>/fingerCount",
"interactions": "",
"processors": "",
"groups": "Touch",
"action": "FIngerCount",
"isComposite": false,
"isPartOfComposite": false
}
]
}