MRTK-FixedRotationToUserConstraint

作者:追风剑情 发布于:2023-10-26 10:31 分类:Unity3d

拖动对象时,约束对象始终面向用户。

MRTK原版代码

原版代码有个缺点,拖动界面时,界面会根据头盔在Z轴上的旋转而绕Z轴旋转。

  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3.  
  4. using Microsoft.MixedReality.Toolkit.Utilities;
  5. using UnityEngine;
  6.  
  7. namespace Microsoft.MixedReality.Toolkit.UI
  8. {
  9. /// <summary>
  10. /// Component for fixing the rotation of a manipulated object relative to the user
  11. /// </summary>
  12. public class FixedRotationToUserConstraint : TransformConstraint
  13. {
  14. #region Properties
  15.  
  16. public override TransformFlags ConstraintType => TransformFlags.Rotate;
  17.  
  18. private Quaternion startObjectRotationCameraSpace;
  19.  
  20. #endregion Properties
  21.  
  22. #region Public Methods
  23.  
  24. /// <inheritdoc />
  25. public override void Initialize(MixedRealityTransform worldPose)
  26. {
  27. base.Initialize(worldPose);
  28.  
  29. startObjectRotationCameraSpace = Quaternion.Inverse(CameraCache.Main.transform.rotation) * worldPose.Rotation;
  30. }
  31.  
  32. /// <summary>
  33. /// Updates the objects rotation so that the rotation relative to the user
  34. /// is fixed
  35. /// </summary>
  36. public override void ApplyConstraint(ref MixedRealityTransform transform)
  37. {
  38. Vector3 euler = CameraCache.Main.transform.rotation.eulerAngles;
  39. // don't use roll (feels awkward) - just maintain yaw / pitch angle
  40. transform.Rotation = Quaternion.Euler(euler.x, euler.y, 0) * startObjectRotationCameraSpace;
  41. }
  42.  
  43. #endregion Public Methods
  44. }
  45. }

修改后的代码

去掉被操作对象在Z轴上的旋转。

  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3.  
  4. using Microsoft.MixedReality.Toolkit.Utilities;
  5. using UnityEngine;
  6.  
  7. namespace Microsoft.MixedReality.Toolkit.UI
  8. {
  9. /// <summary>
  10. /// Component for fixing the rotation of a manipulated object relative to the user
  11. /// </summary>
  12. public class FixedRotationToUserConstraint2 : TransformConstraint
  13. {
  14. #region Properties
  15.  
  16. public override TransformFlags ConstraintType => TransformFlags.Rotate;
  17.  
  18. private Quaternion startObjectRotationCameraSpace;
  19.  
  20. #endregion Properties
  21.  
  22. #region Public Methods
  23.  
  24. /// <inheritdoc />
  25. public override void Initialize(MixedRealityTransform worldPose)
  26. {
  27. base.Initialize(worldPose);
  28.  
  29. startObjectRotationCameraSpace = worldPose.Rotation;
  30. }
  31.  
  32. /// <summary>
  33. /// Updates the objects rotation so that the rotation relative to the user
  34. /// is fixed
  35. /// </summary>
  36. public override void ApplyConstraint(ref MixedRealityTransform transform)
  37. {
  38. Vector3 euler = CameraCache.Main.transform.rotation.eulerAngles;
  39. // don't use roll (feels awkward) - just maintain yaw / pitch angle
  40. transform.Rotation = Quaternion.Euler(euler.x, euler.y, 0);
  41. }
  42.  
  43. #endregion Public Methods
  44. }
  45. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号