-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathRotationAxisConstraint.cs
More file actions
82 lines (70 loc) · 2.8 KB
/
RotationAxisConstraint.cs
File metadata and controls
82 lines (70 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using Unity.XR.CoreUtils.GUI;
using UnityEngine;
namespace MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Component for limiting the rotation axes for <see cref="ObjectManipulator"/>
/// or <see cref="BoundsControl"/> components.
/// </summary>
/// <remarks>
/// The constraint system might be reworked in the future. In such a case, these existing components will be deprecated.
/// </remarks>
[AddComponentMenu("MRTK/Spatial Manipulation/Rotation Axis Constraint")]
public class RotationAxisConstraint : TransformConstraint
{
#region Properties
[SerializeField, FlagsProperty]
[Tooltip("Constrain rotation about an axis")]
private AxisFlags constraintOnRotation = 0;
/// <summary>
/// Constrain rotation about an axis
/// </summary>
public AxisFlags ConstraintOnRotation
{
get => constraintOnRotation;
set => constraintOnRotation = value;
}
[SerializeField]
[Tooltip("Check if object rotation should be in local space of object being manipulated instead of world space.")]
private bool useLocalSpaceForConstraint = false;
/// <summary>
/// Gets or sets whether the constraints should be applied in local space of the object being manipulated or world space.
/// </summary>
public bool UseLocalSpaceForConstraint
{
get => useLocalSpaceForConstraint;
set => useLocalSpaceForConstraint = value;
}
/// <inheritdoc />
public override TransformFlags ConstraintType => TransformFlags.Rotate;
#endregion Properties
#region Public Methods
/// <summary>
/// Removes rotation about given axis if its flag is found
/// in ConstraintOnRotation
/// </summary>
public override void ApplyConstraint(ref MixedRealityTransform transform)
{
Quaternion rotation = transform.Rotation * Quaternion.Inverse(WorldPoseOnManipulationStart.Rotation);
Vector3 eulers = rotation.eulerAngles;
if (constraintOnRotation.IsMaskSet(AxisFlags.XAxis))
{
eulers.x = 0;
}
if (constraintOnRotation.IsMaskSet(AxisFlags.YAxis))
{
eulers.y = 0;
}
if (constraintOnRotation.IsMaskSet(AxisFlags.ZAxis))
{
eulers.z = 0;
}
transform.Rotation = useLocalSpaceForConstraint
? WorldPoseOnManipulationStart.Rotation * Quaternion.Euler(eulers)
: Quaternion.Euler(eulers) * WorldPoseOnManipulationStart.Rotation;
}
#endregion Public Methods
}
}