workFace.threejs.base.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 { number } from 'vue-types';
  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. setControls = () => {
  63. if (this.model && this.model.orbitControls) {
  64. this.model.orbitControls.panSpeed = 0.5;
  65. this.model.orbitControls.rotateSpeed = 0.5;
  66. this.model.orbitControls.maxPolarAngle = Math.PI / 3;
  67. this.model.orbitControls.minPolarAngle = Math.PI / 4;
  68. this.model.orbitControls.minAzimuthAngle = -Math.PI / 3;
  69. this.model.orbitControls.maxAzimuthAngle = Math.PI / 4;
  70. }
  71. };
  72. render() {
  73. this.model.renderer?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  74. // const _this = this;
  75. // if (this.model && this.model.scene.getObjectByName(this.modelName)) {
  76. // this.group?.traverse((obj) => {
  77. // _this.darkenNonBloomed(obj);
  78. // });
  79. // this.bloomComposer?.render();
  80. // this.group?.traverse((obj) => {
  81. // _this.restoreMaterial(obj);
  82. // });
  83. // this.finalComposer?.render();
  84. // }
  85. }
  86. // 绘制抽采单元
  87. setPlanes = (n, colors = new Array(n).fill(new THREE.Color('rgb(100%, 0%, 0%)'))) => {
  88. colors = gradientColors('#00FF2C', '#FF0000', n, 2);
  89. this.planeNum = n;
  90. const lenScale = 0.77 / n;
  91. const planeGeo = new THREE.PlaneGeometry();
  92. planeGeo.applyMatrix4(new THREE.Matrix4().makeTranslation(-1, 0, 0));
  93. for (let i = 0; i < n; i++) {
  94. const material = new THREE.MeshBasicMaterial({ color: colors[i], transparent: true, opacity: 0.6, depthTest: false, depthWrite: false });
  95. const plane = new THREE.Mesh(planeGeo, material);
  96. plane.name = 'unit' + i;
  97. plane.rotation.x = -Math.PI / 2;
  98. plane.scale.set(lenScale - 0.001, 0.375, 1.0);
  99. plane.position.set(0.282 - lenScale * (i - 0.5), 0.015, 0.142);
  100. this.planeGroup.add(plane);
  101. }
  102. };
  103. // 清除抽采单元绘制面
  104. clearPlanes = () => {
  105. for (let i = 0; i < this.planeNum; i++) {
  106. const plane = this.planeGroup.getObjectByName(`unit${i}`);
  107. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  108. if (plane) this.planeGroup.remove(plane);
  109. if (label) this.planeGroup.remove(label);
  110. }
  111. };
  112. // 抽采单元内容显示
  113. setCss3D = () => {
  114. const obj = this.group.getObjectByName(`unitText`);
  115. if (!obj) {
  116. const element = document.getElementById(`gasUnitBox`) as HTMLElement;
  117. if (element) {
  118. const gasUnitCSS3D = new CSS3DObject(element);
  119. gasUnitCSS3D.name = `unitText`;
  120. gasUnitCSS3D.scale.set(0.0009, 0.0009, 0.0009);
  121. gasUnitCSS3D.position.set(-0.1, 0.11, 0.05);
  122. gasUnitCSS3D.lookAt(-0.1, 0.5, 1);
  123. this.planeGroup.add(gasUnitCSS3D);
  124. }
  125. }
  126. for (let i = 0; i < this.planeNum; i++) {
  127. const lenScale = 0.77 / this.planeNum;
  128. const label = setTag3D(`抽采单元${i + 1}`, 'gas_unit_text');
  129. label.scale.set(0.0018, 0.0018, 1); //根据相机渲染范围控制HTML 3D标签尺寸
  130. label.position.set(0.282 - lenScale * (i + 0.5), 0.015, 0.142);
  131. label.name = 'planeText' + i;
  132. this.planeGroup.add(label);
  133. }
  134. };
  135. // 显示或隐藏抽采单元显示内容
  136. changeCss3D = (isHide) => {
  137. for (let i = 0; i < this.planeNum; i++) {
  138. const obj = this.group.getObjectByName(`unitText${i}`);
  139. if (obj) {
  140. obj.visible = isHide;
  141. }
  142. }
  143. };
  144. // 清除抽采单元显示内容
  145. clearCss3D = () => {
  146. const obj = this.group.getObjectByName(`unitText`);
  147. if (obj) this.group.remove(obj);
  148. const element = document.getElementById(`gasUnitBox`) as HTMLElement;
  149. if (element) {
  150. element.remove();
  151. }
  152. for (let i = 0; i < this.planeNum; i++) {
  153. const label = this.planeGroup.getObjectByName(`planeText${i}`);
  154. if (label) this.planeGroup.remove(label);
  155. }
  156. };
  157. setRenderPass = () => {
  158. this.bloomLayer.set(this.glob.BLOOM_SCENE);
  159. const params = {
  160. bloomStrength: 2.5,
  161. bloomThreshold: 0,
  162. bloomRadius: 0,
  163. };
  164. const renderScene = new RenderPass(this.model.scene, this.model.camera);
  165. const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
  166. bloomPass.strength = params.bloomStrength;
  167. bloomPass.radius = params.bloomRadius;
  168. bloomPass.threshold = params.bloomThreshold;
  169. this.bloomComposer = new EffectComposer(this.model.renderer);
  170. this.bloomComposer.renderToScreen = false;
  171. this.bloomComposer.addPass(renderScene);
  172. this.bloomComposer.addPass(bloomPass);
  173. const finalPass = new ShaderPass(
  174. new THREE.ShaderMaterial({
  175. uniforms: {
  176. baseTexture: { value: null },
  177. bloomTexture: { value: this.bloomComposer.renderTarget2.texture },
  178. },
  179. vertexShader: `
  180. varying vec2 vUv;
  181. void main() {
  182. vUv = uv;
  183. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  184. }`,
  185. fragmentShader: `uniform sampler2D baseTexture;
  186. uniform sampler2D bloomTexture;
  187. varying vec2 vUv;
  188. void main() {
  189. gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0, 1.0, 1.0, 0.0 ) * texture2D( bloomTexture, vUv ) );
  190. }`,
  191. defines: {},
  192. }),
  193. 'baseTexture'
  194. );
  195. // const gammaCorrection = new ShaderPass(GammaCorrectionShader);
  196. const outputPass = new OutputPass();
  197. finalPass.needsSwap = true;
  198. this.model.renderer.toneMapping = THREE.ReinhardToneMapping;
  199. this.finalComposer = new EffectComposer(this.model.renderer);
  200. this.finalComposer.addPass(renderScene);
  201. this.finalComposer.addPass(outputPass);
  202. this.finalComposer.addPass(finalPass);
  203. const effectFXAA = new ShaderPass(FXAAShader);
  204. effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight);
  205. this.finalComposer.addPass(effectFXAA);
  206. // this.outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), this.model.scene, this.model.camera);
  207. // this.finalComposer.addPass(this.outlinePass);
  208. };
  209. getPositions(num = 40) {
  210. const curve1 = new THREE.LineCurve3(new THREE.Vector3(-595.2, 0.046, -2.863), new THREE.Vector3(595.2, 0.046, -2.863)); // 前
  211. const curve2 = new THREE.LineCurve3(new THREE.Vector3(-595.065, 0.014, 1.696), new THREE.Vector3(595.048, 0.014, 1.696)); // 中
  212. const curve3 = new THREE.LineCurve3(new THREE.Vector3(0.0, 0.0, 190.611), new THREE.Vector3(0.0, 0.0, -190.611)); // 后‘
  213. const len1 = curve1.getLength();
  214. const len2 = curve2.getLength();
  215. const len3 = curve3.getLength();
  216. const unit = (len1 + len2 + len3) / num;
  217. const num1 = Math.floor(len1 / unit);
  218. const num2 = Math.floor(len2 / unit);
  219. const num3 = Math.floor(len3 / unit);
  220. const points1 = curve1.getPoints(num1);
  221. const points2 = curve2.getPoints(num2);
  222. const points3 = curve3.getPoints(num3);
  223. this.positions = [points1, points2, points3];
  224. }
  225. drawSpheres = () => {
  226. const _this = this;
  227. const pointLines = new THREE.Object3D();
  228. pointLines.name = 'pointLines';
  229. return new Promise((resolve) => {
  230. new THREE.TextureLoader().load('/model/img/texture-smoke.png', (texture) => {
  231. const material = new THREE.PointsMaterial({
  232. color: '#FFFFFF',
  233. size: 0.008,
  234. map: texture,
  235. opacity: 0.8,
  236. transparent: true, // 开启透明度
  237. });
  238. _this.positions.forEach((position, index) => {
  239. const geometry = new THREE.BufferGeometry();
  240. geometry.setFromPoints(position);
  241. const points = new THREE.Points(geometry, material);
  242. points.renderOrder = 0;
  243. index == 0 ? (points.name = 'line_q') : index == 1 ? (points.name = 'line_h') : (points.name = 'line_z');
  244. let opticalfiber;
  245. if (index == 0) {
  246. points.name = 'line_q';
  247. opticalfiber = this.group.getObjectByName('opticalfiber03');
  248. } else if (index == 1) {
  249. points.name = 'line_h';
  250. opticalfiber = this.group.getObjectByName('opticalfiber01');
  251. } else {
  252. points.name = 'line_z';
  253. opticalfiber = this.group.getObjectByName('opticalfiber02');
  254. }
  255. if (opticalfiber) points.applyMatrix4(opticalfiber.matrix);
  256. const box = new THREE.Box3();
  257. box.setFromObject(points);
  258. // points.geometry.boundingBox?.set(box);
  259. pointLines.add(points);
  260. points.layers.enable(_this.glob.BLOOM_SCENE);
  261. });
  262. this.group.add(pointLines);
  263. resolve(null);
  264. texture.dispose();
  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 = this.group?.getObjectByName('optical_fiber_02');
  285. const optical1 = this.group?.getObjectByName('optical_fiber_01');
  286. const optical2 = this.group?.getObjectByName('optical_fiber_03');
  287. if (optical && optical1 && optical2) {
  288. optical.renderOrder = 100;
  289. optical1.renderOrder = 100;
  290. optical2.renderOrder = 100;
  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.45,
  299. });
  300. }
  301. }
  302. /* 点击 */
  303. mousedownModel(rayCaster: THREE.Raycaster) {
  304. const opticalFiber = this.group.getObjectByName('opticalfiber');
  305. if (opticalFiber) {
  306. const intersects = rayCaster?.intersectObjects([...opticalFiber.children]) as THREE.Intersection[];
  307. // 判断是否点击到视频
  308. intersects.find((intersect) => {
  309. const mesh = intersect.object;
  310. if (mesh.name.startsWith('optical_fiber_')) {
  311. // outlinePass?.selectedObjects.push(mesh);
  312. return true;
  313. }
  314. return false;
  315. });
  316. }
  317. this.render();
  318. }
  319. mouseUpModel() {
  320. //
  321. }
  322. setThreePlane() {
  323. const gltfModal = this.group.getObjectByName('workFace');
  324. const PouMian01 = gltfModal?.getObjectByName('PouMian01');
  325. const DiXing = PouMian01?.getObjectByName('DiXing');
  326. // 绘制采空区三带
  327. // new THREE.Vector3(934.695, -141.85, -365.375),
  328. // new THREE.Vector3(934.695, 623.933, -365.375),
  329. //-365.355
  330. const material1 = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, vertexColors: true });
  331. const offeset = 10;
  332. const yellowLen = offeset * 8;
  333. // 红=》红
  334. const curveRed = new THREE.CatmullRomCurve3([new THREE.Vector3(-477.721, -141.83, -365.375), new THREE.Vector3(-477.721, 623.933, -365.375)]);
  335. const curve0 = new THREE.CatmullRomCurve3([
  336. new THREE.Vector3(-231.811 - 150, -141.83, -365.375),
  337. new THREE.Vector3(-121.899 - 150, 67.201, -365.375),
  338. new THREE.Vector3(-242.441 - 150, 408.812, -365.375),
  339. new THREE.Vector3(-179.811 - 150, 620.836, -365.375),
  340. ]);
  341. const pointsRed = curveRed.getPoints(80);
  342. const points0 = curve0.getPoints(80);
  343. const newPointRed: number[] = [];
  344. const normalsRed: number[] = [];
  345. const colorsRed: number[] = [];
  346. for (let i = 0; i < 80; i++) {
  347. newPointRed.push(pointsRed[i].x, pointsRed[i].y, pointsRed[i].z);
  348. newPointRed.push(points0[i].x, points0[i].y, points0[i].z);
  349. newPointRed.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  350. newPointRed.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  351. newPointRed.push(pointsRed[i + 1].x, pointsRed[i + 1].y, pointsRed[i + 1].z);
  352. newPointRed.push(pointsRed[i].x, pointsRed[i].y, pointsRed[i].z);
  353. normalsRed.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  354. colorsRed.push(1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0);
  355. }
  356. const geometryRed = new THREE.BufferGeometry();
  357. geometryRed.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPointRed), 3));
  358. geometryRed.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normalsRed), 3));
  359. geometryRed.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colorsRed), 3));
  360. const meshRed = new THREE.Mesh(geometryRed, material1);
  361. meshRed.position.setZ(-1);
  362. DiXing?.add(meshRed);
  363. // 红=》黄
  364. const curve1 = new THREE.CatmullRomCurve3([
  365. new THREE.Vector3(-231.811 - yellowLen, -141.83, -365.375),
  366. new THREE.Vector3(-121.899 - yellowLen, 67.201, -365.375),
  367. new THREE.Vector3(-242.441 - yellowLen, 408.812, -365.375),
  368. new THREE.Vector3(-179.811 - yellowLen, 620.836, -365.375),
  369. ]);
  370. const points1 = curve1.getPoints(80);
  371. const newPoints0: number[] = [];
  372. const normals0: number[] = [];
  373. const colors0: number[] = [];
  374. for (let i = 0; i < 80; i++) {
  375. newPoints0.push(points0[i].x, points0[i].y, points0[i].z);
  376. newPoints0.push(points1[i].x, points1[i].y, points1[i].z);
  377. newPoints0.push(points1[i + 1].x, points1[i + 1].y, points1[i + 1].z);
  378. newPoints0.push(points1[i + 1].x, points1[i + 1].y, points1[i + 1].z);
  379. newPoints0.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  380. newPoints0.push(points0[i].x, points0[i].y, points0[i].z);
  381. normals0.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  382. colors0.push(1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0);
  383. }
  384. const geometry0 = new THREE.BufferGeometry();
  385. geometry0.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPoints0), 3));
  386. geometry0.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals0), 3));
  387. geometry0.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors0), 3));
  388. geometry0.computeBoundingBox();
  389. // const material1 = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, color: '#fff' });
  390. const mesh0 = new THREE.Mesh(geometry0, material1);
  391. mesh0.name = 'yellow';
  392. mesh0.position.setZ(-1);
  393. DiXing?.add(mesh0);
  394. // 黄=》黄
  395. const curveYellow1 = new THREE.CatmullRomCurve3([
  396. new THREE.Vector3(784.664 - yellowLen, 623.933, -365.355),
  397. new THREE.Vector3(167.212 - yellowLen, 450.517, -365.355),
  398. new THREE.Vector3(7.587 - yellowLen, 262.225, -365.355),
  399. new THREE.Vector3(426.499 - yellowLen, 31.416, -365.355),
  400. new THREE.Vector3(261.665 - yellowLen, -139.495, -365.355),
  401. ]);
  402. const curveYellow2 = new THREE.CatmullRomCurve3([
  403. new THREE.Vector3(-179.811 - yellowLen, 620.836, -365.375),
  404. new THREE.Vector3(-242.441 - yellowLen, 408.812, -365.375),
  405. new THREE.Vector3(-121.899 - yellowLen, 67.201, -365.375),
  406. new THREE.Vector3(-231.811 - yellowLen, -141.83, -365.375),
  407. ]);
  408. const pointsYellow1 = curveYellow1.getPoints(80);
  409. const pointsYellow2 = curveYellow2.getPoints(80);
  410. const newPointYellow: number[] = [];
  411. const normalsYellow: number[] = [];
  412. const colorsYellow: number[] = [];
  413. for (let i = 0; i < 80; i++) {
  414. newPointYellow.push(pointsYellow1[i].x, pointsYellow1[i].y, pointsYellow1[i].z);
  415. newPointYellow.push(pointsYellow2[i].x, pointsYellow2[i].y, pointsYellow2[i].z);
  416. newPointYellow.push(pointsYellow2[i + 1].x, pointsYellow2[i + 1].y, pointsYellow2[i + 1].z);
  417. newPointYellow.push(pointsYellow2[i + 1].x, pointsYellow2[i + 1].y, pointsYellow2[i + 1].z);
  418. newPointYellow.push(pointsYellow1[i + 1].x, pointsYellow1[i + 1].y, pointsYellow1[i + 1].z);
  419. newPointYellow.push(pointsYellow1[i].x, pointsYellow1[i].y, pointsYellow1[i].z);
  420. normalsYellow.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  421. colorsYellow.push(1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0);
  422. }
  423. const geometryYellow = new THREE.BufferGeometry();
  424. geometryYellow.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPointYellow), 3));
  425. geometryYellow.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normalsYellow), 3));
  426. geometryYellow.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colorsYellow), 3));
  427. const meshYellow = new THREE.Mesh(geometryYellow, material1);
  428. meshYellow.position.setZ(-1);
  429. DiXing?.add(meshYellow);
  430. // 蓝 =》 蓝
  431. const curve3 = new THREE.CatmullRomCurve3([
  432. new THREE.Vector3(784.664, 623.933, -365.355),
  433. new THREE.Vector3(167.212, 450.517, -365.355),
  434. new THREE.Vector3(7.587, 262.225, -365.355),
  435. new THREE.Vector3(426.499, 31.416, -365.355),
  436. new THREE.Vector3(261.665, -139.495, -365.355),
  437. ]);
  438. const points = curve3.getPoints(80);
  439. points.unshift(new THREE.Vector3(934.695, 623.933, -365.375));
  440. points.unshift(new THREE.Vector3(934.695, -141.85, -365.375));
  441. points.push(new THREE.Vector3(934.695, -141.85, -365.375));
  442. const newPoints: THREE.Vector2[] = [];
  443. for (let i = 0; i < points.length; i++) {
  444. const point = points[i];
  445. newPoints.push(new THREE.Vector2(point.x, point.y));
  446. }
  447. const shape = new THREE.Shape(newPoints);
  448. const geometry = new THREE.ShapeGeometry(shape);
  449. const material = new THREE.MeshBasicMaterial({ side: THREE.BackSide, color: '#00F' });
  450. const mesh = new THREE.Mesh(geometry, material);
  451. mesh.position.setZ(-366.485);
  452. DiXing?.add(mesh);
  453. // 黄色-》蓝色
  454. const curve2 = new THREE.CatmullRomCurve3([
  455. new THREE.Vector3(784.664 - yellowLen, 623.933, -365.355),
  456. new THREE.Vector3(167.212 - yellowLen, 450.517, -365.355),
  457. new THREE.Vector3(7.587 - yellowLen, 262.225, -365.355),
  458. new THREE.Vector3(426.499 - yellowLen, 31.416, -365.355),
  459. new THREE.Vector3(261.665 - yellowLen, -139.495, -365.355),
  460. ]);
  461. const curve4 = new THREE.CatmullRomCurve3([
  462. new THREE.Vector3(784.664, 623.933, -365.355),
  463. new THREE.Vector3(167.212, 450.517, -365.355),
  464. new THREE.Vector3(7.587, 262.225, -365.355),
  465. new THREE.Vector3(426.499, 31.416, -365.355),
  466. new THREE.Vector3(261.665, -139.495, -365.355),
  467. ]);
  468. const points2 = curve2.getPoints(80);
  469. const points3 = curve4.getPoints(80);
  470. const newPoints1: number[] = [];
  471. const normals: number[] = [];
  472. const colors: number[] = [];
  473. for (let i = 0; i < 80; i++) {
  474. newPoints1.push(points2[i].x, points2[i].y, points2[i].z);
  475. newPoints1.push(points3[i].x, points3[i].y, points3[i].z);
  476. newPoints1.push(points3[i + 1].x, points3[i + 1].y, points3[i + 1].z);
  477. newPoints1.push(points3[i + 1].x, points3[i + 1].y, points3[i + 1].z);
  478. newPoints1.push(points2[i + 1].x, points2[i + 1].y, points2[i + 1].z);
  479. newPoints1.push(points2[i].x, points2[i].y, points2[i].z);
  480. normals.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  481. colors.push(1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0);
  482. }
  483. const geometry1 = new THREE.BufferGeometry();
  484. geometry1.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPoints1), 3));
  485. geometry1.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals), 3));
  486. geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
  487. geometry1.computeBoundingBox();
  488. const mesh1 = new THREE.Mesh(geometry1, material1);
  489. mesh1.name = 'blue';
  490. mesh1.position.setZ(-1);
  491. DiXing?.add(mesh1);
  492. }
  493. setModalType(modalType) {
  494. // debugger;
  495. const gltfModal = this.group.getObjectByName('workFace');
  496. const workFace1 = gltfModal?.getObjectByName('YiJinYiHui');
  497. const workFace2 = gltfModal?.getObjectByName('LiangJinYiHui');
  498. const workFace3 = gltfModal?.getObjectByName('LiangJinLiangHui');
  499. if (workFace1 && workFace2 && workFace3) {
  500. if (modalType === 'workFace1') {
  501. // 单进单回
  502. workFace1.visible = true;
  503. workFace2.visible = false;
  504. workFace3.visible = false;
  505. } else if (modalType === 'workFace3') {
  506. // 双进单回
  507. workFace1.visible = false;
  508. workFace2.visible = true;
  509. workFace3.visible = false;
  510. } else if (modalType === 'workFace4') {
  511. workFace1.visible = false;
  512. workFace2.visible = false;
  513. workFace3.visible = true;
  514. }
  515. }
  516. }
  517. mountedThree() {
  518. return new Promise(async (resolve) => {
  519. this.model.renderer.sortObjects = true;
  520. this.model.orbitControls.update();
  521. this.model.setGLTFModel('workFace').then(async (gltf) => {
  522. const gltfModal = gltf[0];
  523. this.group = gltfModal;
  524. this.group.name = this.modelName;
  525. setModalCenter(this.group);
  526. this.group.scale.set(2.5, 2.5, 2.5);
  527. // this.resetMesh();
  528. this.getPositions(this.glob.N);
  529. this.addLight();
  530. this.setThreePlane();
  531. this.setControls();
  532. // this.drawSpheres();
  533. resolve(null);
  534. });
  535. });
  536. }
  537. destroy() {
  538. this.model.clearGroup(this.group);
  539. this.model = null;
  540. this.group = null;
  541. this.bloomComposer?.dispose();
  542. this.finalComposer?.dispose();
  543. }
  544. }
  545. export default WorkFace;