| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- using System;
- using System.Globalization;
- namespace UnityEngine
- {
- [Serializable]
- public struct Vector3: IEquatable<Vector3>
- {
- private const float k1OverSqrt2 = 0.7071068f;
- private const float epsilon = 1E-05f;
- public static readonly Vector3 zero = new Vector3();
- public static readonly Vector3 one = new Vector3(1f, 1f, 1f);
- public static readonly Vector3 up = new Vector3(0.0f, 1f, 0.0f);
- public static readonly Vector3 down = new Vector3(0.0f, -1f, 0.0f);
- public static readonly Vector3 right = new Vector3(1f, 0.0f, 0.0f);
- public static readonly Vector3 left = new Vector3(-1f, 0.0f, 0.0f);
- public static readonly Vector3 forward = new Vector3(0.0f, 0.0f, 1f);
- public static readonly Vector3 back = new Vector3(0.0f, 0.0f, -1f);
- public float x;
- public float y;
- public float z;
- public Vector3(float x, float y, float z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector3(float value)
- {
- this.x = this.y = this.z = value;
- }
- public Vector3(Vector2 value, float z)
- {
- this.x = value.x;
- this.y = value.y;
- this.z = z;
- }
- public override string ToString()
- {
- CultureInfo currentCulture = CultureInfo.CurrentCulture;
- return string.Format(currentCulture, "({0}, {1}, {2})", this.x.ToString(currentCulture),
- this.y.ToString(currentCulture),
- this.z.ToString(currentCulture));
- }
- public bool Equals(Vector3 other)
- {
- if (this.x == (double) other.x && this.y == (double) other.y)
- return this.z == (double) other.z;
- return false;
- }
- public override bool Equals(object obj)
- {
- bool flag = false;
- if (obj is Vector3)
- flag = this.Equals((Vector3) obj);
- return flag;
- }
- public override int GetHashCode()
- {
- return this.x.GetHashCode() + this.y.GetHashCode() + this.z.GetHashCode();
- }
- public float Length()
- {
- return (float) Math.Sqrt(this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z);
- }
- public float LengthSquared()
- {
- return (float) (this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z);
- }
- public float magnitude
- {
- get
- {
- return this.Length();
- }
- }
-
- public float sqrMagnitude
- {
- get
- {
- return this.LengthSquared();
- }
- }
- public static float Distance(Vector3 value1, Vector3 value2)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- float num3 = value1.z - value2.z;
- return (float) Math.Sqrt(num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3);
- }
- public static void Distance(ref Vector3 value1, ref Vector3 value2, out float result)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- float num3 = value1.z - value2.z;
- float num4 = (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3);
- result = (float) Math.Sqrt(num4);
- }
- public static float DistanceSquared(Vector3 value1, Vector3 value2)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- float num3 = value1.z - value2.z;
- return (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3);
- }
- public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- float num3 = value1.z - value2.z;
- result = (float) (num1 * (double) num1 + num2 * (double) num2 + num3 * (double) num3);
- }
- public static float Dot(Vector3 vector1, Vector3 vector2)
- {
- return (float) (vector1.x * (double) vector2.x + vector1.y * (double) vector2.y +
- vector1.z * (double) vector2.z);
- }
- public static void Dot(ref Vector3 vector1, ref Vector3 vector2, out float result)
- {
- result = (float) (vector1.x * (double) vector2.x + vector1.y * (double) vector2.y +
- vector1.z * (double) vector2.z);
- }
- public void Normalize()
- {
- float num1 = (float) (this.x * (double) this.x + this.y * (double) this.y + this.z * (double) this.z);
- if (num1 < (double) Mathf.Epsilon)
- return;
- float num2 = 1f / (float) Math.Sqrt(num1);
- this.x *= num2;
- this.y *= num2;
- this.z *= num2;
- }
-
- public Vector3 normalized
- {
- get
- {
- return Vector3.Normalize(this);
- }
- }
- public static Vector3 Normalize(Vector3 value)
- {
- float num1 = (float) (value.x * (double) value.x + value.y * (double) value.y + value.z * (double) value.z);
- if (num1 < (double) Mathf.Epsilon)
- return value;
- float num2 = 1f / (float) Math.Sqrt(num1);
- Vector3 vector3;
- vector3.x = value.x * num2;
- vector3.y = value.y * num2;
- vector3.z = value.z * num2;
- return vector3;
- }
- public static void Normalize(ref Vector3 value, out Vector3 result)
- {
- float num1 = (float) (value.x * (double) value.x + value.y * (double) value.y + value.z * (double) value.z);
- if (num1 < (double) Mathf.Epsilon)
- {
- result = value;
- }
- else
- {
- float num2 = 1f / (float) Math.Sqrt(num1);
- result.x = value.x * num2;
- result.y = value.y * num2;
- result.z = value.z * num2;
- }
- }
- public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
- {
- Vector3 vector3;
- vector3.x = (float) (vector1.y * (double) vector2.z - vector1.z * (double) vector2.y);
- vector3.y = (float) (vector1.z * (double) vector2.x - vector1.x * (double) vector2.z);
- vector3.z = (float) (vector1.x * (double) vector2.y - vector1.y * (double) vector2.x);
- return vector3;
- }
- public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result)
- {
- float num1 = (float) (vector1.y * (double) vector2.z - vector1.z * (double) vector2.y);
- float num2 = (float) (vector1.z * (double) vector2.x - vector1.x * (double) vector2.z);
- float num3 = (float) (vector1.x * (double) vector2.y - vector1.y * (double) vector2.x);
- result.x = num1;
- result.y = num2;
- result.z = num3;
- }
- public static Vector3 Reflect(Vector3 vector, Vector3 normal)
- {
- float num =
- (float) (vector.x * (double) normal.x + vector.y * (double) normal.y + vector.z * (double) normal.z);
- Vector3 vector3;
- vector3.x = vector.x - 2f * num * normal.x;
- vector3.y = vector.y - 2f * num * normal.y;
- vector3.z = vector.z - 2f * num * normal.z;
- return vector3;
- }
- public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result)
- {
- float num =
- (float) (vector.x * (double) normal.x + vector.y * (double) normal.y + vector.z * (double) normal.z);
- result.x = vector.x - 2f * num * normal.x;
- result.y = vector.y - 2f * num * normal.y;
- result.z = vector.z - 2f * num * normal.z;
- }
- public static Vector3 Min(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
- vector3.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
- vector3.z = (double) value1.z < (double) value2.z? value1.z : value2.z;
- return vector3;
- }
- public static void Min(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
- result.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
- result.z = (double) value1.z < (double) value2.z? value1.z : value2.z;
- }
- public static Vector3 Max(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
- vector3.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
- vector3.z = (double) value1.z > (double) value2.z? value1.z : value2.z;
- return vector3;
- }
- public static void Max(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
- result.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
- result.z = (double) value1.z > (double) value2.z? value1.z : value2.z;
- }
- public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
- {
- float x = value1.x;
- float num1 = (double) x > (double) max.x? max.x : x;
- float num2 = (double) num1 < (double) min.x? min.x : num1;
- float y = value1.y;
- float num3 = (double) y > (double) max.y? max.y : y;
- float num4 = (double) num3 < (double) min.y? min.y : num3;
- float z = value1.z;
- float num5 = (double) z > (double) max.z? max.z : z;
- float num6 = (double) num5 < (double) min.z? min.z : num5;
- Vector3 vector3;
- vector3.x = num2;
- vector3.y = num4;
- vector3.z = num6;
- return vector3;
- }
- public static void Clamp(ref Vector3 value1, ref Vector3 min, ref Vector3 max, out Vector3 result)
- {
- float x = value1.x;
- float num1 = (double) x > (double) max.x? max.x : x;
- float num2 = (double) num1 < (double) min.x? min.x : num1;
- float y = value1.y;
- float num3 = (double) y > (double) max.y? max.y : y;
- float num4 = (double) num3 < (double) min.y? min.y : num3;
- float z = value1.z;
- float num5 = (double) z > (double) max.z? max.z : z;
- float num6 = (double) num5 < (double) min.z? min.z : num5;
- result.x = num2;
- result.y = num4;
- result.z = num6;
- }
- public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
- {
- Vector3 vector3;
- vector3.x = value1.x + (value2.x - value1.x) * amount;
- vector3.y = value1.y + (value2.y - value1.y) * amount;
- vector3.z = value1.z + (value2.z - value1.z) * amount;
- return vector3;
- }
- public static void Lerp(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
- {
- result.x = value1.x + (value2.x - value1.x) * amount;
- result.y = value1.y + (value2.y - value1.y) * amount;
- result.z = value1.z + (value2.z - value1.z) * amount;
- }
- public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount)
- {
- amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
- amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
- Vector3 vector3;
- vector3.x = value1.x + (value2.x - value1.x) * amount;
- vector3.y = value1.y + (value2.y - value1.y) * amount;
- vector3.z = value1.z + (value2.z - value1.z) * amount;
- return vector3;
- }
- public static void SmoothStep(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
- {
- amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
- amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
- result.x = value1.x + (value2.x - value1.x) * amount;
- result.y = value1.y + (value2.y - value1.y) * amount;
- result.z = value1.z + (value2.z - value1.z) * amount;
- }
- public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount)
- {
- float num1 = amount * amount;
- float num2 = amount * num1;
- float num3 = (float) (2.0 * num2 - 3.0 * num1 + 1.0);
- float num4 = (float) (-2.0 * num2 + 3.0 * num1);
- float num5 = num2 - 2f * num1 + amount;
- float num6 = num2 - num1;
- Vector3 vector3;
- vector3.x = (float) (value1.x * (double) num3 + value2.x * (double) num4 + tangent1.x * (double) num5 +
- tangent2.x * (double) num6);
- vector3.y = (float) (value1.y * (double) num3 + value2.y * (double) num4 + tangent1.y * (double) num5 +
- tangent2.y * (double) num6);
- vector3.z = (float) (value1.z * (double) num3 + value2.z * (double) num4 + tangent1.z * (double) num5 +
- tangent2.z * (double) num6);
- return vector3;
- }
- public static void Hermite(
- ref Vector3 value1, ref Vector3 tangent1, ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result)
- {
- float num1 = amount * amount;
- float num2 = amount * num1;
- float num3 = (float) (2.0 * num2 - 3.0 * num1 + 1.0);
- float num4 = (float) (-2.0 * num2 + 3.0 * num1);
- float num5 = num2 - 2f * num1 + amount;
- float num6 = num2 - num1;
- result.x = (float) (value1.x * (double) num3 + value2.x * (double) num4 + tangent1.x * (double) num5 +
- tangent2.x * (double) num6);
- result.y = (float) (value1.y * (double) num3 + value2.y * (double) num4 + tangent1.y * (double) num5 +
- tangent2.y * (double) num6);
- result.z = (float) (value1.z * (double) num3 + value2.z * (double) num4 + tangent1.z * (double) num5 +
- tangent2.z * (double) num6);
- }
- public static Vector3 Negate(Vector3 value)
- {
- Vector3 vector3;
- vector3.x = -value.x;
- vector3.y = -value.y;
- vector3.z = -value.z;
- return vector3;
- }
- public static void Negate(ref Vector3 value, out Vector3 result)
- {
- result.x = -value.x;
- result.y = -value.y;
- result.z = -value.z;
- }
- public static Vector3 Add(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x + value2.x;
- vector3.y = value1.y + value2.y;
- vector3.z = value1.z + value2.z;
- return vector3;
- }
- public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = value1.x + value2.x;
- result.y = value1.y + value2.y;
- result.z = value1.z + value2.z;
- }
- public static Vector3 Sub(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x - value2.x;
- vector3.y = value1.y - value2.y;
- vector3.z = value1.z - value2.z;
- return vector3;
- }
- public static void Sub(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = value1.x - value2.x;
- result.y = value1.y - value2.y;
- result.z = value1.z - value2.z;
- }
- public static Vector3 Multiply(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x * value2.x;
- vector3.y = value1.y * value2.y;
- vector3.z = value1.z * value2.z;
- return vector3;
- }
- public static void Multiply(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = value1.x * value2.x;
- result.y = value1.y * value2.y;
- result.z = value1.z * value2.z;
- }
- public static Vector3 Multiply(Vector3 value1, float scaleFactor)
- {
- Vector3 vector3;
- vector3.x = value1.x * scaleFactor;
- vector3.y = value1.y * scaleFactor;
- vector3.z = value1.z * scaleFactor;
- return vector3;
- }
- public static void Multiply(ref Vector3 value1, float scaleFactor, out Vector3 result)
- {
- result.x = value1.x * scaleFactor;
- result.y = value1.y * scaleFactor;
- result.z = value1.z * scaleFactor;
- }
- public static Vector3 Divide(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x / value2.x;
- vector3.y = value1.y / value2.y;
- vector3.z = value1.z / value2.z;
- return vector3;
- }
- public static void Divide(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
- {
- result.x = value1.x / value2.x;
- result.y = value1.y / value2.y;
- result.z = value1.z / value2.z;
- }
- public static Vector3 Divide(Vector3 value1, float divider)
- {
- float num = 1f / divider;
- Vector3 vector3;
- vector3.x = value1.x * num;
- vector3.y = value1.y * num;
- vector3.z = value1.z * num;
- return vector3;
- }
- public static void Divide(ref Vector3 value1, float divider, out Vector3 result)
- {
- float num = 1f / divider;
- result.x = value1.x * num;
- result.y = value1.y * num;
- result.z = value1.z * num;
- }
- private static float magnitudeStatic(ref Vector3 inV)
- {
- return (float) Math.Sqrt(Vector3.Dot(inV, inV));
- }
- private static Vector3 orthoNormalVectorFast(ref Vector3 n)
- {
- Vector3 vector3;
- if (Math.Abs(n.z) > (double) Vector3.k1OverSqrt2)
- {
- float num = 1f / (float) Math.Sqrt(n.y * (double) n.y + n.z * (double) n.z);
- vector3.x = 0.0f;
- vector3.y = -n.z * num;
- vector3.z = n.y * num;
- }
- else
- {
- float num = 1f / (float) Math.Sqrt(n.x * (double) n.x + n.y * (double) n.y);
- vector3.x = -n.y * num;
- vector3.y = n.x * num;
- vector3.z = 0.0f;
- }
- return vector3;
- }
- public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent)
- {
- float num1 = Vector3.magnitudeStatic(ref normal);
- if (num1 > (double) Mathf.Epsilon)
- normal /= num1;
- else
- normal = new Vector3(1f, 0.0f, 0.0f);
- float num2 = Vector3.Dot(normal, tangent);
- tangent -= num2 * normal;
- float num3 = Vector3.magnitudeStatic(ref tangent);
- if (num3 < (double) Mathf.Epsilon)
- tangent = Vector3.orthoNormalVectorFast(ref normal);
- else
- tangent /= num3;
- }
- public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal)
- {
- float num1 = Vector3.magnitudeStatic(ref normal);
- if (num1 > (double) Mathf.Epsilon)
- normal /= num1;
- else
- normal = new Vector3(1f, 0.0f, 0.0f);
- float num2 = Vector3.Dot(normal, tangent);
- tangent -= num2 * normal;
- float num3 = Vector3.magnitudeStatic(ref tangent);
- if (num3 > (double) Mathf.Epsilon)
- tangent /= num3;
- else
- tangent = Vector3.orthoNormalVectorFast(ref normal);
- float num4 = Vector3.Dot(tangent, binormal);
- float num5 = Vector3.Dot(normal, binormal);
- binormal -= num5 * normal + num4 * tangent;
- float num6 = Vector3.magnitudeStatic(ref binormal);
- if (num6 > (double) Mathf.Epsilon)
- binormal /= num6;
- else
- binormal = Vector3.Cross(normal, tangent);
- }
- public static Vector3 Project(Vector3 vector, Vector3 onNormal)
- {
- return onNormal * Vector3.Dot(vector, onNormal) / Vector3.Dot(onNormal, onNormal);
- }
- public static void Project(ref Vector3 vector, ref Vector3 onNormal, out Vector3 result)
- {
- result = onNormal * Vector3.Dot(vector, onNormal) / Vector3.Dot(onNormal, onNormal);
- }
- public static float Angle(Vector3 from, Vector3 to)
- {
- from.Normalize();
- to.Normalize();
- float result;
- Vector3.Dot(ref from, ref to, out result);
- return Mathf.Cos(Mathf.Clamp(result, -1f, 1f)) * 57.29578f;
- }
- public static void Angle(ref Vector3 from, ref Vector3 to, out float result)
- {
- from.Normalize();
- to.Normalize();
- float result1;
- Vector3.Dot(ref from, ref to, out result1);
- result = Mathf.Cos(Mathf.Clamp(result1, -1f, 1f)) * 57.29578f;
- }
- public static Vector3 operator -(Vector3 value)
- {
- Vector3 vector3;
- vector3.x = -value.x;
- vector3.y = -value.y;
- vector3.z = -value.z;
- return vector3;
- }
- public static bool operator ==(Vector3 lhs, Vector3 rhs)
- {
- return (lhs - rhs).sqrMagnitude < 9.99999943962493E-11;
- }
- public static bool operator !=(Vector3 lhs, Vector3 rhs)
- {
- return !(lhs == rhs);
- }
- public static Vector3 operator +(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x + value2.x;
- vector3.y = value1.y + value2.y;
- vector3.z = value1.z + value2.z;
- return vector3;
- }
- public static Vector3 operator -(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x - value2.x;
- vector3.y = value1.y - value2.y;
- vector3.z = value1.z - value2.z;
- return vector3;
- }
- public static Vector3 operator *(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x * value2.x;
- vector3.y = value1.y * value2.y;
- vector3.z = value1.z * value2.z;
- return vector3;
- }
- public static Vector3 operator *(Vector3 value, float scaleFactor)
- {
- Vector3 vector3;
- vector3.x = value.x * scaleFactor;
- vector3.y = value.y * scaleFactor;
- vector3.z = value.z * scaleFactor;
- return vector3;
- }
- public static Vector3 operator *(float scaleFactor, Vector3 value)
- {
- Vector3 vector3;
- vector3.x = value.x * scaleFactor;
- vector3.y = value.y * scaleFactor;
- vector3.z = value.z * scaleFactor;
- return vector3;
- }
- public static Vector3 operator /(Vector3 value1, Vector3 value2)
- {
- Vector3 vector3;
- vector3.x = value1.x / value2.x;
- vector3.y = value1.y / value2.y;
- vector3.z = value1.z / value2.z;
- return vector3;
- }
- public static Vector3 operator /(Vector3 value, float divider)
- {
- float num = 1f / divider;
- Vector3 vector3;
- vector3.x = value.x * num;
- vector3.y = value.y * num;
- vector3.z = value.z * num;
- return vector3;
- }
- }
- }
|