longmenSide.threejs.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class lmWindRectSide {
  5. model;
  6. modelName = 'lmcfSide';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. player1;
  12. player2;
  13. playerStartClickTime1 = new Date().getTime();
  14. playerStartClickTime2 = new Date().getTime();
  15. deviceRunState = '';
  16. tanTouRunState = '';
  17. constructor(model) {
  18. this.model = model;
  19. this.group.name = this.modelName;
  20. }
  21. addLight() {
  22. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  23. directionalLight.position.set(5.3, 9, 0.8);
  24. this.group.add(directionalLight);
  25. directionalLight.target = this.group;
  26. // gui.add(directionalLight.position, 'x', -10, 20).onChange(function (value) {
  27. // directionalLight.position.x = Number(value);
  28. // _this.render();
  29. // });
  30. // gui.add(directionalLight.position, 'y', -50, 50).onChange(function (value) {
  31. // directionalLight.position.y = Number(value);
  32. // _this.render();
  33. // });
  34. // gui.add(directionalLight.position, 'z', -20, 20).onChange(function (value) {
  35. // directionalLight.position.z = Number(value);
  36. // _this.render();
  37. // });
  38. const spotLight = new THREE.SpotLight();
  39. spotLight.angle = Math.PI / 16;
  40. spotLight.penumbra = 0;
  41. spotLight.castShadow = true;
  42. spotLight.distance = 0;
  43. spotLight.position.set(-470, -500, 500);
  44. this.group.add(spotLight);
  45. spotLight.shadow.camera.near = 0.5; // default
  46. spotLight.shadow.camera.far = 1000; // default
  47. spotLight.shadow.focus = 1;
  48. spotLight.shadow.bias = -0.000002;
  49. }
  50. // 设置模型位置
  51. setModalPosition() {
  52. this.group?.scale.set(22, 22, 22);
  53. this.group?.position.set(-25, 25, 15);
  54. }
  55. addMonitorText(selectData) {
  56. if (!this.group) {
  57. return;
  58. }
  59. const textArr = [
  60. {
  61. text: `煤矿巷道远程测风系统`,
  62. font: 'normal 34px Arial',
  63. color: '#009900',
  64. strokeStyle: '#002200',
  65. x: 125,
  66. y: 42,
  67. },
  68. {
  69. text: `风量(m³/min):`,
  70. font: 'normal 30px Arial',
  71. color: '#009900',
  72. strokeStyle: '#002200',
  73. x: 2,
  74. y: 115,
  75. },
  76. {
  77. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  78. font: 'normal 30px Arial',
  79. color: '#009900',
  80. strokeStyle: '#002200',
  81. x: 331,
  82. y: 115,
  83. },
  84. {
  85. text: `风速(m/s):`,
  86. font: 'normal 30px Arial',
  87. color: '#009900',
  88. strokeStyle: '#002200',
  89. x: 2,
  90. y: 182,
  91. },
  92. {
  93. text: `${selectData.va ? selectData.va : '-'}`,
  94. font: 'normal 30px Arial',
  95. color: '#009900',
  96. strokeStyle: '#002200',
  97. x: 330,
  98. y: 182,
  99. },
  100. {
  101. text: `断面积(MPa): `,
  102. font: 'normal 30px Arial',
  103. color: '#009900',
  104. strokeStyle: '#002200',
  105. x: 2,
  106. y: 245,
  107. },
  108. {
  109. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  110. font: 'normal 30px Arial',
  111. color: '#009900',
  112. strokeStyle: '#002200',
  113. x: 330,
  114. y: 245,
  115. },
  116. {
  117. text: `煤炭科学技术研究院有限公司研制`,
  118. font: 'normal 28px Arial',
  119. color: '#009900',
  120. strokeStyle: '#002200',
  121. x: 60,
  122. y: 303,
  123. },
  124. ];
  125. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  126. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  127. const textMaterial = new THREE.MeshBasicMaterial({
  128. map: textMap, // 设置纹理贴图
  129. transparent: true,
  130. side: THREE.DoubleSide, // 这里是双面渲染的意思
  131. });
  132. textMaterial.blending = THREE.CustomBlending;
  133. const monitorPlane = this.group?.getObjectByName('monitorText');
  134. if (monitorPlane) {
  135. monitorPlane.material = textMaterial;
  136. } else {
  137. const planeGeometry = new THREE.PlaneGeometry(560, 346); // 平面3维几何体PlaneGeometry
  138. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  139. planeMesh.name = 'monitorText';
  140. planeMesh.scale.set(0.0022, 0.0022, 0.0022);
  141. planeMesh.position.set(3.35, -0.002, -0.41);
  142. this.group?.add(planeMesh);
  143. }
  144. });
  145. }
  146. /* 风门动画 */
  147. render() {
  148. if (!this.model) {
  149. return;
  150. }
  151. }
  152. playCamera(flag) {
  153. const _this = this;
  154. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  155. const target = new THREE.Vector3();
  156. probeBar.getWorldPosition(target);
  157. const pos = new THREE.Vector3();
  158. pos.set(target.x, target.y, target.z);
  159. const cameraPos = this.model.camera.position;
  160. if (flag == 'start') {
  161. const obj = {
  162. // 相机整体观察位置
  163. x: cameraPos.x,
  164. y: cameraPos.y,
  165. z: cameraPos.z,
  166. // 相机整体预览对应目标观察点
  167. tx: 0,
  168. ty: 0,
  169. tz: 0,
  170. };
  171. gsap.to(obj, {
  172. // 相机局部观察位置
  173. x: pos.x,
  174. y: pos.y,
  175. z: pos.z,
  176. // 相机局部预览对应目标观察点
  177. tx: target.x,
  178. ty: target.y,
  179. tz: target.z,
  180. duration: 0.8,
  181. overwrite: true,
  182. onUpdate: function () {
  183. _this.model.camera.position.set(obj.x, obj.y, obj.z);
  184. _this.model.camera.lookAt(obj.tx, obj.ty, obj.tz);
  185. _this.model.orbitControls.target.set(obj.tx, obj.ty, obj.tz);
  186. // _this.model.orbitControls.update(); //update()函数内会执行camera.lookAt(controls.targe)
  187. },
  188. });
  189. } else if (flag == 'end') {
  190. //
  191. }
  192. }
  193. /* 提取风门序列帧,初始化前后门动画 */
  194. initAnimation() {
  195. const windGroup = new THREE.Group();
  196. windGroup.name = 'lmTanTou';
  197. if (this.group?.children.length) {
  198. for (let i = this.group?.children.length - 1; i > -1; i--) {
  199. const obj = this.group?.children[i];
  200. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('LMtantou')) {
  201. if (obj.name.startsWith('LMtantou')) {
  202. windGroup.add(obj.clone());
  203. this.group?.remove(obj);
  204. }
  205. }
  206. }
  207. }
  208. this.group?.add(windGroup);
  209. }
  210. /* 点击风窗,风窗全屏 */
  211. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  212. // 判断是否点击到视频
  213. intersects.find((intersect) => {
  214. const mesh = intersect.object;
  215. // if (mesh.name === 'player1') {
  216. // if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  217. // // 双击,视频放大
  218. // if (this.player1) {
  219. // this.player1.requestFullscreen();
  220. // }
  221. // }
  222. // this.playerStartClickTime1 = new Date().getTime();
  223. // return true;
  224. // }
  225. return false;
  226. });
  227. // console.log('999999999', this.model.camera.position, this.model.orbitControls);
  228. }
  229. mouseUpModel() {}
  230. resetModel() {
  231. clearTimeout(this.animationTimer);
  232. }
  233. // 播放动画
  234. play(flag, isDirect = false) {
  235. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  236. const cfTanTou = probeBar.getObjectByName('probe_') as THREE.Object3D;
  237. if (!probeBar || !cfTanTou) return;
  238. switch (flag) {
  239. case 'up':
  240. if (!isDirect) {
  241. if (this.deviceRunState == 'down') probeBar.position.setY(-0.3);
  242. gsap.to(probeBar['position'], {
  243. y: 0.35,
  244. duration: Math.abs(probeBar['position']['y'] - 0.35) * 25,
  245. overwrite: true,
  246. });
  247. } else {
  248. gsap.to(probeBar['position'], {
  249. y: 0.35,
  250. duration: 0,
  251. overwrite: true,
  252. });
  253. }
  254. break;
  255. case 'center':
  256. if (!isDirect) {
  257. gsap.to(probeBar['position'], {
  258. y: 0.05,
  259. duration: Math.abs(probeBar['position']['y'] - 0.05) * 50,
  260. overwrite: true,
  261. });
  262. } else {
  263. gsap.to(probeBar['position'], {
  264. y: 0.05,
  265. duration: 0,
  266. overwrite: true,
  267. });
  268. }
  269. break;
  270. case 'down':
  271. if (!isDirect) {
  272. gsap.to(probeBar['position'], {
  273. y: -0.3,
  274. duration: Math.abs(probeBar['position']['y'] + 0.3) * 50,
  275. overwrite: true,
  276. });
  277. } else {
  278. gsap.to(probeBar['position'], {
  279. y: -0.3,
  280. duration: 0,
  281. overwrite: true,
  282. });
  283. }
  284. break;
  285. case 'left':
  286. if (!isDirect) {
  287. gsap.to(cfTanTou['position'], {
  288. z: 325.262,
  289. duration: Math.abs(cfTanTou['position']['z'] - 325.262) * 0.05,
  290. overwrite: true,
  291. });
  292. } else {
  293. gsap.to(cfTanTou['position'], {
  294. z: 325.262,
  295. duration: 0,
  296. overwrite: true,
  297. });
  298. }
  299. break;
  300. case 'middle':
  301. if (!isDirect) {
  302. gsap.to(cfTanTou['position'], {
  303. z: -6.436,
  304. duration: Math.abs(cfTanTou['position']['z'] + 6.436) * 0.05,
  305. overwrite: true,
  306. });
  307. } else {
  308. gsap.to(cfTanTou['position'], {
  309. z: -6.436,
  310. duration: 0,
  311. overwrite: true,
  312. });
  313. }
  314. break;
  315. case 'right':
  316. if (!isDirect) {
  317. gsap.to(cfTanTou['position'], {
  318. z: -338.22,
  319. duration: Math.abs(cfTanTou['position']['z'] + 338.22) * 0.05,
  320. overwrite: true,
  321. });
  322. } else {
  323. gsap.to(cfTanTou['position'], {
  324. z: -338.22,
  325. duration: 0,
  326. overwrite: true,
  327. });
  328. }
  329. break;
  330. }
  331. }
  332. async initCamera(dom1?) {
  333. const videoPlayer1 = dom1;
  334. let monitorPlane: THREE.Mesh | null = null;
  335. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  336. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  337. const textMaterial = new THREE.MeshBasicMaterial({
  338. map: textMap, // 设置纹理贴图
  339. transparent: true,
  340. side: THREE.DoubleSide, // 这里是双面渲染的意思
  341. });
  342. textMaterial.blending = THREE.CustomBlending;
  343. monitorPlane = this.group?.getObjectByName('noPlayer');
  344. if (monitorPlane) {
  345. monitorPlane.material = textMaterial;
  346. } else {
  347. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  348. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  349. textMaterial.dispose();
  350. planeGeometry.dispose();
  351. }
  352. const videoPlayer = this.group.getObjectByName('player1');
  353. if (videoPlayer) {
  354. this.model.clearMesh(videoPlayer);
  355. this.group.remove(videoPlayer);
  356. }
  357. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  358. if (noPlayer1) {
  359. this.model.clearMesh(noPlayer1);
  360. this.group.remove(noPlayer1);
  361. }
  362. if (!videoPlayer1 && videoPlayer1 === null) {
  363. monitorPlane.name = 'noPlayer1';
  364. monitorPlane.scale.set(0.013, 0.007, 0.012);
  365. monitorPlane.position.set(-2.67, 0.0, -0.39);
  366. this.group?.add(monitorPlane);
  367. } else if (videoPlayer1) {
  368. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  369. if (mesh) {
  370. mesh?.scale.set(0.042, 0.036, 0.022);
  371. mesh?.position.set(-2.65, 0.03, -0.39);
  372. mesh.rotation.y = -Math.PI;
  373. this.group.add(mesh);
  374. }
  375. }
  376. }
  377. mountedThree() {
  378. return new Promise((resolve) => {
  379. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  380. if (gltf[0]) {
  381. this.group = gltf[0];
  382. this.setModalPosition();
  383. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  384. probeBar.position.setY(0.35);
  385. this.addLight();
  386. }
  387. resolve(null);
  388. });
  389. });
  390. }
  391. destroy() {
  392. if (this.group) {
  393. this.model.clearGroup(this.group);
  394. }
  395. this.model = null;
  396. this.group = null;
  397. }
  398. }
  399. export default lmWindRectSide;