workFace.threejs.base.ts 14 KB

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