chamber.threejs.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import ChamberBase from './chamber.threejs.base';
  4. import { animateCamera } from '/@/utils/threejs/util';
  5. import { useAppStore } from '/@/store/modules/app';
  6. // 模型对象、 文字对象
  7. let model,
  8. chamberBaseObj: ChamberBase,
  9. group,
  10. chamberType = 'chamberBase'; // workerFaceFiber
  11. const appStore = useAppStore();
  12. // 鼠标点击事件
  13. const mouseEvent = (event) => {
  14. event.stopPropagation();
  15. if (!model) return;
  16. const widthScale = appStore.getWidthScale;
  17. const heightScale = appStore.getHeightScale;
  18. // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
  19. model.mouse.x =
  20. ((-model.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (model.canvasContainer.clientWidth * widthScale)) * 2 - 1;
  21. model.mouse.y =
  22. -((-model.canvasContainer.getBoundingClientRect().top + event.clientY) / (model.canvasContainer.clientHeight * heightScale)) * 2 + 1;
  23. (model.rayCaster as THREE.Raycaster).setFromCamera(model.mouse, model.camera as THREE.Camera);
  24. if (group) {
  25. if (chamberType === 'chamberBase') {
  26. // chamberBaseObj.mousedownModel.call(chamberBaseObj, model.rayCaster);
  27. }
  28. // const intersects = model.rayCaster?.intersectObjects([...group.children]) as THREE.Intersection[];
  29. // if (intersects.length > 0) {
  30. // if (fiberType === 'beltFiber') {
  31. // beltFiberObj.mousedownModel.call(beltFiberObj, intersects);
  32. // } else if (fiberType === 'workFace') {
  33. // workerFaceFiberObj.mousedownModel.call(workerFaceFiberObj, intersects);
  34. // }
  35. // }
  36. console.log('摄像头,控制器------>', model.camera, model.orbitControls);
  37. }
  38. };
  39. const addMouseEvent = () => {
  40. // 定义鼠标点击事件
  41. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  42. };
  43. const render = () => {
  44. if (model.animationId != -1) {
  45. model.animationId = requestAnimationFrame(render);
  46. model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  47. model.stats?.update();
  48. }
  49. };
  50. export const addChamberText = (selectData) => {
  51. if (chamberType === 'chamberBase') {
  52. return chamberBaseObj.addChamberText.call(chamberBaseObj, selectData);
  53. }
  54. };
  55. // 切换模型类型
  56. export const setModelType = (type) => {
  57. chamberType = type;
  58. return new Promise((resolve) => {
  59. if (chamberType === 'chamberBase') {
  60. group = chamberBaseObj.group;
  61. const oldCameraPosition = { x: 124.736, y: 63.486, z: 103.337 };
  62. model.scene.add(chamberBaseObj.group);
  63. model.camera.position.set(0, 0, 300);
  64. setTimeout(async () => {
  65. // const position = { x: 0, y: 3.8, z: 10.5 };
  66. await animateCamera(
  67. oldCameraPosition,
  68. oldCameraPosition,
  69. { x: 0.519594036828229, y: 411.8877286329627, z: 330.7728952161751 },
  70. { x: 3.534969685817257, y: 14.506061950306899, z: -16.565361577001262 },
  71. model,
  72. 0.8
  73. );
  74. }, 300);
  75. resolve(null);
  76. }
  77. });
  78. };
  79. export const mountedThree = () => {
  80. return new Promise(async (resolve) => {
  81. model = new UseThree('#chamber3D');
  82. model.setEnvMap('test1');
  83. model.renderer.toneMappingExposure = 1;
  84. // model.camera.position.set(100, 0, 1000);
  85. chamberBaseObj = new ChamberBase(model);
  86. await chamberBaseObj.mountedThree();
  87. // model.scene.add(chamberBaseObj.group);
  88. addMouseEvent();
  89. // render();
  90. model.animate();
  91. resolve(null);
  92. });
  93. };
  94. export const destroy = () => {
  95. if (model) {
  96. chamberBaseObj.destroy();
  97. model.deleteModal();
  98. model = null;
  99. group = null;
  100. chamberBaseObj = null;
  101. }
  102. };