源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

537 lines
12 KiB

  1. local math = math
  2. local acos = math.acos
  3. local sqrt = math.sqrt
  4. local max = math.max
  5. local min = math.min
  6. local clamp = Mathf.Clamp
  7. local cos = math.cos
  8. local sin = math.sin
  9. local abs = math.abs
  10. local sign = Mathf.Sign
  11. local setmetatable = setmetatable
  12. local rawset = rawset
  13. local rawget = rawget
  14. local type = type
  15. local rad2Deg = Mathf.Rad2Deg
  16. local deg2Rad = Mathf.Deg2Rad
  17. local Vector3 = {}
  18. local get = tolua.initget(Vector3)
  19. Vector3.__index = function(t,k)
  20. local var = rawget(Vector3, k)
  21. if var == nil then
  22. var = rawget(get, k)
  23. if var ~= nil then
  24. return var(t)
  25. end
  26. end
  27. return var
  28. end
  29. function Vector3.New(x, y, z)
  30. -- print(debug.traceback(""))
  31. -- print("33333333333333333333333333333333 Vector3.New")
  32. local v = {x = x or 0, y = y or 0, z = z or 0}
  33. setmetatable(v, Vector3)
  34. return v
  35. end
  36. local _new = Vector3.New
  37. Vector3.__call = function(t,x,y,z)
  38. return _new(x,y,z)
  39. end
  40. function Vector3:Set(x,y,z)
  41. self.x = x or 0
  42. self.y = y or 0
  43. self.z = z or 0
  44. end
  45. function Vector3:Get()
  46. return self.x, self.y, self.z
  47. end
  48. function Vector3:Clone()
  49. return _new(self.x, self.y, self.z)
  50. end
  51. function Vector3.Distance(va, vb)
  52. return sqrt((va.x - vb.x)^2 + (va.y - vb.y)^2 + (va.z - vb.z)^2)
  53. end
  54. function Vector3.Dot(lhs, rhs)
  55. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z
  56. end
  57. function Vector3.Lerp(from, to, t)
  58. t = clamp(t, 0, 1)
  59. return _new(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t, from.z + (to.z - from.z) * t)
  60. end
  61. function Vector3:Magnitude()
  62. return sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  63. end
  64. function Vector3.Max(lhs, rhs)
  65. return _new(max(lhs.x, rhs.x), max(lhs.y, rhs.y), max(lhs.z, rhs.z))
  66. end
  67. function Vector3.Min(lhs, rhs)
  68. return _new(min(lhs.x, rhs.x), min(lhs.y, rhs.y), min(lhs.z, rhs.z))
  69. end
  70. function Vector3.Normalize(v)
  71. local x,y,z = v.x, v.y, v.z
  72. local num = sqrt(x * x + y * y + z * z)
  73. if num > 1e-5 then
  74. return _new(x/num, y/num, z/num)
  75. end
  76. return _new(0, 0, 0)
  77. end
  78. function Vector3:SetNormalize()
  79. local num = sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  80. if num > 1e-5 then
  81. self.x = self.x / num
  82. self.y = self.y / num
  83. self.z = self.z /num
  84. else
  85. self.x = 0
  86. self.y = 0
  87. self.z = 0
  88. end
  89. return self
  90. end
  91. function Vector3:SqrMagnitude()
  92. return self.x * self.x + self.y * self.y + self.z * self.z
  93. end
  94. local dot = Vector3.Dot
  95. function Vector3.Angle(from, to)
  96. return acos(clamp(dot(from:Normalize(), to:Normalize()), -1, 1)) * rad2Deg
  97. end
  98. function Vector3:ClampMagnitude(maxLength)
  99. if self:SqrMagnitude() > (maxLength * maxLength) then
  100. self:SetNormalize()
  101. self:Mul(maxLength)
  102. end
  103. return self
  104. end
  105. function Vector3.OrthoNormalize(va, vb, vc)
  106. va:SetNormalize()
  107. vb:Sub(vb:Project(va))
  108. vb:SetNormalize()
  109. if vc == nil then
  110. return va, vb
  111. end
  112. vc:Sub(vc:Project(va))
  113. vc:Sub(vc:Project(vb))
  114. vc:SetNormalize()
  115. return va, vb, vc
  116. end
  117. --[[function Vector3.RotateTowards2(from, to, maxRadiansDelta, maxMagnitudeDelta)
  118. local v2 = to:Clone()
  119. local v1 = from:Clone()
  120. local len2 = to:Magnitude()
  121. local len1 = from:Magnitude()
  122. v2:Div(len2)
  123. v1:Div(len1)
  124. local dota = dot(v1, v2)
  125. local angle = acos(dota)
  126. local theta = min(angle, maxRadiansDelta)
  127. local len = 0
  128. if len1 < len2 then
  129. len = min(len2, len1 + maxMagnitudeDelta)
  130. elseif len1 == len2 then
  131. len = len1
  132. else
  133. len = max(len2, len1 - maxMagnitudeDelta)
  134. end
  135. v2:Sub(v1 * dota)
  136. v2:SetNormalize()
  137. v2:Mul(sin(theta))
  138. v1:Mul(cos(theta))
  139. v2:Add(v1)
  140. v2:SetNormalize()
  141. v2:Mul(len)
  142. return v2
  143. end
  144. function Vector3.RotateTowards1(from, to, maxRadiansDelta, maxMagnitudeDelta)
  145. local omega, sinom, scale0, scale1, len, theta
  146. local v2 = to:Clone()
  147. local v1 = from:Clone()
  148. local len2 = to:Magnitude()
  149. local len1 = from:Magnitude()
  150. v2:Div(len2)
  151. v1:Div(len1)
  152. local cosom = dot(v1, v2)
  153. if len1 < len2 then
  154. len = min(len2, len1 + maxMagnitudeDelta)
  155. elseif len1 == len2 then
  156. len = len1
  157. else
  158. len = max(len2, len1 - maxMagnitudeDelta)
  159. end
  160. if 1 - cosom > 1e-6 then
  161. omega = acos(cosom)
  162. theta = min(omega, maxRadiansDelta)
  163. sinom = sin(omega)
  164. scale0 = sin(omega - theta) / sinom
  165. scale1 = sin(theta) / sinom
  166. v1:Mul(scale0)
  167. v2:Mul(scale1)
  168. v2:Add(v1)
  169. v2:Mul(len)
  170. return v2
  171. else
  172. v1:Mul(len)
  173. return v1
  174. end
  175. end]]
  176. function Vector3.MoveTowards(current, target, maxDistanceDelta)
  177. local delta = target - current
  178. local sqrDelta = delta:SqrMagnitude()
  179. local sqrDistance = maxDistanceDelta * maxDistanceDelta
  180. if sqrDelta > sqrDistance then
  181. local magnitude = sqrt(sqrDelta)
  182. --print("察看一下下4 ======== ",magnitude,maxDistanceDelta,delta)
  183. if magnitude > 1e-6 then--
  184. delta:Mul(maxDistanceDelta / magnitude) --magnitude
  185. delta:Add(current)
  186. -- print("察看一下下1 ======== ",delta)
  187. return delta
  188. else
  189. -- print("察看一下下2 ======== ",current)
  190. return current:Clone()
  191. end
  192. end
  193. -- print("察看一下下3 ======== ",target)
  194. return target:Clone()
  195. end
  196. function ClampedMove(lhs, rhs, clampedDelta)
  197. local delta = rhs - lhs
  198. if delta > 0 then
  199. return lhs + min(delta, clampedDelta)
  200. else
  201. return lhs - min(-delta, clampedDelta)
  202. end
  203. end
  204. local overSqrt2 = 0.7071067811865475244008443621048490
  205. local function OrthoNormalVector(vec)
  206. local res = _new()
  207. if abs(vec.z) > overSqrt2 then
  208. local a = vec.y * vec.y + vec.z * vec.z
  209. local k = 1 / sqrt (a)
  210. res.x = 0
  211. res.y = -vec.z * k
  212. res.z = vec.y * k
  213. else
  214. local a = vec.x * vec.x + vec.y * vec.y
  215. local k = 1 / sqrt (a)
  216. res.x = -vec.y * k
  217. res.y = vec.x * k
  218. res.z = 0
  219. end
  220. return res
  221. end
  222. function Vector3.RotateTowards(current, target, maxRadiansDelta, maxMagnitudeDelta)
  223. local len1 = current:Magnitude()
  224. local len2 = target:Magnitude()
  225. if len1 > 1e-6 and len2 > 1e-6 then
  226. local from = current / len1
  227. local to = target / len2
  228. local cosom = dot(from, to)
  229. if cosom > 1 - 1e-6 then
  230. return Vector3.MoveTowards (current, target, maxMagnitudeDelta)
  231. elseif cosom < -1 + 1e-6 then
  232. local axis = OrthoNormalVector(from)
  233. local q = Quaternion.AngleAxis(maxRadiansDelta * rad2Deg, axis)
  234. local rotated = q:MulVec3(from)
  235. local delta = ClampedMove(len1, len2, maxMagnitudeDelta)
  236. rotated:Mul(delta)
  237. return rotated
  238. else
  239. local angle = acos(cosom)
  240. local axis = Vector3.Cross(from, to)
  241. axis:SetNormalize ()
  242. local q = Quaternion.AngleAxis(min(maxRadiansDelta, angle) * rad2Deg, axis)
  243. local rotated = q:MulVec3(from)
  244. local delta = ClampedMove(len1, len2, maxMagnitudeDelta)
  245. rotated:Mul(delta)
  246. return rotated
  247. end
  248. end
  249. return Vector3.MoveTowards(current, target, maxMagnitudeDelta)
  250. end
  251. function Vector3.SmoothDamp(current, target, currentVelocity, smoothTime)
  252. local maxSpeed = Mathf.Infinity
  253. local deltaTime = Time.deltaTime
  254. smoothTime = max(0.0001, smoothTime)
  255. local num = 2 / smoothTime
  256. local num2 = num * deltaTime
  257. local num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2)
  258. local vector2 = target:Clone()
  259. local maxLength = maxSpeed * smoothTime
  260. local vector = current - target
  261. vector:ClampMagnitude(maxLength)
  262. target = current - vector
  263. local vec3 = (currentVelocity + (vector * num)) * deltaTime
  264. currentVelocity = (currentVelocity - (vec3 * num)) * num3
  265. local vector4 = target + (vector + vec3) * num3
  266. if Vector3.Dot(vector2 - current, vector4 - vector2) > 0 then
  267. vector4 = vector2
  268. currentVelocity:Set(0,0,0)
  269. end
  270. return vector4, currentVelocity
  271. end
  272. function Vector3.Scale(a, b)
  273. local x = a.x * b.x
  274. local y = a.y * b.y
  275. local z = a.z * b.z
  276. return _new(x, y, z)
  277. end
  278. function Vector3.Cross(lhs, rhs)
  279. local x = lhs.y * rhs.z - lhs.z * rhs.y
  280. local y = lhs.z * rhs.x - lhs.x * rhs.z
  281. local z = lhs.x * rhs.y - lhs.y * rhs.x
  282. return _new(x,y,z)
  283. end
  284. function Vector3:Equals(other)
  285. return self.x == other.x and self.y == other.y and self.z == other.z
  286. end
  287. function Vector3.Reflect(inDirection, inNormal)
  288. local num = -2 * dot(inNormal, inDirection)
  289. inNormal = inNormal * num
  290. inNormal:Add(inDirection)
  291. return inNormal
  292. end
  293. function Vector3.Project(vector, onNormal)
  294. local num = onNormal:SqrMagnitude()
  295. if num < 1.175494e-38 then
  296. return _new(0,0,0)
  297. end
  298. local num2 = dot(vector, onNormal)
  299. local v3 = onNormal:Clone()
  300. v3:Mul(num2/num)
  301. return v3
  302. end
  303. function Vector3.ProjectOnPlane(vector, planeNormal)
  304. local v3 = Vector3.Project(vector, planeNormal)
  305. v3:Mul(-1)
  306. v3:Add(vector)
  307. return v3
  308. end
  309. function Vector3.Slerp(from, to, t)
  310. local omega, sinom, scale0, scale1
  311. if t <= 0 then
  312. return from:Clone()
  313. elseif t >= 1 then
  314. return to:Clone()
  315. end
  316. local v2 = to:Clone()
  317. local v1 = from:Clone()
  318. local len2 = to:Magnitude()
  319. local len1 = from:Magnitude()
  320. v2:Div(len2)
  321. v1:Div(len1)
  322. local len = (len2 - len1) * t + len1
  323. local cosom = dot(v1, v2)
  324. if cosom > 1 - 1e-6 then
  325. scale0 = 1 - t
  326. scale1 = t
  327. elseif cosom < -1 + 1e-6 then
  328. local axis = OrthoNormalVector(from)
  329. local q = Quaternion.AngleAxis(180.0 * t, axis)
  330. local v = q:MulVec3(from)
  331. v:Mul(len)
  332. return v
  333. else
  334. omega = acos(cosom)
  335. sinom = sin(omega)
  336. scale0 = sin((1 - t) * omega) / sinom
  337. scale1 = sin(t * omega) / sinom
  338. end
  339. v1:Mul(scale0)
  340. v2:Mul(scale1)
  341. v2:Add(v1)
  342. v2:Mul(len)
  343. return v2
  344. end
  345. function Vector3:Mul(q)
  346. if type(q) == "number" then
  347. self.x = self.x * q
  348. self.y = self.y * q
  349. self.z = self.z * q
  350. else
  351. self:MulQuat(q)
  352. end
  353. return self
  354. end
  355. function Vector3:Div(d)
  356. self.x = self.x / d
  357. self.y = self.y / d
  358. self.z = self.z / d
  359. return self
  360. end
  361. function Vector3:Add(vb)
  362. self.x = self.x + vb.x
  363. self.y = self.y + vb.y
  364. self.z = self.z + vb.z
  365. return self
  366. end
  367. function Vector3:Sub(vb)
  368. self.x = self.x - vb.x
  369. self.y = self.y - vb.y
  370. self.z = self.z - vb.z
  371. return self
  372. end
  373. function Vector3:MulQuat(quat)
  374. local num = quat.x * 2
  375. local num2 = quat.y * 2
  376. local num3 = quat.z * 2
  377. local num4 = quat.x * num
  378. local num5 = quat.y * num2
  379. local num6 = quat.z * num3
  380. local num7 = quat.x * num2
  381. local num8 = quat.x * num3
  382. local num9 = quat.y * num3
  383. local num10 = quat.w * num
  384. local num11 = quat.w * num2
  385. local num12 = quat.w * num3
  386. local x = (((1 - (num5 + num6)) * self.x) + ((num7 - num12) * self.y)) + ((num8 + num11) * self.z)
  387. local y = (((num7 + num12) * self.x) + ((1 - (num4 + num6)) * self.y)) + ((num9 - num10) * self.z)
  388. local z = (((num8 - num11) * self.x) + ((num9 + num10) * self.y)) + ((1 - (num4 + num5)) * self.z)
  389. self:Set(x, y, z)
  390. return self
  391. end
  392. function Vector3.AngleAroundAxis (from, to, axis)
  393. from = from - Vector3.Project(from, axis)
  394. to = to - Vector3.Project(to, axis)
  395. local angle = Vector3.Angle (from, to)
  396. return angle * (Vector3.Dot (axis, Vector3.Cross (from, to)) < 0 and -1 or 1)
  397. end
  398. Vector3.__tostring = function(self)
  399. return "["..self.x..","..self.y..","..self.z.."]"
  400. end
  401. Vector3.__div = function(va, d)
  402. return _new(va.x / d, va.y / d, va.z / d)
  403. end
  404. Vector3.__mul = function(va, d)
  405. if type(d) == "number" then
  406. return _new(va.x * d, va.y * d, va.z * d)
  407. else
  408. local vec = va:Clone()
  409. vec:MulQuat(d)
  410. return vec
  411. end
  412. end
  413. Vector3.__add = function(va, vb)
  414. return _new(va.x + vb.x, va.y + vb.y, va.z + vb.z)
  415. end
  416. Vector3.__sub = function(va, vb)
  417. return _new(va.x - vb.x, va.y - vb.y, va.z - vb.z)
  418. end
  419. Vector3.__unm = function(va)
  420. return _new(-va.x, -va.y, -va.z)
  421. end
  422. Vector3.__eq = function(a,b)
  423. local v = a - b
  424. local delta = v:SqrMagnitude()
  425. return delta < 1e-10
  426. end
  427. get.up = function() return _new(0,1,0) end
  428. get.down = function() return _new(0,-1,0) end
  429. get.right = function() return _new(1,0,0) end
  430. get.left = function() return _new(-1,0,0) end
  431. get.forward = function() return _new(0,0,1) end
  432. get.back = function() return _new(0,0,-1) end
  433. get.zero = function() return _new(0,0,0) end
  434. get.one = function() return _new(1,1,1) end
  435. get.magnitude = Vector3.Magnitude
  436. get.normalized = Vector3.Normalize
  437. get.sqrMagnitude= Vector3.SqrMagnitude
  438. UnityEngine.Vector3 = Vector3
  439. setmetatable(Vector3, Vector3)
  440. return Vector3