wokeFace.threejs.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import WorkFace from './workFace.threejs.base';
  4. import { animateCamera } from '/@/utils/threejs/util';
  5. import useEvent from '../../../../utils/threejs/useEvent';
  6. // 模型对象、 文字对象
  7. let model,
  8. workFaceObj: WorkFace | undefined,
  9. group,
  10. fiberType = 'workFace'; // workerFaceFiber
  11. const { mouseDownFn, mousemoveFn, mouseUpFn } = useEvent();
  12. // 鼠标点击、松开事件
  13. const mouseEvent = (event) => {
  14. if (event.button == 0 && group) {
  15. model.canvasContainer?.addEventListener('mousemove', mousemove);
  16. const groud = group.getObjectByName('groud');
  17. const intakewind = group.getObjectByName('intakewind01');
  18. const returnwind = group.getObjectByName('returnwind');
  19. mouseDownFn(model, [groud, intakewind, returnwind], event);
  20. console.log('摄像头控制信息', model.orbitControls, model.camera);
  21. }
  22. };
  23. const mouseUp = () => {
  24. if (!model) return;
  25. model.canvasContainer?.removeEventListener('mousemove', mousemove);
  26. mouseUpFn(model, 0.2);
  27. workFaceObj?.mouseUpModel.call(workFaceObj);
  28. };
  29. const mousemove = () => {
  30. mousemoveFn();
  31. };
  32. const addMouseEvent = () => {
  33. // 定义鼠标点击事件
  34. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  35. model.canvasContainer?.addEventListener('pointerup', mouseUp);
  36. };
  37. const render = () => {
  38. if (model && model.isRender) {
  39. model.animationId = requestAnimationFrame(render);
  40. model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  41. model.stats?.update();
  42. model.orbitControls?.update();
  43. model.camera?.updateMatrixWorld();
  44. workFaceObj?.render();
  45. }
  46. };
  47. export const setCss3D = () => {
  48. workFaceObj?.setCss3D();
  49. };
  50. export const clearCss3D = () => {
  51. workFaceObj?.clearCss3D();
  52. };
  53. // 切换风窗类型
  54. export const setModelType = (type, n = Math.ceil(Math.random() * 10), isShowPlane) => {
  55. fiberType = type;
  56. return new Promise((resolve) => {
  57. if (workFaceObj) {
  58. workFaceObj.clearPlanes();
  59. group = workFaceObj.group;
  60. workFaceObj.setModalType(type);
  61. workFaceObj?.setPlanes(n);
  62. showOrHideGasPlane(isShowPlane);
  63. if (type == 'workFace1') {
  64. // 判断模型类型
  65. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  66. var oldCameraPosition = { x: 0.055, y: 0.062, z: 0.117 };
  67. var newCameraPosition = { x: -0.17182200678070425, y: 1.7188311320712673, z: 1.787394450495768 };
  68. var newControlsPosition = { x: -0.171622930063528, y: -0.1442344658741759, z: 0.032110784753260874 };
  69. } else if (type == 'workFace2') {
  70. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  71. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  72. var newCameraPosition = { x: -0.0271, y: 0.9205, z: 0.1973 };
  73. var newControlsPosition = { x: -0.027, y: 0.1244, z: -0.0306 };
  74. } else if (type == 'workFace3') {
  75. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  76. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  77. var newCameraPosition = { x: -0.026, y: 0.927, z: 0.333 };
  78. var newControlsPosition = { x: -0.026, y: 0.089, z: 0.093 };
  79. } else {
  80. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  81. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  82. var newCameraPosition = { x: -0.17182200678070425, y: 1.7188311320712673, z: 1.787394450495768 };
  83. var newControlsPosition = { x: -0.171622930063528, y: -0.1442344658741759, z: 0.032110784753260874 };
  84. }
  85. if (model.scene.getObjectByName('workFace')) {
  86. model.camera.position.set(oldCameraPosition.x, oldCameraPosition.y, oldCameraPosition.z + 20);
  87. model.orbitControls.target.set(oldControlsPosition.x, oldControlsPosition.y, oldControlsPosition.z);
  88. }
  89. setTimeout(async () => {
  90. resolve(null);
  91. if (!model.scene.getObjectByName('workFace')) {
  92. model.scene.add(workFaceObj?.group);
  93. }
  94. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  95. }, 600);
  96. }
  97. });
  98. };
  99. export const showOrHideGasPlane = (isShowPlane) => {
  100. if (workFaceObj) {
  101. if (isShowPlane) {
  102. workFaceObj.planeGroup.visible = true;
  103. } else {
  104. workFaceObj.planeGroup.visible = false;
  105. }
  106. }
  107. };
  108. export const mountedThree = () => {
  109. return new Promise(async (resolve) => {
  110. model = new UseThree('#workFace3D', '#workFace3DCSS');
  111. model.setEnvMap('test1');
  112. model.renderer.toneMappingExposure = 1.0;
  113. // model.renderer = 1;
  114. workFaceObj = new WorkFace(model);
  115. await workFaceObj.mountedThree();
  116. addMouseEvent();
  117. render();
  118. resolve(null);
  119. });
  120. };
  121. export const destroy = () => {
  122. if (model) {
  123. model.isRender = false;
  124. workFaceObj.destroy();
  125. workFaceObj = undefined;
  126. model.destroy();
  127. model = null;
  128. group = null;
  129. }
  130. };