Mathw.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace TerrainComposer2
  6. {
  7. static public class Mathw
  8. {
  9. static public readonly byte[] bit8 = { 1, 2, 4, 8, 16, 32, 64, 128 };
  10. static public float Clamp01(float v)
  11. {
  12. if (v < 0) v = 0;
  13. else if (v > 1) v = 1;
  14. return v;
  15. }
  16. static public float Clamp(float v, float min, float max)
  17. {
  18. if (v < min) v = min;
  19. else if (v > max) v = max;
  20. return v;
  21. }
  22. static public int Clamp(int v, int min, int max)
  23. {
  24. if (v < min) v = min;
  25. else if (v > max) v = max;
  26. return v;
  27. }
  28. static public byte Clamp(byte v, byte min, byte max)
  29. {
  30. if (v < min) v = min;
  31. else if (v > max) v = max;
  32. return v;
  33. }
  34. static public float Abs(float v)
  35. {
  36. return v < 0 ? v * -1 : v;
  37. }
  38. static public int Abs(int v)
  39. {
  40. return v < 0 ? v * -1 : v;
  41. }
  42. static public double Abs(double v)
  43. {
  44. return v < 0 ? v * -1 : v;
  45. }
  46. static public float GetColorBrightness(Color color)
  47. {
  48. return 0.299f * color.r + 0.587f * color.g + 0.114f * color.b;
  49. }
  50. static public float Frac(float v)
  51. {
  52. return v - Mathf.Floor(v);
  53. }
  54. static public bool ArrayContains<T>(T[] array, T obj) where T : class
  55. {
  56. for (int i = 0; i < array.Length; i++) if (array[i] == obj) return true;
  57. return false;
  58. }
  59. static public UnityEngine.Object[] AddToArray(UnityEngine.Object[] array, UnityEngine.Object obj)
  60. {
  61. List<UnityEngine.Object> objs = new List<UnityEngine.Object>();
  62. objs.AddRange(array);
  63. objs.Add(obj);
  64. return objs.ToArray();
  65. }
  66. static public T[] AddToArray<T>(T[] array, T t)
  67. {
  68. T[] temp = new T[array.Length + 1];
  69. for (int i = 0; i < array.Length; i++) temp[i] = array[i];
  70. temp[array.Length] = t;
  71. return temp;
  72. }
  73. static public T[] ResizeArray<T>(T[] array, int length)
  74. {
  75. // Debug.Log("Resize array");
  76. if (array == null) return new T[length];
  77. T[] temp = new T[length];
  78. int copyLength = array.Length > length ? length : array.Length;
  79. for (int i = 0; i < copyLength; i++) temp[i] = array[i];
  80. return temp;
  81. }
  82. static public UnityEngine.Object[] RemoveFromArray(UnityEngine.Object[] array, UnityEngine.Object obj)
  83. {
  84. List<UnityEngine.Object> objs = new List<UnityEngine.Object>();
  85. objs.AddRange(array);
  86. int index = objs.IndexOf(obj);
  87. if (index != -1) objs.RemoveAt(index);
  88. return objs.ToArray();
  89. }
  90. static public AnimationCurve InvertCurve(AnimationCurve curve)
  91. {
  92. Keyframe[] keys = new Keyframe[curve.keys.Length];
  93. for (int i = 0; i < curve.keys.Length; i++)
  94. {
  95. keys[i] = new Keyframe(curve.keys[i].time, 1 - curve.keys[i].value, curve.keys[i].inTangent * -1, curve.keys[i].outTangent * -1);
  96. }
  97. return new AnimationCurve(keys);
  98. }
  99. static public void EncapsulteRect(ref Rect rect, Rect rect2)
  100. {
  101. rect.xMin = Mathf.Min(rect.xMin, rect2.xMin);
  102. rect.yMin = Mathf.Min(rect.yMin, rect2.yMin);
  103. rect.xMax = Mathf.Max(rect.xMax, rect2.xMax);
  104. rect.yMax = Mathf.Max(rect.yMax, rect2.yMax);
  105. }
  106. public static Rect ClampRect (Rect baseRect, Rect clampRect)
  107. {
  108. Rect rect = new Rect ();
  109. rect.xMin = Mathf.Max (baseRect.xMin, clampRect.xMin);
  110. rect.xMax = Mathf.Min (baseRect.xMax, clampRect.xMax);
  111. rect.yMin = Mathf.Max (baseRect.yMin, clampRect.yMin);
  112. rect.yMax = Mathf.Min (baseRect.yMax, clampRect.yMax);
  113. return rect;
  114. }
  115. public static bool OverlapRect (Rect baseRect, Rect testRect, out Rect overlapRect)
  116. {
  117. overlapRect = new Rect(0, 0, 0, 0);
  118. if (testRect.xMax > baseRect.xMin && testRect.xMin < baseRect.xMax && testRect.yMax > baseRect.yMin && testRect.yMin < baseRect.yMax)
  119. {
  120. overlapRect = ClampRect (baseRect, testRect);
  121. return true;
  122. }
  123. return false;
  124. }
  125. public static Rect UniformRectToResolution (Rect rect, Int2 targetRes, Int2 sampleRes, out Int2 samplePos)
  126. {
  127. Vector2 ratio = new Vector2 ((float)targetRes.x/sampleRes.x, (float)targetRes.y/sampleRes.y);
  128. samplePos = new Int2 (Mathf.FloorToInt (rect.x*sampleRes.x), Mathf.FloorToInt (rect.y*sampleRes.y));
  129. Vector2 size = new Vector2 (Mathf.Ceil (rect.width*sampleRes.x)*ratio.x, Mathf.Ceil (rect.height*sampleRes.y)*ratio.y);
  130. Vector2 pos = new Vector2 (samplePos.x*ratio.x, samplePos.y*ratio.y);
  131. return new Rect (pos, size);
  132. }
  133. static public AnimationCurve SetAnimationCurveLinear(AnimationCurve curve)
  134. {
  135. AnimationCurve newCurve = new AnimationCurve();
  136. float inTangent, outTangent;
  137. bool inTangentSet, outTangentSet;
  138. Vector2 point1, point2, deltaPoint;
  139. Keyframe key;
  140. for (int count_key = 0; count_key < curve.keys.Length; ++count_key)
  141. {
  142. inTangent = 0.0f;
  143. outTangent = 0.0f;
  144. inTangentSet = false;
  145. outTangentSet = false;
  146. point1 = Vector2.zero;
  147. point2 = Vector2.zero;
  148. deltaPoint = Vector2.zero;
  149. key = curve[count_key];
  150. if (count_key == 0) { inTangent = 0.0f; inTangentSet = true; }
  151. if (count_key == curve.keys.Length - 1) { outTangent = 0.0f; outTangentSet = true; }
  152. if (!inTangentSet)
  153. {
  154. point1.x = curve.keys[count_key - 1].time;
  155. point1.y = curve.keys[count_key - 1].value;
  156. point2.x = curve.keys[count_key].time;
  157. point2.y = curve.keys[count_key].value;
  158. deltaPoint = point2 - point1;
  159. inTangent = deltaPoint.y / deltaPoint.x;
  160. }
  161. if (!outTangentSet)
  162. {
  163. point1.x = curve.keys[count_key].time;
  164. point1.y = curve.keys[count_key].value;
  165. point2.x = curve.keys[count_key + 1].time;
  166. point2.y = curve.keys[count_key + 1].value;
  167. deltaPoint = point2 - point1;
  168. outTangent = deltaPoint.y / deltaPoint.x;
  169. }
  170. key.inTangent = inTangent;
  171. key.outTangent = outTangent;
  172. newCurve.AddKey(key);
  173. }
  174. return newCurve;
  175. }
  176. static public Vector2 VectorMul(Vector2 p, float v)
  177. {
  178. return new Vector2(p.x * v, p.y * v);
  179. }
  180. static public Vector3 VectorMul(Vector3 p, float v)
  181. {
  182. return new Vector3(p.x * v, p.y * v, p.z * v);
  183. }
  184. static public Vector2 VectorDiv(Vector2 p, float v)
  185. {
  186. return new Vector2(p.x / v, p.y / v);
  187. }
  188. static public Vector3 VectorDiv(Vector3 p, float v)
  189. {
  190. return new Vector3(p.x / v, p.y / v, p.z / v);
  191. }
  192. static public Vector4[] ColorsToVector4(Color[] colors)
  193. {
  194. Vector4[] vColors = new Vector4[colors.Length];
  195. for (int i = 0; i < colors.Length; i++) vColors[i] = colors[i];
  196. return vColors;
  197. }
  198. static public float Snap(float v, float snapValue)
  199. {
  200. return ((int)(v / snapValue) * snapValue);
  201. }
  202. static public Vector2 SnapVector2(Vector2 v, float snapValue)
  203. {
  204. v.x = ((int)(v.x / snapValue)) * snapValue;
  205. v.y = ((int)(v.y / snapValue)) * snapValue;
  206. return v;
  207. }
  208. static public Vector3 SnapVector3(Vector3 v, float snapValue)
  209. {
  210. v.x = ((int)(v.x / snapValue)) * snapValue;
  211. v.y = ((int)(v.y / snapValue)) * snapValue;
  212. v.z = ((int)(v.z / snapValue)) * snapValue;
  213. return v;
  214. }
  215. static public Vector3 SnapRoundVector3(Vector3 v, float snapValue)
  216. {
  217. v.x = Mathf.Round(v.x / snapValue) * snapValue;
  218. v.y = Mathf.Round(v.y / snapValue) * snapValue;
  219. v.z = Mathf.Round(v.z / snapValue) * snapValue;
  220. return v;
  221. }
  222. static public Vector3 SnapVector3xz(Vector3 v, float snapValue)
  223. {
  224. v.x = ((int)(v.x / snapValue)) * snapValue;
  225. v.z = ((int)(v.z / snapValue)) * snapValue;
  226. return v;
  227. }
  228. static public bool BitSwitch(int v, int index)
  229. {
  230. int compareValue = (int)Mathf.Pow(2, index);
  231. return (v & compareValue) == compareValue;
  232. }
  233. static public int SetBitSwitch(int v, int index)
  234. {
  235. return (v & (int)Mathf.Pow(2, index));
  236. }
  237. static public string CutString(string name, int length)
  238. {
  239. if (length > name.Length) return name; else return name.Substring(0, length);
  240. }
  241. }
  242. [Serializable]
  243. public struct Int2
  244. {
  245. public int x, y;
  246. public Int2(int x, int y)
  247. {
  248. this.x = x;
  249. this.y = y;
  250. }
  251. public Int2(float x, float y)
  252. {
  253. this.x = (int)x;
  254. this.y = (int)y;
  255. }
  256. public Int2(Vector2 v)
  257. {
  258. x = (int)v.x;
  259. y = (int)v.y;
  260. }
  261. public Vector2 ToVector2()
  262. {
  263. return new Vector2(x, y);
  264. }
  265. static public Int2 One = new Int2(1, 1);
  266. public override string ToString()
  267. {
  268. return x.ToString() + "x" + y.ToString();
  269. }
  270. }
  271. [Serializable]
  272. public struct Int3
  273. {
  274. public int x, y, z;
  275. public Int3(int x, int y, int z)
  276. {
  277. this.x = x;
  278. this.y = y;
  279. this.z = z;
  280. }
  281. public Int3(float x, float y, float z)
  282. {
  283. this.x = (int)x;
  284. this.y = (int)y;
  285. this.z = (int)z;
  286. }
  287. public Int3(Vector3 v)
  288. {
  289. x = (int)v.x;
  290. y = (int)v.y;
  291. z = (int)v.z;
  292. }
  293. }
  294. }