Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting remaining MRTK2 constraints for completeness #11085

Merged
merged 5 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion com.microsoft.mrtk.core/Utilities/AxisFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,22 @@ public enum AxisFlags
/// </summary>
ZAxis = 1 << 2
}
}

/// <summary>
/// Extension methods specific to the <see cref="InteractionFlags"/> enum.
/// </summary>
public static class AxisFlagsExtensions
{
/// <summary>
/// Checks to determine if all bits in a provided mask are set.
/// </summary>
/// <param name="a"><see cref="AxisFlags"/> value.</param>
/// <param name="b"><see cref="AxisFlags"/> mask.</param>
/// <returns>True if all of the bits in the specified mask are set in the
/// current value.</returns>
public static bool IsMaskSet(this AxisFlags a, AxisFlags b)
{
return ((a & b) == b);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
/// <summary>
/// Manages constraints for a given object and ensures that Scale/Rotation/Translation
/// constraints are executed separately.
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
[HelpURL("https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/ux-building-blocks/constraint-manager")]
[AddComponentMenu("MRTK/Spatial Manipulation/Constraint Manager")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
/// <summary>
/// Component for fixing the rotation of a manipulated object such that
/// it always faces or faces away from the user
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
[AddComponentMenu("MRTK/Spatial Manipulation/Face User Constraint")]
public class FaceUserConstraint : TransformConstraint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Component for setting the min/max scale values for ObjectManipulator
/// or BoundsControl
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
public class FixedDistanceConstraint : TransformConstraint
{
#region Properties

[SerializeField]
[Tooltip("Transform to fix distance to. Defaults to the head.")]
private Transform constraintTransform = null;

/// <summary>
/// Transform to fix distance to. Defaults to the head.
/// </summary>
public Transform ConstraintTransform
{
get => constraintTransform;
set => constraintTransform = value;
}

public override TransformFlags ConstraintType => TransformFlags.Move;

private float distanceAtManipulationStart;

#endregion Properties

#region MonoBehaviour Methods

public void Start()
{
EnsureConstraintTransform();
}

#endregion MonoBehaviour Methods

#region Public Methods

/// <inheritdoc />
public override void Initialize(MixedRealityTransform worldPose)
{
base.Initialize(worldPose);
EnsureConstraintTransform();
distanceAtManipulationStart = Vector3.Distance(worldPose.Position, constraintTransform.position);
}

/// <summary>
/// Constrains position such that the distance between pose and
/// the ConstraintTransform does not change from manipulation start
/// </summary>
public override void ApplyConstraint(ref MixedRealityTransform transform)
{
Vector3 constraintToPose = transform.Position - constraintTransform.position;
constraintToPose = constraintToPose.normalized * distanceAtManipulationStart;
transform.Position = constraintTransform.position + constraintToPose;
}

#endregion Public Methods

private void EnsureConstraintTransform()
{
if (ConstraintTransform == null)
{
ConstraintTransform = Camera.main.transform;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Component for fixing the rotation of a manipulated object relative to the user
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
public class FixedRotationToUserConstraint : TransformConstraint
{
#region Properties

public override TransformFlags ConstraintType => TransformFlags.Rotate;

private Quaternion startObjectRotationCameraSpace;

#endregion Properties

#region Public Methods

/// <inheritdoc />
public override void Initialize(MixedRealityTransform worldPose)
{
base.Initialize(worldPose);

startObjectRotationCameraSpace = Quaternion.Inverse(Camera.main.transform.rotation) * worldPose.Rotation;
}

/// <summary>
/// Updates the objects rotation so that the rotation relative to the user
/// is fixed
/// </summary>
public override void ApplyConstraint(ref MixedRealityTransform transform)
{
Vector3 euler = Camera.main.transform.rotation.eulerAngles;
// don't use roll (feels awkward) - just maintain yaw / pitch angle
transform.Rotation = Quaternion.Euler(euler.x, euler.y, 0) * startObjectRotationCameraSpace;
}

#endregion Public Methods
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Component for fixing the rotation of a manipulated object relative to the world
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
public class FixedRotationToWorldConstraint : TransformConstraint
{
#region Properties

public override TransformFlags ConstraintType => TransformFlags.Rotate;

#endregion Properties

#region Public Methods

/// <summary>
/// Fix rotation to the rotation from manipulation start
/// </summary>
public override void ApplyConstraint(ref MixedRealityTransform transform)
{
transform.Rotation = this.worldPoseOnManipulationStart.Rotation;
}

#endregion Public Methods
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Component for setting the min/max scale values for ObjectManipulator
/// or BoundsControl
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
public class MaintainApparentSizeConstraint : TransformConstraint
{
#region Properties

private float initialDist;

public override TransformFlags ConstraintType => TransformFlags.Scale;

#endregion Properties

#region Public Methods

/// <inheritdoc />
public override void Initialize(MixedRealityTransform worldPose)
{
base.Initialize(worldPose);

initialDist = (worldPose.Position - Camera.main.transform.position).magnitude;
}

/// <summary>
/// Constrains scale such that the object's apparent size at manipulation
/// start does not change when the object is moved towards and away from
/// the head.
/// </summary>
public override void ApplyConstraint(ref MixedRealityTransform transform)
{
float dist = (transform.Position - Camera.main.transform.position).magnitude;
transform.Scale = (dist / initialDist) * worldPoseOnManipulationStart.Scale;
}

#endregion Public Methods
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.MixedReality.Toolkit.SpatialManipulation
/// <summary>
/// Component for setting the min/max scale values for ObjectManipulator
/// or BoundsControl
/// We're looking to rework this system in the future. These existing components will be deprecated then.
/// </summary>
[AddComponentMenu("MRTK/Spatial Manipulation/Min Max Scale Constraint")]
public class MinMaxScaleConstraint : TransformConstraint
Expand Down
Loading