window.threejs.ts 16 KB

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