wokeFace.threejs.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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)) => {
  55. debugger;
  56. fiberType = type;
  57. return new Promise((resolve) => {
  58. if (workFaceObj) {
  59. workFaceObj.clearPlanes();
  60. group = workFaceObj.group;
  61. workFaceObj.setModalType(type);
  62. // 判断模型类型
  63. if (type == 'workFace1') {
  64. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  65. var oldCameraPosition = { x: 0.055, y: 0.062, z: 0.117 };
  66. var newCameraPosition = { x: -0.17182200678070425, y: 1.7188311320712673, z: 1.787394450495768 };
  67. var newControlsPosition = { x: -0.171622930063528, y: -0.1442344658741759, z: 0.032110784753260874 };
  68. } else if (type == 'workFace2') {
  69. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  70. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  71. var newCameraPosition = { x: -0.0271, y: 0.9205, z: 0.1973 };
  72. var newControlsPosition = { x: -0.027, y: 0.1244, z: -0.0306 };
  73. } else if (type == 'workFace3') {
  74. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  75. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  76. var newCameraPosition = { x: -0.026, y: 0.927, z: 0.333 };
  77. var newControlsPosition = { x: -0.026, y: 0.089, z: 0.093 };
  78. } else {
  79. var oldControlsPosition = { x: 0.055, y: 0.062, z: 0.117 };
  80. var oldCameraPosition = { x: 1.403, y: 3.354, z: 2.873 };
  81. var newCameraPosition = { x: -0.17182200678070425, y: 1.7188311320712673, z: 1.787394450495768 };
  82. var newControlsPosition = { x: -0.171622930063528, y: -0.1442344658741759, z: 0.032110784753260874 };
  83. }
  84. if (model.scene.getObjectByName('workFace')) {
  85. model.camera.position.set(oldCameraPosition.x, oldCameraPosition.y, oldCameraPosition.z+20);
  86. model.orbitControls.target.set(oldControlsPosition.x, oldControlsPosition.y, oldControlsPosition.z);
  87. }
  88. setTimeout(async () => {
  89. resolve(null);
  90. if (!model.scene.getObjectByName('workFace')) {
  91. model.scene.add(workFaceObj?.group);
  92. }
  93. workFaceObj?.setPlanes(n);
  94. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  95. }, 600);
  96. }
  97. });
  98. };
  99. export const mountedThree = () => {
  100. return new Promise(async (resolve) => {
  101. model = new UseThree('#workFace3D', '#workFace3DCSS');
  102. model.setEnvMap('test1');
  103. model.renderer.toneMappingExposure = 1.0;
  104. // model.renderer = 1;
  105. workFaceObj = new WorkFace(model);
  106. await workFaceObj.mountedThree();
  107. addMouseEvent();
  108. render();
  109. resolve(null);
  110. });
  111. };
  112. export const destroy = () => {
  113. if (model) {
  114. model.isRender = false;
  115. workFaceObj.destroy();
  116. workFaceObj = undefined;
  117. model.destroy();
  118. model = null;
  119. group = null;
  120. }
  121. };