workFace.threejs.base.ts 13 KB

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