longmen.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class lmWindRect {
  5. model;
  6. modelName = 'lmcf';
  7. group: THREE.Group | null = null;
  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. constructor(model, playerVal1, playerVal2) {
  17. this.model = model;
  18. this.player1 = playerVal1;
  19. this.player2 = playerVal2;
  20. }
  21. addLight() {
  22. const pointLight2 = new THREE.PointLight(0xffeeee, 1, 0);
  23. pointLight2.position.set(317, 41, -21);
  24. pointLight2.shadow.bias = 0.05;
  25. this.model.add(pointLight2);
  26. //
  27. const pointLight3 = new THREE.PointLight(0xffffff, 1, 138);
  28. pointLight3.position.set(-202, 50, 27.8);
  29. pointLight3.shadow.bias = 0.05;
  30. this.model.add(pointLight3);
  31. //
  32. const pointLight4 = new THREE.PointLight(0xffeeee, 1, 162);
  33. pointLight4.position.set(83, 62, -14);
  34. pointLight4.shadow.bias = 0.05;
  35. this.model.add(pointLight4);
  36. //
  37. const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 176);
  38. pointLight5.position.set(-123, 61.6, -18);
  39. pointLight5.shadow.bias = 0.05;
  40. this.model.add(pointLight5);
  41. //
  42. const pointLight6 = new THREE.PointLight(0xffffff, 1.5, 64);
  43. pointLight6.position.set(-76, 48, 18);
  44. pointLight6.shadow.bias = 0.05;
  45. this.model.add(pointLight6);
  46. const pointLight7 = new THREE.PointLight(0xffffff, 1, 302);
  47. pointLight7.position.set(-220, -177, -3.5);
  48. pointLight7.shadow.bias = -0.05;
  49. this.model.add(pointLight7);
  50. const spotLight = new THREE.SpotLight();
  51. spotLight.angle = Math.PI / 16;
  52. spotLight.penumbra = 0;
  53. spotLight.castShadow = true;
  54. spotLight.distance = 0;
  55. spotLight.position.set(-470, -500, 500);
  56. this.model.add(spotLight);
  57. spotLight.shadow.camera.near = 0.5; // default
  58. spotLight.shadow.camera.far = 1000; // default
  59. spotLight.shadow.focus = 1;
  60. spotLight.shadow.bias = -0.000002;
  61. }
  62. // 设置模型位置
  63. setModalPosition() {
  64. this.group?.scale.set(22, 22, 22);
  65. this.group?.position.set(-25, 25, 15);
  66. }
  67. addFmText(selectData) {
  68. if (!this.group) {
  69. return;
  70. }
  71. const textArr = [
  72. {
  73. text: `煤矿巷道远程风窗系统`,
  74. font: 'normal 2.2rem Arial',
  75. color: '#009900',
  76. strokeStyle: '#002200',
  77. x: 90,
  78. y: 95,
  79. },
  80. {
  81. text: `过风量(m3/min):`,
  82. font: 'normal 30px Arial',
  83. color: '#009900',
  84. strokeStyle: '#002200',
  85. x: 5,
  86. y: 115,
  87. },
  88. {
  89. text: `${
  90. selectData.frontRearDifference && selectData.rearPresentValue
  91. ? Math.min(selectData.frontRearDifference, selectData.rearPresentValue)
  92. : selectData.frontRearDifference || selectData.rearPresentValue || '-'
  93. }`,
  94. font: 'normal 30px Arial',
  95. color: '#009900',
  96. strokeStyle: '#002200',
  97. x: 235,
  98. y: 115,
  99. },
  100. {
  101. text: `过风面积(m2): `,
  102. font: 'normal 30px Arial',
  103. color: '#009900',
  104. strokeStyle: '#002200',
  105. x: 5,
  106. y: 182,
  107. },
  108. {
  109. text: `${
  110. selectData.forntArea && selectData.rearArea
  111. ? Math.min(selectData.forntArea, selectData.rearArea)
  112. : selectData.forntArea || selectData.rearArea || '-'
  113. }`,
  114. font: 'normal 30px Arial',
  115. color: '#009900',
  116. strokeStyle: '#002200',
  117. x: 200,
  118. y: 182,
  119. },
  120. {
  121. text: `风窗压差(Pa):`,
  122. font: 'normal 30px Arial',
  123. color: '#009900',
  124. strokeStyle: '#002200',
  125. x: 5,
  126. y: 245,
  127. },
  128. {
  129. text: `${selectData.dataDh}`,
  130. font: 'normal 30px Arial',
  131. color: '#009900',
  132. strokeStyle: '#002200',
  133. x: 200,
  134. y: 245,
  135. },
  136. {
  137. text: `调节精度:`,
  138. font: 'normal 30px Arial',
  139. color: '#009900',
  140. strokeStyle: '#002200',
  141. x: 320,
  142. y: 115,
  143. },
  144. {
  145. text: `1% FS`,
  146. font: 'normal 30px Arial',
  147. color: '#009900',
  148. strokeStyle: '#002200',
  149. x: 460,
  150. y: 115,
  151. },
  152. {
  153. text: `调节范围:`,
  154. font: 'normal 30px Arial',
  155. color: '#009900',
  156. strokeStyle: '#002200',
  157. x: 320,
  158. y: 182,
  159. },
  160. {
  161. text: `${selectData.maxarea}`,
  162. font: 'normal 30px Arial',
  163. color: '#009900',
  164. strokeStyle: '#002200',
  165. x: 460,
  166. y: 182,
  167. },
  168. {
  169. text: `煤炭科学技术研究院有限公司研制`,
  170. font: 'normal 28px Arial',
  171. color: '#009900',
  172. strokeStyle: '#002200',
  173. x: 60,
  174. y: 302,
  175. },
  176. ];
  177. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  178. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  179. const textMaterial = new THREE.MeshBasicMaterial({
  180. map: textMap, // 设置纹理贴图
  181. transparent: true,
  182. side: THREE.DoubleSide, // 这里是双面渲染的意思
  183. });
  184. textMaterial.blending = THREE.CustomBlending;
  185. const monitorPlane = this.group?.getObjectByName('monitorText');
  186. if (monitorPlane) {
  187. monitorPlane.material = textMaterial;
  188. } else {
  189. const planeGeometry = new THREE.PlaneGeometry(560, 346); // 平面3维几何体PlaneGeometry
  190. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  191. planeMesh.name = 'monitorText';
  192. planeMesh.scale.set(0.0022, 0.0022, 0.0022);
  193. planeMesh.position.set(3.25, -0.002, -0.41);
  194. this.group?.add(planeMesh);
  195. }
  196. });
  197. }
  198. /* 风门动画 */
  199. render() {
  200. if (!this.model) {
  201. return;
  202. }
  203. if (this.isLRAnimation && this.group) {
  204. // 左右摇摆动画
  205. if (Math.abs(this.group.rotation.y) >= 0.2) {
  206. this.direction = -this.direction;
  207. this.group.rotation.y += 0.00002 * 30 * this.direction;
  208. } else {
  209. this.group.rotation.y += 0.00002 * 30 * this.direction;
  210. }
  211. }
  212. }
  213. /* 提取风门序列帧,初始化前后门动画 */
  214. initAnimation() {
  215. const windGroup = new THREE.Group();
  216. windGroup.name = 'lmTanTou';
  217. if (this.group?.children.length) {
  218. for (let i = this.group?.children.length - 1; i > -1; i--) {
  219. const obj = this.group?.children[i];
  220. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('LMtantou')) {
  221. if (obj.name.startsWith('LMtantou')) {
  222. windGroup.add(obj.clone());
  223. this.group?.remove(obj);
  224. }
  225. }
  226. }
  227. }
  228. this.group?.add(windGroup);
  229. }
  230. /* 点击风窗,风窗全屏 */
  231. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  232. this.isLRAnimation = false;
  233. if (this.animationTimer) {
  234. clearTimeout(this.animationTimer);
  235. this.animationTimer = null;
  236. }
  237. // 判断是否点击到视频
  238. intersects.find((intersect) => {
  239. const mesh = intersect.object;
  240. if (mesh.name === 'player1') {
  241. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  242. // 双击,视频放大
  243. if (this.player1) {
  244. this.player1.requestFullscreen();
  245. }
  246. }
  247. this.playerStartClickTime1 = new Date().getTime();
  248. return true;
  249. } else if (mesh.name === 'player2') {
  250. if (new Date().getTime() - this.playerStartClickTime2 < 400) {
  251. // 双击,视频放大
  252. if (this.player2) {
  253. this.player2.requestFullscreen();
  254. }
  255. }
  256. this.playerStartClickTime2 = new Date().getTime();
  257. return true;
  258. }
  259. return false;
  260. });
  261. }
  262. mouseUpModel() {
  263. // 10s后开始摆动
  264. if (!this.animationTimer && !this.isLRAnimation) {
  265. this.animationTimer = setTimeout(() => {
  266. this.isLRAnimation = true;
  267. }, 10000);
  268. }
  269. }
  270. resetModel() {
  271. clearTimeout(this.animationTimer);
  272. this.isLRAnimation = false;
  273. }
  274. // 播放动画
  275. play(flag, isDirect = false) {
  276. debugger
  277. const cfTanTou = this.group?.getObjectByName('lmTanTou') as THREE.Group;
  278. if (!cfTanTou) return;
  279. switch (flag) {
  280. case 'up':
  281. if (this.deviceRunState == 'up' || this.deviceRunState == 'center') {
  282. return;
  283. }
  284. this.deviceRunState = 'up';
  285. cfTanTou.position.setY(-0.48);
  286. if (!isDirect) {
  287. gsap.to(cfTanTou['position'], {
  288. y: 0,
  289. duration: Math.abs(cfTanTou['position']['y'] - 0) * 25,
  290. overwrite: true,
  291. });
  292. } else {
  293. cfTanTou.position.setY(0);
  294. }
  295. break;
  296. case 'center':
  297. if (this.deviceRunState == 'center') {
  298. return;
  299. }
  300. this.deviceRunState = 'center';
  301. cfTanTou.position.setY(0);
  302. if (!isDirect) {
  303. gsap.to(cfTanTou['position'], {
  304. y: -0.24,
  305. duration: Math.abs(cfTanTou['position']['y'] + 0.24) * 50,
  306. overwrite: true,
  307. });
  308. } else {
  309. cfTanTou.position.setY(-0.24);
  310. }
  311. break;
  312. case 'down':
  313. if (this.deviceRunState == 'down') {
  314. return;
  315. }
  316. this.deviceRunState = 'down';
  317. cfTanTou.position.setY(-0.24);
  318. if (!isDirect) {
  319. gsap.to(cfTanTou['position'], {
  320. y: -0.48,
  321. duration: Math.abs(cfTanTou['position']['y'] + 0.48) * 50,
  322. overwrite: true,
  323. });
  324. } else {
  325. cfTanTou.position.setY(-0.48);
  326. }
  327. break;
  328. }
  329. }
  330. mountedThree() {
  331. return new Promise((resolve) => {
  332. this.model.setModel(this.modelName).then((gltf) => {
  333. this.group = gltf.scene;
  334. this.setModalPosition();
  335. this.initAnimation();
  336. setTimeout(async () => {
  337. const videoPlayer1 = document.getElementById('cf-player1')?.getElementsByClassName('vjs-tech')[0];
  338. // const videoPlayer2 = document.getElementById('cf-player2')?.getElementsByClassName('vjs-tech')[0];
  339. if (videoPlayer1) {
  340. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  341. mesh?.scale.set(0.042, 0.036, 0.022);
  342. mesh?.position.set(-2.74, 0.03, -0.39);
  343. this.group?.add(mesh as THREE.Mesh);
  344. }
  345. // if (videoPlayer2) {
  346. // const mesh = renderVideo(this.group, videoPlayer2, 'player2');
  347. // mesh?.scale.set(0.0385, 0.028, 0.022);
  348. // mesh?.position.set(-86.77, 0.405, -9.62);
  349. // this.group?.add(mesh as THREE.Mesh);
  350. // }
  351. resolve(null);
  352. }, 0);
  353. });
  354. });
  355. }
  356. destroy() {
  357. if (this.group) {
  358. this.model.clearGroup(this.group);
  359. }
  360. this.model = null;
  361. this.group = null;
  362. }
  363. }
  364. export default lmWindRect;