Common.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. var Common = {
  3. sortByScore: function(list, request, rank){
  4. var i = request ? Common.filterAll(list, request) : list
  5. return 0 === i.length ? null : i = i.map(function(e) {
  6. return {
  7. item: e,
  8. score: rank.reduce(function(t, i) {
  9. return t + i(e)
  10. }, 0)
  11. }
  12. }).sort(function(e, t) {
  13. return t.score - e.score;
  14. })
  15. }
  16. ,
  17. filterAll: function(e, t) {
  18. return e.filter(function (e) {
  19. return t.every(function (t) {
  20. return t(e)
  21. })
  22. })
  23. },
  24. //---------------
  25. find : function(list, request, rank, sortByScore ) {
  26. if(sortByScore){
  27. var r = this.sortByScore(list, request, rank)
  28. return r && r[0] && r[0].item
  29. }else{
  30. var i = request ? Common.filterAll(list, request) : list
  31. return 0 === i.length ? null : (rank && rank.forEach(function(e) {
  32. i = Common.stableSort(i, e)
  33. }),
  34. i[0])
  35. }
  36. }
  37. ,
  38. stableSort: function(e, f) {//用到排序函数,涉及到两个item相减
  39. return e.map(function(e, i) {
  40. return {
  41. value: e,
  42. index: i
  43. }
  44. }).sort(function(e, u) {
  45. var n = f(e.value, u.value);
  46. return 0 !== n ? n : e.index - u.index //似乎就是加多了这一步:若差距为0,按照原顺序
  47. }).map(function(e) {
  48. return e.value
  49. })
  50. },
  51. average: function (e, t) {
  52. if (0 === e.length)
  53. return null;
  54. for (var i = 0, n = 0, r = 0; r < e.length; r++) {
  55. var o = t ? e[r][t] : e[r];
  56. i += o,
  57. n++
  58. }
  59. return i / n
  60. },
  61. //---------------------------
  62. getMixedSet : function(arr1, arr2){//交集
  63. return arr1.filter(item=>arr2.includes(item));
  64. },
  65. getUnionSet : function(arr1, arr2){//并集
  66. return arr1.concat(arr2.filter(item=>!arr1.includes(item)))
  67. },
  68. getDifferenceSet : function(arr1, arr2){//差集 不能识别重复的,如getDifferenceSet([1,2,2],[1,1,2]) 为空
  69. var arr11 = arr1.filter(item=>!arr2.includes(item));
  70. var arr22 = arr2.filter(item=>!arr1.includes(item));
  71. return arr11.concat(arr22)
  72. },
  73. getDifferenceSetMuti : function(arr){//收集绝对没有重复的元素,也就是判断出现次数=1的
  74. var set = [];
  75. arr.forEach(arr1=>{
  76. arr1.forEach(item=>{
  77. var index = set.indexOf(item)
  78. if(index>-1){
  79. set.splice(index, 1)
  80. }else{
  81. set.push(item)
  82. }
  83. })
  84. })
  85. return set;
  86. }
  87. ,
  88. CloneJson : function(data){
  89. var str = JSON.stringify(data)
  90. return JSON.parse(str)
  91. }
  92. ,
  93. CloneObject : function(copyObj, result, isSimpleCopy, simpleCopyList=[]) {
  94. //isSimpleCopy 只复制最外层
  95. //复制json result的可能:普通数字或字符串、普通数组、复杂对象
  96. simpleCopyList.push(THREE.Object3D) //遇到simpleCopyList中的类直接使用不拷贝
  97. if(!copyObj || typeof copyObj == 'number' || typeof copyObj == 'string' || copyObj instanceof Function || simpleCopyList.some(className => copyObj instanceof className)){
  98. return copyObj
  99. }
  100. result = result || {};
  101. if (copyObj instanceof Array) {
  102. return copyObj.map(e=>{
  103. return this.CloneObject(e)
  104. })
  105. }else{
  106. if(copyObj.clone instanceof Function ){ //解决一部分
  107. return copyObj.clone()
  108. }
  109. }
  110. for (var key in copyObj) {
  111. if (copyObj[key] instanceof Object && !isSimpleCopy)
  112. result[key] = this.CloneObject(copyObj[key]);
  113. else
  114. result[key] = copyObj[key];
  115. //如果是函数类同基本数据,即复制引用
  116. }
  117. return result;
  118. }
  119. ,
  120. CloneClassObject :function(copyObj ){//复杂类对象
  121. var newobj = new copyObj.constructor();
  122. this.CopyClassObject(newobj, copyObj)
  123. return newobj
  124. }
  125. ,
  126. CopyClassObject :function(targetObj, copyObj){//复杂类对象
  127. for(let i in copyObj){
  128. if(i in copyObj.__proto__)break; //到函数了跳出
  129. targetObj[i] = this.CloneObject(copyObj[i], null )
  130. /* else if(copyObj[i].clone instanceof Function ){
  131. targetObj[i] = copyObj[i].clone()
  132. }else{
  133. targetObj[i] = copyObj[i];
  134. } */
  135. }
  136. }
  137. ,
  138. ifSame : function(object1, object2){
  139. if(object1 == object2 )return true // 0 != undefined , 0 == ''
  140. else if(!object1 || !object2) return false
  141. else if(object1.constructor != object2.constructor){
  142. return false
  143. }else if(object1 instanceof Array ) {
  144. if(object1.length != object2.length)return false;
  145. var _object2 = object2.slice(0);
  146. for(let i=0;i<object1.length;i++){
  147. var u = _object2.find(e=>ifSame(object1[i], e));
  148. if(u == void 0 && !_object2.includes(u) && !object1.includes(u))return false;
  149. else{
  150. let index = _object2.indexOf(u);
  151. _object2.splice(index,1);
  152. }
  153. }
  154. return true
  155. }else if(object1.equals instanceof Function ){//复杂数据仅支持这种,其他的可能卡住?
  156. return object1.equals(object2)
  157. }else if(typeof object1 == 'number' || typeof object1 == 'string'){
  158. if(isNaN(object1) && isNaN(object2))return true
  159. else return object1 == object2
  160. }else if(typeof object1 == "object"){
  161. var keys1 = Object.keys(object1)
  162. var keys2 = Object.keys(object2)
  163. if(!ifSame(keys1,keys2))return false;
  164. for(let i in object1){
  165. var same = ifSame(object1[i], object2[i]);
  166. if(!same)return false
  167. }
  168. return true
  169. }else{
  170. console.log('isSame出现例外')
  171. }
  172. }
  173. ,
  174. replaceAll : function (str, f, e) {
  175. //f全部替换成e
  176. var reg = new RegExp(f, "g"); //创建正则RegExp对象
  177. return str.replace(reg, e);
  178. }
  179. ,
  180. downloadFile : function(data, filename, cb) {
  181. var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
  182. save_link.href = data;
  183. save_link.download = filename;
  184. var event = document.createEvent('MouseEvents');
  185. event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  186. save_link.dispatchEvent(event);
  187. cb && cb();
  188. },
  189. intervalTool:{ //延时update,防止卡顿
  190. list:[],
  191. isWaiting:function(name, func, delayTime){
  192. if(!this.list.includes(name)){ //如果没有该项, 则开始判断
  193. var needWait = func(); //触发了改变,则等待一段时间后再自动判断
  194. if(needWait){
  195. this.list.push(name);
  196. setTimeout(()=>{
  197. var a = this.list.indexOf(name);
  198. this.list.splice(a,1);
  199. this.isWaiting(name, func, delayTime) //循环
  200. },delayTime)
  201. }
  202. }
  203. },
  204. /* wait:function(name, delayTime){
  205. this.list.push(name);
  206. setTimeout(()=>{
  207. },delayTime)
  208. }, */
  209. }
  210. ,
  211. pushToGroupAuto : function(items, groups, recognizeFunction){//自动分组。 items是将分到一起的组合。items.length = 1 or 2.
  212. recognizeFunction = recognizeFunction || function(){}
  213. var atGroups = groups.filter(group=>group.find(
  214. item => items[0] == item || recognizeFunction(item, items[0]) || items[1] == item || items[1] && recognizeFunction(item, items[1])
  215. ))
  216. if(atGroups.length){//在不同组
  217. //因为items是一组的,所以先都放入组1
  218. items.forEach(item=> {if(!atGroups[0].includes(item)) atGroups[0].push(item);})
  219. if(atGroups.length>1){//如果在不同组,说明这两个组需要合并
  220. var combineGroup = []
  221. atGroups.forEach(group=>{
  222. combineGroup = Common.getUnionSet(combineGroup, group)
  223. groups.splice(groups.indexOf(group),1)
  224. })
  225. groups.push(combineGroup)
  226. }
  227. }else{//直接加入为一组
  228. groups.push(items)
  229. }
  230. },
  231. addOrRemoveDefine(material, defineName, type, value=''){
  232. let defines = material.defines
  233. if(type == 'add'){
  234. if(defines[defineName] != void 0 && defines[defineName] == value)return
  235. defines[defineName] = value
  236. }else{
  237. if(defines[defineName] != void 0)return;
  238. delete defines[defineName]
  239. }
  240. material.needsUpdate = true;
  241. }
  242. }
  243. export default Common