SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.1 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.XR.ARSubsystems;
  4. namespace UnityEngine.XR.ARFoundation
  5. {
  6. /// <summary>
  7. /// Several method extensions to <c>Transform</c> for transforming and inverse-transforming additional Unity types.
  8. /// </summary>
  9. public static class TransformExtensions
  10. {
  11. /// <summary>
  12. /// Transforms a <c>Ray</c>
  13. /// </summary>
  14. /// <param name="transform">The <c>Transform</c> component</param>
  15. /// <param name="ray">The <c>Ray</c> to transform</param>
  16. /// <returns>A new <c>Ray</c> representing the transformed <paramref name="ray"/></returns>
  17. public static Ray TransformRay(this Transform transform, Ray ray)
  18. {
  19. if (transform == null)
  20. throw new ArgumentNullException("transform");
  21. return new Ray(
  22. transform.TransformPoint(ray.origin),
  23. transform.TransformDirection(ray.direction));
  24. }
  25. /// <summary>
  26. /// Inverse transforms a <c>Ray</c>
  27. /// </summary>
  28. /// <param name="transform">The <c>Transform</c> component</param>
  29. /// <param name="ray">The <c>Ray</c> to inversely transform</param>
  30. /// <returns>A new <c>Ray</c> representing the inversely transformed <paramref name="ray"/></returns>
  31. public static Ray InverseTransformRay(this Transform transform, Ray ray)
  32. {
  33. if (transform == null)
  34. throw new ArgumentNullException("transform");
  35. return new Ray(
  36. transform.InverseTransformPoint(ray.origin),
  37. transform.InverseTransformDirection(ray.direction));
  38. }
  39. /// <summary>
  40. /// Transforms a <c>Pose</c>
  41. /// </summary>
  42. /// <param name="transform">The <c>Transform</c> component</param>
  43. /// <param name="pose">The <c>Pose</c> to transform</param>
  44. /// <returns>A new <c>Pose</c> representing the transformed <paramref name="pose"/></returns>
  45. public static Pose TransformPose(this Transform transform, Pose pose)
  46. {
  47. return pose.GetTransformedBy(transform);
  48. }
  49. /// <summary>
  50. /// Inverse transforms a <c>Pose</c>
  51. /// </summary>
  52. /// <param name="transform">The <c>Transform</c> component</param>
  53. /// <param name="pose">The <c>Pose</c> to inversely transform</param>
  54. /// <returns>A new <c>Pose</c> representing the inversely transformed <paramref name="pose"/></returns>
  55. public static Pose InverseTransformPose(this Transform transform, Pose pose)
  56. {
  57. if (transform == null)
  58. throw new ArgumentNullException("transform");
  59. return new Pose
  60. {
  61. position = transform.InverseTransformPoint(pose.position),
  62. rotation = Quaternion.Inverse(transform.rotation) * pose.rotation
  63. };
  64. }
  65. /// <summary>
  66. /// Transforms a <c>List</c> of positions.
  67. /// </summary>
  68. /// <param name="transform">The <c>Transform</c> component</param>
  69. /// <param name="points">The points to transform. The points are transformed in-place.</param>
  70. public static void TransformPointList(this Transform transform, List<Vector3> points)
  71. {
  72. if (transform == null)
  73. throw new ArgumentNullException("transform");
  74. if (points == null)
  75. throw new ArgumentNullException("points");
  76. for (int i = 0; i < points.Count; ++i)
  77. {
  78. points[i] = transform.TransformPoint(points[i]);
  79. }
  80. }
  81. /// <summary>
  82. /// Inverse transforms a <c>List</c> of <c>Vector3</c>s.
  83. /// </summary>
  84. /// <param name="transform">The <c>Transform</c> component</param>
  85. /// <param name="points">The points to inverse transform. This is done in-place.</param>
  86. public static void InverseTransformPointList(this Transform transform, List<Vector3> points)
  87. {
  88. if (transform == null)
  89. throw new ArgumentNullException("transform");
  90. if (points == null)
  91. throw new ArgumentNullException("points");
  92. for (int i = 0; i < points.Count; ++i)
  93. {
  94. points[i] = transform.InverseTransformPoint(points[i]);
  95. }
  96. }
  97. /// <summary>
  98. /// Sets the layer for the <c>GameObject</c> associated with <paramref name="transform"/> and all its children.
  99. /// </summary>
  100. /// <param name="transform">The <c>Transform</c> component</param>
  101. /// <param name="layer">The layer in which the game object should be.</param>
  102. public static void SetLayerRecursively(this Transform transform, int layer)
  103. {
  104. if (transform == null)
  105. throw new ArgumentNullException("transform");
  106. // Set self
  107. transform.gameObject.layer = layer;
  108. // Set all child layers recursively
  109. for (var i = 0; i < transform.childCount; ++i)
  110. {
  111. var child = transform.GetChild(i);
  112. child.SetLayerRecursively(layer);
  113. }
  114. }
  115. }
  116. }