/MonoGame.Framework/Vector3.cs

https://bitbucket.org/refuzion/monogame · C# · 736 lines · 547 code · 118 blank · 71 comment · 10 complexity · 4f9d9865698a28a0bbdb83560a5b0f6e MD5 · raw file

  1. #region License
  2. /*
  3. MIT License
  4. Copyright © 2006 The Mono.Xna Team
  5. All rights reserved.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. #endregion License
  23. using System;
  24. using System.ComponentModel;
  25. using System.Diagnostics;
  26. using System.Text;
  27. using System.Runtime.Serialization;
  28. namespace Microsoft.Xna.Framework
  29. {
  30. [DataContract]
  31. public struct Vector3 : IEquatable<Vector3>
  32. {
  33. #region Private Fields
  34. private static Vector3 zero = new Vector3(0f, 0f, 0f);
  35. private static Vector3 one = new Vector3(1f, 1f, 1f);
  36. private static Vector3 unitX = new Vector3(1f, 0f, 0f);
  37. private static Vector3 unitY = new Vector3(0f, 1f, 0f);
  38. private static Vector3 unitZ = new Vector3(0f, 0f, 1f);
  39. private static Vector3 up = new Vector3(0f, 1f, 0f);
  40. private static Vector3 down = new Vector3(0f, -1f, 0f);
  41. private static Vector3 right = new Vector3(1f, 0f, 0f);
  42. private static Vector3 left = new Vector3(-1f, 0f, 0f);
  43. private static Vector3 forward = new Vector3(0f, 0f, -1f);
  44. private static Vector3 backward = new Vector3(0f, 0f, 1f);
  45. #endregion Private Fields
  46. #region Public Fields
  47. [DataMember]
  48. public float X;
  49. [DataMember]
  50. public float Y;
  51. [DataMember]
  52. public float Z;
  53. #endregion Public Fields
  54. #region Properties
  55. public static Vector3 Zero
  56. {
  57. get { return zero; }
  58. }
  59. public static Vector3 One
  60. {
  61. get { return one; }
  62. }
  63. public static Vector3 UnitX
  64. {
  65. get { return unitX; }
  66. }
  67. public static Vector3 UnitY
  68. {
  69. get { return unitY; }
  70. }
  71. public static Vector3 UnitZ
  72. {
  73. get { return unitZ; }
  74. }
  75. public static Vector3 Up
  76. {
  77. get { return up; }
  78. }
  79. public static Vector3 Down
  80. {
  81. get { return down; }
  82. }
  83. public static Vector3 Right
  84. {
  85. get { return right; }
  86. }
  87. public static Vector3 Left
  88. {
  89. get { return left; }
  90. }
  91. public static Vector3 Forward
  92. {
  93. get { return forward; }
  94. }
  95. public static Vector3 Backward
  96. {
  97. get { return backward; }
  98. }
  99. #endregion Properties
  100. #region Constructors
  101. public Vector3(float x, float y, float z)
  102. {
  103. this.X = x;
  104. this.Y = y;
  105. this.Z = z;
  106. }
  107. public Vector3(float value)
  108. {
  109. this.X = value;
  110. this.Y = value;
  111. this.Z = value;
  112. }
  113. public Vector3(Vector2 value, float z)
  114. {
  115. this.X = value.X;
  116. this.Y = value.Y;
  117. this.Z = z;
  118. }
  119. #endregion Constructors
  120. #region Public Methods
  121. public static Vector3 Add(Vector3 value1, Vector3 value2)
  122. {
  123. value1.X += value2.X;
  124. value1.Y += value2.Y;
  125. value1.Z += value2.Z;
  126. return value1;
  127. }
  128. public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  129. {
  130. result.X = value1.X + value2.X;
  131. result.Y = value1.Y + value2.Y;
  132. result.Z = value1.Z + value2.Z;
  133. }
  134. public static Vector3 Barycentric(Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2)
  135. {
  136. return new Vector3(
  137. MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
  138. MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2),
  139. MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2));
  140. }
  141. public static void Barycentric(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, float amount1, float amount2, out Vector3 result)
  142. {
  143. result = new Vector3(
  144. MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
  145. MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2),
  146. MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2));
  147. }
  148. public static Vector3 CatmullRom(Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount)
  149. {
  150. return new Vector3(
  151. MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
  152. MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount),
  153. MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount));
  154. }
  155. public static void CatmullRom(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result)
  156. {
  157. result = new Vector3(
  158. MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
  159. MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount),
  160. MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount));
  161. }
  162. public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
  163. {
  164. return new Vector3(
  165. MathHelper.Clamp(value1.X, min.X, max.X),
  166. MathHelper.Clamp(value1.Y, min.Y, max.Y),
  167. MathHelper.Clamp(value1.Z, min.Z, max.Z));
  168. }
  169. public static void Clamp(ref Vector3 value1, ref Vector3 min, ref Vector3 max, out Vector3 result)
  170. {
  171. result = new Vector3(
  172. MathHelper.Clamp(value1.X, min.X, max.X),
  173. MathHelper.Clamp(value1.Y, min.Y, max.Y),
  174. MathHelper.Clamp(value1.Z, min.Z, max.Z));
  175. }
  176. public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
  177. {
  178. Cross(ref vector1, ref vector2, out vector1);
  179. return vector1;
  180. }
  181. public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result)
  182. {
  183. result = new Vector3(vector1.Y * vector2.Z - vector2.Y * vector1.Z,
  184. -(vector1.X * vector2.Z - vector2.X * vector1.Z),
  185. vector1.X * vector2.Y - vector2.X * vector1.Y);
  186. }
  187. public static float Distance(Vector3 vector1, Vector3 vector2)
  188. {
  189. float result;
  190. DistanceSquared(ref vector1, ref vector2, out result);
  191. return (float)Math.Sqrt(result);
  192. }
  193. public static void Distance(ref Vector3 value1, ref Vector3 value2, out float result)
  194. {
  195. DistanceSquared(ref value1, ref value2, out result);
  196. result = (float)Math.Sqrt(result);
  197. }
  198. public static float DistanceSquared(Vector3 value1, Vector3 value2)
  199. {
  200. float result;
  201. DistanceSquared(ref value1, ref value2, out result);
  202. return result;
  203. }
  204. public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result)
  205. {
  206. result = (value1.X - value2.X) * (value1.X - value2.X) +
  207. (value1.Y - value2.Y) * (value1.Y - value2.Y) +
  208. (value1.Z - value2.Z) * (value1.Z - value2.Z);
  209. }
  210. public static Vector3 Divide(Vector3 value1, Vector3 value2)
  211. {
  212. value1.X /= value2.X;
  213. value1.Y /= value2.Y;
  214. value1.Z /= value2.Z;
  215. return value1;
  216. }
  217. public static Vector3 Divide(Vector3 value1, float value2)
  218. {
  219. float factor = 1 / value2;
  220. value1.X *= factor;
  221. value1.Y *= factor;
  222. value1.Z *= factor;
  223. return value1;
  224. }
  225. public static void Divide(ref Vector3 value1, float divisor, out Vector3 result)
  226. {
  227. float factor = 1 / divisor;
  228. result.X = value1.X * factor;
  229. result.Y = value1.Y * factor;
  230. result.Z = value1.Z * factor;
  231. }
  232. public static void Divide(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  233. {
  234. result.X = value1.X / value2.X;
  235. result.Y = value1.Y / value2.Y;
  236. result.Z = value1.Z / value2.Z;
  237. }
  238. public static float Dot(Vector3 vector1, Vector3 vector2)
  239. {
  240. return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z;
  241. }
  242. public static void Dot(ref Vector3 vector1, ref Vector3 vector2, out float result)
  243. {
  244. result = vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z;
  245. }
  246. public override bool Equals(object obj)
  247. {
  248. return (obj is Vector3) ? this == (Vector3)obj : false;
  249. }
  250. public bool Equals(Vector3 other)
  251. {
  252. return this == other;
  253. }
  254. public override int GetHashCode()
  255. {
  256. return (int)(this.X + this.Y + this.Z);
  257. }
  258. public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount)
  259. {
  260. Vector3 result = new Vector3();
  261. Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
  262. return result;
  263. }
  264. public static void Hermite(ref Vector3 value1, ref Vector3 tangent1, ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result)
  265. {
  266. result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
  267. result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
  268. result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount);
  269. }
  270. public float Length()
  271. {
  272. float result;
  273. DistanceSquared(ref this, ref zero, out result);
  274. return (float)Math.Sqrt(result);
  275. }
  276. public float LengthSquared()
  277. {
  278. float result;
  279. DistanceSquared(ref this, ref zero, out result);
  280. return result;
  281. }
  282. public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
  283. {
  284. return new Vector3(
  285. MathHelper.Lerp(value1.X, value2.X, amount),
  286. MathHelper.Lerp(value1.Y, value2.Y, amount),
  287. MathHelper.Lerp(value1.Z, value2.Z, amount));
  288. }
  289. public static void Lerp(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
  290. {
  291. result = new Vector3(
  292. MathHelper.Lerp(value1.X, value2.X, amount),
  293. MathHelper.Lerp(value1.Y, value2.Y, amount),
  294. MathHelper.Lerp(value1.Z, value2.Z, amount));
  295. }
  296. public static Vector3 Max(Vector3 value1, Vector3 value2)
  297. {
  298. return new Vector3(
  299. MathHelper.Max(value1.X, value2.X),
  300. MathHelper.Max(value1.Y, value2.Y),
  301. MathHelper.Max(value1.Z, value2.Z));
  302. }
  303. public static void Max(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  304. {
  305. result = new Vector3(
  306. MathHelper.Max(value1.X, value2.X),
  307. MathHelper.Max(value1.Y, value2.Y),
  308. MathHelper.Max(value1.Z, value2.Z));
  309. }
  310. public static Vector3 Min(Vector3 value1, Vector3 value2)
  311. {
  312. return new Vector3(
  313. MathHelper.Min(value1.X, value2.X),
  314. MathHelper.Min(value1.Y, value2.Y),
  315. MathHelper.Min(value1.Z, value2.Z));
  316. }
  317. public static void Min(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  318. {
  319. result = new Vector3(
  320. MathHelper.Min(value1.X, value2.X),
  321. MathHelper.Min(value1.Y, value2.Y),
  322. MathHelper.Min(value1.Z, value2.Z));
  323. }
  324. public static Vector3 Multiply(Vector3 value1, Vector3 value2)
  325. {
  326. value1.X *= value2.X;
  327. value1.Y *= value2.Y;
  328. value1.Z *= value2.Z;
  329. return value1;
  330. }
  331. public static Vector3 Multiply(Vector3 value1, float scaleFactor)
  332. {
  333. value1.X *= scaleFactor;
  334. value1.Y *= scaleFactor;
  335. value1.Z *= scaleFactor;
  336. return value1;
  337. }
  338. public static void Multiply(ref Vector3 value1, float scaleFactor, out Vector3 result)
  339. {
  340. result.X = value1.X * scaleFactor;
  341. result.Y = value1.Y * scaleFactor;
  342. result.Z = value1.Z * scaleFactor;
  343. }
  344. public static void Multiply(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  345. {
  346. result.X = value1.X * value2.X;
  347. result.Y = value1.Y * value2.Y;
  348. result.Z = value1.Z * value2.Z;
  349. }
  350. public static Vector3 Negate(Vector3 value)
  351. {
  352. value = new Vector3(-value.X, -value.Y, -value.Z);
  353. return value;
  354. }
  355. public static void Negate(ref Vector3 value, out Vector3 result)
  356. {
  357. result = new Vector3(-value.X, -value.Y, -value.Z);
  358. }
  359. public void Normalize()
  360. {
  361. Normalize(ref this, out this);
  362. }
  363. public static Vector3 Normalize(Vector3 vector)
  364. {
  365. Normalize(ref vector, out vector);
  366. return vector;
  367. }
  368. public static void Normalize(ref Vector3 value, out Vector3 result)
  369. {
  370. float factor;
  371. Distance(ref value, ref zero, out factor);
  372. factor = 1f / factor;
  373. result.X = value.X * factor;
  374. result.Y = value.Y * factor;
  375. result.Z = value.Z * factor;
  376. }
  377. public static Vector3 Reflect(Vector3 vector, Vector3 normal)
  378. {
  379. // I is the original array
  380. // N is the normal of the incident plane
  381. // R = I - (2 * N * ( DotProduct[ I,N] ))
  382. Vector3 reflectedVector;
  383. // inline the dotProduct here instead of calling method
  384. float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z);
  385. reflectedVector.X = vector.X - (2.0f * normal.X) * dotProduct;
  386. reflectedVector.Y = vector.Y - (2.0f * normal.Y) * dotProduct;
  387. reflectedVector.Z = vector.Z - (2.0f * normal.Z) * dotProduct;
  388. return reflectedVector;
  389. }
  390. public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result)
  391. {
  392. // I is the original array
  393. // N is the normal of the incident plane
  394. // R = I - (2 * N * ( DotProduct[ I,N] ))
  395. // inline the dotProduct here instead of calling method
  396. float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z);
  397. result.X = vector.X - (2.0f * normal.X) * dotProduct;
  398. result.Y = vector.Y - (2.0f * normal.Y) * dotProduct;
  399. result.Z = vector.Z - (2.0f * normal.Z) * dotProduct;
  400. }
  401. public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount)
  402. {
  403. return new Vector3(
  404. MathHelper.SmoothStep(value1.X, value2.X, amount),
  405. MathHelper.SmoothStep(value1.Y, value2.Y, amount),
  406. MathHelper.SmoothStep(value1.Z, value2.Z, amount));
  407. }
  408. public static void SmoothStep(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
  409. {
  410. result = new Vector3(
  411. MathHelper.SmoothStep(value1.X, value2.X, amount),
  412. MathHelper.SmoothStep(value1.Y, value2.Y, amount),
  413. MathHelper.SmoothStep(value1.Z, value2.Z, amount));
  414. }
  415. public static Vector3 Subtract(Vector3 value1, Vector3 value2)
  416. {
  417. value1.X -= value2.X;
  418. value1.Y -= value2.Y;
  419. value1.Z -= value2.Z;
  420. return value1;
  421. }
  422. public static void Subtract(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
  423. {
  424. result.X = value1.X - value2.X;
  425. result.Y = value1.Y - value2.Y;
  426. result.Z = value1.Z - value2.Z;
  427. }
  428. public override string ToString()
  429. {
  430. StringBuilder sb = new StringBuilder(32);
  431. sb.Append("{X:");
  432. sb.Append(this.X);
  433. sb.Append(" Y:");
  434. sb.Append(this.Y);
  435. sb.Append(" Z:");
  436. sb.Append(this.Z);
  437. sb.Append("}");
  438. return sb.ToString();
  439. }
  440. public static Vector3 Transform(Vector3 position, Matrix matrix)
  441. {
  442. Transform(ref position, ref matrix, out position);
  443. return position;
  444. }
  445. public static void Transform(ref Vector3 position, ref Matrix matrix, out Vector3 result)
  446. {
  447. result = new Vector3((position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41,
  448. (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42,
  449. (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43);
  450. }
  451. public static void Transform(Vector3[] sourceArray, ref Matrix matrix, Vector3[] destinationArray)
  452. {
  453. Debug.Assert(destinationArray.Length >= sourceArray.Length, "The destination array is smaller than the source array.");
  454. // TODO: Are there options on some platforms to implement a vectorized version of this?
  455. for (var i = 0; i < sourceArray.Length; i++)
  456. {
  457. var position = sourceArray[i];
  458. destinationArray[i] =
  459. new Vector3(
  460. (position.X*matrix.M11) + (position.Y*matrix.M21) + (position.Z*matrix.M31) + matrix.M41,
  461. (position.X*matrix.M12) + (position.Y*matrix.M22) + (position.Z*matrix.M32) + matrix.M42,
  462. (position.X*matrix.M13) + (position.Y*matrix.M23) + (position.Z*matrix.M33) + matrix.M43);
  463. }
  464. }
  465. /// <summary>
  466. /// Transforms a vector by a quaternion rotation.
  467. /// </summary>
  468. /// <param name="vec">The vector to transform.</param>
  469. /// <param name="quat">The quaternion to rotate the vector by.</param>
  470. /// <returns>The result of the operation.</returns>
  471. public static Vector3 Transform(Vector3 vec, Quaternion quat)
  472. {
  473. Vector3 result;
  474. Transform(ref vec, ref quat, out result);
  475. return result;
  476. }
  477. /// <summary>
  478. /// Transforms a vector by a quaternion rotation.
  479. /// </summary>
  480. /// <param name="vec">The vector to transform.</param>
  481. /// <param name="quat">The quaternion to rotate the vector by.</param>
  482. /// <param name="result">The result of the operation.</param>
  483. // public static void Transform(ref Vector3 vec, ref Quaternion quat, out Vector3 result)
  484. // {
  485. // // Taken from the OpentTK implementation of Vector3
  486. // // Since vec.W == 0, we can optimize quat * vec * quat^-1 as follows:
  487. // // vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec)
  488. // Vector3 xyz = quat.Xyz, temp, temp2;
  489. // Vector3.Cross(ref xyz, ref vec, out temp);
  490. // Vector3.Multiply(ref vec, quat.W, out temp2);
  491. // Vector3.Add(ref temp, ref temp2, out temp);
  492. // Vector3.Cross(ref xyz, ref temp, out temp);
  493. // Vector3.Multiply(ref temp, 2, out temp);
  494. // Vector3.Add(ref vec, ref temp, out result);
  495. // }
  496. /// <summary>
  497. /// Transforms a vector by a quaternion rotation.
  498. /// </summary>
  499. /// <param name="vec">The vector to transform.</param>
  500. /// <param name="quat">The quaternion to rotate the vector by.</param>
  501. /// <param name="result">The result of the operation.</param>
  502. public static void Transform(ref Vector3 value, ref Quaternion rotation, out Vector3 result)
  503. {
  504. float x = 2 * (rotation.Y * value.Z - rotation.Z * value.Y);
  505. float y = 2 * (rotation.Z * value.X - rotation.X * value.Z);
  506. float z = 2 * (rotation.X * value.Y - rotation.Y * value.X);
  507. result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y);
  508. result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z);
  509. result.Z = value.Z + z * rotation.W + (rotation.X * y - rotation.Y * x);
  510. }
  511. /// <summary>
  512. /// Transforms an array of vectors by a quaternion rotation.
  513. /// </summary>
  514. /// <param name="sourceArray">The vectors to transform</param>
  515. /// <param name="rotation">The quaternion to rotate the vector by.</param>
  516. /// <param name="destinationArray">The result of the operation.</param>
  517. public static void Transform(Vector3[] sourceArray, ref Quaternion rotation, Vector3[] destinationArray)
  518. {
  519. Debug.Assert(destinationArray.Length >= sourceArray.Length, "The destination array is smaller than the source array.");
  520. // TODO: Are there options on some platforms to implement a vectorized version of this?
  521. for (var i = 0; i < sourceArray.Length; i++)
  522. {
  523. var position = sourceArray[i];
  524. float x = 2 * (rotation.Y * position.Z - rotation.Z * position.Y);
  525. float y = 2 * (rotation.Z * position.X - rotation.X * position.Z);
  526. float z = 2 * (rotation.X * position.Y - rotation.Y * position.X);
  527. destinationArray[i] =
  528. new Vector3(
  529. position.X + x * rotation.W + (rotation.Y * z - rotation.Z * y),
  530. position.Y + y * rotation.W + (rotation.Z * x - rotation.X * z),
  531. position.Z + z * rotation.W + (rotation.X * y - rotation.Y * x));
  532. }
  533. }
  534. public static Vector3 TransformNormal(Vector3 normal, Matrix matrix)
  535. {
  536. TransformNormal(ref normal, ref matrix, out normal);
  537. return normal;
  538. }
  539. public static void TransformNormal(ref Vector3 normal, ref Matrix matrix, out Vector3 result)
  540. {
  541. result = new Vector3((normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31),
  542. (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32),
  543. (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33));
  544. }
  545. #endregion Public methods
  546. #region Operators
  547. public static bool operator ==(Vector3 value1, Vector3 value2)
  548. {
  549. return value1.X == value2.X
  550. && value1.Y == value2.Y
  551. && value1.Z == value2.Z;
  552. }
  553. public static bool operator !=(Vector3 value1, Vector3 value2)
  554. {
  555. return !(value1 == value2);
  556. }
  557. public static Vector3 operator +(Vector3 value1, Vector3 value2)
  558. {
  559. value1.X += value2.X;
  560. value1.Y += value2.Y;
  561. value1.Z += value2.Z;
  562. return value1;
  563. }
  564. public static Vector3 operator -(Vector3 value)
  565. {
  566. value = new Vector3(-value.X, -value.Y, -value.Z);
  567. return value;
  568. }
  569. public static Vector3 operator -(Vector3 value1, Vector3 value2)
  570. {
  571. value1.X -= value2.X;
  572. value1.Y -= value2.Y;
  573. value1.Z -= value2.Z;
  574. return value1;
  575. }
  576. public static Vector3 operator *(Vector3 value1, Vector3 value2)
  577. {
  578. value1.X *= value2.X;
  579. value1.Y *= value2.Y;
  580. value1.Z *= value2.Z;
  581. return value1;
  582. }
  583. public static Vector3 operator *(Vector3 value, float scaleFactor)
  584. {
  585. value.X *= scaleFactor;
  586. value.Y *= scaleFactor;
  587. value.Z *= scaleFactor;
  588. return value;
  589. }
  590. public static Vector3 operator *(float scaleFactor, Vector3 value)
  591. {
  592. value.X *= scaleFactor;
  593. value.Y *= scaleFactor;
  594. value.Z *= scaleFactor;
  595. return value;
  596. }
  597. public static Vector3 operator /(Vector3 value1, Vector3 value2)
  598. {
  599. value1.X /= value2.X;
  600. value1.Y /= value2.Y;
  601. value1.Z /= value2.Z;
  602. return value1;
  603. }
  604. public static Vector3 operator /(Vector3 value, float divider)
  605. {
  606. float factor = 1 / divider;
  607. value.X *= factor;
  608. value.Y *= factor;
  609. value.Z *= factor;
  610. return value;
  611. }
  612. #endregion
  613. }
  614. }