-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3Extension.cs
137 lines (125 loc) · 5.42 KB
/
Vector3Extension.cs
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
132
133
134
135
136
137
using UnityEngine;
namespace Cnoom.UnityTool.Extensions
{
public static class Vector3Extension
{
/// <summary>
/// 创建一个新的 Vector3,其中 x、y 和 z 分量都设置为指定的值
/// </summary>
/// <param name="value">要设置的 x、y 和 z 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 UniValue(float value)
{
return new Vector3(value, value, value);
}
/// <summary>
/// 创建一个新的 Vector3,其中 x 分量设置为指定的值,y 和 z 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="x">要设置的 x 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 SetX(this Vector3 self, float x)
{
return new Vector3(x, self.y, self.z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 y 分量设置为指定的值,x 和 z 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="y">要设置的 y 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 SetY(this Vector3 self, float y)
{
return new Vector3(self.x, y, self.z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 z 分量设置为指定的值,x 和 y 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="z">要设置的 z 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 SetZ(this Vector3 self, float z)
{
return new Vector3(self.x, self.y, z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 x 分量增加指定的值,y 和 z 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="x">要增加的 x 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 AddX(this Vector3 self, float x)
{
return new Vector3(self.x + x, self.y, self.z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 y 分量增加指定的值,x 和 z 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="y">要增加的 y 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 AddY(this Vector3 self, float y)
{
return new Vector3(self.x, self.y + y, self.z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 z 分量增加指定的值,x 和 y 分量保持不变
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="z">要增加的 z 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 AddZ(this Vector3 self, float z)
{
return new Vector3(self.x, self.y, self.z + z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 x、y 和 z 分量都增加指定的值
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="x">要增加的 x 分量的值</param>
/// <param name="y">要增加的 y 分量的值</param>
/// <param name="z">要增加的 z 分量的值</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 Add(this Vector3 self, float x, float y, float z)
{
return new Vector3(self.x + x, self.y + y, self.z + z);
}
/// <summary>
/// 创建一个新的 Vector3,其中 x、y 和 z 分量都增加另一个 Vector3 的对应分量
/// </summary>
/// <param name="self">原始的 Vector3 对象</param>
/// <param name="other">要增加的 Vector3 对象</param>
/// <returns>新的 Vector3 对象</returns>
public static Vector3 Add(this Vector3 self, Vector3 other)
{
return self.Add(other.x, other.y, other.z);
}
/// <summary>
/// 计算两个 Vector3 之间的平方距离
/// </summary>
/// <param name="self">第一个 Vector3 对象</param>
/// <param name="other">第二个 Vector3 对象</param>
/// <returns>两个 Vector3 之间的平方距离</returns>
public static float Distance2(this Vector3 self, Vector3 other)
{
float dx = other.x - self.x;
float dy = other.y - self.y;
float dz = other.z - self.z;
return dx * dx + dy * dy + dz * dz;
}
/// <summary>
/// 计算两个 Vector3 之间的距离
/// </summary>
/// <param name="self">第一个 Vector3 对象</param>
/// <param name="other">第二个 Vector3 对象</param>
/// <returns>两个 Vector3 之间的距离</returns>
public static float Distance(this Vector3 self, Vector3 other)
{
return Mathf.Sqrt(Distance2(self, other));
}
public static Vector2 ToVector2(this Vector3 self)
{
return new Vector2(self.x, self.y);
}
}
}