Skip to content

Commit 879f0c5

Browse files
authored
Fix pose unlock smooths in wrong coordinate space (#145)
* Define IinputRig * Introduce IController.TryGetPose * Remove a few camera.main references to test
1 parent 5173af8 commit 879f0c5

12 files changed

+95
-42
lines changed

Runtime/Input/Controllers/BaseController.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ public void TryRenderControllerModel()
196196
return;
197197
}
198198

199-
var rigTransform = Camera.main.transform.parent;
200-
199+
var rigTransform = InputService.InputRig.RigTransform;
201200
var controllerObject = Object.Instantiate(controllerPrefab, rigTransform);
202201
controllerObject.name = GetType().Name;
203202
Visualizer = controllerObject.GetComponent<IControllerVisualizer>();
@@ -242,5 +241,27 @@ public void TryRenderControllerModel()
242241
Debug.LogError($"{GetType().Name} prefab must have a {nameof(IControllerVisualizer)} component attached.");
243242
}
244243
}
244+
245+
/// <inheritdoc />
246+
public bool TryGetPose(Space space, out Pose pose)
247+
{
248+
if (!IsPositionAvailable || !IsRotationAvailable)
249+
{
250+
pose = default;
251+
return false;
252+
}
253+
254+
if (space == Space.Self)
255+
{
256+
pose = Pose;
257+
return true;
258+
}
259+
260+
pose = new Pose(
261+
InputService.InputRig.RigTransform.TransformPoint(Pose.position),
262+
InputService.InputRig.RigTransform.rotation * Pose.rotation);
263+
264+
return true;
265+
}
245266
}
246267
}

Runtime/Input/Controllers/BaseControllerVisualizer.cs

-23
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public abstract class BaseControllerVisualizer : ControllerPoseSynchronizer, ICo
2121
/// <inheritdoc />
2222
public GameObject GameObject => gameObject;
2323

24-
/// <inheritdoc />
25-
public Pose SourcePose { get; private set; }
26-
2724
/// <inheritdoc />
2825
public bool OverrideSourcePose { get; set; }
2926

@@ -44,11 +41,6 @@ public Transform GripPose
4441
/// <inheritdoc />
4542
public override void OnSourcePoseChanged(SourcePoseEventData<Pose> eventData)
4643
{
47-
if (eventData.SourceId == Controller?.InputSource.SourceId)
48-
{
49-
SourcePose = eventData.SourceData;
50-
}
51-
5244
if (OverrideSourcePose)
5345
{
5446
return;
@@ -60,11 +52,6 @@ public override void OnSourcePoseChanged(SourcePoseEventData<Pose> eventData)
6052
/// <inheritdoc />
6153
public override void OnSourcePoseChanged(SourcePoseEventData<Quaternion> eventData)
6254
{
63-
if (eventData.SourceId == Controller?.InputSource.SourceId)
64-
{
65-
SourcePose = new Pose(SourcePose.position, eventData.SourceData);
66-
}
67-
6855
if (OverrideSourcePose)
6956
{
7057
return;
@@ -76,11 +63,6 @@ public override void OnSourcePoseChanged(SourcePoseEventData<Quaternion> eventDa
7663
/// <inheritdoc />
7764
public override void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData)
7865
{
79-
if (eventData.SourceId == Controller?.InputSource.SourceId)
80-
{
81-
SourcePose = new Pose(eventData.SourceData, SourcePose.rotation);
82-
}
83-
8466
if (OverrideSourcePose)
8567
{
8668
return;
@@ -92,11 +74,6 @@ public override void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData)
9274
/// <inheritdoc />
9375
public override void OnSourcePoseChanged(SourcePoseEventData<Vector3> eventData)
9476
{
95-
if (eventData.SourceId == Controller?.InputSource.SourceId)
96-
{
97-
SourcePose = new Pose(eventData.SourceData, SourcePose.rotation);
98-
}
99-
10077
if (OverrideSourcePose)
10178
{
10279
return;

Runtime/Input/Controllers/IController.cs

+9
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,14 @@ public interface IController
108108
/// Updates the <see cref="IController"/>'s state.
109109
/// </summary>
110110
void UpdateController();
111+
112+
/// <summary>
113+
/// Attempts to retrieve the <see cref="IController"/>'s pose in the scene in either the local space,
114+
/// that is in the <see cref="IInputRig.RigTransform"/> space or in world space.
115+
/// </summary>
116+
/// <param name="space">The space to get the <see cref="UnityEngine.Pose"/> in.</param>
117+
/// <param name="pose">The pose.</param>
118+
/// <returns><c>true</c>, if found Will return <c>false</c>, if not <see cref="IsPositionAvailable"/> or <see cref="IsRotationAvailable"/>.</returns>
119+
bool TryGetPose(Space space, out Pose pose);
111120
}
112121
}

Runtime/Input/Controllers/IControllerVisualizer.cs

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ public interface IControllerVisualizer : IControllerPoseSynchronizer
1818
/// </remarks>
1919
GameObject GameObject { get; }
2020

21-
/// <summary>
22-
/// This is the actual pose of this controller, regardless of <see cref="OverrideSourcePose"/>
23-
/// and the <see cref="IControllerPoseSynchronizer.PoseDriver"/> pose.
24-
/// </summary>
25-
Pose SourcePose { get; }
26-
2721
/// <summary>
2822
/// If set, the <see cref="IControllerPoseSynchronizer.PoseDriver"/>'s pose in the scene
2923
/// is override and the actual <see cref="IController.InputSource"/> pose is ignored.

Runtime/Input/Cursors/MeshCursor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ private void LateUpdate()
6262
if (targetRenderer == null) { return; }
6363

6464
var targetTransform = targetRenderer.transform;
65-
var targetCamera = Camera.main;
65+
var targetCamera = InputService.InputRig.CameraTransform;
6666

6767
var cameraPosition = targetCamera.transform.position;
6868
var distance = (cameraPosition - targetTransform.position).magnitude;
69-
var size = distance * fixedSize * targetCamera.fieldOfView;
69+
var size = distance * fixedSize * InputService.InputRig.RigCamera.fieldOfView;
7070

7171
targetTransform.localScale = Vector3.one * size;
7272
targetTransform.localPosition = new Vector3(fixedSizeOffset.x * size, fixedSizeOffset.y * size, fixedSizeOffset.z * size);

Runtime/Input/Hands/HandController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,10 @@ public bool TryGetJointPose(HandJoint joint, out Pose pose, Space relativeTo = S
788788
};
789789

790790
// Translate to world space.
791-
if (Camera.main.transform.parent.IsNotNull())
791+
if (InputService.InputRig.RigTransform.IsNotNull())
792792
{
793-
pose.position = Camera.main.transform.parent.TransformPoint(pose.position);
794-
pose.rotation = Camera.main.transform.parent.rotation * pose.rotation;
793+
pose.position = InputService.InputRig.RigTransform.TransformPoint(pose.position);
794+
pose.rotation = InputService.InputRig.RigTransform.rotation * pose.rotation;
795795
}
796796

797797
return lastHandRootPose != Pose.identity;

Runtime/Input/Hands/HandDataPostProcessor.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright (c) Reality Collective. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using RealityCollective.ServiceFramework.Services;
45
using RealityCollective.Utilities.Extensions;
56
using RealityToolkit.Definitions.Devices;
67
using RealityToolkit.Input.Definitions;
78
using RealityToolkit.Input.Hands.Poses;
9+
using RealityToolkit.Input.Interfaces;
810
using System.Collections.Generic;
911
using UnityEngine;
1012

@@ -38,9 +40,9 @@ private static Camera PlayerCamera
3840
{
3941
get
4042
{
41-
if (playerCamera == null)
43+
if (playerCamera.IsNull())
4244
{
43-
playerCamera = Camera.main;
45+
playerCamera = ServiceManager.Instance.GetService<IInputService>().InputRig.RigCamera;
4446
}
4547

4648
return playerCamera;

Runtime/Input/InputService.cs

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public InputService(string name, uint priority, InputServiceProfile profile)
7070
/// <inheritdoc/>
7171
public bool FarInteractionEnabled { get; set; }
7272

73+
/// <inheritdoc/>
74+
public IInputRig InputRig { get; set; }
75+
7376
private readonly HashSet<IInputSource> detectedInputSources = new HashSet<IInputSource>();
7477

7578
/// <inheritdoc />

Runtime/Input/InteractionBehaviours/LockControllerVisualizerBehaviour.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ protected override void Update()
6060
continue;
6161
}
6262

63-
var unlockPose = visualizer.SourcePose;
64-
unlockPose.position = Vector3.Slerp(smoothingStartPose[visualizer].position, unlockPose.position, smoothingProgress[visualizer]);
65-
unlockPose.rotation = Quaternion.Slerp(smoothingStartPose[visualizer].rotation, unlockPose.rotation, smoothingProgress[visualizer]);
66-
visualizer.PoseDriver.SetPositionAndRotation(unlockPose.position, unlockPose.rotation);
63+
if (visualizer.Controller.TryGetPose(Space.World, out var unlockPose))
64+
{
65+
unlockPose.position = Vector3.Slerp(smoothingStartPose[visualizer].position, unlockPose.position, smoothingProgress[visualizer]);
66+
unlockPose.rotation = Quaternion.Slerp(smoothingStartPose[visualizer].rotation, unlockPose.rotation, smoothingProgress[visualizer]);
67+
visualizer.PoseDriver.SetPositionAndRotation(unlockPose.position, unlockPose.rotation);
68+
}
6769
}
6870
else
6971
{

Runtime/Input/Interfaces/IInputRig.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Reality Collective. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using UnityEngine;
5+
6+
namespace RealityToolkit.Input.Interfaces
7+
{
8+
/// <summary>
9+
/// The <see cref="IInputRig"/> defines the coordinate space in which <see cref="IInputService"/> objects, such as
10+
/// <see cref="IInputSource"/>s live. It is used to perform pose transformations for controllers, visualizers and interactors.
11+
/// </summary>
12+
public interface IInputRig
13+
{
14+
/// <summary>
15+
/// The root rig <see cref="Transform"/>.
16+
/// </summary>
17+
Transform RigTransform { get; }
18+
19+
/// <summary>
20+
/// The <see cref="Transform"/> where the <see cref="Camera"/> component is located.
21+
/// </summary>
22+
Transform CameraTransform { get; }
23+
24+
/// <summary>
25+
/// The rig's <see cref="Camera"/> reference.
26+
/// </summary>
27+
Camera RigCamera { get; }
28+
}
29+
}

Runtime/Input/Interfaces/IInputRig.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Input/Interfaces/IInputService.cs

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public interface IInputService : IEventService, IRealityToolkitService
4040
/// </summary>
4141
bool FarInteractionEnabled { get; set; }
4242

43+
/// <summary>
44+
/// The active <see cref="IInputRig"/> the <see cref="IInputService"/> is using.
45+
/// </summary>
46+
IInputRig InputRig { get; set; }
47+
4348
/// <summary>
4449
/// List of the Interaction Input Sources as detected by the input manager like hands or motion controllers.
4550
/// </summary>

0 commit comments

Comments
 (0)