gasAssessmen.threejs.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. export const setCss3D = (unitList: any[]) => {
  53. workFaceObj?.setCss3D(unitList);
  54. };
  55. export const clearCss3D = () => {
  56. workFaceObj?.clearCss3D();
  57. };
  58. export const setPlanes = (unitList) => {
  59. workFaceObj?.setPlanes(unitList);
  60. };
  61. // 切换风窗类型
  62. export const setModelType = (type) => {
  63. debugger;
  64. return new Promise((resolve) => {
  65. const newCameraPosition = { x: -0.1077663653208413, y: 3.730662630250735, z: 5.174545297338427 };
  66. const newControlsPosition = { x: -0.0997084705839992, y: -0.050186843433472336, z: -0.3263852289773498 };
  67. if (type == 'toUnit' && workFaceObj && gasUnit) {
  68. if (modalType) modalType.value = 'gasUnit';
  69. gsap.to(workFaceObj.group.scale, {
  70. x: 30,
  71. y: 30,
  72. z: 30,
  73. duration: 1,
  74. ease: 'easeInCirc',
  75. onComplete: function () {
  76. gasUnit.group.scale.set(20, 20, 20);
  77. workFaceObj.group.visible = false;
  78. setModalCenter(gasUnit.group);
  79. gasUnit.group.visible = true;
  80. const newCameraPosition1 = { x: 0.07728832423767938, y: 2.000608719643587, z: 3.920565793165089 };
  81. const newControlsPosition1 = { x: 0.08291552616960282, y: -0.6396998462924676, z: 0.07906121096410117 };
  82. animateCamera(newCameraPosition, newControlsPosition, newCameraPosition1, newControlsPosition1, model, 0.8);
  83. gsap.to(gasUnit.group.scale, {
  84. x: 1,
  85. y: 1,
  86. z: 1,
  87. duration: 2,
  88. ease: 'easeOutCubic',
  89. onComplete: function () {},
  90. });
  91. },
  92. });
  93. } else if (type == 'toWorkFace') {
  94. // 由抽采单元切换到工作面
  95. gsap.to(gasUnit.group.scale, {
  96. x: 20,
  97. y: 20,
  98. z: 20,
  99. duration: 1,
  100. ease: 'easeInCirc',
  101. onComplete: function () {
  102. workFaceObj.group.scale.set(20, 20, 20);
  103. gasUnit.group.visible = false;
  104. setModalCenter(workFaceObj.group);
  105. workFaceObj.group.visible = true;
  106. const newCameraPosition1 = { x: 0.07728832423767938, y: 2.000608719643587, z: 3.920565793165089 };
  107. const newControlsPosition1 = { x: 0.08291552616960282, y: -0.6396998462924676, z: 0.07906121096410117 };
  108. animateCamera(newCameraPosition1, newControlsPosition1, newCameraPosition, newControlsPosition, model, 0.8);
  109. gsap.to(workFaceObj.group.scale, {
  110. x: 1,
  111. y: 1,
  112. z: 1,
  113. duration: 1,
  114. ease: 'easeOutCubic',
  115. onComplete: function () {
  116. if (modalType) modalType.value = 'workFace';
  117. },
  118. });
  119. },
  120. });
  121. } else {
  122. // 初始加载
  123. group = workFaceObj.group;
  124. showOrHideGasPlane(true);
  125. const oldControlsPosition = { x: -0.086221, y: -0.612538, z: 0.33468 };
  126. const oldCameraPosition = { x: 19.589025920044726, y: 7.437905524957071, z: 23.032636074396976 };
  127. if (model.scene.getObjectByName('workFace')) {
  128. model.camera.position.set(oldCameraPosition.x, oldCameraPosition.y, oldCameraPosition.z + 20);
  129. model.orbitControls.target.set(oldControlsPosition.x, oldControlsPosition.y, oldControlsPosition.z);
  130. }
  131. setTimeout(async () => {
  132. resolve(null);
  133. if (!model.scene.getObjectByName('workFace')) {
  134. model.scene.add(group);
  135. model.scene.add(gasUnit.group);
  136. }
  137. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  138. }, 600);
  139. }
  140. });
  141. };
  142. export const showOrHideGasPlane = (isShowPlane) => {
  143. if (workFaceObj) {
  144. if (isShowPlane) {
  145. workFaceObj.planeGroup.visible = true;
  146. } else {
  147. workFaceObj.planeGroup.visible = false;
  148. }
  149. }
  150. };
  151. export const mountedThree = (pageType: Ref<string>, activeUnitId: Ref<string>) => {
  152. modalType = pageType;
  153. activeId = activeUnitId;
  154. return new Promise(async (resolve) => {
  155. model = new UseThree('#workFace3D', '#workFace3DCSS');
  156. model.setEnvMap('test1.hdr');
  157. model.renderer.toneMappingExposure = 1.0;
  158. // model.renderer = 1;
  159. workFaceObj = new GasAssessmen(model);
  160. await workFaceObj.mountedThree();
  161. gasUnit = new GasUnit(model);
  162. await gasUnit.mountedThree();
  163. addMouseEvent();
  164. render();
  165. resolve(null);
  166. });
  167. };
  168. export const destroy = () => {
  169. if (model) {
  170. model.isRender = false;
  171. workFaceObj?.destroy();
  172. workFaceObj = undefined;
  173. model.destroy();
  174. model = null;
  175. group = null;
  176. }
  177. };