Vector4.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. using System;
  2. using System.Globalization;
  3. namespace UnityEngine
  4. {
  5. [Serializable]
  6. public struct Vector4: IEquatable<Vector4>
  7. {
  8. public static readonly Vector4 zero = new Vector4();
  9. public static readonly Vector4 one = new Vector4(1f, 1f, 1f, 1f);
  10. public float x;
  11. public float y;
  12. public float z;
  13. public float w;
  14. public Vector4(float x, float y, float z, float w)
  15. {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. this.w = w;
  20. }
  21. public Vector4(Vector2 value, float z, float w)
  22. {
  23. this.x = value.x;
  24. this.y = value.y;
  25. this.z = z;
  26. this.w = w;
  27. }
  28. public Vector4(Vector3 value, float w)
  29. {
  30. this.x = value.x;
  31. this.y = value.y;
  32. this.z = value.z;
  33. this.w = w;
  34. }
  35. public Vector4(float value)
  36. {
  37. this.x = this.y = this.z = this.w = value;
  38. }
  39. public override string ToString()
  40. {
  41. CultureInfo currentCulture = CultureInfo.CurrentCulture;
  42. return string.Format(currentCulture, "{0}, {1}, {2}, {3}", (object) this.x.ToString(currentCulture),
  43. (object) this.y.ToString(currentCulture),
  44. (object) this.z.ToString(currentCulture),
  45. (object) this.w.ToString(currentCulture));
  46. }
  47. public bool Equals(Vector4 other)
  48. {
  49. if (this.x == (double) other.x && this.y == (double) other.y && this.z == (double) other.z)
  50. return this.w == (double) other.w;
  51. return false;
  52. }
  53. public override bool Equals(object obj)
  54. {
  55. bool flag = false;
  56. if (obj is Vector4)
  57. flag = this.Equals((Vector4) obj);
  58. return flag;
  59. }
  60. public override int GetHashCode()
  61. {
  62. return this.x.GetHashCode() + this.y.GetHashCode() + this.z.GetHashCode() + this.w.GetHashCode();
  63. }
  64. public float Length()
  65. {
  66. return (float) Math.Sqrt(this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z +
  67. this.w * (double) this.w);
  68. }
  69. public float LengthSquared()
  70. {
  71. return (float) (this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z +
  72. this.w * (double) this.w);
  73. }
  74. public static float Distance(Vector4 value1, Vector4 value2)
  75. {
  76. float num1 = value1.x - value2.x;
  77. float num2 = value1.y - value2.y;
  78. float num3 = value1.z - value2.z;
  79. float num4 = value1.w - value2.w;
  80. return (float) Math.Sqrt(num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3 +
  81. num4 * (double) num4);
  82. }
  83. public static void Distance(ref Vector4 value1, ref Vector4 value2, out float result)
  84. {
  85. float num1 = value1.x - value2.x;
  86. float num2 = value1.y - value2.y;
  87. float num3 = value1.z - value2.z;
  88. float num4 = value1.w - value2.w;
  89. float num5 = (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3 +
  90. num4 * (double) num4);
  91. result = (float) Math.Sqrt(num5);
  92. }
  93. public static float DistanceSquared(Vector4 value1, Vector4 value2)
  94. {
  95. float num1 = value1.x - value2.x;
  96. float num2 = value1.y - value2.y;
  97. float num3 = value1.z - value2.z;
  98. float num4 = value1.w - value2.w;
  99. return (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3 +
  100. num4 * (double) num4);
  101. }
  102. public static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out float result)
  103. {
  104. float num1 = value1.x - value2.x;
  105. float num2 = value1.y - value2.y;
  106. float num3 = value1.z - value2.z;
  107. float num4 = value1.w - value2.w;
  108. result = (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3 +
  109. num4 * (double) num4);
  110. }
  111. public static float Dot(Vector4 vector1, Vector4 vector2)
  112. {
  113. return (float) (vector1.x * (double) vector2.x + vector1.y * (double) vector2.y +
  114. vector1.z * (double) vector2.z + vector1.w * (double) vector2.w);
  115. }
  116. public static void Dot(ref Vector4 vector1, ref Vector4 vector2, out float result)
  117. {
  118. result = (float) (vector1.x * (double) vector2.x + vector1.y * (double) vector2.y +
  119. vector1.z * (double) vector2.z + vector1.w * (double) vector2.w);
  120. }
  121. public void Normalize()
  122. {
  123. float num1 = (float) (this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z +
  124. this.w * (double) this.w);
  125. if (num1 < (double) Mathf.Epsilon)
  126. return;
  127. float num2 = 1f / (float) Math.Sqrt(num1);
  128. this.x *= num2;
  129. this.y *= num2;
  130. this.z *= num2;
  131. this.w *= num2;
  132. }
  133. public static Vector4 Normalize(Vector4 vector)
  134. {
  135. float num1 = (float) (vector.x * (double) vector.x + vector.y * (double) vector.y +
  136. vector.z * (double) vector.z + vector.w * (double) vector.w);
  137. if (num1 < (double) Mathf.Epsilon)
  138. return vector;
  139. float num2 = 1f / (float) Math.Sqrt(num1);
  140. Vector4 vector4;
  141. vector4.x = vector.x * num2;
  142. vector4.y = vector.y * num2;
  143. vector4.z = vector.z * num2;
  144. vector4.w = vector.w * num2;
  145. return vector4;
  146. }
  147. public static void Normalize(ref Vector4 vector, out Vector4 result)
  148. {
  149. float num1 = (float) (vector.x * (double) vector.x + vector.y * (double) vector.y +
  150. vector.z * (double) vector.z + vector.w * (double) vector.w);
  151. if (num1 < (double) Mathf.Epsilon)
  152. {
  153. result = vector;
  154. }
  155. else
  156. {
  157. float num2 = 1f / (float) Math.Sqrt(num1);
  158. result.x = vector.x * num2;
  159. result.y = vector.y * num2;
  160. result.z = vector.z * num2;
  161. result.w = vector.w * num2;
  162. }
  163. }
  164. public static Vector4 Min(Vector4 value1, Vector4 value2)
  165. {
  166. Vector4 vector4;
  167. vector4.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
  168. vector4.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
  169. vector4.z = (double) value1.z < (double) value2.z? value1.z : value2.z;
  170. vector4.w = (double) value1.w < (double) value2.w? value1.w : value2.w;
  171. return vector4;
  172. }
  173. public static void Min(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  174. {
  175. result.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
  176. result.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
  177. result.z = (double) value1.z < (double) value2.z? value1.z : value2.z;
  178. result.w = (double) value1.w < (double) value2.w? value1.w : value2.w;
  179. }
  180. public static Vector4 Max(Vector4 value1, Vector4 value2)
  181. {
  182. Vector4 vector4;
  183. vector4.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
  184. vector4.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
  185. vector4.z = (double) value1.z > (double) value2.z? value1.z : value2.z;
  186. vector4.w = (double) value1.w > (double) value2.w? value1.w : value2.w;
  187. return vector4;
  188. }
  189. public static void Max(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  190. {
  191. result.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
  192. result.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
  193. result.z = (double) value1.z > (double) value2.z? value1.z : value2.z;
  194. result.w = (double) value1.w > (double) value2.w? value1.w : value2.w;
  195. }
  196. public static Vector4 Clamp(Vector4 value1, Vector4 min, Vector4 max)
  197. {
  198. float x = value1.x;
  199. float num1 = (double) x > (double) max.x? max.x : x;
  200. float num2 = (double) num1 < (double) min.x? min.x : num1;
  201. float y = value1.y;
  202. float num3 = (double) y > (double) max.y? max.y : y;
  203. float num4 = (double) num3 < (double) min.y? min.y : num3;
  204. float z = value1.z;
  205. float num5 = (double) z > (double) max.z? max.z : z;
  206. float num6 = (double) num5 < (double) min.z? min.z : num5;
  207. float w = value1.w;
  208. float num7 = (double) w > (double) max.w? max.w : w;
  209. float num8 = (double) num7 < (double) min.w? min.w : num7;
  210. Vector4 vector4;
  211. vector4.x = num2;
  212. vector4.y = num4;
  213. vector4.z = num6;
  214. vector4.w = num8;
  215. return vector4;
  216. }
  217. public static void Clamp(ref Vector4 value1, ref Vector4 min, ref Vector4 max, out Vector4 result)
  218. {
  219. float x = value1.x;
  220. float num1 = (double) x > (double) max.x? max.x : x;
  221. float num2 = (double) num1 < (double) min.x? min.x : num1;
  222. float y = value1.y;
  223. float num3 = (double) y > (double) max.y? max.y : y;
  224. float num4 = (double) num3 < (double) min.y? min.y : num3;
  225. float z = value1.z;
  226. float num5 = (double) z > (double) max.z? max.z : z;
  227. float num6 = (double) num5 < (double) min.z? min.z : num5;
  228. float w = value1.w;
  229. float num7 = (double) w > (double) max.w? max.w : w;
  230. float num8 = (double) num7 < (double) min.w? min.w : num7;
  231. result.x = num2;
  232. result.y = num4;
  233. result.z = num6;
  234. result.w = num8;
  235. }
  236. public static Vector4 Lerp(Vector4 value1, Vector4 value2, float amount)
  237. {
  238. Vector4 vector4;
  239. vector4.x = value1.x + (value2.x - value1.x) * amount;
  240. vector4.y = value1.y + (value2.y - value1.y) * amount;
  241. vector4.z = value1.z + (value2.z - value1.z) * amount;
  242. vector4.w = value1.w + (value2.w - value1.w) * amount;
  243. return vector4;
  244. }
  245. public static void Lerp(ref Vector4 value1, ref Vector4 value2, float amount, out Vector4 result)
  246. {
  247. result.x = value1.x + (value2.x - value1.x) * amount;
  248. result.y = value1.y + (value2.y - value1.y) * amount;
  249. result.z = value1.z + (value2.z - value1.z) * amount;
  250. result.w = value1.w + (value2.w - value1.w) * amount;
  251. }
  252. public static Vector4 SmoothStep(Vector4 value1, Vector4 value2, float amount)
  253. {
  254. amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
  255. amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
  256. Vector4 vector4;
  257. vector4.x = value1.x + (value2.x - value1.x) * amount;
  258. vector4.y = value1.y + (value2.y - value1.y) * amount;
  259. vector4.z = value1.z + (value2.z - value1.z) * amount;
  260. vector4.w = value1.w + (value2.w - value1.w) * amount;
  261. return vector4;
  262. }
  263. public static void SmoothStep(ref Vector4 value1, ref Vector4 value2, float amount, out Vector4 result)
  264. {
  265. amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
  266. amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
  267. result.x = value1.x + (value2.x - value1.x) * amount;
  268. result.y = value1.y + (value2.y - value1.y) * amount;
  269. result.z = value1.z + (value2.z - value1.z) * amount;
  270. result.w = value1.w + (value2.w - value1.w) * amount;
  271. }
  272. public static Vector4 Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount)
  273. {
  274. float num1 = amount * amount;
  275. float num2 = amount * num1;
  276. float num3 = (float) (2.0 * num2 - 3.0 * num1 + 1.0);
  277. float num4 = (float) (-2.0 * num2 + 3.0 * num1);
  278. float num5 = num2 - 2f * num1 + amount;
  279. float num6 = num2 - num1;
  280. Vector4 vector4;
  281. vector4.x = (float) (value1.x * (double) num3 + value2.x * (double) num4 + tangent1.x * (double) num5 +
  282. tangent2.x * (double) num6);
  283. vector4.y = (float) (value1.y * (double) num3 + value2.y * (double) num4 + tangent1.y * (double) num5 +
  284. tangent2.y * (double) num6);
  285. vector4.z = (float) (value1.z * (double) num3 + value2.z * (double) num4 + tangent1.z * (double) num5 +
  286. tangent2.z * (double) num6);
  287. vector4.w = (float) (value1.w * (double) num3 + value2.w * (double) num4 + tangent1.w * (double) num5 +
  288. tangent2.w * (double) num6);
  289. return vector4;
  290. }
  291. public static void Hermite(
  292. ref Vector4 value1, ref Vector4 tangent1, ref Vector4 value2, ref Vector4 tangent2, float amount, out Vector4 result)
  293. {
  294. float num1 = amount * amount;
  295. float num2 = amount * num1;
  296. float num3 = (float) (2.0 * num2 - 3.0 * num1 + 1.0);
  297. float num4 = (float) (-2.0 * num2 + 3.0 * num1);
  298. float num5 = num2 - 2f * num1 + amount;
  299. float num6 = num2 - num1;
  300. result.x = (float) (value1.x * (double) num3 + value2.x * (double) num4 + tangent1.x * (double) num5 +
  301. tangent2.x * (double) num6);
  302. result.y = (float) (value1.y * (double) num3 + value2.y * (double) num4 + tangent1.y * (double) num5 +
  303. tangent2.y * (double) num6);
  304. result.z = (float) (value1.z * (double) num3 + value2.z * (double) num4 + tangent1.z * (double) num5 +
  305. tangent2.z * (double) num6);
  306. result.w = (float) (value1.w * (double) num3 + value2.w * (double) num4 + tangent1.w * (double) num5 +
  307. tangent2.w * (double) num6);
  308. }
  309. public static Vector4 Project(Vector4 vector, Vector4 onNormal)
  310. {
  311. return onNormal * Vector4.Dot(vector, onNormal) / Vector4.Dot(onNormal, onNormal);
  312. }
  313. public static void Project(ref Vector4 vector, ref Vector4 onNormal, out Vector4 result)
  314. {
  315. result = onNormal * Vector4.Dot(vector, onNormal) / Vector4.Dot(onNormal, onNormal);
  316. }
  317. public static Vector4 Negate(Vector4 value)
  318. {
  319. Vector4 vector4;
  320. vector4.x = -value.x;
  321. vector4.y = -value.y;
  322. vector4.z = -value.z;
  323. vector4.w = -value.w;
  324. return vector4;
  325. }
  326. public static void Negate(ref Vector4 value, out Vector4 result)
  327. {
  328. result.x = -value.x;
  329. result.y = -value.y;
  330. result.z = -value.z;
  331. result.w = -value.w;
  332. }
  333. public static Vector4 Add(Vector4 value1, Vector4 value2)
  334. {
  335. Vector4 vector4;
  336. vector4.x = value1.x + value2.x;
  337. vector4.y = value1.y + value2.y;
  338. vector4.z = value1.z + value2.z;
  339. vector4.w = value1.w + value2.w;
  340. return vector4;
  341. }
  342. public static void Add(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  343. {
  344. result.x = value1.x + value2.x;
  345. result.y = value1.y + value2.y;
  346. result.z = value1.z + value2.z;
  347. result.w = value1.w + value2.w;
  348. }
  349. public static Vector4 Sub(Vector4 value1, Vector4 value2)
  350. {
  351. Vector4 vector4;
  352. vector4.x = value1.x - value2.x;
  353. vector4.y = value1.y - value2.y;
  354. vector4.z = value1.z - value2.z;
  355. vector4.w = value1.w - value2.w;
  356. return vector4;
  357. }
  358. public static void Sub(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  359. {
  360. result.x = value1.x - value2.x;
  361. result.y = value1.y - value2.y;
  362. result.z = value1.z - value2.z;
  363. result.w = value1.w - value2.w;
  364. }
  365. public static Vector4 Multiply(Vector4 value1, Vector4 value2)
  366. {
  367. Vector4 vector4;
  368. vector4.x = value1.x * value2.x;
  369. vector4.y = value1.y * value2.y;
  370. vector4.z = value1.z * value2.z;
  371. vector4.w = value1.w * value2.w;
  372. return vector4;
  373. }
  374. public static void Multiply(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  375. {
  376. result.x = value1.x * value2.x;
  377. result.y = value1.y * value2.y;
  378. result.z = value1.z * value2.z;
  379. result.w = value1.w * value2.w;
  380. }
  381. public static Vector4 Multiply(Vector4 value1, float scaleFactor)
  382. {
  383. Vector4 vector4;
  384. vector4.x = value1.x * scaleFactor;
  385. vector4.y = value1.y * scaleFactor;
  386. vector4.z = value1.z * scaleFactor;
  387. vector4.w = value1.w * scaleFactor;
  388. return vector4;
  389. }
  390. public static void Multiply(ref Vector4 value1, float scaleFactor, out Vector4 result)
  391. {
  392. result.x = value1.x * scaleFactor;
  393. result.y = value1.y * scaleFactor;
  394. result.z = value1.z * scaleFactor;
  395. result.w = value1.w * scaleFactor;
  396. }
  397. public static Vector4 Divide(Vector4 value1, Vector4 value2)
  398. {
  399. Vector4 vector4;
  400. vector4.x = value1.x / value2.x;
  401. vector4.y = value1.y / value2.y;
  402. vector4.z = value1.z / value2.z;
  403. vector4.w = value1.w / value2.w;
  404. return vector4;
  405. }
  406. public static void Divide(ref Vector4 value1, ref Vector4 value2, out Vector4 result)
  407. {
  408. result.x = value1.x / value2.x;
  409. result.y = value1.y / value2.y;
  410. result.z = value1.z / value2.z;
  411. result.w = value1.w / value2.w;
  412. }
  413. public static Vector4 Divide(Vector4 value1, float divider)
  414. {
  415. float num = 1f / divider;
  416. Vector4 vector4;
  417. vector4.x = value1.x * num;
  418. vector4.y = value1.y * num;
  419. vector4.z = value1.z * num;
  420. vector4.w = value1.w * num;
  421. return vector4;
  422. }
  423. public static void Divide(ref Vector4 value1, float divider, out Vector4 result)
  424. {
  425. float num = 1f / divider;
  426. result.x = value1.x * num;
  427. result.y = value1.y * num;
  428. result.z = value1.z * num;
  429. result.w = value1.w * num;
  430. }
  431. public static Vector4 operator -(Vector4 value)
  432. {
  433. Vector4 vector4;
  434. vector4.x = -value.x;
  435. vector4.y = -value.y;
  436. vector4.z = -value.z;
  437. vector4.w = -value.w;
  438. return vector4;
  439. }
  440. public static bool operator ==(Vector4 value1, Vector4 value2)
  441. {
  442. if (value1.x == (double) value2.x && value1.y == (double) value2.y && value1.z == (double) value2.z)
  443. return value1.w == (double) value2.w;
  444. return false;
  445. }
  446. public static bool operator !=(Vector4 value1, Vector4 value2)
  447. {
  448. if (value1.x == (double) value2.x && value1.y == (double) value2.y && value1.z == (double) value2.z)
  449. return value1.w != (double) value2.w;
  450. return true;
  451. }
  452. public static Vector4 operator +(Vector4 value1, Vector4 value2)
  453. {
  454. Vector4 vector4;
  455. vector4.x = value1.x + value2.x;
  456. vector4.y = value1.y + value2.y;
  457. vector4.z = value1.z + value2.z;
  458. vector4.w = value1.w + value2.w;
  459. return vector4;
  460. }
  461. public static Vector4 operator -(Vector4 value1, Vector4 value2)
  462. {
  463. Vector4 vector4;
  464. vector4.x = value1.x - value2.x;
  465. vector4.y = value1.y - value2.y;
  466. vector4.z = value1.z - value2.z;
  467. vector4.w = value1.w - value2.w;
  468. return vector4;
  469. }
  470. public static Vector4 operator *(Vector4 value1, Vector4 value2)
  471. {
  472. Vector4 vector4;
  473. vector4.x = value1.x * value2.x;
  474. vector4.y = value1.y * value2.y;
  475. vector4.z = value1.z * value2.z;
  476. vector4.w = value1.w * value2.w;
  477. return vector4;
  478. }
  479. public static Vector4 operator *(Vector4 value1, float scaleFactor)
  480. {
  481. Vector4 vector4;
  482. vector4.x = value1.x * scaleFactor;
  483. vector4.y = value1.y * scaleFactor;
  484. vector4.z = value1.z * scaleFactor;
  485. vector4.w = value1.w * scaleFactor;
  486. return vector4;
  487. }
  488. public static Vector4 operator *(float scaleFactor, Vector4 value1)
  489. {
  490. Vector4 vector4;
  491. vector4.x = value1.x * scaleFactor;
  492. vector4.y = value1.y * scaleFactor;
  493. vector4.z = value1.z * scaleFactor;
  494. vector4.w = value1.w * scaleFactor;
  495. return vector4;
  496. }
  497. public static Vector4 operator /(Vector4 value1, Vector4 value2)
  498. {
  499. Vector4 vector4;
  500. vector4.x = value1.x / value2.x;
  501. vector4.y = value1.y / value2.y;
  502. vector4.z = value1.z / value2.z;
  503. vector4.w = value1.w / value2.w;
  504. return vector4;
  505. }
  506. public static Vector4 operator /(Vector4 value1, float divider)
  507. {
  508. float num = 1f / divider;
  509. Vector4 vector4;
  510. vector4.x = value1.x * num;
  511. vector4.y = value1.y * num;
  512. vector4.z = value1.z * num;
  513. vector4.w = value1.w * num;
  514. return vector4;
  515. }
  516. }
  517. }