main.worker.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import modalWorker from './loadGltf.worker?worker';
  2. import Dexie from 'dexie';
  3. import { useModelStore } from '/@/store/modules/threejs';
  4. import * as THREE from 'three';
  5. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  6. export function initModalWorker() {
  7. type model = {
  8. modelName: string;
  9. modalVal: any;
  10. version: string;
  11. };
  12. const modalUrlArr = [
  13. // 'fm/fm_2023-06-02.glb',
  14. // 'fm/Fm-door_2023-11-29.glb',
  15. // 'fm/Fm-wire_2023-11-29.glb',
  16. // 'fm/Fm-wire-qd_2023-11-28.glb',
  17. // 'fm/Fm-wall_2023-11-29.glb',
  18. // 'fm/Fm-wall-qd_2023-11-28.glb',
  19. // 'fm/fmThree_2023-07-25.glb',
  20. // 'fm/fmThreeBase_2023-07-25.glb',
  21. // 'fm/fmXr-door_2023-12-28.glb',
  22. // 'fm/fmXr-wire_2023-12-29.glb',
  23. // 'fm/fmXr-wall_2023-12-29.glb',
  24. // 'fm/quickObfurage-door_2023-06-02.glb',
  25. // 'fm/quickObfurage-wire_2023-06-02.glb',
  26. // 'fm/quickObfurage-wall_2023-06-02.glb',
  27. // 'fc/wall_2023-10-11.glb',
  28. // // 'fc/ddFc_2023-10-11.glb',
  29. // // 'fc/sdFc_2023-10-19.glb',
  30. // 'fc/ddFc_2023-06-02.glb',
  31. // 'fc/sdFc_2023-06-02.glb',
  32. // 'cf/lmcf_2023-06-02.glb',
  33. // 'cf/lmcfSide_2023-06-02.glb',
  34. // 'cf/zdcf_2023-06-02.glb',
  35. // 'cf/dscf_2023-06-02.glb',
  36. // 'cf/dsgd_2023-06-02.glb',
  37. // 'cf/ddcf_2023-12-09.glb',
  38. // 'cf/dsmove_2023-06-02.glb',
  39. // 'cf/fixedCf_2023-11-29.glb',
  40. 'jbfj/jbfj-hd_2025-01-09.glb',
  41. 'jbfj/jbfj-fm_2023-06-02.glb',
  42. 'jbfj/jbfj-fc_2023-06-02.glb',
  43. 'jbfj/jbfj-dual_2025-01-22.glb',
  44. 'ztfj/dj1_2023-06-02.glb',
  45. 'ztfj/dj2_2023-06-02.glb',
  46. 'ztfj/bg_2023-06-02.glb',
  47. 'ztfj/bg1_2023-06-02.glb',
  48. 'ztfj/dzp_2023-06-02.glb',
  49. 'ztfj/fbm_2023-06-02.glb',
  50. 'ztfj/ztfj-fc_2023-06-02.glb',
  51. 'ztfj/ztfj_2025-02-21.glb',
  52. 'ztfj/ztfj-xj_2025-02-22.glb',
  53. 'fire/laneway_2024-03-04.glb',
  54. 'fire/laneway-device_2024-03-19.glb',
  55. 'fire/chamber_2023-06-02.glb',
  56. // 'fire/workFace_2023-06-29.glb',
  57. // 'fire/tunFace_2025-01-08.glb',
  58. // 'fire/tunFace_2025-01-09.glb',
  59. 'fire/tunFace_2025-01-13.glb',
  60. 'fire/nitrogen_2023-06-02.glb',
  61. // 'fire/nitrogenUnderground_2023-09-15.glb',
  62. 'fire/nitrogenUnderground_2024-04-09.glb',
  63. 'fire/grout_2023-06-02.glb',
  64. 'fire/Bertai_2023-11-29.glb',
  65. 'fire/balancePress_2024-03-14.glb',
  66. 'yafeng/compressor_2023-07-10.glb',
  67. 'gas/gasPump_2024-03-04.glb',
  68. 'gas/gasPumpUnder_2023-10-05.glb',
  69. 'mb/obfurage_2024-03-19.glb',
  70. // 'workFace/workFace-base_2024-03-04.glb',
  71. // 'workFace/workFace-jin_2024-03-04.glb',
  72. // 'workFace/workFace-hui_2024-03-04.glb',
  73. 'workFace/workFace1-1_2024-04-09.glb',
  74. 'workFace/workFace2-1_2024-04-09.glb',
  75. 'workFace/workFace_2024-09-20.glb',
  76. 'workFace/workFace1_2024-12-06.glb',
  77. 'gas/gasUnit_2024-12-18.glb',
  78. ];
  79. const db: any = new Dexie('DB');
  80. window['CustomDB'] = db;
  81. const modelStore = useModelStore();
  82. db.version(1).stores({
  83. modal: '&modalName, modalVal, versionStr',
  84. });
  85. db.open();
  86. new Promise(async (resolve) => {
  87. const validateModelUrlArr: string[] = [];
  88. for (let i = 0; i < modalUrlArr.length; i++) {
  89. const url = modalUrlArr[i];
  90. const fileName = url.replace(/(.*\/)*([^.]+).*/gi, '$2');
  91. const modalName = fileName.slice(0, -11);
  92. const version = fileName.slice(-11);
  93. const modalArr = await db.modal.where('modalName').equals(modalName).toArray();
  94. if (modalArr.length < 1) {
  95. validateModelUrlArr.push(url);
  96. } else {
  97. // 判断该版本是否一致,不一致删除
  98. const data = modelStore.modelArr.get(modalName);
  99. const versionDB = modalArr[0].versionStr;
  100. if (versionDB == version) {
  101. // 版本一致
  102. if (!data) {
  103. const model: model = {
  104. modelName: modalName,
  105. modalVal: modalArr[0].modalVal,
  106. version: modalArr[0].versionStr,
  107. };
  108. // modelStore.setModel(model);
  109. }
  110. } else {
  111. // 版本不一致,删除模型,重新下载
  112. await db.modal.delete(modalName);
  113. validateModelUrlArr.push(url);
  114. }
  115. }
  116. }
  117. resolve(validateModelUrlArr);
  118. }).then((validateModelUrlArr: string[]) => {
  119. validateModelUrlArr.forEach((url) => {
  120. const worker = new modalWorker();
  121. worker.onmessage = async function (e) {
  122. const { data, message } = e.data;
  123. if (message === 'end') {
  124. if (data) {
  125. modelStore.setModel(data);
  126. }
  127. worker.terminate();
  128. }
  129. };
  130. worker.postMessage({ message: 'load', data: url });
  131. });
  132. });
  133. }
  134. export function initTHREE() {
  135. if (!window['$renderer']) {
  136. const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true }) as THREE.WebGLRenderer;
  137. renderer?.setPixelRatio(window.devicePixelRatio);
  138. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  139. // renderer.shadowMap.enabled = true;
  140. renderer.toneMappingExposure = 1;
  141. // renderer.outputColorSpace= THREE.SRGBColorSpace;
  142. renderer.setClearAlpha(0);
  143. //renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  144. //renderer.physicallyCorrectLights = true;
  145. window['$renderer'] = renderer;
  146. }
  147. if (!window['$camera']) {
  148. const camera = new THREE.PerspectiveCamera(50, document.body.clientWidth / document.body.clientHeight, 0.0000001, 1000);
  149. window['$camera'] = camera;
  150. }
  151. if (!window['$orbitControls']) {
  152. const orbitControls = new OrbitControls(window['$camera'] as THREE.Camera, window['$renderer']?.domElement) as OrbitControls;
  153. orbitControls.mouseButtons = {
  154. MIDDLE: THREE.MOUSE.PAN,
  155. RIGHT: THREE.MOUSE.ROTATE,
  156. };
  157. window['$orbitControls'] = orbitControls;
  158. }
  159. }