workFace.threejs.base.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import * as THREE from 'three';
  2. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  3. import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
  4. import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
  5. import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
  6. import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader.js';
  7. import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';
  8. import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js';
  9. import { setModalCenter, setTag3D } from '/@/utils/threejs/util';
  10. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  11. import { gradientColors } from '/@/utils/threejs/util';
  12. // import * as dat from 'dat.gui';
  13. // const gui = new dat.GUI();
  14. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  15. class WorkFace {
  16. model;
  17. modelName = 'workFace';
  18. group: THREE.Object3D = new THREE.Object3D();
  19. planeGroup: THREE.Group = new THREE.Group();
  20. bloomComposer: EffectComposer | null = null;
  21. finalComposer: EffectComposer | null = null;
  22. outlinePass: OutlinePass | null = null;
  23. positions: THREE.Vector3[][] = [];
  24. bloomLayer = new THREE.Layers();
  25. darkMaterial = new THREE.MeshBasicMaterial({ color: 'black', transparent: true, side: THREE.DoubleSide });
  26. materials = {};
  27. glob = {
  28. ENTIRE_SCENE: 0,
  29. BLOOM_SCENE: 10,
  30. N: 100,
  31. };
  32. locationTexture: THREE.Texture | null = null;
  33. warningLocationTexture: THREE.Texture | null = null;
  34. errorLocationTexture: THREE.Texture | null = null;
  35. playerStartClickTime1 = new Date().getTime();
  36. playerStartClickTime2 = new Date().getTime();
  37. planeNum = 0;
  38. constructor(model) {
  39. this.model = model;
  40. this.group.name = this.modelName;
  41. }
  42. addLight() {
  43. // const _this = this;
  44. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  45. directionalLight.position.set(-196, 150, 258);
  46. this.group.add(directionalLight);
  47. directionalLight.target = this.group;
  48. // const pointLight = new THREE.PointLight(0xffffff, 1, 1000);
  49. // pointLight.position.set(12, 51, -27);
  50. // pointLight.shadow.bias = 0.05;
  51. // this.group.add(pointLight);
  52. // gui.add(directionalLight.position, 'x', -1000, 1000).onChange(() => {
  53. // _this.render();
  54. // });
  55. // gui.add(directionalLight.position, 'y', -1000, 1000).onChange(() => {
  56. // _this.render();
  57. // });
  58. // gui.add(directionalLight.position, 'z', -1000, 1000).onChange(() => {
  59. // _this.render();
  60. // });
  61. }
  62. render() {
  63. this.model.renderer?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  64. // const _this = this;
  65. // if (this.model && this.model.scene.getObjectByName(this.modelName)) {
  66. // this.group?.traverse((obj) => {
  67. // _this.darkenNonBloomed(obj);
  68. // });
  69. // this.bloomComposer?.render();
  70. // this.group?.traverse((obj) => {
  71. // _this.restoreMaterial(obj);
  72. // });
  73. // this.finalComposer?.render();
  74. // }
  75. }
  76. // 绘制抽采单元
  77. setPlanes = (n, colors = new Array(n).fill(new THREE.Color('rgb(100%, 0%, 0%)'))) => {
  78. colors = gradientColors('#00FF2C', '#FF0000', n, 2);
  79. this.planeNum = n;
  80. const lenScale = 0.77 / n;
  81. const planeGeo = new THREE.PlaneGeometry();
  82. planeGeo.applyMatrix4(new THREE.Matrix4().makeTranslation(-1, 0, 0));
  83. for (let i = 0; i < n; i++) {
  84. const material = new THREE.MeshBasicMaterial({ color: colors[i], transparent: true, opacity: 0.6, depthTest: false, depthWrite: false });
  85. const plane = new THREE.Mesh(planeGeo, material);
  86. plane.name = 'unit' + i;
  87. plane.rotation.x = -Math.PI / 2;
  88. plane.scale.set(lenScale - 0.001, 0.375, 1.0);
  89. plane.position.set(0.282 - lenScale * (i - 0.5), 0.015, 0.142);
  90. this.planeGroup.add(plane);
  91. }
  92. };
  93. // 清除抽采单元绘制面
  94. clearPlanes = () => {
  95. for (let i = 0; i < this.planeNum; i++) {
  96. const plane = this.planeGroup.getObjectByName(`unit${i}`);
  97. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  98. if (plane) this.planeGroup.remove(plane);
  99. if (label) this.planeGroup.remove(label);
  100. }
  101. };
  102. // 抽采单元内容显示
  103. setCss3D = () => {
  104. const obj = this.group.getObjectByName(`unitText`);
  105. if (!obj) {
  106. const element = document.getElementById(`gasUnitBox`) as HTMLElement;
  107. if (element) {
  108. const gasUnitCSS3D = new CSS3DObject(element);
  109. gasUnitCSS3D.name = `unitText`;
  110. gasUnitCSS3D.scale.set(0.0009, 0.0009, 0.0009);
  111. gasUnitCSS3D.position.set(-0.1, 0.11, 0.05);
  112. gasUnitCSS3D.lookAt(-0.1, 0.5, 1);
  113. this.group.add(gasUnitCSS3D);
  114. }
  115. }
  116. for (let i = 0; i < this.planeNum; i++) {
  117. const lenScale = 0.77 / this.planeNum;
  118. const label = setTag3D(`抽采单元${i + 1}`, 'gas_unit_text');
  119. label.scale.set(0.0018, 0.0018, 1); //根据相机渲染范围控制HTML 3D标签尺寸
  120. label.position.set(0.282 - lenScale * (i + 0.5), 0.015, 0.142);
  121. label.name = 'planeText' + i;
  122. this.planeGroup.add(label);
  123. }
  124. };
  125. // 显示或隐藏抽采单元显示内容
  126. changeCss3D = (isHide) => {
  127. for (let i = 0; i < this.planeNum; i++) {
  128. const obj = this.group.getObjectByName(`unitText${i}`);
  129. if (obj) {
  130. obj.visible = isHide;
  131. }
  132. }
  133. };
  134. // 清除抽采单元显示内容
  135. clearCss3D = () => {
  136. const obj = this.group.getObjectByName(`unitText`);
  137. if (obj) this.group.remove(obj);
  138. const element = document.getElementById(`gasUnitBox`) as HTMLElement;
  139. if (element) {
  140. element.remove();
  141. }
  142. for (let i = 0; i < this.planeNum; i++) {
  143. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  144. if (label) this.planeGroup.remove(label);
  145. }
  146. };
  147. setRenderPass = () => {
  148. this.bloomLayer.set(this.glob.BLOOM_SCENE);
  149. const params = {
  150. bloomStrength: 2.5,
  151. bloomThreshold: 0,
  152. bloomRadius: 0,
  153. };
  154. const renderScene = new RenderPass(this.model.scene, this.model.camera);
  155. const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
  156. bloomPass.strength = params.bloomStrength;
  157. bloomPass.radius = params.bloomRadius;
  158. bloomPass.threshold = params.bloomThreshold;
  159. this.bloomComposer = new EffectComposer(this.model.renderer);
  160. this.bloomComposer.renderToScreen = false;
  161. this.bloomComposer.addPass(renderScene);
  162. this.bloomComposer.addPass(bloomPass);
  163. const finalPass = new ShaderPass(
  164. new THREE.ShaderMaterial({
  165. uniforms: {
  166. baseTexture: { value: null },
  167. bloomTexture: { value: this.bloomComposer.renderTarget2.texture },
  168. },
  169. vertexShader: `
  170. varying vec2 vUv;
  171. void main() {
  172. vUv = uv;
  173. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  174. }`,
  175. fragmentShader: `uniform sampler2D baseTexture;
  176. uniform sampler2D bloomTexture;
  177. varying vec2 vUv;
  178. void main() {
  179. gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0, 1.0, 1.0, 0.0 ) * texture2D( bloomTexture, vUv ) );
  180. }`,
  181. defines: {},
  182. }),
  183. 'baseTexture'
  184. );
  185. // const gammaCorrection = new ShaderPass(GammaCorrectionShader);
  186. const outputPass = new OutputPass();
  187. finalPass.needsSwap = true;
  188. this.model.renderer.toneMapping = THREE.ReinhardToneMapping;
  189. this.finalComposer = new EffectComposer(this.model.renderer);
  190. this.finalComposer.addPass(renderScene);
  191. this.finalComposer.addPass(outputPass);
  192. this.finalComposer.addPass(finalPass);
  193. const effectFXAA = new ShaderPass(FXAAShader);
  194. effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight);
  195. this.finalComposer.addPass(effectFXAA);
  196. // this.outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), this.model.scene, this.model.camera);
  197. // this.finalComposer.addPass(this.outlinePass);
  198. };
  199. getPositions(num = 40) {
  200. const curve1 = new THREE.LineCurve3(new THREE.Vector3(-595.2, 0.046, -2.863), new THREE.Vector3(595.2, 0.046, -2.863)); // 前
  201. const curve2 = new THREE.LineCurve3(new THREE.Vector3(-595.065, 0.014, 1.696), new THREE.Vector3(595.048, 0.014, 1.696)); // 中
  202. const curve3 = new THREE.LineCurve3(new THREE.Vector3(0.0, 0.0, 190.611), new THREE.Vector3(0.0, 0.0, -190.611)); // 后‘
  203. const len1 = curve1.getLength();
  204. const len2 = curve2.getLength();
  205. const len3 = curve3.getLength();
  206. const unit = (len1 + len2 + len3) / num;
  207. const num1 = Math.floor(len1 / unit);
  208. const num2 = Math.floor(len2 / unit);
  209. const num3 = Math.floor(len3 / unit);
  210. const points1 = curve1.getPoints(num1);
  211. const points2 = curve2.getPoints(num2);
  212. const points3 = curve3.getPoints(num3);
  213. this.positions = [points1, points2, points3];
  214. }
  215. drawSpheres = () => {
  216. const _this = this;
  217. const pointLines = new THREE.Object3D();
  218. pointLines.name = 'pointLines';
  219. return new Promise((resolve) => {
  220. new THREE.TextureLoader().load('/model/img/texture-smoke.png', (texture) => {
  221. const material = new THREE.PointsMaterial({
  222. color: '#FFFFFF',
  223. size: 0.008,
  224. map: texture,
  225. opacity: 0.8,
  226. transparent: true, // 开启透明度
  227. });
  228. _this.positions.forEach((position, index) => {
  229. const geometry = new THREE.BufferGeometry();
  230. geometry.setFromPoints(position);
  231. const points = new THREE.Points(geometry, material);
  232. points.renderOrder = 0;
  233. index == 0 ? (points.name = 'line_q') : index == 1 ? (points.name = 'line_h') : (points.name = 'line_z');
  234. let opticalfiber;
  235. if (index == 0) {
  236. points.name = 'line_q';
  237. opticalfiber = this.group.getObjectByName('opticalfiber03');
  238. } else if (index == 1) {
  239. points.name = 'line_h';
  240. opticalfiber = this.group.getObjectByName('opticalfiber01');
  241. } else {
  242. points.name = 'line_z';
  243. opticalfiber = this.group.getObjectByName('opticalfiber02');
  244. }
  245. if (opticalfiber) points.applyMatrix4(opticalfiber.matrix);
  246. const box = new THREE.Box3();
  247. box.setFromObject(points);
  248. // points.geometry.boundingBox?.set(box);
  249. pointLines.add(points);
  250. points.layers.enable(_this.glob.BLOOM_SCENE);
  251. });
  252. this.group.add(pointLines);
  253. resolve(null);
  254. texture.dispose();
  255. });
  256. });
  257. };
  258. darkenNonBloomed(obj) {
  259. if (obj.isMesh && this.bloomLayer.test(obj.layers) === false) {
  260. const opacity = obj.material.opacity;
  261. this.materials[obj.uuid] = obj.material;
  262. obj.material = this.darkMaterial.clone();
  263. obj.material.opacity = opacity;
  264. }
  265. }
  266. restoreMaterial(obj) {
  267. if (this.materials[obj.uuid]) {
  268. obj.material = this.materials[obj.uuid];
  269. delete this.materials[obj.uuid];
  270. }
  271. }
  272. resetMesh() {
  273. // const opticalFiber = this.group.getObjectByName('opticalfiber');
  274. const optical = this.group?.getObjectByName('optical_fiber_02');
  275. const optical1 = this.group?.getObjectByName('optical_fiber_01');
  276. const optical2 = this.group?.getObjectByName('optical_fiber_03');
  277. if (optical && optical1 && optical2) {
  278. optical.renderOrder = 100;
  279. optical1.renderOrder = 100;
  280. optical2.renderOrder = 100;
  281. optical.material =
  282. optical1.material =
  283. optical2.material =
  284. new THREE.MeshStandardMaterial({
  285. color: 0x555555,
  286. side: THREE.DoubleSide,
  287. transparent: true,
  288. opacity: 0.45,
  289. });
  290. }
  291. }
  292. /* 点击 */
  293. mousedownModel(rayCaster: THREE.Raycaster) {
  294. const opticalFiber = this.group.getObjectByName('opticalfiber');
  295. if (opticalFiber) {
  296. const intersects = rayCaster?.intersectObjects([...opticalFiber.children]) as THREE.Intersection[];
  297. // 判断是否点击到视频
  298. intersects.find((intersect) => {
  299. const mesh = intersect.object;
  300. if (mesh.name.startsWith('optical_fiber_')) {
  301. // outlinePass?.selectedObjects.push(mesh);
  302. return true;
  303. }
  304. return false;
  305. });
  306. }
  307. this.render();
  308. }
  309. mouseUpModel() {
  310. //
  311. }
  312. setModalType(modalType) {
  313. const intakeWind = this.group.getObjectByName('intakewind01');
  314. const returnWind = this.group.getObjectByName('returnwind');
  315. if (intakeWind && returnWind) {
  316. if (modalType === 'workFace1') {
  317. // 单进单回
  318. intakeWind.visible = false;
  319. returnWind.visible = false;
  320. } else if (modalType === 'workFace2') {
  321. // 单进双回
  322. intakeWind.visible = false;
  323. returnWind.visible = true;
  324. } else if (modalType === 'workFace3') {
  325. // 双进单回
  326. intakeWind.visible = true;
  327. returnWind.visible = false;
  328. } else if (modalType === 'workFace4') {
  329. // 双进双回
  330. intakeWind.visible = true;
  331. returnWind.visible = true;
  332. }
  333. setModalCenter(this.group);
  334. }
  335. }
  336. mountedThree() {
  337. return new Promise(async (resolve) => {
  338. this.model.renderer.sortObjects = true;
  339. // this.model.camera.position.set(0, 3.1, 500);
  340. // this.setRenderPass();
  341. this.model.orbitControls.update();
  342. this.model.setGLTFModel(['workFace-base', 'workFace-jin', 'workFace-hui'], this.group).then(async () => {
  343. this.group.name = this.modelName;
  344. this.group.add(this.planeGroup);
  345. this.planeGroup.visible = false;
  346. // this.group.position.set(-0.06, 0.28, 0.07);
  347. this.group.scale.set(2.5, 2.5, 2.5);
  348. // this.resetMesh();
  349. this.getPositions(this.glob.N);
  350. this.addLight();
  351. // this.drawSpheres();
  352. resolve(null);
  353. });
  354. });
  355. }
  356. destroy() {
  357. this.model.clearGroup(this.group);
  358. this.model = null;
  359. this.group = null;
  360. this.bloomComposer?.dispose();
  361. this.finalComposer?.dispose();
  362. }
  363. }
  364. export default WorkFace;