123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- import * as THREE from "../../libs/three.js/build/three.module.js";
- var Common = {
- sortByScore: function(list, request, rank){
- var i = request ? Common.filterAll(list, request) : list
- return 0 === i.length ? null : i = i.map(function(e) {
- return {
- item: e,
- score: rank.reduce(function(t, i) {
- return t + i(e)
- }, 0)
- }
- }).sort(function(e, t) {
- return t.score - e.score;
- })
- }
- ,
-
- filterAll: function(e, t) {
- return e.filter(function (e) {
- return t.every(function (t) {
- return t(e)
- })
- })
- },
-
-
-
- //---------------
- find : function(list, request, rank, sortByScore ) {
- if(sortByScore){
- var r = this.sortByScore(list, request, rank)
- return r && r[0] && r[0].item
- }else{
- var i = request ? Common.filterAll(list, request) : list
- return 0 === i.length ? null : (rank && rank.forEach(function(e) {
- i = Common.stableSort(i, e)
- }),
- i[0])
- }
-
- }
-
- ,
- stableSort: function(e, f) {//用到排序函数,涉及到两个item相减
- return e.map(function(e, i) {
- return {
- value: e,
- index: i
- }
- }).sort(function(e, u) {
- var n = f(e.value, u.value);
- return 0 !== n ? n : e.index - u.index //似乎就是加多了这一步:若差距为0,按照原顺序
- }).map(function(e) {
- return e.value
- })
- },
-
- average: function (e, t) {
- if (0 === e.length)
- return null;
- for (var i = 0, n = 0, r = 0; r < e.length; r++) {
- var o = t ? e[r][t] : e[r];
- i += o,
- n++
- }
- return i / n
- },
-
-
- //---------------------------
-
-
- getMixedSet : function(arr1, arr2){//交集
- return arr1.filter(item=>arr2.includes(item));
- },
- getUnionSet : function(arr1, arr2){//并集
- return arr1.concat(arr2.filter(item=>!arr1.includes(item)))
- },
- getDifferenceSet : function(arr1, arr2){//差集 不能识别重复的,如getDifferenceSet([1,2,2],[1,1,2]) 为空
- var arr11 = arr1.filter(item=>!arr2.includes(item));
- var arr22 = arr2.filter(item=>!arr1.includes(item));
- return arr11.concat(arr22)
- },
- getDifferenceSetMuti : function(arr){//收集绝对没有重复的元素,也就是判断出现次数=1的
- var set = [];
- arr.forEach(arr1=>{
- arr1.forEach(item=>{
- var index = set.indexOf(item)
- if(index>-1){
- set.splice(index, 1)
- }else{
- set.push(item)
- }
- })
- })
- return set;
- }
- ,
-
-
- CloneJson : function(data){
- var str = JSON.stringify(data)
- return JSON.parse(str)
- }
-
- ,
-
- CloneObject : function(copyObj, result, isSimpleCopy, simpleCopyList=[]) {
- //isSimpleCopy 只复制最外层
- //复制json result的可能:普通数字或字符串、普通数组、复杂对象
-
- simpleCopyList.push(THREE.Object3D) //遇到simpleCopyList中的类直接使用不拷贝
-
- if(!copyObj || typeof copyObj == 'number' || typeof copyObj == 'string' || copyObj instanceof Function || simpleCopyList.some(className => copyObj instanceof className)){
- return copyObj
- }
-
- result = result || {};
- if (copyObj instanceof Array) {
- return copyObj.map(e=>{
- return this.CloneObject(e)
- })
- }else{
- if(copyObj.clone instanceof Function ){ //解决一部分
- return copyObj.clone()
- }
- }
- for (var key in copyObj) {
- if (copyObj[key] instanceof Object && !isSimpleCopy)
- result[key] = this.CloneObject(copyObj[key]);
- else
- result[key] = copyObj[key];
- //如果是函数类同基本数据,即复制引用
- }
- return result;
- }
- ,
- CloneClassObject :function(copyObj ){//复杂类对象
- var newobj = new copyObj.constructor();
- this.CopyClassObject(newobj, copyObj)
-
- return newobj
- }
-
- ,
-
- CopyClassObject :function(targetObj, copyObj){//复杂类对象
- for(let i in copyObj){
- if(i in copyObj.__proto__)break; //到函数了跳出
-
- targetObj[i] = this.CloneObject(copyObj[i], null )
-
-
-
- /* else if(copyObj[i].clone instanceof Function ){
- targetObj[i] = copyObj[i].clone()
- }else{
- targetObj[i] = copyObj[i];
- } */
- }
- }
- ,
-
- ifSame : function(object1, object2){
- if(object1 == object2 )return true // 0 != undefined , 0 == ''
- else if(!object1 || !object2) return false
- else if(object1.constructor != object2.constructor){
- return false
- }else if(object1 instanceof Array ) {
- if(object1.length != object2.length)return false;
- var _object2 = object2.slice(0);
-
- for(let i=0;i<object1.length;i++){
- var u = _object2.find(e=>ifSame(object1[i], e));
- if(u == void 0 && !_object2.includes(u) && !object1.includes(u))return false;
- else{
- let index = _object2.indexOf(u);
- _object2.splice(index,1);
- }
- }
-
- return true
- }else if(object1.equals instanceof Function ){//复杂数据仅支持这种,其他的可能卡住?
-
- return object1.equals(object2)
-
- }else if(typeof object1 == 'number' || typeof object1 == 'string'){
- if(isNaN(object1) && isNaN(object2))return true
- else return object1 == object2
-
- }else if(typeof object1 == "object"){
- var keys1 = Object.keys(object1)
- var keys2 = Object.keys(object2)
- if(!ifSame(keys1,keys2))return false;
-
- for(let i in object1){
- var same = ifSame(object1[i], object2[i]);
- if(!same)return false
- }
- return true
- }else{
- console.log('isSame出现例外')
- }
-
- }
- ,
- replaceAll : function (str, f, e) {
- //f全部替换成e
- var reg = new RegExp(f, "g"); //创建正则RegExp对象
- return str.replace(reg, e);
- }
- ,
- downloadFile : function(data, filename, cb) {
- var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
- save_link.href = data;
- save_link.download = filename;
- var event = document.createEvent('MouseEvents');
- event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
- save_link.dispatchEvent(event);
- cb && cb();
- },
-
- intervalTool:{ //延时update,防止卡顿
- list:[],
-
- isWaiting:function(name, func, delayTime){
- if(!this.list.includes(name)){ //如果没有该项, 则开始判断
- var needWait = func(); //触发了改变,则等待一段时间后再自动判断
- if(needWait){
- this.list.push(name);
- setTimeout(()=>{
- var a = this.list.indexOf(name);
- this.list.splice(a,1);
- this.isWaiting(name, func, delayTime) //循环
- },delayTime)
- }
- }
- },
- /* wait:function(name, delayTime){
- this.list.push(name);
- setTimeout(()=>{
-
- },delayTime)
- }, */
- }
- ,
- pushToGroupAuto : function(items, groups, recognizeFunction){//自动分组。 items是将分到一起的组合。items.length = 1 or 2.
-
- recognizeFunction = recognizeFunction || function(){}
-
- var atGroups = groups.filter(group=>group.find(
- item => items[0] == item || recognizeFunction(item, items[0]) || items[1] == item || items[1] && recognizeFunction(item, items[1])
-
- ))
- if(atGroups.length){//在不同组
- //因为items是一组的,所以先都放入组1
- items.forEach(item=> {if(!atGroups[0].includes(item)) atGroups[0].push(item);})
-
- if(atGroups.length>1){//如果在不同组,说明这两个组需要合并
- var combineGroup = []
- atGroups.forEach(group=>{
- combineGroup = Common.getUnionSet(combineGroup, group)
- groups.splice(groups.indexOf(group),1)
-
- })
- groups.push(combineGroup)
-
- }
- }else{//直接加入为一组
- groups.push(items)
- }
- },
-
- addOrRemoveDefine(material, defineName, type, value=''){
- let defines = material.defines
- if(type == 'add'){
- if(defines[defineName] != void 0 && defines[defineName] == value)return
- defines[defineName] = value
- }else{
- if(defines[defineName] != void 0)return;
- delete defines[defineName]
- }
- material.needsUpdate = true;
- }
- }
-
- export default Common
|