TransformExtensions.cs
5.15 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Several method extensions to <c>Transform</c> for transforming and inverse-transforming additional Unity types.
/// </summary>
public static class TransformExtensions
{
/// <summary>
/// Transforms a <c>Ray</c>
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="ray">The <c>Ray</c> to transform</param>
/// <returns>A new <c>Ray</c> representing the transformed <paramref name="ray"/></returns>
public static Ray TransformRay(this Transform transform, Ray ray)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
return new Ray(
transform.TransformPoint(ray.origin),
transform.TransformDirection(ray.direction));
}
/// <summary>
/// Inverse transforms a <c>Ray</c>
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="ray">The <c>Ray</c> to inversely transform</param>
/// <returns>A new <c>Ray</c> representing the inversely transformed <paramref name="ray"/></returns>
public static Ray InverseTransformRay(this Transform transform, Ray ray)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
return new Ray(
transform.InverseTransformPoint(ray.origin),
transform.InverseTransformDirection(ray.direction));
}
/// <summary>
/// Transforms a <c>Pose</c>
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="pose">The <c>Pose</c> to transform</param>
/// <returns>A new <c>Pose</c> representing the transformed <paramref name="pose"/></returns>
public static Pose TransformPose(this Transform transform, Pose pose)
{
return pose.GetTransformedBy(transform);
}
/// <summary>
/// Inverse transforms a <c>Pose</c>
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="pose">The <c>Pose</c> to inversely transform</param>
/// <returns>A new <c>Pose</c> representing the inversely transformed <paramref name="pose"/></returns>
public static Pose InverseTransformPose(this Transform transform, Pose pose)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
return new Pose
{
position = transform.InverseTransformPoint(pose.position),
rotation = Quaternion.Inverse(transform.rotation) * pose.rotation
};
}
/// <summary>
/// Transforms a <c>List</c> of positions.
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="points">The points to transform. The points are transformed in-place.</param>
public static void TransformPointList(this Transform transform, List<Vector3> points)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
if (points == null)
throw new ArgumentNullException(nameof(points));
for (int i = 0; i < points.Count; ++i)
{
points[i] = transform.TransformPoint(points[i]);
}
}
/// <summary>
/// Inverse transforms a <c>List</c> of <c>Vector3</c>s.
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="points">The points to inverse transform. This is done in-place.</param>
public static void InverseTransformPointList(this Transform transform, List<Vector3> points)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
if (points == null)
throw new ArgumentNullException(nameof(points));
for (int i = 0; i < points.Count; ++i)
{
points[i] = transform.InverseTransformPoint(points[i]);
}
}
/// <summary>
/// Sets the layer for the <c>GameObject</c> associated with <paramref name="transform"/> and all its children.
/// </summary>
/// <param name="transform">The <c>Transform</c> component</param>
/// <param name="layer">The layer in which the game object should be.</param>
public static void SetLayerRecursively(this Transform transform, int layer)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
// Set self
transform.gameObject.layer = layer;
// Set all child layers recursively
for (var i = 0; i < transform.childCount; ++i)
{
var child = transform.GetChild(i);
child.SetLayerRecursively(layer);
}
}
}
}