util.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import Codes from "./enum/Codes.js"
  2. import CodeErrorMap from "./error/CodeErrorMap.js"
  3. import InternalError from "./error/InternalError.js"
  4. var util = {
  5. uuid() {
  6. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, i=>{
  7. const e = Math.random() * 16 | 0;
  8. return (i === "x" ? e : e & 3 | 8).toString(16)
  9. }
  10. )
  11. },
  12. getFormattedDate(i) {
  13. const e = i.getMonth() + 1
  14. , t = i.getDate()
  15. , r = i.getHours()
  16. , n = i.getMinutes()
  17. , o = i.getSeconds()
  18. , a = i.getMilliseconds()
  19. , s = (e < 10 ? "0" : "") + e
  20. , l = (t < 10 ? "0" : "") + t
  21. , u = (r < 10 ? "0" : "") + r
  22. , c = (n < 10 ? "0" : "") + n
  23. , h = (o < 10 ? "0" : "") + o;
  24. return i.getFullYear() + "-" + s + "-" + l + " " + u + ":" + c + ":" + h + "." + a
  25. },
  26. createInstance(i) {
  27. var e = new Axios(i)
  28. , t = bind(Axios.prototype.request, e);
  29. return utils.extend(t, Axios.prototype, e),
  30. utils.extend(t, e),
  31. t.create = function(n) {
  32. return createInstance(mergeConfig(i, n))
  33. }
  34. ,
  35. t
  36. },
  37. mapLimit(i, e, t) {
  38. return new Promise((r,n)=>{
  39. const o = i.length;
  40. let a = e - 1
  41. , s = 0;
  42. const l = u=>{
  43. u.forEach(c=>{
  44. t(c).then(()=>{
  45. if (s++,
  46. s === o) {
  47. r();
  48. return
  49. }
  50. a++;
  51. const h = i[a];
  52. h && l([h])
  53. }
  54. , h=>{
  55. n(h)
  56. }
  57. )
  58. }
  59. )
  60. }
  61. ;
  62. l(i.slice(0, e))
  63. }
  64. )
  65. },
  66. isWebAssemblySupported(){
  67. try {
  68. if (typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function") {
  69. const i = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
  70. if (i instanceof WebAssembly.Module)
  71. return new WebAssembly.Instance(i)instanceof WebAssembly.Instance
  72. }
  73. } catch {}
  74. return console.log("wasm is not supported"),
  75. !1
  76. },
  77. isSupported() {
  78. return typeof RTCPeerConnection == "function" && this.isWebAssemblySupported()
  79. },
  80. objectParseFloat(i){
  81. const e = {};
  82. return i && Object.keys(i).forEach(t=>{
  83. e[t] = parseFloat(i[t])
  84. }
  85. ),
  86. e
  87. },
  88. getRandomItem(i){
  89. if(i.length === 0){
  90. return null
  91. }
  92. else{
  93. return i[Math.floor(Math.random() * i.length)]
  94. }
  95. //i.length === 0 ? null : i[Math.floor(Math.random() * i.length)]
  96. },
  97. getErrorByCode(i) {
  98. if (i === Codes.Success)
  99. return InternalError;
  100. const e = CodeErrorMap[i];
  101. return e || console.warn("unkown code", i),
  102. e || InternalError
  103. },
  104. getDistance(i, e) {
  105. const {x: t, y: r, z: n} = i
  106. , {x: o, y: a, z: s} = e;
  107. return Math.sqrt(Math.abs(t - o) ** 2 + Math.abs(r - a) ** 2 + Math.abs(n - s) ** 2)
  108. }
  109. ,
  110. //pitch,yaw,roll转rotation
  111. ue4Rotation2Xverse(i){
  112. if(this.isRotationCorrect(i)){
  113. if(i.pitch >= 89.5){
  114. i.pitch = 89.5
  115. }
  116. else if(i.pitch <= -89.5){
  117. i.pitch = -89.5
  118. }
  119. if(Math.PI * i.roll / 180 < .001){
  120. return new BABYLON.Vector3(-1 * Math.PI * i.pitch / 180,Math.PI * i.yaw / 180 - Math.PI * 27 / 18, 0 )
  121. }
  122. else{
  123. return new BABYLON.Vector3(-1 * Math.PI * i.pitch / 180,Math.PI * i.yaw / 180 - Math.PI * 27 / 18, Math.PI * i.roll / 180 )
  124. }
  125. }
  126. else{
  127. return null
  128. }
  129. },
  130. ue4Rotation2Xverse_mesh(i){
  131. if(this.isRotationCorrect(i)){
  132. if(Math.abs(Math.PI * i.roll) / 180 < .001){
  133. return new BABYLON.Vector3(Math.PI * i.pitch / 180,Math.PI * i.yaw / 180,0)
  134. }
  135. else{
  136. return new BABYLON.Vector3(Math.PI * i.pitch / 180,Math.PI * i.yaw / 180, -1 * (Math.PI * i.roll) / 180)
  137. }
  138. }
  139. else{
  140. return null;
  141. }
  142. },
  143. ue4Scaling2Xverse(i){
  144. if(this.isScalingCorrect(i)){
  145. return new BABYLON.Vector3(i.x,i.z,-1 * i.y)
  146. }
  147. else{
  148. return null;
  149. }
  150. },
  151. ue4Position2Xverse(i){
  152. if(this.isPositionCorrect(i)){
  153. return new BABYLON.Vector3(i.x * .01,i.z * .01,-1 * i.y * .01)
  154. }
  155. else{
  156. return null;
  157. }
  158. },
  159. //rotation转成pitch,yaw,roll
  160. xversePosition2Ue4(i){
  161. if(this.isPositionCorrect(i)){
  162. return {
  163. x: i.x * 100,
  164. y: -1 * i.z * 100,
  165. z: i.y * 100
  166. }
  167. }
  168. else{
  169. return null;
  170. }
  171. },
  172. xverseRotation2Ue4(i){
  173. if (this.isPositionCorrect(i)) {
  174. let e = 0;
  175. i.z == 0 ? e = 0 : e = 180 * i.z / Math.PI;
  176. return {
  177. pitch: 180 * i.x * -1 / Math.PI,
  178. yaw: (i.y + Math.PI * 27 / 18) * 180 / Math.PI,
  179. roll: e
  180. }
  181. }
  182. else
  183. {
  184. return null
  185. }
  186. },
  187. //isRotationCorrect = i=>!0
  188. isRotationCorrect(i){
  189. if(i){
  190. return true
  191. }
  192. else{
  193. return false
  194. }
  195. },
  196. isPositionCorrect(i){
  197. if(i){
  198. return true
  199. }
  200. else{
  201. return false
  202. }
  203. },
  204. isScalingCorrect(i){
  205. if(i){
  206. return true
  207. }
  208. else{
  209. return false
  210. }
  211. }
  212. }
  213. export default util