PlayerUtils.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. const NULL = 0;
  2. const PI = 3.14159265358979323846264338327;
  3. const AreaShowType = {
  4. Area_AabbBox: 0, //Aabb包围盒
  5. Area_Hexaherdron: 1, //六面体
  6. Area_TriangularPrism: 2, //三棱柱
  7. Area_Cylinder: 3, //圆柱
  8. }
  9. // 添加 Math.clamp 函数
  10. if (!Math.clamp)
  11. {
  12. Math.clamp = function(a, min, max) {
  13. if (a < min) return min;
  14. else if (a > max) return max;
  15. return a;
  16. }
  17. }
  18. /**
  19. * 三维向量,包含向量常用的运算方法,当前系统使用数组表示向量,三维向量是包含三个Number的数组
  20. */
  21. let Vec3 = (function(){
  22. return {
  23. /**
  24. * 判断对象是否是合格的向量
  25. * @param {*} a 检查对象
  26. */
  27. check: function(a)
  28. {
  29. if (!Array.isArray(a))
  30. return false;
  31. for(let i = 0; i < 3; ++i)
  32. if (isNaN(a[i]))
  33. return false;
  34. return true;
  35. },
  36. /**
  37. * 向量a与向量b相加,a + b
  38. * @param {[Number, Number, Number]} a 向量a
  39. * @param {[Number, Number, Number]} b 向量b
  40. */
  41. add: function(a, b)
  42. {
  43. return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
  44. },
  45. /**
  46. * 向量a与常数b相加,a + b
  47. * @param {[Number, Number, Number]} a 向量a
  48. * @param {Number} b 常数b
  49. */
  50. addScalar: function(a, b)
  51. {
  52. return [a[0] + b, a[1] + b, a[2] + b];
  53. },
  54. /**
  55. * 向量a与向量b相减, a - b
  56. * @param {[Number, Number, Number]} a 向量a
  57. * @param {[Number, Number, Number]} b 向量b
  58. */
  59. sub: function(a, b)
  60. {
  61. return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
  62. },
  63. /**
  64. * 向量a与常数b相减,a - b
  65. * @param {[Number, Number, Number]} a 向量a
  66. * @param {Number} b 常数b
  67. */
  68. subScalar: function(a, b)
  69. {
  70. return [a[0] - b, a[1] - b, a[2] - b];
  71. },
  72. /**
  73. * 向量a与向量b相乘, a * b
  74. * @param {[Number, Number, Number]} a 向量a
  75. * @param {[Number, Number, Number]} b 向量b
  76. */
  77. multiply: function(a, b)
  78. {
  79. return [a[0] * b[0], a[1] * b[1], a[2] * b[2]];
  80. },
  81. /**
  82. * 向量a与常数b相乘,a * b
  83. * @param {[Number, Number, Number]} a 向量a
  84. * @param {Number} b 常数b
  85. */
  86. multiplyScalar: function(a, b)
  87. {
  88. return [a[0] * b, a[1] * b, a[2] * b];
  89. },
  90. /**
  91. * 向量a与向量b相除, a / b
  92. * @param {[Number, Number, Number]} a 向量a
  93. * @param {[Number, Number, Number]} b 向量b
  94. * @note 向量b各分量不能为0
  95. */
  96. divide: function(a, b)
  97. {
  98. return [a[0] / b[0], a[1] / b[1], a[2] / b[2]];
  99. },
  100. /**
  101. * 向量a与常数b相除,a / b
  102. * @param {[Number, Number, Number]} a 向量a
  103. * @param {Number} b 常数b
  104. * @note 常数b不能为0
  105. */
  106. divideScalar: function(a, b)
  107. {
  108. return [a[0] / b, a[1] / b, a[2] / b];
  109. },
  110. /**
  111. * 向量a与向量b点乘, dot(a, b) = |a| * |b| * cos
  112. * @param {[Number, Number, Number]} a 向量a
  113. * @param {[Number, Number, Number]} b 向量b
  114. */
  115. dot: function(a, b)
  116. {
  117. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  118. },
  119. /**
  120. * 向量a与向量b叉乘, cross(a, b) = 法向量,同时垂直于向量a和向量b
  121. * @param {[Number, Number, Number]} a 向量a
  122. * @param {[Number, Number, Number]} b 向量b
  123. */
  124. cross: function(a, b)
  125. {
  126. return [a[1] * b[2] - b[1] * a[2],
  127. a[2] * b[0] - b[2] * a[0],
  128. a[0] * b[1] - b[0] * a[1]
  129. ];
  130. },
  131. /**
  132. * 获取向量a的模长/长度, length(a) = |a|
  133. * @param {[Number, Number, Number]} a 向量a
  134. */
  135. length: function(a)
  136. {
  137. return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
  138. },
  139. /**
  140. * 获取向量a的模长/长度的平方,lengthSq(a) = length(a) * length(a)
  141. * @param {[Number, Number, Number]} a 向量a
  142. */
  143. lengthSq: function(a)
  144. {
  145. return a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
  146. },
  147. /**
  148. * 向量取反
  149. * @param {[Number, Number, Number]} a 向量a
  150. */
  151. negate: function(a)
  152. {
  153. return [-a[0], -a[1], -a[2]];
  154. },
  155. /**
  156. * 获取单位向量
  157. * @param {[Number, Number, Number]} a 向量a
  158. */
  159. normalize: function(a)
  160. {
  161. var len = Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]) || 1;
  162. return [a[0] / len, a[1] / len, a[2] / len];
  163. },
  164. /**
  165. * 向量a与向量b的夹角
  166. * @param {[Number, Number, Number]} a 向量a
  167. * @param {[Number, Number, Number]} b 向量b
  168. * @param 返回两个向量的夹角(角度制)
  169. */
  170. angleBetween: function (a, b)
  171. {
  172. const denominator = Vec3.length(a) * Vec3.length(b);
  173. if (denominator === 0)
  174. return Math.PI / 2 * (180 / Math.PI);
  175. let theta = Vec3.dot(a, b) / denominator;
  176. if (theta < -1)
  177. theta = -1;
  178. if (theta > 1)
  179. theta = 1;
  180. let angle = Math.acos(theta)
  181. angle *= 180 / Math.PI
  182. return angle
  183. }
  184. }
  185. })();
  186. /**
  187. * 行优先4x4矩阵
  188. */
  189. let Matrix4 = (function(){
  190. return {
  191. /**
  192. * 获取单位矩阵
  193. * @returns 单位矩阵
  194. */
  195. identity: function()
  196. {
  197. return [ 1, 0, 0, 0
  198. ,0, 1, 0, 0
  199. ,0, 0, 1, 0
  200. ,0, 0, 0, 1];
  201. },
  202. /**
  203. * 检查a是否是合法的矩阵
  204. * @param {*} a 检查对象
  205. */
  206. check: function(a)
  207. {
  208. if (!Array.isArray(a))
  209. return false;
  210. for(let i = 0; i < 16; ++i)
  211. if (isNaN(a[i]))
  212. return false;
  213. return true;
  214. },
  215. /**
  216. * 矩阵a与矩阵b相乘
  217. * @param {[...Number]} a
  218. * @param {[...Number]} b
  219. */
  220. multiply: function(a, b) {
  221. const ae = a;
  222. const be = b;
  223. const a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  224. const a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  225. const a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  226. const a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  227. const b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  228. const b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  229. const b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  230. const b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  231. let rMat =[]
  232. rMat[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  233. rMat[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  234. rMat[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  235. rMat[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  236. rMat[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  237. rMat[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  238. rMat[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  239. rMat[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  240. rMat[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  241. rMat[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  242. rMat[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  243. rMat[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  244. rMat[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  245. rMat[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  246. rMat[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  247. rMat[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  248. return rMat;
  249. },
  250. /**
  251. * 矩阵a与三维坐标b相乘,返回变换后的坐标
  252. * @param {[...Number]} a 矩阵a
  253. * @param {[Number,Number,Number]} b 三维坐标b
  254. */
  255. multiplyPos: function(a, b) {
  256. var ret = [];
  257. for(var i = 0; i < 3; ++i)
  258. {
  259. ret[i] = a[12 + i];
  260. for(var j = 0; j < 3; ++j)
  261. {
  262. ret[i] += b[j] * a[j * 4 + i];
  263. }
  264. }
  265. return ret;
  266. },
  267. /**
  268. * 矩阵a与方向向量b相乘,返回变化后的方向
  269. * @param {[...Number]} a 矩阵a
  270. * @param {[Number,Number,Number]} b 三维向量b
  271. */
  272. multiplyDir: function(a, b) {
  273. var ret = [];
  274. for(var i = 0; i < 3; ++i)
  275. {
  276. ret[i] = 0;
  277. for(var j = 0; j < 3; ++j)
  278. {
  279. ret[i] += b[j] * a[j * 4 + i];
  280. }
  281. }
  282. return ret;
  283. },
  284. /**
  285. * 构造TRS矩阵
  286. * @param position 位置
  287. * @param quaternion 旋转四元数(顺序wxyz)
  288. * @param scale 缩放
  289. * @returns 4x4 TRS矩阵
  290. */
  291. compose: function(position, quaternion, scale) {
  292. var ret = [];
  293. const x = quaternion[1];
  294. const y = quaternion[2];
  295. const z = quaternion[3];
  296. const w = quaternion[0];
  297. const x2 = x + x;
  298. const y2 = y + y;
  299. const z2 = z + z;
  300. const xx = x * x2;
  301. const xy = x * y2;
  302. const xz = x * z2;
  303. const yy = y * y2;
  304. const yz = y * z2;
  305. const zz = z * z2;
  306. const wx = w * x2;
  307. const wy = w * y2;
  308. const wz = w * z2;
  309. const sx = scale[0];
  310. const sy = scale[1];
  311. const sz = scale[2];
  312. ret[0] = (1 - (yy + zz)) * sx;
  313. ret[1] = (xy + wz) * sx;
  314. ret[2] = (xz - wy) * sx;
  315. ret[3] = 0;
  316. ret[4] = (xy - wz) * sy;
  317. ret[5] = (1 - (xx + zz)) * sy;
  318. ret[6] = (yz + wx) * sy;
  319. ret[7] = 0;
  320. ret[8] = (xz + wy) * sz;
  321. ret[9] = (yz - wx) * sz;
  322. ret[10] = (1 - (xx + yy)) * sz;
  323. ret[11] = 0;
  324. ret[12] = position[0];
  325. ret[13] = position[1];
  326. ret[14] = position[2];
  327. ret[15] = 1;
  328. return ret;
  329. },
  330. /**
  331. * 矩阵萃取
  332. * @param {*} m 将被萃取的矩阵对象
  333. * @returns 萃取结果
  334. * - position: 萃取的位置信息
  335. * - quaternion: 萃取的旋转四元数信息
  336. * - rotation: 萃取的旋转矩阵信息
  337. * - scale:萃取的缩放信息
  338. */
  339. decompose: function(m)
  340. {
  341. var a = {};
  342. var vx = [m[0], m[1], m[2]];
  343. var vy = [m[4], m[5], m[6]];
  344. var vz = [m[8], m[9], m[10]];
  345. let sx = Vec3.length(vx);
  346. const sy = Vec3.length(vy);
  347. const sz = Vec3.length(vz);
  348. // if determine is negative, we need to invert one scale
  349. const det = Matrix4.determinant(m);
  350. if ( det < 0 )
  351. sx = - sx;
  352. a.position = [];
  353. a.position[0] = m[ 12 ];
  354. a.position[1] = m[ 13 ];
  355. a.position[2] = m[ 14 ];
  356. // scale the rotation part
  357. var rMat = m;
  358. rMat[12] = 0;
  359. rMat[13] = 0;
  360. rMat[14] = 0;
  361. const invSX = 1 / sx;
  362. const invSY = 1 / sy;
  363. const invSZ = 1 / sz;
  364. rMat[ 0 ] *= invSX;
  365. rMat[ 1 ] *= invSX;
  366. rMat[ 2 ] *= invSX;
  367. rMat[ 4 ] *= invSY;
  368. rMat[ 5 ] *= invSY;
  369. rMat[ 6 ] *= invSY;
  370. rMat[ 8 ] *= invSZ;
  371. rMat[ 9 ] *= invSZ;
  372. rMat[ 10 ] *= invSZ;
  373. a.rotation = rMat;
  374. a.quaternion = Quat.fromRotationMatrix(rMat);
  375. a.scale = [];
  376. a.scale[0] = sx;
  377. a.scale[1] = sy;
  378. a.scale[2] = sz;
  379. return a;
  380. },
  381. /**
  382. * 计算矩阵行列式
  383. * @param {*} m 将被计算行列式的矩阵对象
  384. * @returns 矩阵的行列式值
  385. */
  386. determinant: function(m)
  387. {
  388. const n11 = m[ 0 ], n12 = m[ 1 ], n13 = m[ 2 ], n14 = m[ 3 ];
  389. const n21 = m[ 4 ], n22 = m[ 5 ], n23 = m[ 6 ], n24 = m[ 7 ];
  390. const n31 = m[ 8 ], n32 = m[ 9 ], n33 = m[ 10 ], n34 = m[ 11 ];
  391. const n41 = m[ 12 ], n42 = m[ 13 ], n43 = m[ 14 ], n44 = m[ 15 ];
  392. return (
  393. n41 * (
  394. + n14 * n23 * n32
  395. - n13 * n24 * n32
  396. - n14 * n22 * n33
  397. + n12 * n24 * n33
  398. + n13 * n22 * n34
  399. - n12 * n23 * n34
  400. ) +
  401. n42 * (
  402. + n11 * n23 * n34
  403. - n11 * n24 * n33
  404. + n14 * n21 * n33
  405. - n13 * n21 * n34
  406. + n13 * n24 * n31
  407. - n14 * n23 * n31
  408. ) +
  409. n43 * (
  410. + n11 * n24 * n32
  411. - n11 * n22 * n34
  412. - n14 * n21 * n32
  413. + n12 * n21 * n34
  414. + n14 * n22 * n31
  415. - n12 * n24 * n31
  416. ) +
  417. n44 * (
  418. - n13 * n22 * n31
  419. - n11 * n23 * n32
  420. + n11 * n22 * n33
  421. + n13 * n21 * n32
  422. - n12 * n21 * n33
  423. + n12 * n23 * n31
  424. )
  425. );
  426. },
  427. /**
  428. * 计算矩阵的逆
  429. * @param {*} m 将被计算逆的矩阵对象
  430. * @returns 矩阵的逆矩阵
  431. * */
  432. inverse: function(m) {
  433. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  434. const n11 = m[0], n21 = m[1], n31 = m[2], n41 = m[3],
  435. n12 = m[4], n22 = m[5], n32 = m[6], n42 = m[7],
  436. n13 = m[8], n23 = m[9], n33 = m[10], n43 = m[11],
  437. n14 = m[12], n24 = m[13], n34 = m[14], n44 = m[15],
  438. t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
  439. t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
  440. t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
  441. t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  442. const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
  443. if (det === 0) return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  444. const detInv = 1 / det;
  445. let invMat = [];
  446. invMat[0] = t11 * detInv;
  447. invMat[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
  448. invMat[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
  449. invMat[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
  450. invMat[4] = t12 * detInv;
  451. invMat[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
  452. invMat[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
  453. invMat[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
  454. invMat[8] = t13 * detInv;
  455. invMat[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
  456. invMat[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
  457. invMat[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
  458. invMat[12] = t14 * detInv;
  459. invMat[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
  460. invMat[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
  461. invMat[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
  462. return invMat;
  463. },
  464. /**
  465. * 构造平移矩阵
  466. * @param {[Number, Number, Number]} t 平移距离
  467. * @returns 4x4 的平移矩阵
  468. */
  469. makeTranslate: function(t)
  470. {
  471. var mat = this.identity();
  472. mat[12] = t[0];
  473. mat[13] = t[1];
  474. mat[14] = t[2];
  475. return mat;
  476. },
  477. /**
  478. * 指定旋转角和旋转轴计算一个旋转矩阵
  479. * @param {Number} angle 旋转角(角度制)
  480. * @param {[Number, Number, Number]} axis 旋转轴(单位向量)
  481. * @returns 4x4的旋转矩阵
  482. * */
  483. makeRotationAxis: function(angle, axis){
  484. const tAngne = angle * Math.PI / 180;
  485. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  486. const c = Math.cos(tAngne);
  487. const s = Math.sin(tAngne);
  488. const t = 1 - c;
  489. const x = axis[0], y = axis[1], z = axis[2];
  490. const tx = t * x, ty = t * y;
  491. let rMat = [];
  492. rMat[0] = tx * x + c; rMat[4] = tx * y - s * z; rMat[8] = tx * z + s * y; rMat[12] =0;
  493. rMat[1] = tx * y + s * z; rMat[5] = ty * y + c; rMat[9] = ty * z - s * x; rMat[13] =0;
  494. rMat[2] = tx * z - s * y; rMat[6] = ty * z + s * x; rMat[10] = t * z * z + c; rMat[14] =0;
  495. rMat[3] = 0; rMat[7] = 0; rMat[11] = 0; rMat[15] =1;
  496. return rMat;
  497. },
  498. }
  499. })();
  500. /**
  501. * 四元素
  502. * 注:这里四元数对应Player返回的四元数对象,Player返回的四元数的顺序是 [w,x,y,z]
  503. * 因此这里四元数所有算法也应该遵循Player中的 [w,x,y,z]顺序
  504. */
  505. let Quat = (function(){
  506. return {
  507. /**
  508. * 检查a是否是合法的四元数
  509. * @param {*} a 检查对象
  510. */
  511. check: function(a)
  512. {
  513. if (!Array.isArray(a))
  514. return false;
  515. for(let i = 0; i < 4; ++i)
  516. if (isNaN(a[i]))
  517. return false;
  518. if (Math.abs(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] - 1) > 0.01)
  519. return false;
  520. return true;
  521. },
  522. /**
  523. * 转换旋转矩阵到四元数
  524. * @param {*} m 旋转矩阵
  525. * @returns 旋转四元数
  526. */
  527. fromRotationMatrix: function( m)
  528. {
  529. m11 = m[ 0 ], m12 = m[ 4 ], m13 = m[ 8 ],
  530. m21 = m[ 1 ], m22 = m[ 5 ], m23 = m[ 9 ],
  531. m31 = m[ 2 ], m32 = m[ 6 ], m33 = m[ 10 ],
  532. trace = m11 + m22 + m33;
  533. q = [];
  534. if ( trace > 0 )
  535. {
  536. const s = 0.5 / Math.sqrt( trace + 1.0 );
  537. q[0] = 0.25 / s;
  538. q[1] = ( m32 - m23 ) * s;
  539. q[2] = ( m13 - m31 ) * s;
  540. q[3] = ( m21 - m12 ) * s;
  541. }
  542. else if ( m11 > m22 && m11 > m33 )
  543. {
  544. const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  545. q[0] = ( m32 - m23 ) / s;
  546. q[1] = 0.25 * s;
  547. q[2] = ( m12 + m21 ) / s;
  548. q[3] = ( m13 + m31 ) / s;
  549. }
  550. else if ( m22 > m33 )
  551. {
  552. const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  553. q[0] = ( m13 - m31 ) / s;
  554. q[1] = ( m12 + m21 ) / s;
  555. q[2] = 0.25 * s;
  556. q[3] = ( m23 + m32 ) / s;
  557. }
  558. else
  559. {
  560. const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  561. q[0] = ( m21 - m12 ) / s;
  562. q[1] = ( m13 + m31 ) / s;
  563. q[2] = ( m23 + m32 ) / s;
  564. q[3] = 0.25 * s;
  565. }
  566. return q;
  567. },
  568. /**
  569. * 转换四元数到旋转矩阵
  570. * @param m 旋转四元数
  571. * @returns 旋转矩阵
  572. */
  573. toRotationMatrix: function (q)
  574. {
  575. return Matrix4.compose([0, 0, 0], q, [1, 1, 1]);
  576. },
  577. }})();
  578. let EulerAngle = (function () {
  579. return {
  580. /**
  581. * 转换旋转矩阵到欧拉角
  582. * @param m 旋转矩阵(4x4)
  583. * @param order 欧拉角顺序, 取值分别为: "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"
  584. * @returns 欧拉角
  585. */
  586. fromRotationMatrix: function (m, order = 'XYZ') {
  587. var ret = []
  588. const te = m;
  589. const m11 = te[0], m12 = te[4], m13 = te[8];
  590. const m21 = te[1], m22 = te[5], m23 = te[9];
  591. const m31 = te[2], m32 = te[6], m33 = te[10];
  592. switch (order) {
  593. case 'XYZ':
  594. ret[1] = Math.asin(Math.clamp(m13, - 1, 1));
  595. if (Math.abs(m13) < 0.9999999) {
  596. ret[0] = Math.atan2(- m23, m33);
  597. ret[2] = Math.atan2(- m12, m11);
  598. } else {
  599. ret[0] = Math.atan2(m32, m22);
  600. ret[2] = 0;
  601. }
  602. break;
  603. case 'YXZ':
  604. ret[0] = Math.asin(- clamp(m23, - 1, 1));
  605. if (Math.abs(m23) < 0.9999999) {
  606. ret[1] = Math.atan2(m13, m33);
  607. ret[2] = Math.atan2(m21, m22);
  608. } else {
  609. ret[1] = Math.atan2(- m31, m11);
  610. ret[2] = 0;
  611. }
  612. break;
  613. case 'ZXY':
  614. ret[0] = Math.asin(clamp(m32, - 1, 1));
  615. if (Math.abs(m32) < 0.9999999) {
  616. ret[1] = Math.atan2(- m31, m33);
  617. ret[2] = Math.atan2(- m12, m22);
  618. } else {
  619. ret[1] = 0;
  620. ret[2] = Math.atan2(m21, m11);
  621. }
  622. break;
  623. case 'ZYX':
  624. ret[1] = Math.asin(- clamp(m31, - 1, 1));
  625. if (Math.abs(m31) < 0.9999999) {
  626. ret[0] = Math.atan2(m32, m33);
  627. ret[2] = Math.atan2(m21, m11);
  628. } else {
  629. ret[0] = 0;
  630. ret[2] = Math.atan2(- m12, m22);
  631. }
  632. break;
  633. case 'YZX':
  634. ret[2] = Math.asin(clamp(m21, - 1, 1));
  635. if (Math.abs(m21) < 0.9999999) {
  636. ret[0] = Math.atan2(- m23, m22);
  637. ret[1] = Math.atan2(- m31, m11);
  638. } else {
  639. ret[0] = 0;
  640. ret[1] = Math.atan2(m13, m33);
  641. }
  642. break;
  643. case 'XZY':
  644. ret[2] = Math.asin(- clamp(m12, - 1, 1));
  645. if (Math.abs(m12) < 0.9999999) {
  646. ret[0] = Math.atan2(m32, m22);
  647. ret[1] = Math.atan2(m13, m11);
  648. } else {
  649. ret[0] = Math.atan2(- m23, m33);
  650. ret[1] = 0;
  651. }
  652. break;
  653. default:
  654. console.warn(' unknown order: ' + order);
  655. }
  656. //弧度制转角度制
  657. ret[0] = ret[0] * 180 / Math.PI;
  658. ret[1] = ret[1] * 180 / Math.PI;
  659. ret[2] = ret[2] * 180 / Math.PI;
  660. return ret;
  661. },
  662. /**
  663. * 转换欧拉角到旋转矩阵
  664. * @param euler 欧拉角
  665. * @param order 欧拉角顺序, 取值分别为: "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"
  666. * @returns 4x4旋转矩阵
  667. */
  668. toRotationMatrix: function (euler, order = 'XYZ'){
  669. var ret = [];
  670. const x = euler[0] * Math.PI / 180;
  671. const y = euler[1] * Math.PI / 180;
  672. const z = euler[2] * Math.PI / 180;
  673. const a = Math.cos(x);
  674. const b = Math.sin(x);
  675. const c = Math.cos(y);
  676. const d = Math.sin(y);
  677. const e = Math.cos(z);
  678. const f = Math.sin(z);
  679. if (order === 'XYZ') {
  680. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  681. ret[0] = c * e;
  682. ret[4] = - c * f;
  683. ret[8] = d;
  684. ret[1] = af + be * d;
  685. ret[5] = ae - bf * d;
  686. ret[9] = - b * c;
  687. ret[2] = bf - ae * d;
  688. ret[6] = be + af * d;
  689. ret[10] = a * c;
  690. } else if (order === 'YXZ') {
  691. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  692. ret[0] = ce + df * b;
  693. ret[4] = de * b - cf;
  694. ret[8] = a * d;
  695. ret[1] = a * f;
  696. ret[5] = a * e;
  697. ret[9] = - b;
  698. ret[2] = cf * b - de;
  699. ret[6] = df + ce * b;
  700. ret[10] = a * c;
  701. } else if (order === 'ZXY') {
  702. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  703. ret[0] = ce - df * b;
  704. ret[4] = - a * f;
  705. ret[8] = de + cf * b;
  706. ret[1] = cf + de * b;
  707. ret[5] = a * e;
  708. ret[9] = df - ce * b;
  709. ret[2] = - a * d;
  710. ret[6] = b;
  711. ret[10] = a * c;
  712. } else if (order === 'ZYX') {
  713. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  714. ret[0] = c * e;
  715. ret[4] = be * d - af;
  716. ret[8] = ae * d + bf;
  717. ret[1] = c * f;
  718. ret[5] = bf * d + ae;
  719. ret[9] = af * d - be;
  720. ret[2] = - d;
  721. ret[6] = b * c;
  722. ret[10] = a * c;
  723. } else if (order === 'YZX') {
  724. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  725. ret[0] = c * e;
  726. ret[4] = bd - ac * f;
  727. ret[8] = bc * f + ad;
  728. ret[1] = f;
  729. ret[5] = a * e;
  730. ret[9] = - b * e;
  731. ret[2] = - d * e;
  732. ret[6] = ad * f + bc;
  733. ret[10] = ac - bd * f;
  734. } else if (order === 'XZY') {
  735. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  736. ret[0] = c * e;
  737. ret[4] = - f;
  738. ret[8] = d * e;
  739. ret[1] = ac * f + bd;
  740. ret[5] = a * e;
  741. ret[9] = ad * f - bc;
  742. ret[2] = bc * f - ad;
  743. ret[6] = b * e;
  744. ret[10] = bd * f + ac;
  745. }
  746. // bottom row
  747. ret[3] = 0;
  748. ret[7] = 0;
  749. ret[11] = 0;
  750. // last column
  751. ret[12] = 0;
  752. ret[13] = 0;
  753. ret[14] = 0;
  754. ret[15] = 1;
  755. return ret;
  756. },
  757. /**
  758. * 转换旋转四元数到欧拉角
  759. * @param q 旋转四元数(wxyz)
  760. * @param order 欧拉角顺序, 取值分别为: "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"
  761. * @returns 旋转四元数
  762. */
  763. fromQuat: function (q, order = 'XYZ') {
  764. return EulerAngle.fromRotationMatrix(Quat.toRotationMatrix(q), order);
  765. },
  766. /**
  767. * 转换欧拉角到旋转四元数
  768. * @param euler 欧拉角
  769. * @param order 欧拉角顺序, 取值分别为: "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"
  770. * @returns 旋转四元数(wxyz)
  771. */
  772. toQuat: function (euler, order = 'XYZ') {
  773. var q = [];
  774. const x = euler[0] * Math.PI / 180;
  775. const y = euler[1] * Math.PI / 180;
  776. const z = euler[2] * Math.PI / 180;
  777. const cos = Math.cos;
  778. const sin = Math.sin;
  779. const c1 = cos(x / 2);
  780. const c2 = cos(y / 2);
  781. const c3 = cos(z / 2);
  782. const s1 = sin(x / 2);
  783. const s2 = sin(y / 2);
  784. const s3 = sin(z / 2);
  785. switch (order) {
  786. case 'XYZ':
  787. q[1] = s1 * c2 * c3 + c1 * s2 * s3;
  788. q[2] = c1 * s2 * c3 - s1 * c2 * s3;
  789. q[3] = c1 * c2 * s3 + s1 * s2 * c3;
  790. q[0] = c1 * c2 * c3 - s1 * s2 * s3;
  791. break;
  792. case 'YXZ':
  793. q[1] = s1 * c2 * c3 + c1 * s2 * s3;
  794. q[2] = c1 * s2 * c3 - s1 * c2 * s3;
  795. q[3] = c1 * c2 * s3 - s1 * s2 * c3;
  796. q[0] = c1 * c2 * c3 + s1 * s2 * s3;
  797. break;
  798. case 'ZXY':
  799. q[1] = s1 * c2 * c3 - c1 * s2 * s3;
  800. q[2] = c1 * s2 * c3 + s1 * c2 * s3;
  801. q[3] = c1 * c2 * s3 + s1 * s2 * c3;
  802. q[0] = c1 * c2 * c3 - s1 * s2 * s3;
  803. break;
  804. case 'ZYX':
  805. q[1] = s1 * c2 * c3 - c1 * s2 * s3;
  806. q[2] = c1 * s2 * c3 + s1 * c2 * s3;
  807. q[3] = c1 * c2 * s3 - s1 * s2 * c3;
  808. q[0] = c1 * c2 * c3 + s1 * s2 * s3;
  809. break;
  810. case 'YZX':
  811. q[1] = s1 * c2 * c3 + c1 * s2 * s3;
  812. q[2] = c1 * s2 * c3 + s1 * c2 * s3;
  813. q[3] = c1 * c2 * s3 - s1 * s2 * c3;
  814. q[0] = c1 * c2 * c3 - s1 * s2 * s3;
  815. break;
  816. case 'XZY':
  817. q[1] = s1 * c2 * c3 - c1 * s2 * s3;
  818. q[2] = c1 * s2 * c3 - s1 * c2 * s3;
  819. q[3] = c1 * c2 * s3 + s1 * s2 * c3;
  820. q[0] = c1 * c2 * c3 + s1 * s2 * s3;
  821. break;
  822. default:
  823. console.warn('unknown order: ' + order);
  824. }
  825. return q;
  826. }
  827. };
  828. })();
  829. /**
  830. * 包围盒
  831. */
  832. let Aabb = (function () {
  833. return {
  834. /**
  835. * 检查a是否是合法的包围盒
  836. * @param {*} a 检查对象
  837. */
  838. check: function (a) {
  839. if (!Array.isArray(a))
  840. return false;
  841. if (isNaN(a.min) || isNaN(a.max))
  842. return false;
  843. for (let i = 0; i < 3; ++i)
  844. if (isNaN(a.min[i]) || isNaN(a.max[i]))
  845. return false;
  846. return true;
  847. },
  848. /**
  849. * map(min、max)解成二维数组
  850. * @param {[{"min", [Number, Number, Number]}, {"max", [Number, Number, Number]}]} a map a
  851. */
  852. deMap: function (a) {
  853. return [a.min, a.max];
  854. },
  855. /**
  856. * 获取包围盒8个顶点
  857. * @param a [ [Number, Number, Number],[Number, Number, Number]] a aabb
  858. */
  859. vertex8: function (a) {
  860. return [
  861. [a[0][0], a[0][1], a[0][2]]
  862. , [a[0][0], a[1][1], a[0][2]]
  863. , [a[1][0], a[1][1], a[0][2]]
  864. , [a[1][0], a[0][1], a[0][2]]
  865. , [a[1][0], a[1][1], a[1][2]]
  866. , [a[0][0], a[1][1], a[1][2]]
  867. , [a[0][0], a[0][1], a[1][2]]
  868. , [a[1][0], a[0][1], a[1][2]]];
  869. },
  870. /**
  871. * 获取包围盒某个面
  872. * @param a [ [Number, Number, Number],[Number, Number, Number]] a aabb
  873. * @param faceName 面名称: X,XN,Y,YN,Z,ZN
  874. */
  875. face:function(a, faceName = 'Z')
  876. {
  877. let fIdx = [];
  878. switch (faceName)
  879. {
  880. case 'X':
  881. {
  882. fIdx = [ 3, 2, 4, 7 ]; //X
  883. }break;
  884. case 'XN':
  885. {
  886. fIdx = [1, 0, 6, 5 ]; //XN
  887. }break;
  888. case 'Y':
  889. {
  890. fIdx = [2, 1, 5, 4 ]; //Y
  891. }break;
  892. case 'YN':
  893. {
  894. fIdx = [0, 3, 7, 6 ]; //YN
  895. }break;
  896. case 'Z':
  897. {
  898. fIdx = [6, 7, 4, 5 ]; //Z
  899. }break;
  900. case 'ZN':
  901. {
  902. fIdx = [1, 2, 3, 0 ]; //ZN
  903. }break;
  904. default:
  905. {
  906. console.warn('unknown face: ' + faceName);
  907. return [];
  908. }break;
  909. }
  910. const v8 = Aabb.vertex8(a)
  911. return [v8[fIdx[0]], v8[fIdx[1]], v8[fIdx[2]], v8[fIdx[3]]]
  912. },
  913. /**
  914. * 获取包围盒某个面中心点
  915. * @param a [ [Number, Number, Number],[Number, Number, Number]] a aabb
  916. * @param faceName 面名称: X,XN,Y,YN,Z,ZN
  917. */
  918. faceCenter:function(a, faceName = 'Z')
  919. {
  920. const face = Aabb.face(a, faceName);
  921. const a0 = Vec3.add(face[0], face[1]);
  922. const a1 = Vec3.add(a0, face[2]);
  923. let a2 = Vec3.add(a1, face[3]);
  924. a2[0] /= 4;
  925. a2[1] /= 4;
  926. a2[2] /= 4;
  927. return a2;
  928. },
  929. /**
  930. * 获取包围盒中心点
  931. * @param [ [Number, Number, Number],[Number, Number, Number]] a aabb
  932. */
  933. center: function(a)
  934. {
  935. let cen = Vec3.add(a[0], a[1])
  936. cen = Vec3.multiplyScalar(cen, 0.5)
  937. return cen
  938. }
  939. }
  940. })();
  941. /**
  942. * 射线 [[Number,Number,Number],[Number,Number,Number]] 第一个数据表示射线端点,第二个数据表示射线防线
  943. */
  944. let Ray = (function () {
  945. return {
  946. /**
  947. * @brief 根据参数 t(距离) 获取射线上的点
  948. * 当 t = 0 时, 结果点为射线原点
  949. * 当 t > 0 时, 结果点在射线上
  950. * 当 t < 0 是, 结果点在射线的背面
  951. */
  952. at: function(ray, t)
  953. {
  954. let pt = Vec3.add(ray[0], Vec3.multiplyScalar(ray[1], t))
  955. return pt;
  956. },
  957. /**
  958. * @brief 射线与平面相交
  959. * @param ray 射线对象
  960. * @param planePosition 平面上的任意一点
  961. * @param planeNormal 平面发向量
  962. */
  963. intersectPlane: function(ray, planePosition, planeNormal)
  964. {
  965. let denominator = Vec3.dot(planeNormal, ray[1])
  966. if (denominator === 0.0)
  967. {
  968. let dt = Vec3.dot(planeNormal, ray[0])
  969. if (dt === 0.0)
  970. {
  971. return { bIntersect: true, rayParamT: 0 };
  972. }
  973. return { bIntersect: false, rayParamT: -1 };
  974. }
  975. let t = Vec3.dot(planeNormal, Vec3.sub(planePosition, ray[0])) / denominator;
  976. if (t >= 0)
  977. {
  978. return { bIntersect: true, rayParamT: t };
  979. }
  980. return { bIntersect: false, rayParamT: -1 };
  981. },
  982. }
  983. })();
  984. /**
  985. * Player常用函数集合
  986. */
  987. let PlayerUtils = (function() {
  988. return {
  989. /**
  990. * 使用Promise封装异步接口调用
  991. * @param {Function} fun 要调用的函数
  992. * @param {...any} args 参数
  993. * @example
  994. * var info = await PlayerUtils.call(player.Model.getInfo, 'sampler.pr') //使用await
  995. * PlayerUtils.call(player.Model.getInfo, 'sampler.pr').then((info)=>{ console.log(info) }); //使用then
  996. */
  997. call: function(fun, ...args) {
  998. return new Promise((resolve)=>{
  999. fun(...args, (ret)=>{
  1000. resolve(ret);
  1001. });
  1002. });
  1003. },
  1004. /**
  1005. * 计算包围盒的视口信息
  1006. * @param {{min:[Number,Number,Number],max:[Number,Number,Number]}} aabb 目标包围盒
  1007. * @param {[Number,Number,Number]} eyeDir 摄像机方向
  1008. * @param {[Number,Number,Number]} upDir 摄像机上方向,不能与eyeDir重合
  1009. * @param {Number} lenScalar 摄像机距离系数,越大越远
  1010. */
  1011. calAabbViewParam: function(aabb, eyeDir, upDir, lenScalar) {
  1012. var target = Vec3.divideScalar(Vec3.add(aabb.min, aabb.max), 2);
  1013. var dir = Vec3.normalize(eyeDir);
  1014. var len = Vec3.length(Vec3.sub(aabb.max, aabb.min)) * lenScalar;
  1015. var eye = Vec3.add(Vec3.multiplyScalar(dir, len), target);
  1016. var up = Vec3.normalize(Vec3.cross(Vec3.cross(dir, upDir), dir));
  1017. return {
  1018. eye: eye,
  1019. target: target,
  1020. up: up
  1021. }
  1022. },
  1023. /**
  1024. * 移动相机看向模型,适用于模型所在高程瓦片已加载完成或禁用地形跟随
  1025. * @param {RemotePlayer} player Player对象
  1026. * @param {String} modelId 模型ID
  1027. * @param {[Number,Number,Number]|Function} eyeDir 摄像机方向,基于模型局部坐标系,或计算相机方向的函数
  1028. * @param {[Number,Number,Number]} upDir 摄像机上方向,不能与eyeDir重合,基于模型局部坐标系
  1029. * @param {Number} lenScalar 摄像机距离系数,越大越远
  1030. * @param {Number} second 摄像机过渡时间
  1031. * @example
  1032. * PlayerUtils.moveToModel(player, "data://models/sampler.pr", [1,0,0], [0,0,1], 2); //[1,0,0]=>从右看向左边
  1033. */
  1034. moveToModel: async function (player, modelId, eyeDir, upDir, lenScalar = 1, second = 1) {
  1035. var info = await PlayerUtils.call(player.Native.Model.getInfo, modelId);
  1036. var mat = await PlayerUtils.call(player.Native.Model.getModelMatrix, modelId);
  1037. if (typeof(eyeDir) == "function") eyeDir = eyeDir(info.srcAabb);
  1038. var param = PlayerUtils.calAabbViewParam(info.srcAabb, eyeDir, upDir, lenScalar);
  1039. param.eye = Matrix4.multiplyPos(mat, param.eye);
  1040. param.target = Matrix4.multiplyPos(mat, param.target);
  1041. param.up = Matrix4.multiplyDir(mat, param.up);
  1042. await PlayerUtils.call(player.Native.Camera.moveTo, param.eye, param.target, param.up, second);
  1043. },
  1044. /**
  1045. * 移动相机看向包围盒
  1046. * @param {RemotePlayer} player Player对象
  1047. * @param {{min:[Number,Number,Number], max:[Number,Number,Number]}} aabb 模型ID
  1048. * @param {[...Number]} mat 包围盒需要运用的矩阵
  1049. * @param {[Number,Number,Number]} eyeDir 摄像机方向,基于模型局部坐标系
  1050. * @param {[Number,Number,Number]} upDir 摄像机上方向,不能与eyeDir重合,基于模型局部坐标系
  1051. * @param {Number} lenScalar 摄像机距离系数,越大越远
  1052. * @param {Number} second 摄像机过渡时间
  1053. * @example
  1054. * PlayerUtils.moveToAabb(player, {min:[-1,-1,-1],max:[1,1,1]}, [1,0,0], [0,0,1], 2); //[1,0,0]=>从右看向左边
  1055. */
  1056. moveToAabb: async function (player, aabb, mat, eyeDir, upDir, lenScalar = 1, second = 1) {
  1057. var param = PlayerUtils.calAabbViewParam(aabb, eyeDir, upDir, lenScalar);
  1058. param.eye = Matrix4.multiplyPos(mat, param.eye);
  1059. param.target = Matrix4.multiplyPos(mat, param.target);
  1060. param.up = Matrix4.multiplyDir(mat, param.up);
  1061. await PlayerUtils.call(player.Native.Camera.moveTo, param.eye, param.target, param.up, second);
  1062. },
  1063. /**
  1064. * 添加移动相机看向包围盒任务
  1065. * @param {RemotePlayer} player Player对象
  1066. * @param {{min:[Number,Number,Number], max:[Number,Number,Number]}} aabb 模型ID
  1067. * @param {[...Number]} mat 包围盒需要运用的矩阵
  1068. * @param {[Number,Number,Number]} eyeDir 摄像机方向,基于模型局部坐标系
  1069. * @param {[Number,Number,Number]} upDir 摄像机上方向,不能与eyeDir重合,基于模型局部坐标系
  1070. * @param {Number} lenScalar 摄像机距离系数,越大越远
  1071. * @param {Number} second 摄像机过渡时间
  1072. * @example
  1073. * PlayerUtils.addMoveToAabb(player, {min:[-1,-1,-1],max:[1,1,1]}, [1,0,0], [0,0,1], 2); //[1,0,0]=>从右看向左边
  1074. */
  1075. addMoveToAabb: async function (player, aabb, mat, eyeDir, upDir, lenScalar = 1, second = 1) {
  1076. var param = PlayerUtils.calAabbViewParam(aabb, eyeDir, upDir, lenScalar);
  1077. param.eye = Matrix4.multiplyPos(mat, param.eye);
  1078. param.target = Matrix4.multiplyPos(mat, param.target);
  1079. param.up = Matrix4.multiplyDir(mat, param.up);
  1080. await PlayerUtils.call(player.Native.Camera.addMoveTo, param.eye, param.target, param.up, second);
  1081. }
  1082. }
  1083. })();