workFace.threejs.base.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import * as THREE from 'three';
  2. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  3. import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
  4. import { setModalCenter } from '/@/utils/threejs/util';
  5. // import * as dat from 'dat.gui';
  6. // const gui = new dat.GUI();
  7. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  8. class WorkFace {
  9. model;
  10. modelName = 'workFace';
  11. group: THREE.Object3D = new THREE.Object3D();
  12. planeGroup: THREE.Group = new THREE.Group();
  13. bloomComposer: EffectComposer | null = null;
  14. finalComposer: EffectComposer | null = null;
  15. outlinePass: OutlinePass | null = null;
  16. positions: THREE.Vector3[][] = [];
  17. bloomLayer = new THREE.Layers();
  18. darkMaterial = new THREE.MeshBasicMaterial({ color: 'black', transparent: true, side: THREE.DoubleSide });
  19. materials = {};
  20. glob = {
  21. ENTIRE_SCENE: 0,
  22. BLOOM_SCENE: 10,
  23. N: 100,
  24. };
  25. locationTexture: THREE.Texture | null = null;
  26. warningLocationTexture: THREE.Texture | null = null;
  27. errorLocationTexture: THREE.Texture | null = null;
  28. playerStartClickTime1 = new Date().getTime();
  29. playerStartClickTime2 = new Date().getTime();
  30. planeNum = 0;
  31. constructor(model) {
  32. this.model = model;
  33. this.group.name = this.modelName;
  34. }
  35. addLight() {
  36. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  37. directionalLight.position.set(-196, 150, 258);
  38. this.group.add(directionalLight);
  39. directionalLight.target = this.group;
  40. }
  41. setControls = () => {
  42. if (this.model && this.model.orbitControls) {
  43. this.model.orbitControls.panSpeed = 0.5;
  44. this.model.orbitControls.rotateSpeed = 0.5;
  45. this.model.orbitControls.maxPolarAngle = Math.PI / 3;
  46. this.model.orbitControls.minPolarAngle = Math.PI / 4;
  47. this.model.orbitControls.minAzimuthAngle = -Math.PI / 3;
  48. this.model.orbitControls.maxAzimuthAngle = Math.PI / 4;
  49. }
  50. };
  51. render() {
  52. this.model.renderer?.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  53. }
  54. /* 点击 */
  55. mousedownModel(rayCaster: THREE.Raycaster) {
  56. const opticalFiber = this.group.getObjectByName('opticalfiber');
  57. if (opticalFiber) {
  58. const intersects = rayCaster?.intersectObjects([...opticalFiber.children]) as THREE.Intersection[];
  59. // 判断是否点击到视频
  60. intersects.find((intersect) => {
  61. const mesh = intersect.object;
  62. if (mesh.name.startsWith('optical_fiber_')) {
  63. // outlinePass?.selectedObjects.push(mesh);
  64. return true;
  65. }
  66. return false;
  67. });
  68. }
  69. this.render();
  70. }
  71. mouseUpModel() {
  72. //
  73. }
  74. setThreePlane() {
  75. const gltfModal = this.group.getObjectByName('workFace');
  76. const PouMian01 = gltfModal?.getObjectByName('PouMian01');
  77. const DiXing = PouMian01?.getObjectByName('DiXing');
  78. // 绘制采空区三带
  79. // new THREE.Vector3(934.695, -141.85, -365.375),
  80. // new THREE.Vector3(934.695, 623.933, -365.375),
  81. //-365.355
  82. const material1 = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, vertexColors: true });
  83. const offeset = 10;
  84. const yellowLen = offeset * 8;
  85. // 红=》红
  86. const curveRed = new THREE.CatmullRomCurve3([new THREE.Vector3(-477.721, -141.83, -365.375), new THREE.Vector3(-477.721, 623.933, -365.375)]);
  87. const curve0 = new THREE.CatmullRomCurve3([
  88. new THREE.Vector3(-231.811 - 150, -141.83, -365.375),
  89. new THREE.Vector3(-121.899 - 150, 67.201, -365.375),
  90. new THREE.Vector3(-242.441 - 150, 408.812, -365.375),
  91. new THREE.Vector3(-179.811 - 150, 620.836, -365.375),
  92. ]);
  93. const pointsRed = curveRed.getPoints(80);
  94. const points0 = curve0.getPoints(80);
  95. const newPointRed: number[] = [];
  96. const normalsRed: number[] = [];
  97. const colorsRed: number[] = [];
  98. for (let i = 0; i < 80; i++) {
  99. newPointRed.push(pointsRed[i].x, pointsRed[i].y, pointsRed[i].z);
  100. newPointRed.push(points0[i].x, points0[i].y, points0[i].z);
  101. newPointRed.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  102. newPointRed.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  103. newPointRed.push(pointsRed[i + 1].x, pointsRed[i + 1].y, pointsRed[i + 1].z);
  104. newPointRed.push(pointsRed[i].x, pointsRed[i].y, pointsRed[i].z);
  105. normalsRed.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  106. colorsRed.push(1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0);
  107. }
  108. const geometryRed = new THREE.BufferGeometry();
  109. geometryRed.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPointRed), 3));
  110. geometryRed.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normalsRed), 3));
  111. geometryRed.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colorsRed), 3));
  112. const meshRed = new THREE.Mesh(geometryRed, material1);
  113. meshRed.position.setZ(-1);
  114. DiXing?.add(meshRed);
  115. // 红=》黄
  116. const curve1 = new THREE.CatmullRomCurve3([
  117. new THREE.Vector3(-231.811 - yellowLen, -141.83, -365.375),
  118. new THREE.Vector3(-121.899 - yellowLen, 67.201, -365.375),
  119. new THREE.Vector3(-242.441 - yellowLen, 408.812, -365.375),
  120. new THREE.Vector3(-179.811 - yellowLen, 620.836, -365.375),
  121. ]);
  122. const points1 = curve1.getPoints(80);
  123. const newPoints0: number[] = [];
  124. const normals0: number[] = [];
  125. const colors0: number[] = [];
  126. for (let i = 0; i < 80; i++) {
  127. newPoints0.push(points0[i].x, points0[i].y, points0[i].z);
  128. newPoints0.push(points1[i].x, points1[i].y, points1[i].z);
  129. newPoints0.push(points1[i + 1].x, points1[i + 1].y, points1[i + 1].z);
  130. newPoints0.push(points1[i + 1].x, points1[i + 1].y, points1[i + 1].z);
  131. newPoints0.push(points0[i + 1].x, points0[i + 1].y, points0[i + 1].z);
  132. newPoints0.push(points0[i].x, points0[i].y, points0[i].z);
  133. normals0.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  134. colors0.push(1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0);
  135. }
  136. const geometry0 = new THREE.BufferGeometry();
  137. geometry0.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPoints0), 3));
  138. geometry0.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals0), 3));
  139. geometry0.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors0), 3));
  140. geometry0.computeBoundingBox();
  141. // const material1 = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, color: '#fff' });
  142. const mesh0 = new THREE.Mesh(geometry0, material1);
  143. mesh0.name = 'yellow';
  144. mesh0.position.setZ(-1);
  145. DiXing?.add(mesh0);
  146. // 黄=》黄
  147. const curveYellow1 = new THREE.CatmullRomCurve3([
  148. new THREE.Vector3(784.664 - yellowLen, 623.933, -365.355),
  149. new THREE.Vector3(167.212 - yellowLen, 450.517, -365.355),
  150. new THREE.Vector3(7.587 - yellowLen, 262.225, -365.355),
  151. new THREE.Vector3(426.499 - yellowLen, 31.416, -365.355),
  152. new THREE.Vector3(261.665 - yellowLen, -139.495, -365.355),
  153. ]);
  154. const curveYellow2 = new THREE.CatmullRomCurve3([
  155. new THREE.Vector3(-179.811 - yellowLen, 620.836, -365.375),
  156. new THREE.Vector3(-242.441 - yellowLen, 408.812, -365.375),
  157. new THREE.Vector3(-121.899 - yellowLen, 67.201, -365.375),
  158. new THREE.Vector3(-231.811 - yellowLen, -141.83, -365.375),
  159. ]);
  160. const pointsYellow1 = curveYellow1.getPoints(80);
  161. const pointsYellow2 = curveYellow2.getPoints(80);
  162. const newPointYellow: number[] = [];
  163. const normalsYellow: number[] = [];
  164. const colorsYellow: number[] = [];
  165. for (let i = 0; i < 80; i++) {
  166. newPointYellow.push(pointsYellow1[i].x, pointsYellow1[i].y, pointsYellow1[i].z);
  167. newPointYellow.push(pointsYellow2[i].x, pointsYellow2[i].y, pointsYellow2[i].z);
  168. newPointYellow.push(pointsYellow2[i + 1].x, pointsYellow2[i + 1].y, pointsYellow2[i + 1].z);
  169. newPointYellow.push(pointsYellow2[i + 1].x, pointsYellow2[i + 1].y, pointsYellow2[i + 1].z);
  170. newPointYellow.push(pointsYellow1[i + 1].x, pointsYellow1[i + 1].y, pointsYellow1[i + 1].z);
  171. newPointYellow.push(pointsYellow1[i].x, pointsYellow1[i].y, pointsYellow1[i].z);
  172. normalsYellow.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  173. colorsYellow.push(1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0);
  174. }
  175. const geometryYellow = new THREE.BufferGeometry();
  176. geometryYellow.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPointYellow), 3));
  177. geometryYellow.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normalsYellow), 3));
  178. geometryYellow.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colorsYellow), 3));
  179. const meshYellow = new THREE.Mesh(geometryYellow, material1);
  180. meshYellow.position.setZ(-1);
  181. DiXing?.add(meshYellow);
  182. // 蓝 =》 蓝
  183. const curve3 = new THREE.CatmullRomCurve3([
  184. new THREE.Vector3(784.664, 623.933, -365.355),
  185. new THREE.Vector3(167.212, 450.517, -365.355),
  186. new THREE.Vector3(7.587, 262.225, -365.355),
  187. new THREE.Vector3(426.499, 31.416, -365.355),
  188. new THREE.Vector3(261.665, -139.495, -365.355),
  189. ]);
  190. const points = curve3.getPoints(80);
  191. points.unshift(new THREE.Vector3(934.695, 623.933, -365.375));
  192. points.unshift(new THREE.Vector3(934.695, -141.85, -365.375));
  193. points.push(new THREE.Vector3(934.695, -141.85, -365.375));
  194. const newPoints: THREE.Vector2[] = [];
  195. for (let i = 0; i < points.length; i++) {
  196. const point = points[i];
  197. newPoints.push(new THREE.Vector2(point.x, point.y));
  198. }
  199. const shape = new THREE.Shape(newPoints);
  200. const geometry = new THREE.ShapeGeometry(shape);
  201. const material = new THREE.MeshBasicMaterial({ side: THREE.BackSide, color: '#00F' });
  202. const mesh = new THREE.Mesh(geometry, material);
  203. mesh.position.setZ(-366.485);
  204. DiXing?.add(mesh);
  205. // 黄色-》蓝色
  206. const curve2 = new THREE.CatmullRomCurve3([
  207. new THREE.Vector3(784.664 - yellowLen, 623.933, -365.355),
  208. new THREE.Vector3(167.212 - yellowLen, 450.517, -365.355),
  209. new THREE.Vector3(7.587 - yellowLen, 262.225, -365.355),
  210. new THREE.Vector3(426.499 - yellowLen, 31.416, -365.355),
  211. new THREE.Vector3(261.665 - yellowLen, -139.495, -365.355),
  212. ]);
  213. const curve4 = new THREE.CatmullRomCurve3([
  214. new THREE.Vector3(784.664, 623.933, -365.355),
  215. new THREE.Vector3(167.212, 450.517, -365.355),
  216. new THREE.Vector3(7.587, 262.225, -365.355),
  217. new THREE.Vector3(426.499, 31.416, -365.355),
  218. new THREE.Vector3(261.665, -139.495, -365.355),
  219. ]);
  220. const points2 = curve2.getPoints(80);
  221. const points3 = curve4.getPoints(80);
  222. const newPoints1: number[] = [];
  223. const normals: number[] = [];
  224. const colors: number[] = [];
  225. for (let i = 0; i < 80; i++) {
  226. newPoints1.push(points2[i].x, points2[i].y, points2[i].z);
  227. newPoints1.push(points3[i].x, points3[i].y, points3[i].z);
  228. newPoints1.push(points3[i + 1].x, points3[i + 1].y, points3[i + 1].z);
  229. newPoints1.push(points3[i + 1].x, points3[i + 1].y, points3[i + 1].z);
  230. newPoints1.push(points2[i + 1].x, points2[i + 1].y, points2[i + 1].z);
  231. newPoints1.push(points2[i].x, points2[i].y, points2[i].z);
  232. normals.push(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1);
  233. colors.push(1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0);
  234. }
  235. const geometry1 = new THREE.BufferGeometry();
  236. geometry1.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newPoints1), 3));
  237. geometry1.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals), 3));
  238. geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
  239. geometry1.computeBoundingBox();
  240. const mesh1 = new THREE.Mesh(geometry1, material1);
  241. mesh1.name = 'blue';
  242. mesh1.position.setZ(-1);
  243. DiXing?.add(mesh1);
  244. }
  245. setModalType(modalType) {
  246. // debugger;
  247. const gltfModal = this.group.getObjectByName('workFace');
  248. const workFace1 = gltfModal?.getObjectByName('YiJinYiHui');
  249. const workFace2 = gltfModal?.getObjectByName('LiangJinYiHui');
  250. const workFace3 = gltfModal?.getObjectByName('LiangJinLiangHui');
  251. if (workFace1 && workFace2 && workFace3) {
  252. if (modalType === 'workFace1') {
  253. // 单进单回
  254. workFace1.visible = true;
  255. workFace2.visible = false;
  256. workFace3.visible = false;
  257. } else if (modalType === 'workFace3') {
  258. // 双进单回
  259. workFace1.visible = false;
  260. workFace2.visible = true;
  261. workFace3.visible = false;
  262. } else if (modalType === 'workFace4') {
  263. workFace1.visible = false;
  264. workFace2.visible = false;
  265. workFace3.visible = true;
  266. }
  267. }
  268. }
  269. mountedThree() {
  270. return new Promise(async (resolve) => {
  271. this.model.renderer.sortObjects = true;
  272. this.model.orbitControls.update();
  273. this.model.setGLTFModel('workFace').then(async (gltf) => {
  274. const gltfModal = gltf[0];
  275. this.group = gltfModal;
  276. this.group.name = this.modelName;
  277. setModalCenter(this.group);
  278. this.group.scale.set(2.5, 2.5, 2.5);
  279. this.addLight();
  280. this.setThreePlane();
  281. this.setControls();
  282. resolve(null);
  283. });
  284. });
  285. }
  286. destroy() {
  287. this.model.clearGroup(this.group);
  288. this.model = null;
  289. this.group = null;
  290. this.bloomComposer?.dispose();
  291. this.finalComposer?.dispose();
  292. }
  293. }
  294. export default WorkFace;