Mathf_Tips.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. --------------------------------------------------------------------------------
  2. -- Copyright (c) 2015 - 2016 , 蒙占志(topameng) topameng@gmail.com
  3. -- All rights reserved.
  4. -- Use, modification and distribution are subject to the "MIT License"
  5. --------------------------------------------------------------------------------
  6. -- added by wsh @ 2017-12-28
  7. -- 注意:
  8. -- 1、已经被修改,别从tolua轻易替换来做升级
  9. local math = math
  10. local floor = math.floor
  11. local abs = math.abs
  12. ---@class Mathf
  13. Mathf = {}
  14. local unity_mathf = CS.UnityEngine.Mathf
  15. Mathf.Deg2Rad = math.rad(1)
  16. Mathf.Epsilon = 1.4013e-45
  17. Mathf.Infinity = math.huge
  18. Mathf.NegativeInfinity = -math.huge
  19. Mathf.PI = math.pi
  20. Mathf.Rad2Deg = math.deg(1)
  21. Mathf.Abs = math.abs
  22. Mathf.Acos = math.acos
  23. Mathf.Asin = math.asin
  24. Mathf.Atan = math.atan
  25. Mathf.Atan2 = math.atan2
  26. Mathf.Ceil = math.ceil
  27. Mathf.Cos = math.cos
  28. Mathf.Exp = math.exp
  29. Mathf.Floor = math.floor
  30. Mathf.Log = math.log
  31. Mathf.Log10 = math.log10
  32. Mathf.Max = math.max
  33. Mathf.Min = math.min
  34. Mathf.Pow = math.pow
  35. Mathf.Sin = math.sin
  36. Mathf.Sqrt = math.sqrt
  37. Mathf.Tan = math.tan
  38. Mathf.Deg = math.deg
  39. Mathf.Rad = math.rad
  40. Mathf.Random = math.random
  41. Mathf.__index = function(t, k)
  42. local var = rawget(Mathf, k)
  43. if var ~= nil then
  44. return var
  45. end
  46. return rawget(unity_mathf, k)
  47. end
  48. function Mathf.Approximately(a, b)
  49. return abs(b - a) < math.max(1e-6 * math.max(abs(a), abs(b)), 1.121039e-44)
  50. end
  51. function Mathf.Clamp(value, min, max)
  52. if value < min then
  53. value = min
  54. elseif value > max then
  55. value = max
  56. end
  57. return value
  58. end
  59. function Mathf.Clamp01(value)
  60. if value < 0 then
  61. return 0
  62. elseif value > 1 then
  63. return 1
  64. end
  65. return value
  66. end
  67. function Mathf.DeltaAngle(current, target)
  68. local num = Mathf.Repeat(target - current, 360)
  69. if num > 180 then
  70. num = num - 360
  71. end
  72. return num
  73. end
  74. function Mathf.Gamma(value, absmax, gamma)
  75. local flag = false
  76. if value < 0 then
  77. flag = true
  78. end
  79. local num = abs(value)
  80. if num > absmax then
  81. return (not flag) and num or -num
  82. end
  83. local num2 = math.pow(num / absmax, gamma) * absmax
  84. return (not flag) and num2 or -num2
  85. end
  86. function Mathf.InverseLerp(from, to, value)
  87. if from < to then
  88. if value < from then
  89. return 0
  90. end
  91. if value > to then
  92. return 1
  93. end
  94. value = value - from
  95. value = value/(to - from)
  96. return value
  97. end
  98. if from <= to then
  99. return 0
  100. end
  101. if value < to then
  102. return 1
  103. end
  104. if value > from then
  105. return 0
  106. end
  107. return 1 - ((value - to) / (from - to))
  108. end
  109. function Mathf.Lerp(from, to, t)
  110. return from + (to - from) * Mathf.Clamp01(t)
  111. end
  112. function Mathf.LerpAngle(a, b, t)
  113. local num = Mathf.Repeat(b - a, 360)
  114. if num > 180 then
  115. num = num - 360
  116. end
  117. return a + num * Mathf.Clamp01(t)
  118. end
  119. function Mathf.LerpUnclamped(a, b, t)
  120. return a + (b - a) * t
  121. end
  122. function Mathf.MoveTowards(current, target, maxDelta)
  123. if abs(target - current) <= maxDelta then
  124. return target
  125. end
  126. return current + Mathf.Sign(target - current) * maxDelta
  127. end
  128. function Mathf.MoveTowardsAngle(current, target, maxDelta)
  129. target = current + Mathf.DeltaAngle(current, target)
  130. return Mathf.MoveTowards(current, target, maxDelta)
  131. end
  132. function Mathf.PingPong(t, length)
  133. t = Mathf.Repeat(t, length * 2)
  134. return length - abs(t - length)
  135. end
  136. function Mathf.Repeat(t, length)
  137. return t - (floor(t / length) * length)
  138. end
  139. function Mathf.Round(num)
  140. return floor(num + 0.5)
  141. end
  142. function Mathf.Sign(num)
  143. if num > 0 then
  144. num = 1
  145. elseif num < 0 then
  146. num = -1
  147. else
  148. num = 0
  149. end
  150. return num
  151. end
  152. function Mathf.SmoothDamp(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  153. maxSpeed = maxSpeed or Mathf.Infinity
  154. deltaTime = deltaTime or Time.deltaTime
  155. smoothTime = Mathf.Max(0.0001, smoothTime)
  156. local num = 2 / smoothTime
  157. local num2 = num * deltaTime
  158. local num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2)
  159. local num4 = current - target
  160. local num5 = target
  161. local max = maxSpeed * smoothTime
  162. num4 = Mathf.Clamp(num4, -max, max)
  163. target = current - num4
  164. local num7 = (currentVelocity + (num * num4)) * deltaTime
  165. currentVelocity = (currentVelocity - num * num7) * num3
  166. local num8 = target + (num4 + num7) * num3
  167. if (num5 > current) == (num8 > num5) then
  168. num8 = num5
  169. currentVelocity = (num8 - num5) / deltaTime
  170. end
  171. return num8,currentVelocity
  172. end
  173. function Mathf.SmoothDampAngle(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  174. deltaTime = deltaTime or Time.deltaTime
  175. maxSpeed = maxSpeed or Mathf.Infinity
  176. target = current + Mathf.DeltaAngle(current, target)
  177. return Mathf.SmoothDamp(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  178. end
  179. function Mathf.SmoothStep(from, to, t)
  180. t = Mathf.Clamp01(t)
  181. t = -2 * t * t * t + 3 * t * t
  182. return to * t + from * (1 - t)
  183. end
  184. function Mathf.HorizontalAngle(dir)
  185. return math.deg(math.atan2(dir.x, dir.z))
  186. end
  187. function Mathf.IsNan(number)
  188. return not (number == number)
  189. end
  190. ---@return number @防止除法除以0
  191. function Mathf.Div(v1,v2)
  192. v2 = (v2==nil or v2==0) and 1 or v2
  193. return v1/v2;
  194. end
  195. Mathf.unity_mathf = CS.UnityEngine.Mathf
  196. CS.UnityEngine.Mathf = Mathf
  197. setmetatable(Mathf, Mathf)