utils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. var lerp = {
  2. vector: function (e, t, f) {//xzw change, add f
  3. var i = e.clone();
  4. return t = t.clone(),
  5. function (n) {
  6. e.set(i.x * (1 - n) + t.x * n, i.y * (1 - n) + t.y * n, i.z * (1 - n) + t.z * n)
  7. f && f(e, n);
  8. }
  9. },
  10. quaternion: function (e, t, f) {//xzw change, add f
  11. var i = e.clone();
  12. return function (n) {
  13. e.copy(i).slerp(t, n);
  14. f && f(e, n);
  15. }
  16. },
  17. property: function (e, t, i, n) {
  18. var r = e[t];
  19. return function (o) {
  20. e[t] = r * (1 - o) + i * o,
  21. n && n(e[t])
  22. }
  23. },
  24. uniform: function (e, t, i) {
  25. var n = e.material.uniforms[t].value;
  26. return function (r) {
  27. try {
  28. e.material.uniforms[t] && (e.material.uniforms[t].value = n * (1 - r) + i * r)
  29. } catch (e) {
  30. console.log(1)
  31. }
  32. }
  33. },
  34. matrix4: function (e, t) {
  35. var i = e.clone();
  36. return function (n) {
  37. for (var r = e.elements, o = i.elements, a = t.elements, s = 0; s < 16; s++)
  38. r[s] = o[s] * (1 - n) + a[s] * n
  39. }
  40. },
  41. allUniforms: function (e, t, i) {
  42. var n = e.map(function (e) {
  43. return this.uniform(e, t, i)
  44. }
  45. .bind(this));
  46. return function (e) {
  47. n.forEach(function (t) {
  48. t(e)
  49. })
  50. }
  51. }
  52. };
  53. //////
  54. var easing = {};
  55. //渐变曲线函数,反应加速度的变化
  56. easing.linearTween = function (e, t, i, n) {
  57. return i * e / n + t
  58. }
  59. ,
  60. easing.easeInQuad = function (e, t, i, n) {
  61. return e /= n,
  62. i * e * e + t
  63. }
  64. ,
  65. easing.easeOutQuad = function (e, t, i, n) {
  66. return e /= n,
  67. -i * e * (e - 2) + t
  68. }
  69. ,
  70. easing.easeInOutQuad = function (e, t, i, n) {
  71. return e /= n / 2,
  72. e < 1 ? i / 2 * e * e + t : (e--,
  73. -i / 2 * (e * (e - 2) - 1) + t)
  74. }
  75. ,
  76. easing.easeInCubic = function (e, t, i, n) {
  77. return e /= n,
  78. i * e * e * e + t
  79. }
  80. ,
  81. easing.easeOutCubic = function (e, t, i, n) {
  82. return e /= n,
  83. e--,
  84. i * (e * e * e + 1) + t
  85. }
  86. ,
  87. easing.easeInOutCubic = function (e, t, i, n) {
  88. return e /= n / 2,
  89. e < 1 ? i / 2 * e * e * e + t : (e -= 2,
  90. i / 2 * (e * e * e + 2) + t)
  91. }
  92. ,
  93. easing.easeInQuart = function (e, t, i, n) {
  94. return e /= n,
  95. i * e * e * e * e + t
  96. }
  97. ,
  98. easing.easeOutQuart = function (e, t, i, n) {
  99. return e /= n,
  100. e--,
  101. -i * (e * e * e * e - 1) + t
  102. }
  103. ,
  104. easing.easeInOutQuart = function (e, t, i, n) {
  105. return e /= n / 2,
  106. e < 1 ? i / 2 * e * e * e * e + t : (e -= 2,
  107. -i / 2 * (e * e * e * e - 2) + t)
  108. }
  109. ,
  110. easing.easeInQuint = function (e, t, i, n) {
  111. return e /= n,
  112. i * e * e * e * e * e + t
  113. }
  114. ,
  115. easing.easeOutQuint = function (e, t, i, n) {
  116. return e /= n,
  117. e--,
  118. i * (e * e * e * e * e + 1) + t
  119. }
  120. ,
  121. easing.easeInOutQuint = function (e, t, i, n) {
  122. return e /= n / 2,
  123. e < 1 ? i / 2 * e * e * e * e * e + t : (e -= 2,
  124. i / 2 * (e * e * e * e * e + 2) + t)
  125. }
  126. ,
  127. easing.easeInSine = function (e, t, i, n) {
  128. return -i * Math.cos(e / n * (Math.PI / 2)) + i + t
  129. }
  130. ,
  131. easing.easeOutSine = function (e, t, i, n) {
  132. return i * Math.sin(e / n * (Math.PI / 2)) + t
  133. }
  134. ,
  135. easing.easeInOutSine = function (e, t, i, n) {
  136. return -i / 2 * (Math.cos(Math.PI * e / n) - 1) + t
  137. }
  138. ,
  139. easing.easeInExpo = function (e, t, i, n) {
  140. return i * Math.pow(2, 10 * (e / n - 1)) + t
  141. }
  142. ,
  143. easing.easeOutExpo = function (e, t, i, n) {
  144. return i * (-Math.pow(2, -10 * e / n) + 1) + t
  145. }
  146. ,
  147. easing.easeInOutExpo = function (e, t, i, n) {
  148. return e /= n / 2,
  149. e < 1 ? i / 2 * Math.pow(2, 10 * (e - 1)) + t : (e--,
  150. i / 2 * (-Math.pow(2, -10 * e) + 2) + t)
  151. }
  152. ,
  153. easing.easeInCirc = function (e, t, i, n) {
  154. return e /= n,
  155. -i * (Math.sqrt(1 - e * e) - 1) + t
  156. }
  157. ,
  158. easing.easeOutCirc = function (e, t, i, n) {
  159. return e /= n,
  160. e--,
  161. i * Math.sqrt(1 - e * e) + t
  162. }
  163. ,
  164. easing.easeInOutCirc = function (e, t, i, n) {
  165. return e /= n / 2,
  166. e < 1 ? -i / 2 * (Math.sqrt(1 - e * e) - 1) + t : (e -= 2,
  167. i / 2 * (Math.sqrt(1 - e * e) + 1) + t)
  168. }
  169. ,
  170. easing.easeInElastic = function (e, t, i, n) {
  171. var r = 1.70158
  172. , o = 0
  173. , a = i;
  174. return 0 === e ? t : 1 === (e /= n) ? t + i : (o || (o = .3 * n),
  175. a < Math.abs(i) ? (a = i,
  176. r = o / 4) : r = o / (2 * Math.PI) * Math.asin(i / a),
  177. -(a * Math.pow(2, 10 * (e -= 1)) * Math.sin((e * n - r) * (2 * Math.PI) / o)) + t)
  178. }
  179. ,
  180. easing.easeOutElastic = function (e, t, i, n) {
  181. var r = 1.70158
  182. , o = 0
  183. , a = i;
  184. return 0 === e ? t : 1 === (e /= n) ? t + i : (o || (o = .3 * n),
  185. a < Math.abs(i) ? (a = i,
  186. r = o / 4) : r = o / (2 * Math.PI) * Math.asin(i / a),
  187. a * Math.pow(2, -10 * e) * Math.sin((e * n - r) * (2 * Math.PI) / o) + i + t)
  188. }
  189. ,
  190. easing.easeInOutElastic = function (e, t, i, n) {
  191. var r = 1.70158
  192. , o = 0
  193. , a = i;
  194. return 0 === e ? t : 2 === (e /= n / 2) ? t + i : (o || (o = n * (.3 * 1.5)),
  195. a < Math.abs(i) ? (a = i,
  196. r = o / 4) : r = o / (2 * Math.PI) * Math.asin(i / a),
  197. e < 1 ? -.5 * (a * Math.pow(2, 10 * (e -= 1)) * Math.sin((e * n - r) * (2 * Math.PI) / o)) + t : a * Math.pow(2, -10 * (e -= 1)) * Math.sin((e * n - r) * (2 * Math.PI) / o) * .5 + i + t)
  198. }
  199. ,
  200. easing.easeInBack = function (e, t, i, n, r) {
  201. return void 0 === r && (r = 1.70158),
  202. i * (e /= n) * e * ((r + 1) * e - r) + t
  203. }
  204. ,
  205. easing.easeOutBack = function (e, t, i, n, r) {
  206. return void 0 === r && (r = 1.70158),
  207. i * ((e = e / n - 1) * e * ((r + 1) * e + r) + 1) + t
  208. }
  209. ,
  210. easing.easeInOutBack = function (e, t, i, n, r) {
  211. return void 0 === r && (r = 1.70158),
  212. (e /= n / 2) < 1 ? i / 2 * (e * e * (((r *= 1.525) + 1) * e - r)) + t : i / 2 * ((e -= 2) * e * (((r *= 1.525) + 1) * e + r) + 2) + t
  213. }
  214. ,
  215. easing.easeOutBounce = function (e, t, i, n) {
  216. return (e /= n) < 1 / 2.75 ? i * (7.5625 * e * e) + t : e < 2 / 2.75 ? i * (7.5625 * (e -= 1.5 / 2.75) * e + .75) + t : e < 2.5 / 2.75 ? i * (7.5625 * (e -= 2.25 / 2.75) * e + .9375) + t : i * (7.5625 * (e -= 2.625 / 2.75) * e + .984375) + t
  217. }
  218. ,
  219. easing.easeInBounce = function (e, t, i, r) {
  220. return i - easing.easeOutBounce(r - e, 0, i, r) + t
  221. }
  222. ,
  223. easing.easeInOutBounce = function (e, t, i, r) {
  224. return e < r / 2 ? .5 * easing.easeInBounce(2 * e, 0, i, r) + t : .5 * easing.easeOutBounce(x, 2 * e - r, 0, i, r) + .5 * i + t
  225. }
  226. /*
  227. 渐变
  228. */
  229. var transitions = {
  230. globalDone: null,
  231. funcs: [],
  232. counter: 0,
  233. uniqueID: 0,
  234. start: function (e, t, i, r, o, a, s, cancelFun) {
  235. return r = r || 0,
  236. this.funcs.push({
  237. func: e,
  238. current: -r * Math.abs(t), //当前时间
  239. duration: (1 - Math.max(r, 0)) * Math.abs(t), //总时长
  240. done: i,
  241. easing: o || easing.linearTween, //渐变曲线
  242. cycling: t < 0,
  243. running: !0,
  244. debug: r < 0,
  245. name: a || "T" + this.counter,
  246. id: void 0 === s ? this.counter : s,
  247. paused: !1,
  248. cancelFun: cancelFun, //取消时执行的函数
  249. }),
  250. e(0, 16),
  251. this.counter += 1,
  252. e
  253. },
  254. trigger: function (e) {
  255. var t = void 0 === e.delayRatio ? 0 : e.delayRatio
  256. , i = e.func || function () { }
  257. , r = void 0 === e.duration ? 0 : e.duration;
  258. void 0 !== e.cycling && e.cycling && (r = -Math.abs(r));
  259. var o = e.done || null
  260. , a = e.easing || easing.linearTween
  261. , s = e.name || "R" + this.counter
  262. , l = void 0 === e.id ? this.counter : e.id;
  263. return this.start(i, r, o, t, a, s, l)
  264. },
  265. setTimeout: function (e, t, i) {
  266. var n = void 0 === i ? this.counter : i;
  267. return this.trigger({
  268. done: e,
  269. duration: void 0 === t ? 0 : t,
  270. name: "O" + this.counter,
  271. id: n
  272. })
  273. },
  274. pause: function () {
  275. this.paused = !0
  276. },
  277. resume: function () {
  278. this.paused = !1
  279. },
  280. update: function (e) {
  281. this.funcs.forEach(function (t) {
  282. if (!(t.paused || (t.current += 1e3 * e,
  283. t.current < 0)))
  284. if (t.current >= t.duration && !t.cycling) {
  285. var i = t.easing(1, 0, 1, 1);
  286. t.func(i, 1e3 * e),
  287. t.done && t.done(),
  288. t.running = !1
  289. } else {
  290. var n = t.easing(t.current % t.duration / t.duration, 0, 1, 1)
  291. , r = t.func(n, 1e3 * e) || !1;
  292. r && (t.done && t.done(),
  293. t.running = !1)
  294. }
  295. });
  296. var t = this.funcs.length;
  297. this.funcs = this.funcs.filter(function (e) {
  298. return e.running
  299. });
  300. var i = this.funcs.length;
  301. if (t > 0 && 0 === i && this.globalDone) {
  302. var n = this.globalDone;
  303. this.globalDone = null,
  304. n()
  305. }
  306. },
  307. adjustSpeed: function (e, t) {
  308. for (var i = this.getById(e), n = 0; n < i.length; n++) {
  309. var r = i[n];
  310. r.duration /= t,
  311. r.current /= t
  312. }
  313. },
  314. getById: function (e) {
  315. return this.funcs.filter(function (t) {
  316. return e === t.id
  317. })
  318. },
  319. get: function (e) {
  320. for (var t = 0; t < this.funcs.length; t += 1)
  321. if (this.funcs[t].func === e)
  322. return this.funcs[t];
  323. return null
  324. },
  325. isRunning: function (e) {
  326. var t = this.get(e);
  327. return null !== t && t.running
  328. },
  329. countActive: function () {
  330. for (var e = 0, t = 0; t < this.funcs.length; t += 1)
  331. e += this.funcs[t].running;
  332. return e
  333. },
  334. listActive: function () {
  335. for (var e = [], t = 0; t < this.funcs.length; t += 1)
  336. this.funcs[t].running && e.push(this.funcs[t].name);
  337. return e
  338. },
  339. done: function (e) {
  340. this.globalDone = e
  341. },
  342. cancelById: function (e, dealCancelFun) { //xzw add dealDone
  343. var t = void 0 === e ? 0 : e;
  344. this.funcs = this.funcs.filter(function (e) {
  345. var is = e.id == t;
  346. if (is && dealCancelFun) {
  347. e.cancelFun && e.cancelFun()
  348. }
  349. return !is
  350. })
  351. },
  352. cancelByName: function (e, dealCancelFun) { //xzw add dealDone
  353. var t = void 0 === e ? 0 : e;
  354. this.funcs = this.funcs.filter(function (e) {
  355. var is = e.name == t;
  356. if (is && dealCancelFun) {
  357. e.cancelFun && e.cancelFun()
  358. }
  359. return !is
  360. })
  361. },
  362. cancel: function (e) {
  363. this.funcs = this.funcs.filter(function (t) {
  364. return t.func !== e
  365. })
  366. },
  367. getUniqueId: function () {
  368. return this.uniqueID -= 1,
  369. this.uniqueID
  370. }
  371. };
  372. let convertTool = {
  373. getPos2d: function (point, camera, dom) {//获取一个三维坐标对应屏幕中的二维坐标
  374. if (!camera) return
  375. var pos = point.clone().project(camera) //比之前hotspot的计算方式写得简单 project用于3转2(求法同shader); unproject用于2转3 :new r.Vector3(e.x, e.y, -1).unproject(this.camera);
  376. var x, y;
  377. x = (pos.x + 1) / 2 * dom.clientWidth;
  378. y = (1 - (pos.y + 1) / 2) * dom.clientHeight;
  379. var inSight = x <= dom.clientWidth && x >= 0 //是否在屏幕中
  380. && y <= dom.clientHeight && y >= 0
  381. return {
  382. pos: new THREE.Vector2(x, y), // 屏幕像素坐标
  383. vector: pos, //(范围 -1 ~ 1)
  384. trueSide: pos.z < 1, //trueSide为false时,即使在屏幕范围内可见,也是反方向的另一个不可以被渲染的点 参见Tag.update
  385. inSight: inSight //在屏幕范围内可见
  386. };
  387. },
  388. ifShelter: function (pos3d, pos2d, camera, colliders, margin = 0) {
  389. //检测某点在视线中是否被mesh遮挡
  390. if (!pos2d) pos2d = convertTool.getPos2d(pos3d)
  391. camera = camera || player.camera
  392. var ori = new THREE.Vector3(pos2d.x, pos2d.y, -1).unproject(camera) //找到视线原点
  393. var dir = pos3d.clone().sub(ori).normalize()
  394. var ray = new THREE.Raycaster(ori, dir); //由外向里 因为模型从内侧是可见的所以从外侧
  395. var o = ray.intersectObjects(colliders);
  396. var len = pos3d.distanceTo(ori);
  397. if (o && o.length) {
  398. for (var i = 0; i < o.length; i++) {
  399. if (o[i].distance < len - margin) { return true; }//有遮挡
  400. }
  401. }
  402. },
  403. updateVisible: function (object, reason, ifShow, level = 0, type) {//当所有加入的条件都不为false时才显示. reason='force'一般是强制、临时的
  404. if (!object.unvisibleReasons) object.unvisibleReasons = []; //如果length>0代表不可见
  405. if (!object.visibleReasons) object.visibleReasons = []; //在同级时,优先可见
  406. var update = function () {
  407. //先按从高到低的level排列
  408. object.unvisibleReasons = object.unvisibleReasons.sort((a, b) => b.level - a.level)
  409. object.visibleReasons = object.visibleReasons.sort((a, b) => b.level - a.level)
  410. var maxVisiLevel = object.visibleReasons[0] ? object.visibleReasons[0].level : -1
  411. var maxunVisiLevel = object.unvisibleReasons[0] ? object.unvisibleReasons[0].level : -1
  412. var shouldVisi = maxVisiLevel >= maxunVisiLevel
  413. var visiBefore = object.visible
  414. if (visiBefore != shouldVisi) {
  415. object.visible = shouldVisi
  416. object.dispatchEvent({
  417. type: 'isVisible',
  418. visible: shouldVisi,
  419. reason,
  420. })
  421. }
  422. }
  423. if (ifShow) {
  424. var index = object.unvisibleReasons.findIndex(e => e.reason == reason)
  425. if (index > -1) {
  426. type = 'cancel'
  427. object.unvisibleReasons.splice(index, 1);
  428. }
  429. if (type == 'add') {
  430. if (!object.visibleReasons.some(e => e.reason == reason)) {
  431. object.visibleReasons.push({ reason, level })
  432. }
  433. }
  434. } else {
  435. var index = object.visibleReasons.findIndex(e => e.reason == reason)
  436. if (index > -1) {
  437. type = 'cancel'
  438. object.visibleReasons.splice(index, 1);
  439. }
  440. if (type != 'cancel') {
  441. if (!object.unvisibleReasons.some(e => e.reason == reason)) {
  442. object.unvisibleReasons.push({ reason, level })
  443. }
  444. }
  445. }
  446. update()
  447. },
  448. toPrecision: function (e, t) {//xzw change 保留小数
  449. var f = function (e, t) {
  450. var i = Math.pow(10, t);
  451. return Math.round(e * i) / i
  452. }
  453. if (e instanceof Array) {
  454. for (var s = 0; s < e.length; s++) {
  455. e[s] = f(e[s], t);
  456. }
  457. return e;
  458. } else if (e instanceof Object) {
  459. for (var s in e) {
  460. e[s] = f(e[s], t);
  461. }
  462. return e;
  463. } else return f(e, t)
  464. },
  465. intervalTool:{ //延时update,防止卡顿
  466. list:[],
  467. isWaiting:function(name, func, delayTime){
  468. if(!this.list.includes(name)){ //如果没有该项, 则开始判断
  469. var needWait = func(); //触发了改变,则等待一段时间后再自动判断
  470. if(needWait){
  471. this.list.push(name);
  472. setTimeout(()=>{
  473. var a = this.list.indexOf(name);
  474. this.list.splice(a,1);
  475. this.isWaiting(name, func, delayTime) //循环
  476. },delayTime)
  477. }
  478. }
  479. },
  480. }
  481. ,
  482. }
  483. let math = {
  484. closeTo : function(a,b, precision=1e-6){
  485. let f = (a,b)=>{
  486. return Math.abs(a-b) < precision;
  487. };
  488. if(typeof (a) == 'number'){
  489. return f(a, b);
  490. }else {
  491. let judge = (name)=>{
  492. if(a[name] == void 0)return true //有值就判断,没值就不判断
  493. else return f(a[name],b[name])
  494. };
  495. return judge('x') && judge('y') && judge('z') && judge('w')
  496. }
  497. },
  498. }