nitrogen.threejs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import * as THREE from 'three';
  2. import { animateCamera, setModalCenter, updateAxisCenter } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import Nitrogen from './nitrogen.dishang.threejs';
  5. import { useAppStore } from '/@/store/modules/app';
  6. // import * as dat from 'dat.gui';
  7. // const gui = new dat.GUI();
  8. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  9. // 模型对象、 文字对象
  10. let model, //
  11. group,
  12. nitrogenObj,
  13. modalType = 'lmWindRect';
  14. const appStore = useAppStore();
  15. // 鼠标点击、松开事件
  16. const mouseEvent = (event) => {
  17. event.stopPropagation();
  18. const widthScale = appStore.getWidthScale;
  19. const heightScale = appStore.getHeightScale;
  20. // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
  21. model.mouse.x =
  22. ((-model.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (model.canvasContainer.clientWidth * widthScale)) * 2 - 1;
  23. model.mouse.y =
  24. -((-model.canvasContainer.getBoundingClientRect().top + event.clientY) / (model.canvasContainer.clientHeight * heightScale)) * 2 + 1;
  25. (model.rayCaster as THREE.Raycaster).setFromCamera(model.mouse, model.camera as THREE.Camera);
  26. // console.log('相机与控制器信息--->', model.camera, model.orbitControls);
  27. // updateAxisCenter(model, event);
  28. if (group) {
  29. const intersects = model.rayCaster?.intersectObjects(group.children, false) as THREE.Intersection[];
  30. if (intersects.length > 0) {
  31. // 单道、 双道
  32. if (modalType === 'nitrogen') {
  33. nitrogenObj.mousedownModel.call(nitrogenObj, intersects);
  34. }
  35. }
  36. console.log('99999------>', model.camera, model.orbitControls);
  37. }
  38. };
  39. /* 添加监控数据 */
  40. export const addText = (selectData) => {
  41. if (modalType === 'nitrogen') {
  42. return nitrogenObj.addText.call(nitrogenObj, selectData);
  43. }
  44. };
  45. export const play = () => {
  46. if (modalType === 'nitrogen') {
  47. return nitrogenObj.play.call(nitrogenObj);
  48. }
  49. };
  50. // 切换风窗类型
  51. export const setModelType = (type) => {
  52. modalType = type;
  53. model.camera.position.set(-1000, 100, 500);
  54. return new Promise((resolve) => {
  55. // 显示双道风窗
  56. if (modalType === 'nitrogen') {
  57. group = nitrogenObj.group;
  58. // setModalCenter(group);
  59. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  60. const oldControlsPosition = { x: -10, y: 10, z: 10 };
  61. model.scene.add(nitrogenObj.group);
  62. setTimeout(async () => {
  63. resolve(null);
  64. await animateCamera(
  65. oldCameraPosition,
  66. oldControlsPosition,
  67. // { x: -0.24658823766780538, y: 35.50352092191473, z: 83.90511756512278 },
  68. // { x: -0.24658823766780538, y: 35.50352092191473, z: 83.90511756512278 },
  69. // { x: 1.5582599568763913, y: -3.2828007511721147, z: 2.2606374587827234 },
  70. { x: 0.2528210526315788, y: 38.731704155124646, z: 66.23189916897506 },
  71. { x: 0, y: 0, z: 0 },
  72. model,
  73. 0.8
  74. );
  75. }, 300);
  76. }
  77. });
  78. };
  79. export const mountedThree = (nitrogenNum) => {
  80. return new Promise(async (resolve) => {
  81. model = new UseThree('#nitrogen3D', '#nitrogenCss3D');
  82. model.setEnvMap('test1');
  83. model.renderer.toneMappingExposure = 0.8;
  84. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  85. nitrogenObj = new Nitrogen(model);
  86. await nitrogenObj.mountedThree(nitrogenNum);
  87. setModelType('nitrogen');
  88. resolve(null);
  89. model.animate();
  90. });
  91. };
  92. export const destroy = () => {
  93. if (model) {
  94. nitrogenObj?.destroy();
  95. model.deleteModal();
  96. model = null;
  97. group = null;
  98. }
  99. };