源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

226 行
4.5 KiB

  1. local math = math
  2. local floor = math.floor
  3. local abs = math.abs
  4. local Mathf = Mathf
  5. Mathf.Deg2Rad = math.rad(1)
  6. Mathf.Epsilon = 1.4013e-45
  7. Mathf.Infinity = math.huge
  8. Mathf.NegativeInfinity = -math.huge
  9. Mathf.PI = math.pi
  10. Mathf.Rad2Deg = math.deg(1)
  11. Mathf.Abs = math.abs
  12. Mathf.Acos = math.acos
  13. Mathf.Asin = math.asin
  14. Mathf.Atan = math.atan
  15. Mathf.Atan2 = math.atan2
  16. Mathf.Ceil = math.ceil
  17. Mathf.Cos = math.cos
  18. Mathf.Exp = math.exp
  19. Mathf.Floor = math.floor
  20. Mathf.Log = math.log
  21. Mathf.Log10 = math.log10
  22. Mathf.Max = math.max
  23. Mathf.Min = math.min
  24. Mathf.Pow = math.pow
  25. Mathf.Sin = math.sin
  26. Mathf.Sqrt = math.sqrt
  27. Mathf.Tan = math.tan
  28. Mathf.Deg = math.deg
  29. Mathf.Rad = math.rad
  30. Mathf.Random = math.random
  31. function Mathf.Sign( value )
  32. if(value < 0) then
  33. return -1
  34. else
  35. return 1
  36. end
  37. end
  38. function Mathf.Approximately(a, b)
  39. return abs(b - a) < math.max(1e-6 * math.max(abs(a), abs(b)), 1.121039e-44)
  40. end
  41. function Mathf.Clamp(value, min, max)
  42. if value < min then
  43. value = min
  44. elseif value > max then
  45. value = max
  46. end
  47. return value
  48. end
  49. function Mathf.Clamp01(value)
  50. if value < 0 then
  51. return 0
  52. elseif value > 1 then
  53. return 1
  54. end
  55. return value
  56. end
  57. function Mathf.DeltaAngle(current, target)
  58. local num = Mathf.Repeat(target - current, 360)
  59. if num > 180 then
  60. num = num - 360
  61. end
  62. return num
  63. end
  64. function Mathf.Gamma(value, absmax, gamma)
  65. local flag = false
  66. if value < 0 then
  67. flag = true
  68. end
  69. local num = abs(value)
  70. if num > absmax then
  71. return (not flag) and num or -num
  72. end
  73. local num2 = math.pow(num / absmax, gamma) * absmax
  74. return (not flag) and num2 or -num2
  75. end
  76. function Mathf.InverseLerp(from, to, value)
  77. if from < to then
  78. if value < from then
  79. return 0
  80. end
  81. if value > to then
  82. return 1
  83. end
  84. value = value - from
  85. value = value/(to - from)
  86. return value
  87. end
  88. if from <= to then
  89. return 0
  90. end
  91. if value < to then
  92. return 1
  93. end
  94. if value > from then
  95. return 0
  96. end
  97. return 1 - ((value - to) / (from - to))
  98. end
  99. function Mathf.Lerp(from, to, t)
  100. return from + (to - from) * Mathf.Clamp01(t)
  101. end
  102. function Mathf.LerpAngle(a, b, t)
  103. local num = Mathf.Repeat(b - a, 360)
  104. if num > 180 then
  105. num = num - 360
  106. end
  107. return a + num * Mathf.Clamp01(t)
  108. end
  109. function Mathf.LerpUnclamped(a, b, t)
  110. return a + (b - a) * t;
  111. end
  112. function Mathf.MoveTowards(current, target, maxDelta)
  113. if abs(target - current) <= maxDelta then
  114. return target
  115. end
  116. return current + Mathf.Sign(target - current) * maxDelta
  117. end
  118. function Mathf.MoveTowardsAngle(current, target, maxDelta)
  119. target = current + Mathf.DeltaAngle(current, target)
  120. return Mathf.MoveTowards(current, target, maxDelta)
  121. end
  122. function Mathf.PingPong(t, length)
  123. t = Mathf.Repeat(t, length * 2)
  124. return length - abs(t - length)
  125. end
  126. function Mathf.Repeat(t, length)
  127. return t - (floor(t / length) * length)
  128. end
  129. function Mathf.Round(num)
  130. return floor(num + 0.5)
  131. end
  132. function Mathf.Sign(num)
  133. if num > 0 then
  134. num = 1
  135. elseif num < 0 then
  136. num = -1
  137. else
  138. num = 0
  139. end
  140. return num
  141. end
  142. function Mathf.SmoothDamp(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  143. maxSpeed = maxSpeed or Mathf.Infinity
  144. deltaTime = deltaTime or Time.deltaTime
  145. smoothTime = Mathf.Max(0.0001, smoothTime)
  146. local num = 2 / smoothTime
  147. local num2 = num * deltaTime
  148. local num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2)
  149. local num4 = current - target
  150. local num5 = target
  151. local max = maxSpeed * smoothTime
  152. num4 = Mathf.Clamp(num4, -max, max)
  153. target = current - num4
  154. local num7 = (currentVelocity + (num * num4)) * deltaTime
  155. currentVelocity = (currentVelocity - num * num7) * num3
  156. local num8 = target + (num4 + num7) * num3
  157. if (num5 > current) == (num8 > num5) then
  158. num8 = num5
  159. currentVelocity = (num8 - num5) / deltaTime
  160. end
  161. return num8
  162. end
  163. function Mathf.SmoothDampAngle(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  164. deltaTime = deltaTime or Time.deltaTime
  165. maxSpeed = maxSpeed or Mathf.Infinity
  166. target = current + Mathf.DeltaAngle(current, target)
  167. return Mathf.SmoothDamp(current, target, currentVelocity, smoothTime, maxSpeed, deltaTime)
  168. end
  169. function Mathf.SmoothStep(from, to, t)
  170. t = Mathf.Clamp01(t)
  171. t = -2 * t * t * t + 3 * t * t
  172. return to * t + from * (1 - t)
  173. end
  174. function Mathf.HorizontalAngle(dir)
  175. return math.deg(math.atan2(dir.x, dir.z))
  176. end
  177. function Mathf.IsNan(number)
  178. return not (number == number)
  179. end
  180. UnityEngine.Mathf = Mathf
  181. return Mathf