longmenSide.threejs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. // if (this.isLRAnimation && this.group) {
  152. // // 左右摇摆动画
  153. // if (Math.abs(this.group.rotation.y) >= 0.2) {
  154. // this.direction = -this.direction;
  155. // this.group.rotation.y += 0.00002 * 30 * this.direction;
  156. // } else {
  157. // this.group.rotation.y += 0.00002 * 30 * this.direction;
  158. // }
  159. // }
  160. }
  161. playCamera(flag) {
  162. const _this = this;
  163. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  164. const target = new THREE.Vector3();
  165. probeBar.getWorldPosition(target);
  166. const pos = new THREE.Vector3();
  167. pos.set(target.x, target.y, target.z);
  168. const cameraPos = this.model.camera.position;
  169. if (flag == 'start') {
  170. const obj = {
  171. // 相机整体观察位置
  172. x: cameraPos.x,
  173. y: cameraPos.y,
  174. z: cameraPos.z,
  175. // 相机整体预览对应目标观察点
  176. tx: 0,
  177. ty: 0,
  178. tz: 0,
  179. };
  180. gsap.to(obj, {
  181. // 相机局部观察位置
  182. x: pos.x,
  183. y: pos.y,
  184. z: pos.z,
  185. // 相机局部预览对应目标观察点
  186. tx: target.x,
  187. ty: target.y,
  188. tz: target.z,
  189. duration: 0.8,
  190. overwrite: true,
  191. onUpdate: function () {
  192. _this.model.camera.position.set(obj.x, obj.y, obj.z);
  193. _this.model.camera.lookAt(obj.tx, obj.ty, obj.tz);
  194. _this.model.orbitControls.target.set(obj.tx, obj.ty, obj.tz);
  195. // _this.model.orbitControls.update(); //update()函数内会执行camera.lookAt(controls.targe)
  196. },
  197. });
  198. } else if (flag == 'end') {
  199. //
  200. }
  201. }
  202. /* 提取风门序列帧,初始化前后门动画 */
  203. initAnimation() {
  204. const windGroup = new THREE.Group();
  205. windGroup.name = 'lmTanTou';
  206. if (this.group?.children.length) {
  207. for (let i = this.group?.children.length - 1; i > -1; i--) {
  208. const obj = this.group?.children[i];
  209. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('LMtantou')) {
  210. if (obj.name.startsWith('LMtantou')) {
  211. windGroup.add(obj.clone());
  212. this.group?.remove(obj);
  213. }
  214. }
  215. }
  216. }
  217. this.group?.add(windGroup);
  218. }
  219. /* 点击风窗,风窗全屏 */
  220. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  221. // this.isLRAnimation = false;
  222. // if (this.animationTimer) {
  223. // clearTimeout(this.animationTimer);
  224. // this.animationTimer = null;
  225. // }
  226. // 判断是否点击到视频
  227. intersects.find((intersect) => {
  228. const mesh = intersect.object;
  229. // if (mesh.name === 'player1') {
  230. // if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  231. // // 双击,视频放大
  232. // if (this.player1) {
  233. // this.player1.requestFullscreen();
  234. // }
  235. // }
  236. // this.playerStartClickTime1 = new Date().getTime();
  237. // return true;
  238. // }
  239. return false;
  240. });
  241. // console.log('999999999', this.model.camera.position, this.model.orbitControls);
  242. }
  243. mouseUpModel() {
  244. // 10s后开始摆动
  245. if (!this.animationTimer && !this.isLRAnimation) {
  246. this.animationTimer = setTimeout(() => {
  247. this.isLRAnimation = true;
  248. }, 10000);
  249. }
  250. }
  251. resetModel() {
  252. clearTimeout(this.animationTimer);
  253. this.isLRAnimation = false;
  254. }
  255. // 播放动画
  256. play(flag, isDirect = false) {
  257. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  258. const cfTanTou = probeBar.getObjectByName('probe_') as THREE.Object3D;
  259. if (!probeBar || !cfTanTou) return;
  260. switch (flag) {
  261. case 'up':
  262. if (!isDirect) {
  263. if (this.deviceRunState == 'down') probeBar.position.setY(-0.3);
  264. gsap.to(probeBar['position'], {
  265. y: 0.35,
  266. duration: Math.abs(probeBar['position']['y'] - 0.35) * 25,
  267. overwrite: true,
  268. });
  269. } else {
  270. gsap.to(probeBar['position'], {
  271. y: 0.35,
  272. duration: 0,
  273. overwrite: true,
  274. });
  275. }
  276. break;
  277. case 'center':
  278. if (!isDirect) {
  279. gsap.to(probeBar['position'], {
  280. y: 0.05,
  281. duration: Math.abs(probeBar['position']['y'] - 0.05) * 50,
  282. overwrite: true,
  283. });
  284. } else {
  285. gsap.to(probeBar['position'], {
  286. y: 0.05,
  287. duration: 0,
  288. overwrite: true,
  289. });
  290. }
  291. break;
  292. case 'down':
  293. if (!isDirect) {
  294. gsap.to(probeBar['position'], {
  295. y: -0.3,
  296. duration: Math.abs(probeBar['position']['y'] + 0.3) * 50,
  297. overwrite: true,
  298. });
  299. } else {
  300. gsap.to(probeBar['position'], {
  301. y: -0.3,
  302. duration: 0,
  303. overwrite: true,
  304. });
  305. }
  306. break;
  307. case 'left':
  308. if (!isDirect) {
  309. gsap.to(cfTanTou['position'], {
  310. z: 325.262,
  311. duration: Math.abs(cfTanTou['position']['z'] - 325.262) * 0.05,
  312. overwrite: true,
  313. });
  314. } else {
  315. gsap.to(cfTanTou['position'], {
  316. z: 325.262,
  317. duration: 0,
  318. overwrite: true,
  319. });
  320. }
  321. break;
  322. case 'middle':
  323. if (!isDirect) {
  324. gsap.to(cfTanTou['position'], {
  325. z: -6.436,
  326. duration: Math.abs(cfTanTou['position']['z'] + 6.436) * 0.05,
  327. overwrite: true,
  328. });
  329. } else {
  330. gsap.to(cfTanTou['position'], {
  331. z: -6.436,
  332. duration: 0,
  333. overwrite: true,
  334. });
  335. }
  336. break;
  337. case 'right':
  338. if (!isDirect) {
  339. gsap.to(cfTanTou['position'], {
  340. z: -338.22,
  341. duration: Math.abs(cfTanTou['position']['z'] + 338.22) * 0.05,
  342. overwrite: true,
  343. });
  344. } else {
  345. gsap.to(cfTanTou['position'], {
  346. z: -338.22,
  347. duration: 0,
  348. overwrite: true,
  349. });
  350. }
  351. break;
  352. }
  353. }
  354. async initCamera(dom1?) {
  355. const videoPlayer1 = dom1;
  356. let monitorPlane: THREE.Mesh | null = null;
  357. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  358. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  359. const textMaterial = new THREE.MeshBasicMaterial({
  360. map: textMap, // 设置纹理贴图
  361. transparent: true,
  362. side: THREE.DoubleSide, // 这里是双面渲染的意思
  363. });
  364. textMaterial.blending = THREE.CustomBlending;
  365. monitorPlane = this.group?.getObjectByName('noPlayer');
  366. if (monitorPlane) {
  367. monitorPlane.material = textMaterial;
  368. } else {
  369. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  370. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  371. textMaterial.dispose();
  372. planeGeometry.dispose();
  373. }
  374. const videoPlayer = this.group.getObjectByName('player1');
  375. if (videoPlayer) {
  376. this.model.clearMesh(videoPlayer);
  377. this.group.remove(videoPlayer);
  378. }
  379. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  380. if (noPlayer1) {
  381. this.model.clearMesh(noPlayer1);
  382. this.group.remove(noPlayer1);
  383. }
  384. if (!videoPlayer1 && videoPlayer1 === null) {
  385. monitorPlane.name = 'noPlayer1';
  386. monitorPlane.scale.set(0.013, 0.007, 0.012);
  387. monitorPlane.position.set(-2.67, 0.0, -0.39);
  388. this.group?.add(monitorPlane);
  389. } else if (videoPlayer1) {
  390. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  391. if (mesh) {
  392. mesh?.scale.set(0.042, 0.036, 0.022);
  393. mesh?.position.set(-2.65, 0.03, -0.39);
  394. mesh.rotation.y = -Math.PI;
  395. this.group.add(mesh);
  396. }
  397. }
  398. }
  399. mountedThree() {
  400. return new Promise((resolve) => {
  401. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  402. if (gltf[0]) {
  403. this.group = gltf[0];
  404. this.setModalPosition();
  405. const probeBar = this.group?.getObjectByName('probe_bar') as THREE.Object3D;
  406. probeBar.position.setY(0.35);
  407. this.addLight();
  408. }
  409. resolve(null);
  410. });
  411. });
  412. }
  413. destroy() {
  414. if (this.group) {
  415. this.model.clearGroup(this.group);
  416. }
  417. this.model = null;
  418. this.group = null;
  419. }
  420. }
  421. export default lmWindRectSide;