1
0

workFace.threejs.base.ts 13 KB

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