Skip to content

Commit 9dc7b8a

Browse files
committed
updating helpers and serializable types with updated version from sph. Adds some necessary code to make them work without errors. New gizmos helpers added to helpers.
1 parent ccd0fc7 commit 9dc7b8a

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

Helpers/Helpers.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ public static T[] RangeSubset<T>(this T[] array, int startIndex, int length) {
9393
Array.Copy(array, startIndex, subset, 0, length);
9494
return subset;
9595
}
96+
97+
// [Source: https://forum.unity.com/threads/encoding-vector2-and-vector3-variables-into-single-int-or-float-and-back.448346/]
98+
// [Commentor: emotitron]
99+
public static int encodeVector3ToInt(Vector3 v) {
100+
//Vectors must stay within the -512 to 512 range per axis - no error handling coded here
101+
//Add 512 to get numbers into the 0-1024 range rather than -512 to 512 range
102+
//Multiply by 10 to save one decimal place from rounding
103+
int xcomp = Mathf.RoundToInt((v.x * 10)) + 512;
104+
int ycomp = Mathf.RoundToInt((v.y * 10)) + 512;
105+
int zcomp = Mathf.RoundToInt((v.z * 10)) + 512;
106+
return xcomp + ycomp * 1024 + zcomp * 1048576;
107+
}
108+
public static Vector3 decodeVector3FromInt(int i) {
109+
//Get the leftmost bits first. The fractional remains are the bits to the right.
110+
// 1024 is 2 ^ 10 - 1048576 is 2 ^ 20 - just saving some calculation time doing that in advance
111+
float z = Mathf.Floor(i / 1048576);
112+
float y = Mathf.Floor ((i - z * 1048576) / 1024);
113+
float x = (i - y * 1024 - z * 1048576);
114+
// subtract 512 to move numbers back into the -512 to 512 range rather than 0 - 1024
115+
return new Vector3 ((x - 512) / 10, (y - 512) / 10, (z - 512) / 10);
116+
}
117+
public static SVector3 decodeSVector3FromInt(int i) {
118+
//Get the leftmost bits first. The fractional remains are the bits to the right.
119+
// 1024 is 2 ^ 10 - 1048576 is 2 ^ 20 - just saving some calculation time doing that in advance
120+
float z = Mathf.Floor(i / 1048576);
121+
float y = Mathf.Floor ((i - z * 1048576) / 1024);
122+
float x = (i - y * 1024 - z * 1048576);
123+
// subtract 512 to move numbers back into the -512 to 512 range rather than 0 - 1024
124+
return new SVector3 ((x - 512) / 10, (y - 512) / 10, (z - 512) / 10);
125+
}
96126
}
97127

98128
[System.Serializable]
@@ -114,6 +144,20 @@ public static bool CheckOrCreateDirectory(string dirPath) {
114144
public static bool CheckFileExists(string filePath) {
115145
return File.Exists(filePath);
116146
}
147+
public static bool DeleteDirectory(string dirPath) {
148+
if (CheckDirectoryExists(dirPath)) {
149+
Directory.Delete(dirPath,true);
150+
return true;
151+
}
152+
return false;
153+
}
154+
public static bool DeleteFile(string filePath) {
155+
if (CheckFileExists(filePath)) {
156+
File.Delete(filePath);
157+
return true;
158+
}
159+
return false;
160+
}
117161

118162
public static string ConvertToJSON<T>(T data) {
119163
return JsonUtility.ToJson(data, true);
@@ -157,4 +201,45 @@ public static string[] ReadCSV(TextAsset asset) {
157201
return asset.text.Split(new string[] {",","\n"}, StringSplitOptions.None);
158202
}
159203
}
204+
205+
// Source: https://forum.unity.com/threads/debug-drawarrow.85980/
206+
[System.Serializable]
207+
public class DrawingMethods {
208+
public static void ForGizmos(Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) {
209+
Gizmos.DrawRay(pos, direction);
210+
211+
Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180+arrowHeadAngle,0) * new Vector3(0,0,1);
212+
Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180-arrowHeadAngle,0) * new Vector3(0,0,1);
213+
Gizmos.DrawRay(pos + direction, right * arrowHeadLength);
214+
Gizmos.DrawRay(pos + direction, left * arrowHeadLength);
215+
}
216+
217+
public static void ForGizmos(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) {
218+
Gizmos.color = color;
219+
Gizmos.DrawRay(pos, direction);
220+
221+
Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180+arrowHeadAngle,0) * new Vector3(0,0,1);
222+
Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180-arrowHeadAngle,0) * new Vector3(0,0,1);
223+
Gizmos.DrawRay(pos + direction, right * arrowHeadLength);
224+
Gizmos.DrawRay(pos + direction, left * arrowHeadLength);
225+
}
226+
227+
public static void ForDebug(Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) {
228+
Debug.DrawRay(pos, direction);
229+
230+
Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180+arrowHeadAngle,0) * new Vector3(0,0,1);
231+
Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180-arrowHeadAngle,0) * new Vector3(0,0,1);
232+
Debug.DrawRay(pos + direction, right * arrowHeadLength);
233+
Debug.DrawRay(pos + direction, left * arrowHeadLength);
234+
}
235+
236+
public static void ForDebug(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) {
237+
Debug.DrawRay(pos, direction, color);
238+
239+
Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180+arrowHeadAngle,0) * new Vector3(0,0,1);
240+
Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0,180-arrowHeadAngle,0) * new Vector3(0,0,1);
241+
Debug.DrawRay(pos + direction, right * arrowHeadLength, color);
242+
Debug.DrawRay(pos + direction, left * arrowHeadLength, color);
243+
}
244+
}
160245
}

Serializables/SerializableTypes.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ public SVector3(float x, float y, float z) {
5959

6060
public override string ToString()
6161
=> $"[{x}, {y}, {z}]";
62-
62+
63+
public override int GetHashCode()
64+
=> x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2);
65+
66+
public override bool Equals(object other) {
67+
if (!(other is SVector3)) return false;
68+
return Equals((SVector3)other);
69+
}
70+
71+
public bool Equals(SVector3 other)
72+
=> x == other.x && y == other.y && z == other.z;
73+
6374
public static implicit operator Vector3(SVector3 s)
6475
=> new Vector3(s.x, s.y, s.z);
6576

@@ -120,6 +131,14 @@ public SVector4(float x, float y, float z, float w) {
120131

121132
public override string ToString()
122133
=> $"[{x}, {y}, {z}, {w}]";
134+
135+
public override int GetHashCode()
136+
=> x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2) ^ (w.GetHashCode() >> 1);
137+
138+
public override bool Equals(object other) {
139+
if (!(other is SVector4)) return false;
140+
return Equals((SVector4)other);
141+
}
123142

124143
public static implicit operator Vector4(SVector4 s)
125144
=> new Vector4(s.x, s.y, s.z, s.w);
@@ -158,6 +177,10 @@ public static implicit operator SVector4(Vector4 v)
158177
=> (Vector4)a != b;
159178
public static bool operator !=(Vector4 a, SVector4 b)
160179
=> a != (Vector4)b;
180+
181+
public float magnitude {
182+
get { return (float)Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w); }
183+
}
161184
}
162185

163186
/// <summary> Serializable version of UnityEngine.Quaternion. </summary>
@@ -177,7 +200,18 @@ public SQuaternion(float x, float y, float z, float w) {
177200

178201
public override string ToString()
179202
=> $"[{x}, {y}, {z}, {w}]";
203+
204+
public override int GetHashCode()
205+
=> x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2) ^ (w.GetHashCode() >> 1);
180206

207+
public override bool Equals(object other) {
208+
if (!(other is SQuaternion)) return false;
209+
return Equals((SQuaternion)other);
210+
}
211+
212+
public bool Equals(SQuaternion other)
213+
=> x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z) && w.Equals(other.w);
214+
181215
public static implicit operator Quaternion(SQuaternion s)
182216
=> new Quaternion(s.x, s.y, s.z, s.w);
183217

0 commit comments

Comments
 (0)