2019년 4월 12일 금요일

# Unity3d 내장되어 있는 Vector3 클래스 소스코드.

* 유니티엔진에서 제공하는 Vector3 클래스 소스를 디컴파일해서 뽑아낸 후 새롭게 작성.
  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using UnityEngine;
using System;
/// <summary>
/// 유니티엔진에 내장된 Vector3를 디컴파일해서 만든 커스텀벡터3.
/// </summary>
[Serializable]
public struct CustomVector3
{
    public const float kEpsilon = 1E-05f;
    public float x;
    public float y;
    public float z;

    public CustomVector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public CustomVector3(float x, float y)
    {
        this.x = x;
        this.y = y;
        this.z = 0f;
    }


    public static CustomVector3 Lerp(CustomVector3 a, CustomVector3 b, float t)
    {
        t = Mathf.Clamp01(t);
        return new CustomVector3(a.x + ((b.x - a.x) * t), a.y + ((b.y - a.y) * t), a.z + ((b.z - a.z) * t));
    }

    public static CustomVector3 LerpUnclamped(CustomVector3 a, CustomVector3 b, float t)
    {
        return new CustomVector3(a.x + ((b.x - a.x) * t), a.y + ((b.y - a.y) * t), a.z + ((b.z - a.z) * t));
    }

    public static CustomVector3 MoveTowards(CustomVector3 current, CustomVector3 target, float maxDistanceDelta)
    {
        CustomVector3 vector = target - current;
        float magnitude = vector.magnitude;
        if ((magnitude <= maxDistanceDelta) || (magnitude < float.Epsilon))
        {
            return target;
        }
        return (current + ((CustomVector3)((vector / magnitude) * maxDistanceDelta)));
    }
   

    public void Set(float newX, float newY, float newZ)
    {
        this.x = newX;
        this.y = newY;
        this.z = newZ;
    }

    public static CustomVector3 Scale(CustomVector3 a, CustomVector3 b)
    {
        return new CustomVector3(a.x * b.x, a.y * b.y, a.z * b.z);
    }

    public void Scale(CustomVector3 scale)
    {
        this.x *= scale.x;
        this.y *= scale.y;
        this.z *= scale.z;
    }

    public static CustomVector3 Cross(CustomVector3 lhs, CustomVector3 rhs)
    {
        return new CustomVector3((lhs.y * rhs.z) - (lhs.z * rhs.y), (lhs.z * rhs.x) - (lhs.x * rhs.z), (lhs.x * rhs.y) - (lhs.y * rhs.x));
    }

    public override int GetHashCode()
    {
        return ((this.x.GetHashCode() ^ (this.y.GetHashCode() << 2)) ^ (this.z.GetHashCode() >> 2));
    }

    public override bool Equals(object other)
    {
        if (!(other is CustomVector3))
        {
            return false;
        }
        CustomVector3 vector = (CustomVector3)other;
        return ((this.x.Equals(vector.x) && this.y.Equals(vector.y)) && this.z.Equals(vector.z));
    }

    public static CustomVector3 Reflect(CustomVector3 inDirection, CustomVector3 inNormal)
    {
        return (((CustomVector3)((-2f * Dot(inNormal, inDirection)) * inNormal)) + inDirection);
    }

    public static CustomVector3 Normalize(CustomVector3 value)
    {
        float num = Magnitude(value);
        if (num > 1E-05f)
        {
            return (CustomVector3)(value / num);
        }
        return zero;
    }

    public void Normalize()
    {
        float num = Magnitude(this);
        if (num > 1E-05f)
        {
            this = (CustomVector3)(this / num);
        }
        else
        {
            this = zero;
        }
    }

    public CustomVector3 normalized
    {
        get
        {
            return Normalize(this);
        }
    }
    public static float Dot(CustomVector3 lhs, CustomVector3 rhs)
    {
        return (((lhs.x * rhs.x) + (lhs.y * rhs.y)) + (lhs.z * rhs.z));
    }

    public static CustomVector3 Project(CustomVector3 vector, CustomVector3 onNormal)
    {
        float num = Dot(onNormal, onNormal);
        if (num < Mathf.Epsilon)
        {
            return zero;
        }
        return (CustomVector3)((onNormal * Dot(vector, onNormal)) / num);
    }

    public static CustomVector3 ProjectOnPlane(CustomVector3 vector, CustomVector3 planeNormal)
    {
        return (vector - Project(vector, planeNormal));
    }

    public static float Angle(CustomVector3 from, CustomVector3 to)
    {
        return (Mathf.Acos(Mathf.Clamp(Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f);
    }

    public static float SignedAngle(CustomVector3 from, CustomVector3 to, CustomVector3 axis)
    {
        CustomVector3 normalized = from.normalized;
        CustomVector3 rhs = to.normalized;
        float num = Mathf.Acos(Mathf.Clamp(Dot(normalized, rhs), -1f, 1f)) * 57.29578f;
        float num2 = Mathf.Sign(Dot(axis, Cross(normalized, rhs)));
        return (num * num2);
    }

    public static float Distance(CustomVector3 a, CustomVector3 b)
    {
        CustomVector3 vector = new CustomVector3(a.x - b.x, a.y - b.y, a.z - b.z);
        return Mathf.Sqrt(((vector.x * vector.x) + (vector.y * vector.y)) + (vector.z * vector.z));
    }

    public static CustomVector3 ClampMagnitude(CustomVector3 vector, float maxLength)
    {
        if (vector.sqrMagnitude > (maxLength * maxLength))
        {
            return (CustomVector3)(vector.normalized * maxLength);
        }
        return vector;
    }

    public static float Magnitude(CustomVector3 vector)
    {
        return Mathf.Sqrt(((vector.x * vector.x) + (vector.y * vector.y)) + (vector.z * vector.z));
    }

    public float magnitude
    {
        get
        {
            return Mathf.Sqrt(((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
        }
    }
    public static float SqrMagnitude(CustomVector3 vector)
    {
        return (((vector.x * vector.x) + (vector.y * vector.y)) + (vector.z * vector.z));
    }

    public float sqrMagnitude
    {
        get
        {
            return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
        }
    }
    public static CustomVector3 Min(CustomVector3 lhs, CustomVector3 rhs)
    {
        return new CustomVector3(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z));
    }

    public static CustomVector3 Max(CustomVector3 lhs, CustomVector3 rhs)
    {
        return new CustomVector3(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z));
    }

    public static CustomVector3 zero { get; private set; }
    public static CustomVector3 one { get; private set; }
    public static CustomVector3 forward { get; private set; }
    public static CustomVector3 back { get; private set; }
    public static CustomVector3 up { get; private set; }
    public static CustomVector3 down { get; private set; }
    public static CustomVector3 left { get; private set; }
    public static CustomVector3 right { get; private set; }
    public static CustomVector3 positiveInfinity { get; private set; }
    public static CustomVector3 negativeInfinity { get; private set; }
    public static CustomVector3 operator +(CustomVector3 a, CustomVector3 b)
    {
        return new CustomVector3(a.x + b.x, a.y + b.y, a.z + b.z);
    }

    public static CustomVector3 operator -(CustomVector3 a, CustomVector3 b)
    {
        return new CustomVector3(a.x - b.x, a.y - b.y, a.z - b.z);
    }

    public static CustomVector3 operator -(CustomVector3 a)
    {
        return new CustomVector3(-a.x, -a.y, -a.z);
    }

    public static CustomVector3 operator *(CustomVector3 a, float d)
    {
        return new CustomVector3(a.x * d, a.y * d, a.z * d);
    }

    public static CustomVector3 operator *(float d, CustomVector3 a)
    {
        return new CustomVector3(a.x * d, a.y * d, a.z * d);
    }

    public static CustomVector3 operator /(CustomVector3 a, float d)
    {
        return new CustomVector3(a.x / d, a.y / d, a.z / d);
    }

    public static bool operator ==(CustomVector3 lhs, CustomVector3 rhs)
    {
        return (SqrMagnitude(lhs - rhs) < 9.999999E-11f);
    }

    public static bool operator !=(CustomVector3 lhs, CustomVector3 rhs)
    {
        return !(lhs == rhs);
    }

    
    static CustomVector3()
    {
        zero = new CustomVector3(0f, 0f, 0f);
        one = new CustomVector3(1f, 1f, 1f);
        up = new CustomVector3(0f, 1f, 0f);
        down = new CustomVector3(0f, -1f, 0f);
        left = new CustomVector3(-1f, 0f, 0f);
        right = new CustomVector3(1f, 0f, 0f);
        forward = new CustomVector3(0f, 0f, 1f);
        back = new CustomVector3(0f, 0f, -1f);
        positiveInfinity = new CustomVector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
        negativeInfinity = new CustomVector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
    }
}

댓글 없음:

댓글 쓰기