wokeFace.threejs.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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('workFace-jin');
  18. const returnwind = group.getObjectByName('workFace-hui');
  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() * 4), 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.078, y: 2.524, z: 0.886 };
  68. var newControlsPosition = { x: -0.078, y: 0.063, z: 0.181 };
  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.078, y: 2.524, z: 0.886 };
  73. var newControlsPosition = { x: -0.078, y: 0.063, z: 0.181 };
  74. } else if (type == 'workFace3') {
  75. // 双进单会
  76. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  77. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  78. var newCameraPosition = { x: -0.078, y: 2.524, z: 0.886 };
  79. var newControlsPosition = { x: -0.078, y: 0.063, z: 0.181 };
  80. } else {
  81. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  82. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  83. var newCameraPosition = { x: -0.17182200678070425, y: 1.7188311320712673, z: 1.787394450495768 };
  84. var newControlsPosition = { x: -0.171622930063528, y: -0.1442344658741759, z: 0.032110784753260874 };
  85. }
  86. if (model.scene.getObjectByName('workFace')) {
  87. model.camera.position.set(oldCameraPosition.x, oldCameraPosition.y, oldCameraPosition.z + 20);
  88. model.orbitControls.target.set(oldControlsPosition.x, oldControlsPosition.y, oldControlsPosition.z);
  89. }
  90. setTimeout(async () => {
  91. resolve(null);
  92. if (!model.scene.getObjectByName('workFace')) {
  93. model.scene.add(workFaceObj?.group);
  94. }
  95. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  96. }, 600);
  97. }
  98. });
  99. };
  100. export const showOrHideGasPlane = (isShowPlane) => {
  101. if (workFaceObj) {
  102. if (isShowPlane) {
  103. workFaceObj.planeGroup.visible = true;
  104. } else {
  105. workFaceObj.planeGroup.visible = false;
  106. }
  107. }
  108. };
  109. export const mountedThree = () => {
  110. return new Promise(async (resolve) => {
  111. model = new UseThree('#workFace3D', '#workFace3DCSS');
  112. model.setEnvMap('test1');
  113. model.renderer.toneMappingExposure = 1.0;
  114. // model.renderer = 1;
  115. workFaceObj = new WorkFace(model);
  116. await workFaceObj.mountedThree();
  117. addMouseEvent();
  118. render();
  119. resolve(null);
  120. });
  121. };
  122. export const destroy = () => {
  123. if (model) {
  124. model.isRender = false;
  125. workFaceObj.destroy();
  126. workFaceObj = undefined;
  127. model.destroy();
  128. model = null;
  129. group = null;
  130. }
  131. };