loadModel.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // const util = require('@/static/utils/util.js');
  2. const config = require('@/services/urlConfig.js');
  3. import modelData from '@/static/layoutModelData.js';
  4. import globlShowModel from '@/static/globlShowModel.js';
  5. // import requestConfig from '@/services/requestConfig.js';
  6. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  7. import * as THREE from 'three';
  8. // import { TWEEN } from '@/webgl/jsm/libs/tween.module.min.js';
  9. export default {
  10. data() {
  11. return {
  12. gltfLayouts: [],
  13. instancedFurList:[],
  14. arrFrunList:[],
  15. promise_list:[],
  16. dracoLoader:null,
  17. type:1,
  18. tmpList:[],
  19. realList:[],
  20. nextString:'_simple',//精简模型后缀
  21. }
  22. },
  23. watch: {},
  24. mounted() {
  25. console.warn("***loadmodel-mounted****")
  26. this.dracoLoader = new DRACOLoader();
  27. this.dracoLoader.setDecoderPath('./gltf/');
  28. this.dracoLoader.setDecoderConfig( { type: 'js' } );
  29. this.dracoLoader.preload();
  30. },
  31. methods: {
  32. // 设置空间数组的墙体信息
  33. async setSpaceListWallInfo(){
  34. for (let index = 0; index < this.spaceList.length; index++) {
  35. let spaceWallInfo = {wallN:false, wallS:false, wallW:false, wallE:false}
  36. const element = this.spaceList[index];
  37. const wallWIndex = this.gltfWalls.findIndex(item=>{
  38. return element.spaceId == item.spaceId && item.wallDirection == "W"
  39. })
  40. spaceWallInfo.wallW = wallWIndex == -1 ? false : true;
  41. const wallEIndex = this.gltfWalls.findIndex(item=>{
  42. return element.spaceId == item.spaceId && item.wallDirection == "E"
  43. })
  44. spaceWallInfo.wallE = wallEIndex == -1 ? false : true;
  45. const wallNIndex = this.gltfWalls.findIndex(item=>{
  46. return element.spaceId == item.spaceId && item.wallDirection == "N"
  47. })
  48. spaceWallInfo.wallN = wallNIndex == -1 ? false : true;
  49. const wallSIndex = this.gltfWalls.findIndex(item=>{
  50. return element.spaceId == item.spaceId && item.wallDirection == "S"
  51. })
  52. spaceWallInfo.wallS = wallSIndex == -1 ? false : true;
  53. element.spaceWallInfo = spaceWallInfo;
  54. }
  55. },
  56. // 批量获取空间模型信息
  57. async getOverallArrangementDetailsList(type=1) {
  58. // 设置空间数组的墙体信息
  59. // this.setSpaceListWallInfo();
  60. this.type = type;//加载类型,1空间加载 2 全局精简加载
  61. let arr = this.spaceList.map(it => it.layoutId).filter(it => it != 0);
  62. let parmas = {
  63. ids: arr,
  64. };
  65. // 默认的布局
  66. let res = await requestConfig("getOverallArrangementDetailsList", parmas);
  67. if (!res.success || !res.list || res.list.length == 0) {
  68. return false;
  69. }
  70. this.promise_list = [];
  71. this.arrFrunList = res.list;
  72. // 通过默认布局
  73. this.furnHandle(this.arrFrunList);
  74. },
  75. //拆分家具模型加载逻辑
  76. furnHandle(arrFrunList, spaceObj = null){
  77. if(!arrFrunList || arrFrunList.length == 0){
  78. return
  79. }
  80. let startTime = new Date().getTime();
  81. let tmpList = [];//临时数据
  82. this.gltfLayouts = []; //模型列表,所有空间里面的每个模型(家具)对应一条记录
  83. let allowSpaceId = [this.curSpaceObj.spaceId];
  84. if(this.type == 2){//全局精简模型;需要的是所有空间的模型
  85. allowSpaceId = this.spaceList.map(it=>it.spaceId)
  86. }else{//空间模型模式
  87. allowSpaceId = [this.curSpaceObj.spaceId];
  88. if(this.curSpaceObj.spaceType==4){//厨房
  89. let list = this.spaceList.filter(it=>it.spaceType==2)
  90. allowSpaceId.push(...list.map(it=>it.spaceId));//客厅
  91. }
  92. if(this.curSpaceObj.spaceType==2){//餐厅
  93. let list = this.spaceList.filter(it=>it.spaceType==4)
  94. allowSpaceId.push(...list.map(it=>it.spaceId));//厨房
  95. }
  96. //花园类型的添加到家具加载目录里面
  97. // this.spaceList.forEach(it=>{
  98. // if(it.spaceType==14){
  99. // allowSpaceId.push(it.spaceId);
  100. // }
  101. // })
  102. }
  103. // allowSpaceId = [388];//模型有警告,需要看下
  104. console.log("重新加载布局", arrFrunList,this.spaceList,this.gltfLayouts, this.curSpaceObj.spaceId);
  105. arrFrunList.forEach(signel => {
  106. let spaceId = this.spaceList.find(it => it.layoutId == signel.id).spaceId;
  107. if(allowSpaceId.includes(spaceId)){//只加载允许的空间的布局,其他不加载
  108. let md = JSON.parse(signel.modelJson); //获取布局里面的模型信息
  109. //遍历模型信息,获取模型列表
  110. for (let i = 0; i < md.modelData.length; i++) {
  111. let item = md.modelData[i];
  112. item.spaceId = spaceId;
  113. item.layoutId = signel.id;
  114. item.h5Id = signel.id+""+item.id;//唯一标识
  115. if(item.modelName=='BP_furnitureBase_C'){
  116. console.warn("***布局1***", item, signel.id)
  117. continue;
  118. }else{
  119. tmpList.push(item);
  120. console.warn("***布局***", item, signel.id)
  121. }
  122. }
  123. }
  124. })
  125. this.tmpList = tmpList;
  126. if(this.type==2){//全局精简模型;需要的是所有空间的模型
  127. this.fliterList(tmpList);
  128. console.warn("***fliterList***",this.realList.length ,this.realList);
  129. tmpList = this.realList;
  130. }
  131. let realFurArr = this.preFurnitureData(tmpList);//统一处理家具模型
  132. if(spaceObj){
  133. this.calculateLayoutModelSizeBySpace(spaceObj) // 提前计算模型的位置
  134. }else{
  135. this.calculateLayoutModelSize() // 提前计算模型的位置
  136. }
  137. console.log("***realFurArr***", realFurArr,this.gltfLayouts);
  138. realFurArr && realFurArr.forEach((item,index) => {
  139. this.promise_list.push(
  140. new Promise((resolve, reject) => {
  141. this.loadLayoutModels(item, resolve);
  142. })
  143. )
  144. });
  145. Promise.all(this.promise_list).then(() => {
  146. let endTime = new Date().getTime();
  147. console.log("家具模型全部加载完成,时间:", endTime - startTime, this.gltfLayouts);
  148. })
  149. },
  150. //过滤掉不需要的家具
  151. fliterList(list){
  152. this.realList = [];
  153. let tempList = globlShowModel.map(it=>it.replace(this.nextString,''));
  154. //找到直接的家具模型
  155. list && list.forEach((mod)=>{
  156. let name = mod.modelName.substring(0,mod.modelName.length-2);
  157. if(tempList.includes(name)){
  158. this.realList.push(mod);
  159. }
  160. })
  161. this.realList.forEach((ref)=>{
  162. this.recursionFliter(ref)
  163. })
  164. },
  165. //递归处理不需要的家具
  166. recursionFliter(item){
  167. let _list = [];//
  168. if(parseInt(item.referenceModelBottom) > 0){
  169. _list.push(item.referenceModelBottom)
  170. }
  171. if(parseInt(item.referenceModelLeft) > 0){
  172. _list.push(item.referenceModelLeft)
  173. }
  174. if(parseInt(item.referenceModelRight) > 0){
  175. _list.push(item.referenceModelRight)
  176. }
  177. if(parseInt(item.referenceModelTop) > 0){
  178. _list.push(item.referenceModelTop)
  179. }
  180. if(_list.length==0){
  181. return false;
  182. }else{
  183. this.tmpList.forEach(tmp=>{
  184. if(_list.includes(tmp.id)){
  185. if(!this.realList.find(it=>it.h5Id==tmp.h5Id)){
  186. this.realList.push(tmp);
  187. this.recursionFliter(tmp);
  188. }
  189. }
  190. })
  191. }
  192. },
  193. //预处理需要加载墙体模型的数据-减少模型请求数
  194. preFurnitureData(list){
  195. let realFurnitureArr = [];//家具列表
  196. list && list.forEach((item, index) => {
  197. item.uniId = Date.now() + index;//唯一标识
  198. //获取墙体对应的gltb模型的相关信息
  199. let modelName = item.modelName;
  200. // console.log("布局模型名称", modelName);
  201. let layoutModel = modelData.find(it => modelName.indexOf(it.modelName) == 0); //模型地址
  202. console.log("要加载的模型数据", item.modelName, layoutModel)
  203. if (layoutModel && layoutModel.url) {//该数据存在模型地址
  204. let object = realFurnitureArr.find(it=>it.url==layoutModel.url);
  205. //列表中还没有这个数据
  206. if(!object){
  207. let url = layoutModel.url;
  208. if(this.type==2){//全局精简模型;需要的是所有空间的模型
  209. //说明当前模型存在精简模型对象,则应该使用精简模型数据
  210. if(globlShowModel.includes(layoutModel.modelName + this.nextString)){
  211. let idx = layoutModel.url.lastIndexOf('.');
  212. url = url.substr(0,idx) + this.nextString + url.substr(idx)
  213. }
  214. }
  215. let it = {
  216. url:url,
  217. name:layoutModel.modelName,
  218. list:[item],
  219. }
  220. realFurnitureArr.push(it)
  221. }else{
  222. object.list.push(item);
  223. }
  224. let position = new THREE.Vector3();//当前几何体的位置参数
  225. let scale = new THREE.Vector3();//当前几何体的缩放参数
  226. let rotation = new THREE.Vector3();//当前几何体的缩放参数
  227. let md = {
  228. uniId:item.uniId,//家具模型实例的唯一标识
  229. spaceId:item.spaceId,
  230. id:item.id,
  231. userData:item,
  232. position:position,
  233. scale:scale,
  234. rotation:rotation,
  235. loaded:false,
  236. };
  237. this.gltfLayouts.push(md);
  238. }
  239. });
  240. return realFurnitureArr;
  241. },
  242. //加载家具模型-实例化方案
  243. loadLayoutModels(realData, resolve){
  244. var that = this;
  245. if(!realData.url){
  246. console.warn("***家具模型不存在***",realData);
  247. resolve();
  248. return false;
  249. }
  250. if(realData.name.includes('BP_XSPACE_deng_01')){//灯光
  251. resolve();
  252. }else{
  253. that.loader.setDRACOLoader(this.dracoLoader);
  254. that.loader.load(realData.url, ( gltf ) => {
  255. gltf.scene.traverse((child)=> {
  256. if (child.isMesh && child.visible) {
  257. let instancedMesh = new THREE.InstancedMesh(child.geometry.clone(), child.material.clone(), realData.list.length);
  258. this.instancedFurList.push(instancedMesh);
  259. //realData 该模型被重复使用时的每一次的形变参数等
  260. realData.list && realData.list.forEach((it,i)=>{
  261. let gltfFurn = that.gltfLayouts.find(itme=>itme.uniId==it.uniId);//判断是否已经添加过
  262. gltf.scene.rotation.y = gltfFurn.rotation.y;
  263. gltf.scene.position.set(gltfFurn.position.x,0,gltfFurn.position.z);
  264. gltf.scene.scale.set(gltfFurn.scale.x,1,gltfFurn.scale.z);
  265. gltf.scene.updateMatrixWorld();//更新世界坐标-这样,子模型也同步更新了
  266. instancedMesh.setMatrixAt(i, child.matrixWorld);
  267. instancedMesh.instanceMatrix.needsUpdate = true;
  268. gltfFurn.loaded = true;
  269. if(!gltfFurn.instancedMeshIndexList){//标识网格实例数组的序号 以及 当前几何体 在网格实例的序号
  270. gltfFurn.instancedMeshIndexList = [
  271. {instancedMeshIndex:this.instancedFurList.length-1,instancedAtIndex:i},
  272. ]
  273. }else{
  274. gltfFurn.instancedMeshIndexList.push({
  275. instancedMeshIndex:this.instancedFurList.length-1,instancedAtIndex:i
  276. })
  277. }
  278. })
  279. instancedMesh.userType = "layoutMesh";
  280. if(realData.name.includes("BP_L_carpet01")){//地毯接收阴影
  281. instancedMesh.receiveShadow = true;//对象是否接收阴影
  282. }else{
  283. instancedMesh.castShadow = true;//对象是否产生阴影
  284. }
  285. this.scene.add(instancedMesh);//添加到场景中
  286. }
  287. });
  288. resolve();
  289. });
  290. }
  291. },
  292. //加载模型
  293. loadLayoutModelsOld(modelObj, resolve) {
  294. let that = this;
  295. const modelName = modelObj.modelName;
  296. console.log("布局模型名称", modelName);
  297. let layoutModel = modelData.find(it => modelName.indexOf(it.modelName) == 0); //模型地址
  298. if (!layoutModel || !layoutModel.url) {
  299. resolve();
  300. return false
  301. }
  302. let url = layoutModel.url;
  303. console.log("布局模型数据", layoutModel);
  304. that.loader.load(url, (gltf) => {
  305. // console.log("布局模型加载成功", gltf);
  306. let model = gltf.scene; // 获取模型
  307. model.name = layoutModel.modelName;
  308. model.userType = "layoutMesh";
  309. model.userData = modelObj;
  310. // model.rotation.y = Math.PI / 2 ; // 旋转 90 度
  311. this.gltfLayouts.push(model);
  312. resolve();
  313. });
  314. },
  315. // 切换家具的显示隐藏
  316. changeLayoutModelState(isShow=false){
  317. console.log("移动过程中显示隐藏空间家具", this.gltfLayouts,isShow, this.leftSpaces, this.rightSpaces);
  318. const changeSpaceList = this.leftSpaces.concat(this.rightSpaces); // 只改变空间尺寸变化的家具模型
  319. for (let index = 0; index < changeSpaceList.length; index++) {
  320. const element = changeSpaceList[index];
  321. const gltfLayoutModels = this.gltfLayouts.filter(item => {
  322. return element.layoutId == item.userData.layoutId && element.spaceId == item.userData.spaceId;
  323. })
  324. if (!gltfLayoutModels || gltfLayoutModels.length == 0) {
  325. continue;
  326. }
  327. for (let j = 0; j < gltfLayoutModels.length; j++) {
  328. const cube = gltfLayoutModels[j];
  329. this.changeCubeState(cube, isShow);
  330. }
  331. }
  332. },
  333. // 显示隐藏指定cube的状态
  334. changeCubeState(cube, isShow){
  335. let lay = this.gltfLayouts.find(it=>it.uniId==cube.uniId);
  336. if(cube.instancedMeshIndexList && cube.instancedMeshIndexList.length>0){
  337. cube.instancedMeshIndexList.forEach(item=>{
  338. let index = item.instancedMeshIndex;
  339. let instancedMesh = this.instancedFurList[index];//网格实例对象
  340. let curMeshIndex = item.instancedAtIndex;//当前家具模型在网格实例对象里面的序号
  341. let stratMatrix = new THREE.Matrix4();//定义一个四维矩阵
  342. instancedMesh.getMatrixAt(curMeshIndex,stratMatrix);//获取当前几何体的四维矩阵到stratMatrix里面
  343. instancedMesh.instanceMatrix.needsUpdate = true;//更新之前,必须开启开关
  344. instancedMesh.visible = isShow;
  345. instancedMesh.setMatrixAt(curMeshIndex,stratMatrix);//更新几何体的世界矩阵
  346. })
  347. }
  348. },
  349. deleteLayoutModel(spaceObj, layoutObj){
  350. // 删除布局数据
  351. for (let index = 0; index < this.arrFrunList.length; index++) {
  352. const element = this.arrFrunList[index];
  353. }
  354. const oldLayoutIndex = this.arrFrunList.findIndex((item)=>{
  355. return item.id == spaceObj.layoutId
  356. })
  357. if(oldLayoutIndex!=-1){
  358. this.arrFrunList[oldLayoutIndex] = layoutObj;
  359. }
  360. console.log("准备删除模型", spaceObj, this.gltfLayouts, this.arrFrunList);
  361. const gltfLayoutModels = this.gltfLayouts.filter(item => {
  362. return spaceObj.layoutId == item.userData.layoutId && spaceObj.spaceId == item.userData.spaceId;
  363. })
  364. if (!gltfLayoutModels || gltfLayoutModels.length == 0) {
  365. return;
  366. }
  367. for (let j = 0; j < gltfLayoutModels.length; j++) {
  368. const cube = gltfLayoutModels[j];
  369. if(cube.instancedMeshIndexList && cube.instancedMeshIndexList.length>0){
  370. cube.instancedMeshIndexList.forEach(item=>{
  371. let index = item.instancedMeshIndex;
  372. let instancedMesh = this.instancedFurList[index];//网格实例对象
  373. this.scene.remove(instancedMesh);//添加到场景中
  374. })
  375. }
  376. console.log("删除原有模型", spaceObj.layoutId, cube);
  377. }
  378. },
  379. // 计算家具的位置
  380. calculateLayoutModelSize() {
  381. console.log("计算家具的位置", this.gltfLayouts);
  382. for (let index = 0; index < this.spaceList.length; index++) {
  383. const element = this.spaceList[index];
  384. const gltfLayoutModels = this.gltfLayouts.filter(item => {
  385. return element.layoutId == item.userData.layoutId && element.spaceId == item.userData.spaceId;
  386. })
  387. //空间下不存在家具模型,则该空间不需要进行下一步处理
  388. if (!gltfLayoutModels || gltfLayoutModels.length == 0) {
  389. continue;
  390. }
  391. gltfLayoutModels.sort(function(a, b) {
  392. return a.userData.level - b.userData.level
  393. });
  394. // console.log("对应空间ID的模型数组", this.gltfLayouts, element.spaceId, gltfLayoutModels)
  395. for (let j = 0; j < gltfLayoutModels.length; j++) {
  396. const cube = gltfLayoutModels[j];
  397. this.drawLayoutModel(element, cube);
  398. }
  399. }
  400. },
  401. // 计算家具的位置
  402. calculateLayoutModelSizeBySpace(spaceObj) {
  403. // for (let index = 0; index < this.spaceList.length; index++) {
  404. const element = spaceObj;
  405. const gltfLayoutModels = this.gltfLayouts.filter(item => {
  406. return element.layoutId == item.userData.layoutId && element.spaceId == item.userData.spaceId;
  407. })
  408. console.log("计算指定空间的家具的位置", spaceObj, element.layoutId, gltfLayoutModels);
  409. //空间下不存在家具模型,则该空间不需要进行下一步处理
  410. if (!gltfLayoutModels || gltfLayoutModels.length == 0) {
  411. return;
  412. }
  413. gltfLayoutModels.sort(function(a, b) {
  414. return a.userData.level - b.userData.level
  415. });
  416. // console.log("对应空间ID的模型数组", this.gltfLayouts, element.spaceId, gltfLayoutModels)
  417. for (let j = 0; j < gltfLayoutModels.length; j++) {
  418. const cube = gltfLayoutModels[j];
  419. this.drawLayoutModel(element, cube);
  420. }
  421. // }
  422. },
  423. // 绘制模型
  424. drawLayoutModel(curSpace, cube) {
  425. const {
  426. centerX,
  427. spaceId
  428. } = curSpace;
  429. const cubeInfo = this.resetModelParameters(curSpace, cube.userData);
  430. // const cubeInfo = cube.userData;
  431. let centerY = curSpace.centerY * -1; // UE和ThreeJS坐标做相反
  432. // 默认空间中心点
  433. let positionX = centerX;
  434. let positionY = centerY;
  435. let rotationY = Math.PI / 2;
  436. let scaleX = 1;
  437. let scaleY = 1;
  438. // 空间尺寸
  439. let spaceWidth = curSpace.spaceWidth;
  440. let spaceHeight = curSpace.spaceHeight;
  441. // 模型尺寸
  442. let modelWidth = cubeInfo.modelWidth;
  443. let modelHeight = cubeInfo.modelHeight;
  444. // 判断旋转
  445. if (parseFloat(cubeInfo.rotation) == 90) {
  446. rotationY = 0;
  447. // 交换尺寸
  448. // modelWidth = cubeInfo.modelHeight;
  449. // modelHeight = cubeInfo.modelWidth;
  450. }
  451. if (parseFloat(cubeInfo.rotation) == 180) {
  452. rotationY = -Math.PI / 2;
  453. // modelWidth = cubeInfo.modelHeight;
  454. // modelHeight = cubeInfo.modelWidth;
  455. }
  456. if (parseFloat(cubeInfo.rotation) == -90) {
  457. rotationY = -Math.PI;
  458. // 交换尺寸
  459. // modelWidth = cubeInfo.modelHeight;
  460. // modelHeight = cubeInfo.modelWidth;
  461. }
  462. // 判断靠墙
  463. if (cubeInfo.isStepAsideLeft == 'true') {
  464. positionX = centerX - (spaceWidth / 2 - modelWidth / 2);
  465. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  466. positionX = centerX - (spaceWidth / 2 - modelWidth / 2);
  467. }
  468. positionX += parseFloat(cubeInfo.marginLeft);
  469. positionX += curSpace.spaceWallInfo.wallW ? 10 : 0;
  470. }
  471. if (cubeInfo.isStepAsideRight == 'true') {
  472. positionX = centerX + (spaceWidth / 2 - modelWidth / 2);
  473. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  474. positionX = centerX + (spaceWidth / 2 - modelWidth / 2);
  475. }
  476. positionX -= parseFloat(cubeInfo.marginRight);
  477. positionX -= curSpace.spaceWallInfo.wallE ? 10 : 0;
  478. }
  479. if (cubeInfo.isStepAsideTop == 'true') {
  480. positionY = centerY - (spaceHeight / 2 - modelHeight / 2);
  481. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  482. positionY = centerY - (spaceHeight / 2 - modelHeight / 2);
  483. }
  484. positionY += parseFloat(cubeInfo.marginTop);
  485. positionY += curSpace.spaceWallInfo.wallN ? 10 : 0;
  486. }
  487. if (cubeInfo.isStepAsideBottom == 'true') {
  488. positionY = centerY + (spaceHeight / 2 - modelHeight / 2);
  489. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  490. positionY = centerY + (spaceHeight / 2 - modelHeight / 2);
  491. }
  492. positionY -= parseFloat(cubeInfo.marginBottom);
  493. positionY -= curSpace.spaceWallInfo.wallS ? 10 : 0;
  494. }
  495. // 参照物
  496. if (parseInt(cubeInfo.referenceModelTop) > 0) {
  497. const referenceModel = this.gltfLayouts.find(item => {
  498. const layoutModelData = item.userData;
  499. return layoutModelData.id == parseInt(cubeInfo.referenceModelTop) && layoutModelData
  500. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  501. })
  502. if(!referenceModel){
  503. console.warn("***no-data***",cubeInfo.referenceModelTop,cubeInfo)
  504. return false
  505. }
  506. positionY = referenceModel.position.z * 100 + (referenceModel.userData.modelHeight / 2 + cubeInfo
  507. .modelHeight / 2);
  508. positionY = positionY + parseFloat(cubeInfo.marginTop);
  509. }
  510. if (parseInt(cubeInfo.referenceModelBottom) > 0) {
  511. const referenceModel = this.gltfLayouts.find(item => {
  512. const layoutModelData = item.userData;
  513. return layoutModelData.id == parseInt(cubeInfo.referenceModelBottom) && layoutModelData
  514. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  515. })
  516. if(!referenceModel){
  517. console.warn("***no-data***",cubeInfo.referenceModelBottom,cubeInfo)
  518. return false
  519. }
  520. positionY = referenceModel.position.z * 100 - (referenceModel.userData.modelHeight / 2 + cubeInfo
  521. .modelHeight / 2);
  522. positionY = positionY - parseFloat(cubeInfo.marginBottom);
  523. }
  524. if (parseInt(cubeInfo.referenceModelLeft) > 0) {
  525. const referenceModel = this.gltfLayouts.find(item => {
  526. const layoutModelData = item.userData;
  527. return layoutModelData.id == parseInt(cubeInfo.referenceModelLeft) && layoutModelData
  528. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  529. })
  530. if(!referenceModel){
  531. console.warn("***no-data***",cubeInfo.referenceModelLeft,cubeInfo)
  532. return false
  533. }
  534. positionX = referenceModel.position.x * 100 + (referenceModel.userData.modelWidth / 2 + cubeInfo
  535. .modelWidth / 2);
  536. positionX = positionX + parseFloat(cubeInfo.marginLeft);
  537. }
  538. if (parseInt(cubeInfo.referenceModelRight) > 0) {
  539. const referenceModel = this.gltfLayouts.find(item => {
  540. const layoutModelData = item.userData;
  541. return layoutModelData.id == parseInt(cubeInfo.referenceModelRight) && layoutModelData
  542. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  543. })
  544. if(!referenceModel){
  545. console.warn("***no-data***",cubeInfo.referenceModelRight,cubeInfo)
  546. return false
  547. }
  548. positionX = referenceModel.position.x * 100 - (referenceModel.userData.modelWidth / 2 + cubeInfo
  549. .modelWidth / 2);
  550. positionX = positionX - parseFloat(cubeInfo.marginRight);
  551. }
  552. if (cubeInfo.isFixedWidth == "false" || cubeInfo.isFixedHeight == "false") { // 固定尺寸
  553. // 靠墙拉伸
  554. if (cubeInfo.isStepAsideTop == 'true' && cubeInfo.isStepAsideBottom == 'true') {
  555. // console.log("高度拉伸", spaceHeight / modelHeight)
  556. positionY = centerY;
  557. if (curSpace.spaceWallInfo.wallN && curSpace.spaceWallInfo.wallS) {
  558. spaceHeight -= 20 + parseFloat(cubeInfo.marginTop) + parseFloat(cubeInfo.marginBottom);
  559. positionY += parseFloat(cubeInfo.marginTop) / 2 ;
  560. positionY -= parseFloat(cubeInfo.marginBottom) / 2;
  561. } else if (curSpace.spaceWallInfo.wallN) {
  562. spaceHeight -= 10 + parseFloat(cubeInfo.marginTop) + parseFloat(cubeInfo.marginBottom);
  563. positionY += 10 / 2;
  564. positionY += parseFloat(cubeInfo.marginTop) / 2;
  565. positionY -= parseFloat(cubeInfo.marginBottom) / 2;
  566. } else if (curSpace.spaceWallInfo.wallS) {
  567. spaceHeight -= 10 + parseFloat(cubeInfo.marginTop) + parseFloat(cubeInfo.marginBottom);
  568. positionY -= 10 / 2;
  569. positionY += parseFloat(cubeInfo.marginTop) / 2 ;
  570. positionY -= parseFloat(cubeInfo.marginBottom) / 2;
  571. }else {
  572. spaceHeight -= parseFloat(cubeInfo.marginTop) + parseFloat(cubeInfo.marginBottom);
  573. positionY += parseFloat(cubeInfo.marginTop) / 2 ;
  574. positionY -= parseFloat(cubeInfo.marginBottom) / 2;
  575. }
  576. // 计算缩放
  577. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  578. scaleY = spaceHeight / modelHeight;
  579. } else {
  580. scaleX = spaceHeight / modelHeight;
  581. }
  582. }
  583. if (cubeInfo.isStepAsideLeft == 'true' && cubeInfo.isStepAsideRight == 'true') {
  584. // console.log("宽度拉伸11111", spaceWidth / modelWidth)
  585. positionX = centerX;
  586. if (curSpace.spaceWallInfo.wallW && curSpace.spaceWallInfo.wallE) {
  587. spaceWidth -= 20 + parseFloat(cubeInfo.marginLeft) + parseFloat(cubeInfo.marginRight);
  588. positionX += parseFloat(cubeInfo.marginLeft) / 2;
  589. positionX -= parseFloat(cubeInfo.marginRight) / 2;
  590. } else if (curSpace.spaceWallInfo.wallW) {
  591. spaceWidth -= 10 + parseFloat(cubeInfo.marginLeft) + parseFloat(cubeInfo.marginRight);
  592. positionX += 10 / 2;
  593. positionX += parseFloat(cubeInfo.marginLeft) / 2;
  594. positionX -= parseFloat(cubeInfo.marginRight) / 2;
  595. } else if (curSpace.spaceWallInfo.wallE) {
  596. spaceWidth -= 10 + parseFloat(cubeInfo.marginLeft) + parseFloat(cubeInfo.marginRight);
  597. positionX -= 10 / 2;
  598. positionX += parseFloat(cubeInfo.marginLeft) / 2;
  599. positionX -= parseFloat(cubeInfo.marginRight) / 2;
  600. }else {
  601. spaceWidth -= parseFloat(cubeInfo.marginLeft) + parseFloat(cubeInfo.marginRight);
  602. positionX += parseFloat(cubeInfo.marginLeft) / 2;
  603. positionX -= parseFloat(cubeInfo.marginRight) / 2;
  604. }
  605. // console.log("模型计算位置" , parseFloat(cubeInfo.rotation))
  606. // 计算缩放
  607. if (parseFloat(cubeInfo.rotation) == 90 || parseFloat(cubeInfo.rotation) == -90) {
  608. scaleX = spaceWidth / modelWidth;
  609. } else {
  610. scaleY = spaceWidth / modelWidth;
  611. }
  612. }
  613. // 单个参照物和墙面拉伸
  614. if (cubeInfo.isStepAsideTop == 'true' && parseInt(cubeInfo.referenceModelBottom) > 0) {
  615. const referenceModel = this.gltfLayouts.find(item => {
  616. const layoutModelData = item.userData;
  617. return layoutModelData.id == parseInt(cubeInfo.referenceModelBottom) && layoutModelData
  618. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  619. })
  620. const referenceModelPositionY = referenceModel.position.z * 100 - referenceModel.userData
  621. .modelHeight / 2 - parseFloat(cubeInfo.marginBottom);
  622. const wallPositionY = centerY - spaceHeight / 2 + (curSpace.spaceWallInfo.wallN ? 10 : 0) +
  623. parseFloat(cubeInfo.marginTop);
  624. const newModelHeight = Math.abs(referenceModelPositionY - wallPositionY);
  625. // console.log("上边拉伸", referenceModelPositionY, wallPositionY, newModelHeight)
  626. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  627. scaleY = newModelHeight / modelHeight;
  628. } else {
  629. scaleX = newModelHeight / modelHeight;
  630. }
  631. positionY = wallPositionY + newModelHeight / 2;
  632. }
  633. if (cubeInfo.isStepAsideBottom == 'true' && parseInt(cubeInfo.referenceModelTop) > 0) {
  634. const referenceModel = this.gltfLayouts.find(item => {
  635. const layoutModelData = item.userData;
  636. return layoutModelData.id == parseInt(cubeInfo.referenceModelTop) && layoutModelData
  637. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  638. })
  639. const referenceModelPositionY = referenceModel.position.z * 100 + referenceModel.userData
  640. .modelHeight / 2 + parseFloat(cubeInfo.marginTop) + (curSpace.spaceWallInfo.wallN ? 10 : 0);
  641. const wallPositionY = centerY + spaceHeight / 2 - (curSpace.spaceWallInfo.wallS ? 10 : 0) -
  642. parseFloat(cubeInfo.marginBottom);
  643. const newModelHeight = Math.abs(referenceModelPositionY - wallPositionY);
  644. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  645. scaleY = newModelHeight / modelHeight;
  646. } else {
  647. scaleX = newModelHeight / modelHeight;
  648. }
  649. positionY = wallPositionY - newModelHeight / 2;
  650. }
  651. if (cubeInfo.isStepAsideLeft == 'true' && parseInt(cubeInfo.referenceModelRight) > 0) {
  652. // console.log("左边拉伸")
  653. const referenceModel = this.gltfLayouts.find(item => {
  654. const layoutModelData = item.userData;
  655. return layoutModelData.id == parseInt(cubeInfo.referenceModelRight) && layoutModelData
  656. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  657. })
  658. const referenceModelPositionX = referenceModel.position.x * 100 - referenceModel.userData
  659. .modelWidth / 2 - parseFloat(cubeInfo.marginLeft);
  660. const wallPositionX = centerX - spaceWidth / 2 + (curSpace.spaceWallInfo.wallE ? 10 : 0) +
  661. parseFloat(cubeInfo.marginRight);
  662. const newModelWidth = Math.abs(referenceModelPositionX - wallPositionX);
  663. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  664. scaleX = newModelWidth / modelWidth;
  665. } else {
  666. scaleY = newModelWidth / modelWidth;
  667. }
  668. positionX = wallPositionX + newModelWidth / 2;
  669. }
  670. if (cubeInfo.isStepAsideRight == 'true' && parseInt(cubeInfo.referenceModelLeft) > 0) {
  671. const referenceModel = this.gltfLayouts.find(item => {
  672. const layoutModelData = item.userData;
  673. return layoutModelData.id == parseInt(cubeInfo.referenceModelLeft) && layoutModelData
  674. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  675. })
  676. const referenceModelPositionX = referenceModel.position.x * 100 + referenceModel.userData
  677. .modelWidth / 2 + parseFloat(cubeInfo.marginLeft);
  678. const wallPositionX = centerX + spaceWidth / 2 - (curSpace.spaceWallInfo.wallE ? 10 : 0) -
  679. parseFloat(cubeInfo.marginRight);
  680. const newModelWidth = Math.abs(referenceModelPositionX - wallPositionX);
  681. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  682. scaleX = newModelWidth / modelWidth;
  683. } else {
  684. scaleY = newModelWidth / modelWidth;
  685. }
  686. positionX = wallPositionX - newModelWidth / 2;
  687. // console.log("右边拉伸", Math.abs(parseFloat(cubeInfo.rotation)))
  688. }
  689. if(parseInt(cubeInfo.referenceModelBottom) > 0 && parseInt(cubeInfo.referenceModelTop) > 0){
  690. // 上边惨遭物
  691. const referenceModelTop = this.gltfLayouts.find(item => {
  692. const layoutModelData = item.userData;
  693. return layoutModelData.id == parseInt(cubeInfo.referenceModelTop) && layoutModelData
  694. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  695. })
  696. // 下边参照物
  697. const referenceModelBottom = this.gltfLayouts.find(item => {
  698. const layoutModelData = item.userData;
  699. return layoutModelData.id == parseInt(cubeInfo.referenceModelBottom) && layoutModelData
  700. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  701. })
  702. const referenceModelTopPositionY = referenceModelTop.position.z * 100 + referenceModelTop.userData
  703. .modelHeight / 2 + parseFloat(cubeInfo.marginTop);
  704. const referenceModelBottomPositionY = referenceModelBottom.position.z * 100 - referenceModelBottom.userData
  705. .modelHeight / 2 - parseFloat(cubeInfo.marginBottom);
  706. const newModelHeight = Math.abs(referenceModelTopPositionY - referenceModelBottomPositionY);
  707. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  708. scaleY = newModelHeight / modelHeight;
  709. } else {
  710. scaleX = newModelHeight / modelHeight;
  711. }
  712. positionY = referenceModelBottomPositionY - newModelHeight / 2;
  713. }
  714. if(parseInt(cubeInfo.referenceModelLeft) > 0 && parseInt(cubeInfo.referenceModelRight) > 0){
  715. const referenceModelLeft = this.gltfLayouts.find(item => {
  716. const layoutModelData = item.userData;
  717. return layoutModelData.id == parseInt(cubeInfo.referenceModelLeft) && layoutModelData
  718. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  719. })
  720. const referenceModelRight = this.gltfLayouts.find(item => {
  721. const layoutModelData = item.userData;
  722. return layoutModelData.id == parseInt(cubeInfo.referenceModelRight) && layoutModelData
  723. .spaceId == cubeInfo.spaceId && layoutModelData.layoutId == cubeInfo.layoutId;
  724. })
  725. const referenceModelLeftPositionX = referenceModelLeft.position.x * 100 + referenceModelLeft.userData
  726. .modelWidth / 2 + parseFloat(cubeInfo.marginLeft);
  727. const referenceModelRightPositionX = referenceModelRight.position.x * 100 - referenceModelRight.userData
  728. .modelWidth / 2 - parseFloat(cubeInfo.marginRight);
  729. const newModelWidth = Math.abs(referenceModelLeftPositionX - referenceModelRightPositionX);
  730. if (Math.abs(parseFloat(cubeInfo.rotation)) == 90) {
  731. scaleX = newModelWidth / modelWidth;
  732. } else {
  733. scaleY = newModelWidth / modelWidth;
  734. }
  735. positionX = referenceModelRightPositionX - newModelWidth / 2;
  736. }
  737. }
  738. // console.log("模型计算位置--", positionX, positionY)
  739. console.log("模型计算位置", cubeInfo, centerX, centerY, spaceWidth, spaceHeight, modelWidth, modelHeight, spaceId, scaleX, scaleY, positionX, positionY, cubeInfo.modelName)
  740. let oldPosition = new THREE.Vector3();//当前几何体的位置参数
  741. oldPosition.copy(cube.position);
  742. cube.position.x = positionX / 100;
  743. cube.position.z = positionY / 100;
  744. cube.rotation.y = rotationY;
  745. cube.scale.x = scaleX;
  746. cube.scale.z = scaleY;
  747. cube.scale.y = 1;
  748. // if (!cube.parent) { //说明还没添加到场景中-旧版方法
  749. // this.scene.add(cube); //处理完毕后在加入场景中
  750. // }
  751. // if(scaleX>1 || scaleY>1){
  752. // console.warn("***scaleinfo***",cubeInfo.id,scaleX,scaleY,cubeInfo.modelName)
  753. // }
  754. if(this.type==2){//全局精简加载
  755. let name = cubeInfo.modelName.substring(0, cubeInfo.modelName.length - 2);
  756. if(globlShowModel.includes(name)){
  757. if(cubeInfo.modelName=='BP_XSPACE_deng_01_C'){//灯光
  758. this.lightHandle(cube)
  759. }else{
  760. this.updateInfo(cube,oldPosition,scaleX,scaleY);//实例化方法
  761. }
  762. }
  763. }else{
  764. if(cubeInfo.modelName=='BP_XSPACE_deng_01_C'){//灯光
  765. this.lightHandle(cube)
  766. }else{
  767. this.updateInfo(cube,oldPosition,scaleX,scaleY);//实例化方法
  768. }
  769. }
  770. },
  771. //更新家具模型到页面中
  772. updateInfo(cube,oldPosition,scaleX,scaleY){
  773. let lay = this.gltfLayouts.find(it=>it.uniId==cube.uniId);
  774. if(cube.instancedMeshIndexList && cube.instancedMeshIndexList.length>0){
  775. cube.instancedMeshIndexList.forEach(item=>{
  776. let index = item.instancedMeshIndex;
  777. let instancedMesh = this.instancedFurList[index];//网格实例对象
  778. let curMeshIndex = item.instancedAtIndex;//当前家具模型在网格实例对象里面的序号
  779. let stratMatrix = new THREE.Matrix4();//定义一个四维矩阵
  780. instancedMesh.getMatrixAt(curMeshIndex,stratMatrix);//获取当前几何体的四维矩阵到stratMatrix里面
  781. let scaleMatrix = new THREE.Matrix4(); //定义一个缩放变化矩阵
  782. let panMatrix = new THREE.Matrix4(); //定义一个平移变化矩阵
  783. if (!lay.loaded) { //说明还没添加到场景中
  784. // scaleMatrix.makeScale(scaleX,1,scaleY); //获得缩放变化矩阵
  785. // panMatrix.makeTranslation(cube.position.x,0,cube.position.z); //获得平移变化矩阵
  786. // stratMatrix.multiply(scaleMatrix).premultiply(panMatrix);//通过矩阵计算获得最终的形变矩阵
  787. // instancedMesh.instanceMatrix.needsUpdate = true;//更新之前,必须开启开关
  788. // instancedMesh.setMatrixAt(curMeshIndex,stratMatrix);//更新几何体的世界矩阵
  789. // this.scene.add(instancedMesh);
  790. }else{//更新形变矩阵
  791. scaleMatrix.makeScale(scaleX, 1, scaleY); //获得缩放变化矩阵
  792. panMatrix.makeTranslation(cube.position.x - oldPosition.x,0,cube.position.z - oldPosition.z); //获得平移变化矩阵
  793. stratMatrix.multiply(scaleMatrix).premultiply(panMatrix);//通过矩阵计算获得最终的形变矩阵
  794. instancedMesh.instanceMatrix.needsUpdate = true;//更新之前,必须开启开关
  795. instancedMesh.setMatrixAt(curMeshIndex,stratMatrix);//更新几何体的世界矩阵
  796. }
  797. })
  798. lay.loaded = true;
  799. }
  800. },
  801. // 空间处理光源
  802. lightHandle(cube){
  803. let light = this.gltfLayouts.find(it=>it.uniId==cube.uniId);
  804. if(!light.loaded){
  805. // 从一个点向各个方向发射的光源。一个常见的例子是模拟一个灯泡发出的光。
  806. let pointLight = new THREE.PointLight(0xffd7b3, 1, 5, 1);
  807. pointLight.position.set(cube.position.x, 1.5, cube.position.z); //default; light shining from top
  808. this.scene.add(pointLight);
  809. let sphereSize = 0.1;
  810. let pointLightHelper = new THREE.PointLightHelper( pointLight, sphereSize );
  811. this.scene.add( pointLightHelper);
  812. // pointLight.castShadow = true; // default false
  813. // // 默认情况下光投影相机区域是一个长宽高为10x10x500的长方体区域,光源投射方向为通过坐标原点
  814. // pointLight.shadow.camera.left = -200; // default
  815. // pointLight.shadow.camera.right = 200; // default
  816. // pointLight.shadow.camera.top = 200; // default
  817. // pointLight.shadow.camera.bottom = -200; // default
  818. this.scene.add(pointLight);
  819. light.loaded = true;
  820. }else{
  821. }
  822. },
  823. // 空间布局旋转镜像重置模型约束
  824. resetModelParameters(curSpace, cubeInfo) {
  825. // 旋转
  826. const defaultLayout = curSpace.layouts.find(item => {
  827. return item.isDefault
  828. })
  829. if (!defaultLayout) {
  830. return cubeInfo
  831. }
  832. let count = 0;
  833. switch (parseInt(defaultLayout.layoutRotate)) {
  834. case 90:
  835. count = 1;
  836. break;
  837. case 180:
  838. count = 2;
  839. break;
  840. case -90:
  841. count = 3;
  842. break;
  843. default:
  844. break;
  845. }
  846. for (let index = 0; index < count; index++) {
  847. let oldCubeInfo = JSON.parse(JSON.stringify(cubeInfo))
  848. oldCubeInfo.isStepAsideLeft = "false"
  849. oldCubeInfo.isStepAsideTop = "false"
  850. oldCubeInfo.isStepAsideRight = "false"
  851. oldCubeInfo.isStepAsideBottom = "false"
  852. oldCubeInfo.referenceModelLeft = "0"
  853. oldCubeInfo.referenceModelTop = "0"
  854. oldCubeInfo.referenceModelRight = "0"
  855. oldCubeInfo.referenceModelBottom = "0"
  856. oldCubeInfo.rotation = "0"
  857. oldCubeInfo.marginLeft = "0";
  858. oldCubeInfo.marginTop = "0";
  859. oldCubeInfo.marginRight = "0";
  860. oldCubeInfo.marginBottom = "0";
  861. // 是否靠墙
  862. if (cubeInfo.isStepAsideLeft == 'true') {
  863. oldCubeInfo.isStepAsideTop = 'true'
  864. }
  865. if (cubeInfo.isStepAsideTop == 'true') {
  866. oldCubeInfo.isStepAsideRight = 'true'
  867. }
  868. if (cubeInfo.isStepAsideRight == 'true') {
  869. oldCubeInfo.isStepAsideBottom = 'true'
  870. }
  871. if (cubeInfo.isStepAsideBottom == 'true') {
  872. oldCubeInfo.isStepAsideLeft = 'true'
  873. }
  874. // 参照物体
  875. if (parseInt(cubeInfo.referenceModelLeft) > 0) {
  876. oldCubeInfo.referenceModelTop = cubeInfo.referenceModelLeft;
  877. }
  878. if (parseInt(cubeInfo.referenceModelTop) > 0) {
  879. oldCubeInfo.referenceModelRight = cubeInfo.referenceModelTop;
  880. }
  881. if (parseInt(cubeInfo.referenceModelRight) > 0) {
  882. oldCubeInfo.referenceModelBottom = cubeInfo.referenceModelRight;
  883. }
  884. if (parseInt(cubeInfo.referenceModelBottom) > 0) {
  885. oldCubeInfo.referenceModelLeft = cubeInfo.referenceModelBottom;
  886. }
  887. // 边距
  888. oldCubeInfo.marginLeft = cubeInfo.marginBottom;
  889. oldCubeInfo.marginTop = cubeInfo.marginLeft;
  890. oldCubeInfo.marginRight = cubeInfo.marginTop;
  891. oldCubeInfo.marginBottom = cubeInfo.marginRight;
  892. oldCubeInfo.modelWidth = cubeInfo.modelHeight;
  893. oldCubeInfo.modelHeight = cubeInfo.modelWidth;
  894. // 旋转
  895. if (parseInt(cubeInfo.rotation) == 0 || parseInt(cubeInfo.rotation) == 90) {
  896. oldCubeInfo.rotation = parseInt(cubeInfo.rotation) + 90;
  897. }
  898. if (parseInt(cubeInfo.rotation) == 180) {
  899. oldCubeInfo.rotation = "-90";
  900. }
  901. if (parseInt(cubeInfo.rotation) == -90) {
  902. oldCubeInfo.rotation = "0";
  903. }
  904. // console.log("旋转前的模型参数X", cubeInfo, oldCubeInfo, defaultLayout)
  905. cubeInfo = oldCubeInfo;
  906. // console.log("旋转后的模型参数X", cubeInfo, oldCubeInfo, defaultLayout)
  907. }
  908. return cubeInfo;
  909. },
  910. // 改变空间模型
  911. changeLayoutModel(){
  912. let changeSpaces = [];
  913. // 去重
  914. for (let index = 0; index < this.changeSpaces.length; index++) {
  915. const element = this.changeSpaces[index];
  916. const findIndex = changeSpaces.findIndex((item)=>{
  917. return item.spaceId == element.spaceId;
  918. })
  919. if(findIndex==-1){
  920. const findListIndex = this.spaceList.findIndex((item)=>{
  921. return item.spaceId == element.spaceId;
  922. })
  923. if(findListIndex !=-1){
  924. changeSpaces.push(this.spaceList[findListIndex])
  925. }
  926. }
  927. }
  928. console.log("所有变化的空间", changeSpaces)
  929. this.changeLayouts = [];
  930. let promise_list = [];
  931. for (let index = 0; index < changeSpaces.length; index++) {
  932. const element = changeSpaces[index];
  933. const layoutObj = this.arrFrunList.find((item)=>{
  934. return item.id == element.layoutId;
  935. })
  936. console.log("请求布局的对象", layoutObj, element);
  937. if(layoutObj){
  938. promise_list.push(
  939. new Promise((resolve, reject) => {
  940. this.loadlayoutByID(layoutObj.groupType, element, resolve);
  941. })
  942. )
  943. }
  944. }
  945. Promise.all(promise_list).then(()=>{
  946. this.$nextTick(()=>{
  947. console.log("新布局替换完成",this.changeLayouts, changeSpaces);
  948. this.furnHandle(this.changeLayouts, changeSpaces[0]);
  949. })
  950. })
  951. },
  952. async loadlayoutByID(groupType,spaceObj, resolve){
  953. let param = {
  954. "brandId": $config.brandId,
  955. "houseId": this.curHouseObj.houseId,
  956. "groupType": groupType
  957. }
  958. // 默认的布局
  959. let res = await requestConfig("findOverallArrangementList", param);
  960. if (!res.success || !res.pageModel || res.pageModel.resultSet.length == 0) {
  961. return resolve();
  962. }
  963. const groupLayouts = res.pageModel.resultSet;
  964. console.log("查询指定分组的布局", groupType, groupLayouts, spaceObj.spaceWidth, spaceObj.spaceHeight)
  965. // 根据空间的尺寸和布局的最小尺寸,筛选出合适的最大的布局
  966. const layouts01 = groupLayouts.filter((item)=>{
  967. return item.minWidth <= spaceObj.spaceWidth && item.minDepth <= spaceObj.spaceHeight
  968. })
  969. console.log("过滤适合的布局01", layouts01)
  970. // 长度和宽度都满足空间尺寸
  971. const layouts02 = layouts01.filter((item)=>{
  972. return (item.minWidth * item.minDepth ) <= (spaceObj.spaceWidth * spaceObj.spaceHeight)
  973. })
  974. console.log("过滤适合的布局02", layouts02)
  975. // 取布局面积最大的一个
  976. let layoutObj = null
  977. for (let index = 0; index < layouts02.length; index++) {
  978. const element = layouts02[index];
  979. if(!layoutObj){
  980. layoutObj = element;
  981. }else{
  982. if((layoutObj.minWidth * layoutObj.minDepth) < (element.minWidth * element.minDepth)){
  983. layoutObj = element;
  984. }
  985. }
  986. }
  987. console.log("找出最合适的布局", layoutObj)
  988. console.log("更新空间的布局ID=", layoutObj.id)
  989. // 合适的布局更现有布局相同
  990. if(spaceObj.layoutId == layoutObj.id){
  991. this.calculateLayoutModelSizeBySpace(spaceObj) // 提前计算模型的位置
  992. this.changeLayoutModelState(true); // 显示家具模型
  993. return resolve()
  994. }
  995. // 更新空间的布局id
  996. for (let index = 0; index < this.spaceList.length; index++) {
  997. const element = this.spaceList[index];
  998. if(element.spaceId == spaceObj.spaceId){
  999. this.deleteLayoutModel(element, layoutObj)
  1000. element.layoutId = layoutObj.id
  1001. console.log("替换空间的布局ID", spaceObj.spaceId, layoutObj.id);
  1002. }
  1003. }
  1004. this.changeLayouts.push(layoutObj)
  1005. resolve()
  1006. }
  1007. }
  1008. }