gasAssessmen.threejs.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import * as THREE from 'three';
  2. import UseThree from '@/utils/threejs/useThree';
  3. import GasAssessmen from './gasAssessmen.threejs.base';
  4. import GasUnit from './gasUnit.threejs.base';
  5. import { animateCamera, setModalCenter } from '/@/utils/threejs/util';
  6. import useEvent from '@/utils/threejs/useEvent';
  7. import gsap from 'gsap';
  8. import { Ref } from 'vue';
  9. // 模型对象、 文字对象
  10. let model, workFaceObj: GasAssessmen | undefined, gasUnit: GasUnit | undefined, group;
  11. let modalType: Ref<string>;
  12. let activeId: Ref<string>;
  13. const { mouseDownFn } = useEvent();
  14. // 鼠标点击、松开事件
  15. const mouseEvent = (event) => {
  16. if (event.button == 0 && group) {
  17. if (modalType?.value == 'workFace') {
  18. mouseDownFn(<UseThree>model, <THREE.Object3D>workFaceObj?.planeGroup, event, (intersects) => {
  19. workFaceObj?.mousedownModel(intersects).then(async (unitId: string) => {
  20. if (activeId) activeId.value = unitId;
  21. await setModelType('toUnit');
  22. });
  23. });
  24. } else if (modalType?.value == 'gasUnit') {
  25. mouseDownFn(<UseThree>model, <THREE.Object3D>gasUnit?.group, event, (intersects) => {
  26. gasUnit?.mousedownModel(intersects).then(async () => {
  27. await setModelType('toWorkFace');
  28. });
  29. });
  30. }
  31. }
  32. };
  33. const mouseUp = () => {
  34. if (!model) return;
  35. workFaceObj?.mouseUpModel.call(workFaceObj);
  36. };
  37. const addMouseEvent = () => {
  38. // 定义鼠标点击事件
  39. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  40. model.canvasContainer?.addEventListener('pointerup', mouseUp);
  41. };
  42. const render = () => {
  43. if (model && model.isRender) {
  44. model.animationId = requestAnimationFrame(render);
  45. model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  46. model.stats?.update();
  47. model.orbitControls?.update();
  48. model.camera?.updateMatrixWorld();
  49. workFaceObj?.render();
  50. }
  51. };
  52. // 切换风窗类型
  53. export const setModelType = (type, n = Math.ceil(Math.random() * 4)) => {
  54. return new Promise((resolve) => {
  55. const newCameraPosition = { x: -0.1077663653208413, y: 3.730662630250735, z: 5.174545297338427 };
  56. const newControlsPosition = { x: -0.0997084705839992, y: -0.050186843433472336, z: -0.3263852289773498 };
  57. if (type == 'toUnit' && workFaceObj && gasUnit) {
  58. if (modalType) modalType.value = 'gasUnit';
  59. gsap.to(workFaceObj.group.scale, {
  60. x: 30,
  61. y: 30,
  62. z: 30,
  63. duration: 1,
  64. ease: 'easeInCirc',
  65. onComplete: function () {
  66. gasUnit.group.scale.set(20, 20, 20);
  67. workFaceObj.group.visible = false;
  68. setModalCenter(gasUnit.group);
  69. gasUnit.group.visible = true;
  70. const newCameraPosition1 = { x: 0.07728832423767938, y: 2.000608719643587, z: 3.920565793165089 };
  71. const newControlsPosition1 = { x: 0.08291552616960282, y: -0.6396998462924676, z: 0.07906121096410117 };
  72. animateCamera(newCameraPosition, newControlsPosition, newCameraPosition1, newControlsPosition1, model, 0.8);
  73. gsap.to(gasUnit.group.scale, {
  74. x: 1,
  75. y: 1,
  76. z: 1,
  77. duration: 2,
  78. ease: 'easeOutCubic',
  79. onComplete: function () {},
  80. });
  81. },
  82. });
  83. } else if (type == 'toWorkFace') {
  84. // 由抽采单元切换到工作面
  85. gsap.to(gasUnit.group.scale, {
  86. x: 20,
  87. y: 20,
  88. z: 20,
  89. duration: 1,
  90. ease: 'easeInCirc',
  91. onComplete: function () {
  92. workFaceObj.group.scale.set(20, 20, 20);
  93. gasUnit.group.visible = false;
  94. setModalCenter(workFaceObj.group);
  95. workFaceObj.group.visible = true;
  96. const newCameraPosition1 = { x: 0.07728832423767938, y: 2.000608719643587, z: 3.920565793165089 };
  97. const newControlsPosition1 = { x: 0.08291552616960282, y: -0.6396998462924676, z: 0.07906121096410117 };
  98. animateCamera(newCameraPosition1, newControlsPosition1, newCameraPosition, newControlsPosition, model, 0.8);
  99. gsap.to(workFaceObj.group.scale, {
  100. x: 1,
  101. y: 1,
  102. z: 1,
  103. duration: 1,
  104. ease: 'easeOutCubic',
  105. onComplete: function () {
  106. if (modalType) modalType.value = 'workFace';
  107. },
  108. });
  109. },
  110. });
  111. } else {
  112. // 初始加载
  113. group = workFaceObj.group;
  114. workFaceObj?.setPlanes(n);
  115. showOrHideGasPlane(true);
  116. const oldControlsPosition = { x: -0.086221, y: -0.612538, z: 0.33468 };
  117. const oldCameraPosition = { x: 19.589025920044726, y: 7.437905524957071, z: 23.032636074396976 };
  118. if (model.scene.getObjectByName('workFace')) {
  119. model.camera.position.set(oldCameraPosition.x, oldCameraPosition.y, oldCameraPosition.z + 20);
  120. model.orbitControls.target.set(oldControlsPosition.x, oldControlsPosition.y, oldControlsPosition.z);
  121. }
  122. setTimeout(async () => {
  123. resolve(null);
  124. if (!model.scene.getObjectByName('workFace')) {
  125. model.scene.add(group);
  126. model.scene.add(gasUnit.group);
  127. }
  128. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  129. }, 600);
  130. }
  131. });
  132. };
  133. export const showOrHideGasPlane = (isShowPlane) => {
  134. if (workFaceObj) {
  135. if (isShowPlane) {
  136. workFaceObj.planeGroup.visible = true;
  137. } else {
  138. workFaceObj.planeGroup.visible = false;
  139. }
  140. }
  141. };
  142. export const mountedThree = (pageType: Ref<string>, activeUnitId: Ref<string>) => {
  143. modalType = pageType;
  144. activeId = activeUnitId;
  145. return new Promise(async (resolve) => {
  146. model = new UseThree('#workFace3D', '#workFace3DCSS');
  147. model.setEnvMap('test1');
  148. model.renderer.toneMappingExposure = 1.0;
  149. // model.renderer = 1;
  150. workFaceObj = new GasAssessmen(model);
  151. await workFaceObj.mountedThree();
  152. gasUnit = new GasUnit(model);
  153. await gasUnit.mountedThree();
  154. addMouseEvent();
  155. render();
  156. resolve(null);
  157. });
  158. };
  159. export const destroy = () => {
  160. if (model) {
  161. model.isRender = false;
  162. workFaceObj?.destroy();
  163. workFaceObj = undefined;
  164. model.destroy();
  165. model = null;
  166. group = null;
  167. }
  168. };