fiber.workFace.threejs.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 { GammaCorrectionShader } from 'three/examples/jsm/shaders/GammaCorrectionShader.js';
  9. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  10. // import * as dat from 'dat.gui';
  11. // const gui = new dat.GUI();
  12. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  13. class WorkFace {
  14. model;
  15. modelName = 'workFace';
  16. group: THREE.Object3D = new THREE.Object3D();
  17. bloomComposer: EffectComposer | null = null;
  18. finalComposer: EffectComposer | null = null;
  19. outlinePass: OutlinePass | null = null;
  20. positions: THREE.Vector3[][] = [];
  21. msgPositions = [];
  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: 1,
  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. constructor(model) {
  36. this.model = model;
  37. this.group.name = this.modelName;
  38. }
  39. addLight() {
  40. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  41. directionalLight.position.set(-0.8, 23, 3.9);
  42. this.group.add(directionalLight);
  43. directionalLight.target = this.group;
  44. // gui.add(directionalLight.position, 'x', -10, 20).onChange(function (value) {
  45. // directionalLight.position.x = Number(value);
  46. // _this.render();
  47. // });
  48. // gui.add(directionalLight.position, 'y', -50, 50).onChange(function (value) {
  49. // directionalLight.position.y = Number(value);
  50. // _this.render();
  51. // });
  52. // gui.add(directionalLight.position, 'z', -20, 20).onChange(function (value) {
  53. // directionalLight.position.z = Number(value);
  54. // _this.render();
  55. // });
  56. // const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 120);
  57. // pointLight5.position.set(-54, 30, 23.8);
  58. // pointLight5.shadow.bias = 0.05;
  59. // this.group.add(pointLight5);
  60. // const pointLight7 = new THREE.PointLight(0xffffff, 1, 1000);
  61. // pointLight7.position.set(45, 51, -4.1);
  62. // pointLight7.shadow.bias = 0.05;
  63. // this.model.scene.add(pointLight7);
  64. const spotLight = new THREE.SpotLight();
  65. spotLight.angle = Math.PI / 2;
  66. spotLight.penumbra = 0;
  67. spotLight.castShadow = true;
  68. spotLight.intensity = 1;
  69. spotLight.shadow.camera.near = 0.5; // default
  70. spotLight.shadow.focus = 1.2;
  71. spotLight.shadow.bias = -0.000002;
  72. spotLight.position.set(-7.19, 199, -68.1);
  73. // this.group.add(spotLight);
  74. // gui.add(directionalLight.position, 'x', -10, 20).onChange(function (value) {
  75. // directionalLight.position.x = Number(value);
  76. // _this.render();
  77. // });
  78. // gui.add(directionalLight.position, 'y', -50, 50).onChange(function (value) {
  79. // directionalLight.position.y = Number(value);
  80. // _this.render();
  81. // });
  82. // gui.add(directionalLight.position, 'z', -20, 20).onChange(function (value) {
  83. // directionalLight.position.z = Number(value);
  84. // _this.render();
  85. // });
  86. // gui.add(spotLight.position, 'x', -600, 600).onChange(function (value) {
  87. // spotLight.position.x = Number(value);
  88. // _this.render();
  89. // });
  90. // gui.add(spotLight.position, 'y', -600, 800).onChange(function (value) {
  91. // spotLight.position.y = Number(value);
  92. // _this.render();
  93. // });
  94. // gui.add(spotLight.position, 'z', -500, 1000).onChange(function (value) {
  95. // spotLight.position.z = Number(value);
  96. // _this.render();
  97. // });
  98. }
  99. // 设置模型位置
  100. setModalPosition() {
  101. this.group?.scale.set(22, 22, 22);
  102. this.group?.position.set(-15, 25, 15);
  103. }
  104. monitorData(warningNum = [3, 6], errorNum = [9]) {
  105. if (this.group) {
  106. if (warningNum.length > 0)
  107. warningNum.forEach((i) => {
  108. const warningMesh = this.group.getObjectByName('location' + i);
  109. if (warningMesh) {
  110. const material = (warningMesh as THREE.Mesh).material as THREE.MeshStandardMaterial;
  111. material.map = this.warningLocationTexture;
  112. material.emissiveMap = this.warningLocationTexture;
  113. }
  114. });
  115. if (errorNum.length > 0)
  116. errorNum.forEach((i) => {
  117. const warningMesh = this.group.getObjectByName('location' + i);
  118. if (warningMesh) {
  119. const material = (warningMesh as THREE.Mesh).material as THREE.MeshStandardMaterial;
  120. material.map = this.errorLocationTexture;
  121. material.emissiveMap = this.errorLocationTexture;
  122. }
  123. });
  124. }
  125. }
  126. addMonitorText(selectData) {
  127. if (!this.group) {
  128. return;
  129. }
  130. const locationMaterial = new THREE.MeshStandardMaterial({
  131. map: this.locationTexture,
  132. emissiveMap: this.locationTexture,
  133. transparent: true,
  134. side: THREE.DoubleSide,
  135. });
  136. // const locationX = [-8.406, -6.597, -4.787, -2.967, -1.118, 0.692, 2.501, 4.411, 6.211, 8.0];
  137. for (let i = 1; i <= this.glob.N; i++) {
  138. if (!this.group.getObjectByName('fiberMeg' + i)) {
  139. const position = this.msgPositions[i];
  140. const element = document.getElementById('fiberMeg' + i) as HTMLElement;
  141. if (element) {
  142. const mainCSS3D = new CSS3DObject(element);
  143. mainCSS3D.name = 'fiberMeg' + i;
  144. mainCSS3D.scale.set(0.008, 0.008, 0.008);
  145. mainCSS3D.position.set(position.x, -97.8, position.z);
  146. const lookAtPosition = this.model.camera.position;
  147. mainCSS3D.lookAt(lookAtPosition.x, lookAtPosition.y, lookAtPosition.z);
  148. this.group.add(mainCSS3D);
  149. }
  150. // const geometry = new THREE.PlaneGeometry(1, 0.9);
  151. // const location = new THREE.Mesh(geometry, locationMaterial);
  152. // location.scale.set(0.3, 0.3, 0.3);
  153. // location.name = 'location' + i;
  154. // location.position.set(locationX[i - 1], 1.384, position.z);
  155. // location.layers.enable(this.glob.ENTIRE_SCENE);
  156. // this.group.add(location);
  157. }
  158. }
  159. }
  160. render() {
  161. const _this = this;
  162. if (this.model && this.model.scene.getObjectByName(this.modelName)) {
  163. this.group?.traverse((obj) => {
  164. _this.darkenNonBloomed(obj);
  165. });
  166. this.bloomComposer?.render();
  167. this.group?.traverse((obj) => {
  168. _this.restoreMaterial(obj);
  169. });
  170. this.finalComposer?.render();
  171. this.model.css3dRender?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  172. }
  173. }
  174. setRenderPass = () => {
  175. this.bloomLayer.set(this.glob.BLOOM_SCENE);
  176. const params = {
  177. bloomStrength: 2.5,
  178. bloomThreshold: 0,
  179. bloomRadius: 0,
  180. };
  181. const renderScene = new RenderPass(this.model.scene, this.model.camera);
  182. const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
  183. bloomPass.strength = params.bloomStrength;
  184. bloomPass.radius = params.bloomRadius;
  185. bloomPass.threshold = params.bloomThreshold;
  186. this.bloomComposer = new EffectComposer(this.model.renderer);
  187. this.bloomComposer.renderToScreen = false;
  188. this.bloomComposer.addPass(renderScene);
  189. this.bloomComposer.addPass(bloomPass);
  190. const finalPass = new ShaderPass(
  191. new THREE.ShaderMaterial({
  192. uniforms: {
  193. baseTexture: { value: null },
  194. bloomTexture: { value: this.bloomComposer.renderTarget2.texture },
  195. },
  196. vertexShader: `
  197. varying vec2 vUv;
  198. void main() {
  199. vUv = uv;
  200. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  201. }`,
  202. fragmentShader: `uniform sampler2D baseTexture;
  203. uniform sampler2D bloomTexture;
  204. varying vec2 vUv;
  205. void main() {
  206. gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0, 1.0, 1.0, 0.0 ) * texture2D( bloomTexture, vUv ) );
  207. }`,
  208. defines: {},
  209. }),
  210. 'baseTexture'
  211. );
  212. const gammaCorrection = new ShaderPass(GammaCorrectionShader);
  213. finalPass.needsSwap = true;
  214. this.finalComposer = new EffectComposer(this.model.renderer);
  215. this.finalComposer.addPass(renderScene);
  216. this.finalComposer.addPass(gammaCorrection);
  217. this.finalComposer.addPass(finalPass);
  218. const effectFXAA = new ShaderPass(FXAAShader);
  219. effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight);
  220. this.finalComposer.addPass(effectFXAA);
  221. // this.outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), this.model.scene, this.model.camera);
  222. // this.finalComposer.addPass(this.outlinePass);
  223. };
  224. getPositions(num = 40) {
  225. const curve1 = new THREE.LineCurve3(new THREE.Vector3(-0.818, 0.002, 0.363), new THREE.Vector3(0.722, 0.002, 0.363)); // 前
  226. const curve2 = new THREE.LineCurve3(new THREE.Vector3(-0.818, 0.002, -0.459), new THREE.Vector3(0.718, -0.002, -0.459)); // 中
  227. const curve3 = new THREE.LineCurve3(new THREE.Vector3(0.345, 0.008, 0.329), new THREE.Vector3(0.345, 0.008, -0.43)); // 后
  228. const len1 = curve1.getLength();
  229. const len2 = curve2.getLength();
  230. const len3 = curve3.getLength();
  231. const unit = (len1 + len2 + len3) / num;
  232. const num1 = Math.floor(len1 / unit);
  233. const num2 = Math.floor(len2 / unit);
  234. const num3 = Math.floor(len3 / unit);
  235. const points1 = curve1.getPoints(num1);
  236. const points2 = curve2.getPoints(num2);
  237. const points3 = curve3.getPoints(num3);
  238. this.positions = [points1, points2, points3];
  239. this.msgPositions = [curve1.getSpacedPoints(10), curve2.getSpacedPoints(10), curve3.getSpacedPoints(10)];
  240. }
  241. drawSpheres = () => {
  242. const _this = this;
  243. const pointLines = new THREE.Object3D();
  244. pointLines.name = 'pointLines';
  245. return new Promise((resolve) => {
  246. new THREE.TextureLoader().load('/model/img/texture-smoke.png', (texture) => {
  247. texture.encoding = THREE.sRGBEncoding;
  248. const material = new THREE.PointsMaterial({
  249. color: '#FFFFAF',
  250. size: 0.03,
  251. map: texture,
  252. transparent: true, // 开启透明度
  253. });
  254. _this.positions.forEach((position, index) => {
  255. const geometry = new THREE.BufferGeometry();
  256. geometry.setFromPoints(position);
  257. const points = new THREE.Points(geometry, material);
  258. points.renderOrder = 0;
  259. index == 0 ? (points.name = 'line_q') : index == 1 ? (points.name = 'line_h') : (points.name = 'line_z');
  260. pointLines.add(points);
  261. points.layers.enable(_this.glob.BLOOM_SCENE);
  262. });
  263. this.group.add(pointLines);
  264. resolve(null);
  265. });
  266. });
  267. };
  268. darkenNonBloomed(obj) {
  269. if (obj.isMesh && this.bloomLayer.test(obj.layers) === false) {
  270. const opacity = obj.material.opacity;
  271. this.materials[obj.uuid] = obj.material;
  272. obj.material = this.darkMaterial.clone();
  273. obj.material.opacity = opacity;
  274. }
  275. }
  276. restoreMaterial(obj) {
  277. if (this.materials[obj.uuid]) {
  278. obj.material = this.materials[obj.uuid];
  279. delete this.materials[obj.uuid];
  280. }
  281. }
  282. resetMesh() {
  283. const opticalFiber = this.group.getObjectByName('opticalfiber');
  284. const optical = opticalFiber?.getObjectByName('optical_fiber_02');
  285. const optical1 = opticalFiber?.getObjectByName('optical_fiber_01');
  286. const optical2 = opticalFiber?.getObjectByName('optical_fiber_03');
  287. if (optical && optical1 && optical2) {
  288. optical.renderOrder = 9;
  289. optical1.renderOrder = 9;
  290. optical2.renderOrder = 9;
  291. optical.material =
  292. optical1.material =
  293. optical2.material =
  294. new THREE.MeshStandardMaterial({
  295. color: 0x555555,
  296. side: THREE.DoubleSide,
  297. transparent: true,
  298. opacity: 0.15,
  299. });
  300. }
  301. }
  302. /* 点击 */
  303. mousedownModel(rayCaster: THREE.Raycaster) {
  304. // const outlinePass = this.outlinePass;
  305. // const selectedObjects = [];
  306. // outlinePass.selectedObjects = selectedObjects;
  307. const opticalFiber = this.group.getObjectByName('opticalfiber');
  308. if (opticalFiber) {
  309. const intersects = rayCaster?.intersectObjects([...opticalFiber.children]) as THREE.Intersection[];
  310. // 判断是否点击到视频
  311. intersects.find((intersect) => {
  312. const mesh = intersect.object;
  313. if (mesh.name.startsWith('optical_fiber_')) {
  314. // outlinePass?.selectedObjects.push(mesh);
  315. return true;
  316. }
  317. return false;
  318. });
  319. }
  320. this.render();
  321. }
  322. mouseUpModel() {
  323. //
  324. }
  325. mountedThree() {
  326. this.locationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  327. this.warningLocationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  328. this.errorLocationTexture = new THREE.TextureLoader().load('/model/img/location.png');
  329. this.locationTexture.encoding = this.warningLocationTexture.encoding = this.warningLocationTexture.encoding = THREE.sRGBEncoding;
  330. return new Promise(async (resolve) => {
  331. this.model.renderer.sortObjects = true;
  332. this.model.camera.position.set(0, 3.1, 500);
  333. this.setRenderPass();
  334. this.model.orbitControls.update();
  335. this.model.setModel(['workFace']).then(async (gltf) => {
  336. this.group = gltf[0];
  337. this.group.position.set(0, 1, 0);
  338. this.group.scale.set(2, 2, 2);
  339. this.resetMesh();
  340. this.getPositions(this.glob.N);
  341. this.addLight();
  342. this.drawSpheres();
  343. resolve(null);
  344. });
  345. // window.onresize = function () {
  346. // const width = window.innerWidth;
  347. // const height = window.innerHeight;
  348. // model.camera.aspect = width / height;
  349. // model.camera.updateProjectionMatrix();
  350. // model.renderer.setSize(width, height);
  351. // bloomComposer.setSize(width, height);
  352. // finalComposer.setSize(width, height);
  353. // render();
  354. // };
  355. });
  356. }
  357. destroy() {
  358. this.model.clearGroup(this.group);
  359. this.model = null;
  360. this.group = null;
  361. }
  362. }
  363. export default WorkFace;