window.threejs.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import ddFc_5 from './dandaoFc.threejs'; // ddFc_5 单道-大窗2列扇叶
  4. import singleWindowXk from './dandaoFcXk.threejs';
  5. import ddFc_1 from './dandaoFcYjl.threejs'; // ddFc_1 单道_小窗两列扇叶
  6. import sdFc_1 from './shuangdaoFc.threejs'; // sdFc_1 双道-带小门
  7. import sdFc_3 from './shuangdaoFcBlt.threejs'; // sdFc_3 双道-带小门-小窗
  8. import sdFc_4 from './shuangdaoFcHj.threejs'; // sdFc_4 双道-带门-卷闸
  9. import sdFc_2 from './shuangdaoFcSw.threejs'; // sdFc_2 单道-带小门-2个窗
  10. import sdFc_5 from './shuangdaoFcYjl.threejs'; // sdFc_5 双道-小门(侧边下)-小窗(榆家梁)
  11. import ddFc_4 from './dandaoFcBd1.threejs'; // ddFc_2 单道-一个小窗两列扇叶
  12. import ddFc_2 from './dandaoFcBd2.threejs'; // ddFc_2 单道-一个大窗两列竖型扇叶
  13. import ddFc_6 from './dandaoFcHjt.threejs'; // ddFc_6 单道-大窗1列扇叶(活鸡兔)
  14. import ddFc_7 from './dandaoFcBd3.threejs'; // ddFc_6 单道-大窗1列扇叶(活鸡兔)
  15. import { animateCamera } from '/@/utils/threejs/util';
  16. import useEvent from '../../../../utils/threejs/useEvent';
  17. import { getDictItemsByCode } from '/@/utils/dict';
  18. // import * as dat from 'dat.gui';
  19. // const gui = new dat.GUI();
  20. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  21. // 模型对象、 文字对象
  22. let model: UseThree,
  23. ddFc5: ddFc_5,
  24. ddFc1: ddFc_1,
  25. sdFc1: sdFc_1,
  26. sdFc3: sdFc_3,
  27. sdFc4: sdFc_4,
  28. sdFc2: sdFc_2,
  29. sdFc5: sdFc_5,
  30. ddFc2: ddFc_2,
  31. ddFc4: ddFc_4,
  32. ddFc6: ddFc_6,
  33. ddFc7: ddFc_7,
  34. singleWindowXkObj: singleWindowXk,
  35. group: THREE.Object3D,
  36. windowType = 'ddFc1';
  37. const rotationParam = {
  38. frontDeg0: 0, // 前门初始
  39. frontDeg1: 0, // 前门目标
  40. backDeg0: 0, // 后门初始
  41. backDeg1: 0, // 后门目标
  42. };
  43. const { mouseDownFn } = useEvent();
  44. // 初始化左右摇摆动画
  45. const startAnimation = () => {
  46. // 定义鼠标点击事件
  47. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  48. model.canvasContainer?.addEventListener('pointerup', (event) => {
  49. event.stopPropagation();
  50. // 单道、 双道
  51. if (windowType === 'ddFc5' && ddFc5) {
  52. ddFc5.mouseUpModel.call(ddFc5);
  53. } else if (windowType === 'ddFc1' && ddFc1) {
  54. ddFc1.mouseUpModel.call(ddFc1);
  55. } else if (windowType === 'ddFc2' && ddFc2) {
  56. ddFc2.mouseUpModel.call(ddFc2);
  57. } else if (windowType === 'ddFc4' && ddFc4) {
  58. ddFc4.mouseUpModel.call(ddFc4);
  59. } else if (windowType === 'ddFc6' && ddFc6) {
  60. ddFc6.mouseUpModel.call(ddFc6);
  61. } else if (windowType === 'ddFc7' && ddFc7) {
  62. ddFc7.mouseUpModel.call(ddFc7);
  63. } else if (windowType === 'sdFc1' && sdFc1) {
  64. sdFc1.mouseUpModel.call(sdFc1);
  65. } else if (windowType === 'sdFc3' && sdFc3) {
  66. sdFc3.mouseUpModel.call(sdFc3);
  67. } else if (windowType === 'sdFc4' && sdFc4) {
  68. sdFc4.mouseUpModel.call(sdFc4);
  69. } else if (windowType === 'sdFc2' && sdFc2) {
  70. sdFc2.mouseUpModel.call(sdFc2);
  71. } else if (windowType === 'sdFc5' && sdFc5) {
  72. sdFc5.mouseUpModel.call(sdFc5);
  73. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  74. singleWindowXkObj.mouseUpModel.call(singleWindowXkObj);
  75. }
  76. });
  77. };
  78. // 鼠标点击、松开事件
  79. const mouseEvent = (event) => {
  80. if (event.button == 0) {
  81. mouseDownFn(model, group, event, (intersects) => {
  82. if (windowType === 'ddFc5' && ddFc5) {
  83. ddFc5.mousedownModel.call(ddFc5, intersects);
  84. } else if (windowType === 'ddFc1' && ddFc1) {
  85. ddFc1.mousedownModel.call(ddFc1, intersects);
  86. } else if (windowType === 'ddFc2' && ddFc2) {
  87. ddFc2.mousedownModel.call(ddFc2, intersects);
  88. } else if (windowType === 'ddFc4' && ddFc4) {
  89. ddFc4.mousedownModel.call(ddFc4, intersects);
  90. } else if (windowType === 'ddFc6' && ddFc6) {
  91. ddFc6.mousedownModel.call(ddFc6, intersects);
  92. } else if (windowType === 'ddFc7' && ddFc7) {
  93. ddFc7.mousedownModel.call(ddFc7, intersects);
  94. } else if (windowType === 'sdFc1' && sdFc1) {
  95. sdFc1.mousedownModel.call(sdFc1, intersects);
  96. } else if (windowType === 'sdFc3' && sdFc3) {
  97. sdFc3.mousedownModel.call(sdFc3, intersects);
  98. } else if (windowType === 'sdFc4' && sdFc4) {
  99. sdFc4.mousedownModel.call(sdFc4, intersects);
  100. } else if (windowType === 'sdFc2' && sdFc2) {
  101. sdFc2.mousedownModel.call(sdFc2, intersects);
  102. } else if (windowType === 'sdFc5' && sdFc5) {
  103. sdFc5.mousedownModel.call(sdFc5, intersects);
  104. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  105. singleWindowXkObj.mousedownModel.call(singleWindowXkObj, intersects);
  106. }
  107. });
  108. console.log('摄像头控制信息', model.orbitControls, model.camera);
  109. }
  110. };
  111. export const addMonitorText = (selectData) => {
  112. if (windowType === 'ddFc5' && ddFc5) {
  113. return ddFc5.addMonitorText.call(ddFc5, selectData);
  114. } else if (windowType === 'ddFc1' && ddFc1) {
  115. return ddFc1.addMonitorText.call(ddFc1, selectData);
  116. } else if (windowType === 'ddFc2' && ddFc2) {
  117. return ddFc2.addMonitorText.call(ddFc2, selectData);
  118. } else if (windowType === 'ddFc4' && ddFc4) {
  119. return ddFc4.addMonitorText.call(ddFc4, selectData);
  120. } else if (windowType === 'ddFc6' && ddFc6) {
  121. return ddFc6.addMonitorText.call(ddFc6, selectData);
  122. } else if (windowType === 'ddFc7' && ddFc7) {
  123. return ddFc7.addMonitorText.call(ddFc7, selectData);
  124. } else if (windowType === 'sdFc1' && sdFc1) {
  125. return sdFc1.addMonitorText.call(sdFc1, selectData);
  126. } else if (windowType === 'sdFc3' && sdFc3) {
  127. return sdFc3.addMonitorText.call(sdFc3, selectData);
  128. } else if (windowType === 'sdFc4' && sdFc4) {
  129. return sdFc4.addMonitorText.call(sdFc4, selectData);
  130. } else if (windowType === 'sdFc2' && sdFc2) {
  131. return sdFc2.addMonitorText.call(sdFc2, selectData);
  132. } else if (windowType === 'sdFc5' && sdFc5) {
  133. return sdFc5.addMonitorText.call(sdFc5, selectData);
  134. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  135. return singleWindowXkObj.addMonitorText.call(singleWindowXkObj, selectData);
  136. }
  137. };
  138. export function computePlay(data, maxarea, isFirst = false) {
  139. const isJz = data.windowModal == 'sdFc4';
  140. if (windowType === 'singleXkWindow') {
  141. const acosToAngle = (cosValue) => {
  142. cosValue = Math.max(Math.min(cosValue, 1), -1);
  143. // 计算角度
  144. return Math.asin(cosValue) * (180 / Math.PI);
  145. };
  146. const sina = Math.sqrt((Math.sin((78 * Math.PI) / 180) ** 2 * parseFloat(data.forntArea)) / parseFloat(maxarea));
  147. const angleInRadians = acosToAngle(sina);
  148. rotationParam.frontDeg1 = angleInRadians;
  149. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  150. // 当返回值有误时默认关闭
  151. play(rotationParam, 0);
  152. } else {
  153. setTimeout(() => {
  154. play(rotationParam, 1);
  155. }, 0);
  156. }
  157. } else {
  158. if (data.OpenDegree || data.OpenDegree1 || data.OpenDegree2) {
  159. maxarea = 180;
  160. if (data.OpenDegree) {
  161. rotationParam.frontDeg0 = (180 / maxarea) * Number(isFirst ? 0 : data.OpenDegree);
  162. rotationParam.frontDeg1 = (180 / maxarea) * Number(data.OpenDegree) || 0;
  163. }
  164. if (data.OpenDegree1) {
  165. rotationParam.frontDeg0 = (180 / maxarea) * Number(isFirst ? 0 : data.OpenDegree1);
  166. rotationParam.frontDeg1 = (180 / maxarea) * Number(data.OpenDegree1) || 0;
  167. }
  168. if (data.OpenDegree2) {
  169. rotationParam.backDeg0 = (180 / maxarea) * Number(isFirst ? 0 : data.OpenDegree2);
  170. rotationParam.backDeg1 = (180 / maxarea) * Number(data.OpenDegree2) || 0;
  171. }
  172. } else {
  173. // 这里判断是扇叶模型还是卷闸模型,如果是卷闸模型时
  174. if (isJz) {
  175. rotationParam.frontDeg0 = 0;
  176. rotationParam.backDeg0 = 0;
  177. rotationParam.frontDeg1 = data.forntArea / maxarea;
  178. rotationParam.backDeg1 = data.rearArea / maxarea;
  179. } else {
  180. if (!maxarea) maxarea = 90;
  181. rotationParam.frontDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.forntArea);
  182. rotationParam.backDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.rearArea);
  183. rotationParam.frontDeg1 = (90 / maxarea) * Number(data.forntArea) || 0;
  184. rotationParam.backDeg1 = (90 / maxarea) * Number(data.rearArea) || 0;
  185. }
  186. }
  187. if (isJz) {
  188. play(1, { scaleY: data.frontArea / maxarea });
  189. play(2, { scaleY: data.rearArea / maxarea });
  190. play(3, { scaleY: data.frontArea3 / maxarea });
  191. play(4, { scaleY: data.frontArea4 / maxarea });
  192. } else {
  193. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  194. // 当返回值有误时默认关闭
  195. play(rotationParam, 0);
  196. } else {
  197. if (data.nwindownum == 1 || data.nwindownum == 2) {
  198. setTimeout(() => {
  199. play(rotationParam, 1);
  200. }, 0);
  201. }
  202. if (data.nwindownum == 2) {
  203. setTimeout(() => {
  204. play(rotationParam, 2);
  205. }, 0);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. export const play = (rotationParam, flag) => {
  212. if (windowType === 'ddFc5' && ddFc5) {
  213. return ddFc5.play.call(ddFc5, rotationParam, flag);
  214. } else if (windowType === 'ddFc1' && ddFc1) {
  215. return ddFc1.play.call(ddFc1, rotationParam, flag);
  216. } else if (windowType === 'ddFc2' && ddFc2) {
  217. return ddFc2.play.call(ddFc2, rotationParam, flag);
  218. } else if (windowType === 'ddFc4' && ddFc4) {
  219. return ddFc4.play.call(ddFc4, rotationParam, flag);
  220. } else if (windowType === 'ddFc6' && ddFc6) {
  221. return ddFc6.play.call(ddFc6, rotationParam, flag);
  222. } else if (windowType === 'ddFc7' && ddFc7) {
  223. return ddFc7.play.call(ddFc7, rotationParam, flag);
  224. } else if (windowType === 'sdFc1' && sdFc1) {
  225. return sdFc1.play.call(sdFc1, rotationParam, flag);
  226. } else if (windowType === 'sdFc3' && sdFc3) {
  227. return sdFc3.play.call(sdFc3, rotationParam, flag);
  228. } else if (windowType === 'sdFc4' && sdFc4) {
  229. return sdFc4.play.call(sdFc4, rotationParam, flag);
  230. } else if (windowType === 'sdFc2' && sdFc2) {
  231. return sdFc2.play.call(sdFc2, rotationParam, flag);
  232. } else if (windowType === 'sdFc5' && sdFc5) {
  233. return sdFc5.play.call(sdFc5, rotationParam, flag);
  234. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  235. return singleWindowXkObj.play.call(singleWindowXkObj, rotationParam, flag);
  236. }
  237. };
  238. // 切换风窗类型
  239. export const setModelType = (type) => {
  240. // if (!model || !model.scene) return;
  241. windowType = type;
  242. const windowConfigurations = {
  243. sdFc1: {
  244. render: sdFc1 ? () => sdFc1.render() : null,
  245. group: sdFc1 ? sdFc1.group : null,
  246. newP: { x: 66.257, y: 57.539, z: 94.313 },
  247. newT: { x: 0, y: 0, z: 0 },
  248. },
  249. ddFc5: {
  250. render: ddFc5 ? () => ddFc5.render() : null,
  251. group: ddFc5 ? ddFc5.group : null,
  252. newP: { x: 66.257, y: 57.539, z: 94.313 },
  253. newT: { x: 0, y: 0, z: 0 },
  254. },
  255. ddFc6: {
  256. render: ddFc6 ? () => ddFc6.render() : null,
  257. group: ddFc6 ? ddFc6.group : null,
  258. newP: { x: 66.257, y: 57.539, z: 94.313 },
  259. newT: { x: 0, y: 0, z: 0 },
  260. },
  261. ddFc7: {
  262. render: ddFc7 ? () => ddFc7.render() : null,
  263. group: ddFc7 ? ddFc7.group : null,
  264. newP: { x: 66.257, y: 57.539, z: 94.313 },
  265. newT: { x: 0, y: 0, z: 0 },
  266. },
  267. ddFc1: {
  268. render: ddFc1 ? () => ddFc1.render() : null,
  269. group: ddFc1 ? ddFc1.group : null,
  270. newP: { x: 34.294433107431956, y: 19.284123769585108, z: 47.717286013509835 },
  271. newT: { x: 12.311816240141978, y: -5.691930035759495, z: -5.57302688985693 },
  272. },
  273. ddFc2: {
  274. render: ddFc2 ? () => ddFc2.render() : null,
  275. group: ddFc2 ? ddFc2.group : null,
  276. newP: { x: 66.257, y: 57.539, z: 94.313 },
  277. newT: { x: 0, y: 0, z: 0 },
  278. },
  279. ddFc4: {
  280. render: ddFc4 ? () => ddFc4.render() : null,
  281. group: ddFc4 ? ddFc4.group : null,
  282. newP: { x: 66.257, y: 57.539, z: 94.313 },
  283. newT: { x: 0, y: 0, z: 0 },
  284. },
  285. sdFc3: {
  286. render: sdFc3 ? () => sdFc3.render() : null,
  287. group: sdFc3 ? sdFc3.group : null,
  288. newP: { x: 66.257, y: 57.539, z: 94.313 },
  289. newT: { x: 0, y: 0, z: 0 },
  290. },
  291. sdFc4: {
  292. render: sdFc4 ? () => sdFc4.render() : null,
  293. group: sdFc4 ? sdFc4.group : null,
  294. newP: { x: 66.257, y: 57.539, z: 94.313 },
  295. newT: { x: 0, y: 0, z: 0 },
  296. },
  297. sdFc2: {
  298. render: sdFc2 ? () => sdFc2.render() : null,
  299. group: sdFc2 ? sdFc2.group : null,
  300. newP: { x: 66.257, y: 57.539, z: 94.313 },
  301. newT: { x: 0, y: 0, z: 0 },
  302. },
  303. sdFc5: {
  304. render: sdFc5 ? () => sdFc5.render() : null,
  305. group: sdFc5 ? sdFc5.group : null,
  306. newP: { x: 34.294433107431956, y: 19.284123769585108, z: 47.717286013509835 },
  307. newT: { x: 12.311816240141978, y: -5.691930035759495, z: -5.57302688985693 },
  308. },
  309. singleXkWindow: {
  310. render: singleWindowXkObj ? () => singleWindowXkObj.render() : null,
  311. group: singleWindowXkObj ? singleWindowXkObj.group : null,
  312. newP: { x: 116.08531358656566, y: 81.45510733175816, z: 193.00752046594465 },
  313. newT: { x: 23.446366480086372, y: -12.335134633777185, z: -5.63294282643405 },
  314. },
  315. };
  316. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  317. model.scene?.remove(group);
  318. function handleWindowType(windowType: string, model: any) {
  319. const config = windowConfigurations[windowType];
  320. if (config && config.group) {
  321. model.startAnimation = config.render;
  322. group = config.group;
  323. return new Promise((resolve) => {
  324. setTimeout(async () => {
  325. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, config.newP, config.newT, model);
  326. model.scene?.add(config.group);
  327. resolve(null);
  328. }, 1000);
  329. });
  330. } else {
  331. throw new Error(`Unsupported windowType: ${windowType}`);
  332. }
  333. }
  334. return handleWindowType(windowType, model);
  335. };
  336. export const mountedThree = () => {
  337. // const { sysOrgCode } = useGlobSetting();
  338. // const sysOrgCode = 'zmhjhzmy';
  339. return new Promise(async (resolve) => {
  340. model = new UseThree('#window3D');
  341. if (!model || !model.renderer || !model.camera) return;
  342. model.setEnvMap('royal_esplanade_1k.hdr');
  343. model.renderer.toneMappingExposure = 1.8;
  344. model.camera.position.set(100, 0, 1000);
  345. // 根据模型类型判断要初始化哪些模型
  346. const dictCodes = getDictItemsByCode('windowModalType');
  347. for (let i = 0; i < dictCodes.length; i++) {
  348. const dict = dictCodes[i];
  349. switch (dict.value) {
  350. case 'sdFc1':
  351. sdFc1 = new sdFc_1(model);
  352. await sdFc1.mountedThree();
  353. break;
  354. case 'ddFc5':
  355. ddFc5 = new ddFc_5(model);
  356. await ddFc5.mountedThree();
  357. break;
  358. case 'ddFc1':
  359. ddFc1 = new ddFc_1(model);
  360. await ddFc1.mountedThree();
  361. break;
  362. case 'ddFc2':
  363. ddFc2 = new ddFc_2(model);
  364. await ddFc2.mountedThree();
  365. break;
  366. case 'ddFc4':
  367. ddFc4 = new ddFc_4(model);
  368. await ddFc4.mountedThree();
  369. break;
  370. case 'ddFc6':
  371. ddFc6 = new ddFc_6(model);
  372. await ddFc6.mountedThree();
  373. break;
  374. case 'ddFc7':
  375. ddFc7 = new ddFc_7(model);
  376. await ddFc7.mountedThree();
  377. break;
  378. case 'sdFc3':
  379. sdFc3 = new sdFc_3(model);
  380. await sdFc3.mountedThree();
  381. break;
  382. case 'sdFc4':
  383. sdFc4 = new sdFc_4(model);
  384. await sdFc4.mountedThree();
  385. break;
  386. case 'sdFc2':
  387. sdFc2 = new sdFc_2(model);
  388. await sdFc2.mountedThree();
  389. break;
  390. case 'sdFc5':
  391. sdFc5 = new sdFc_5(model);
  392. await sdFc5.mountedThree();
  393. break;
  394. case 'singleXkWindow':
  395. singleWindowXkObj = new singleWindowXk(model);
  396. await singleWindowXkObj.mountedThree();
  397. break;
  398. }
  399. }
  400. model.animate();
  401. resolve(null);
  402. });
  403. };
  404. export const destroy = () => {
  405. if (model) {
  406. model.isRender = false;
  407. console.log('场景销毁前信息----------->', model.renderer?.info);
  408. model.isRender = false;
  409. if (ddFc5) ddFc5.destroy();
  410. if (ddFc1) ddFc1.destroy();
  411. if (ddFc2) ddFc2.destroy();
  412. if (ddFc4) ddFc4.destroy();
  413. if (ddFc6) ddFc6.destroy();
  414. if (ddFc7) ddFc7.destroy();
  415. if (sdFc1) sdFc1.destroy();
  416. if (sdFc3) sdFc3.destroy();
  417. if (sdFc4) sdFc4.destroy();
  418. if (sdFc2) sdFc2.destroy();
  419. if (sdFc5) sdFc5.destroy();
  420. singleWindowXkObj.destroy();
  421. model.destroy();
  422. model = null;
  423. group = null;
  424. singleWindowXkObj = null;
  425. ddFc5 = null;
  426. ddFc1 = null;
  427. ddFc2 = null;
  428. ddFc4 = null;
  429. ddFc6 = null;
  430. ddFc7 = null;
  431. sdFc1 = null;
  432. sdFc3 = null;
  433. sdFc4 = null;
  434. sdFc2 = null;
  435. sdFc5 = null;
  436. }
  437. };