longmenSide.threejs.ts 13 KB

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