duishe.threejs.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. import { useGlobSetting } from '/@/hooks/setting';
  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 dsWindRect {
  9. model;
  10. modelName = 'dscf';
  11. group: THREE.Object3D = new THREE.Object3D();
  12. deviceType;
  13. animationTimer;
  14. isLRAnimation = true;
  15. direction = 1;
  16. player1;
  17. playerStartClickTime1 = new Date().getTime();
  18. lineLight;
  19. isRun = false;
  20. constructor(model) {
  21. this.model = model;
  22. this.group.name = this.modelName;
  23. }
  24. setModelType(deviceType) {
  25. // deviceType = 'two';
  26. // debugger;
  27. this.deviceType = deviceType;
  28. const dsgdObj = this.group.getObjectByName('dsgd');
  29. const dsmoveObj = this.group.getObjectByName('dsmove');
  30. if (deviceType == 'move') {
  31. if (dsgdObj) dsgdObj.visible = false;
  32. if (dsmoveObj) dsmoveObj.visible = true;
  33. } else {
  34. if (dsgdObj) dsgdObj.visible = true;
  35. if (dsmoveObj) dsmoveObj.visible = false;
  36. if (deviceType == 'four' && dsgdObj) {
  37. dsgdObj.getObjectByName('fout_ceasourment_01').visible = true;
  38. }
  39. if (deviceType == 'two' && dsgdObj) {
  40. dsgdObj.getObjectByName('fout_ceasourment_01').visible = false;
  41. }
  42. }
  43. }
  44. addLight() {
  45. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  46. directionalLight.position.set(42.4, 248, 86);
  47. this.group.add(directionalLight);
  48. directionalLight.target = this.group;
  49. // gui.add(directionalLight.position, 'x', -500, 500);
  50. // gui.add(directionalLight.position, 'y', -500, 500);
  51. // gui.add(directionalLight.position, 'z', -500, 500);
  52. const spotLight = new THREE.SpotLight();
  53. spotLight.angle = Math.PI / 16;
  54. spotLight.penumbra = 0;
  55. spotLight.castShadow = true;
  56. spotLight.distance = 0;
  57. spotLight.position.set(-470, -500, 500);
  58. this.group.add(spotLight);
  59. spotLight.shadow.camera.near = 0.5; // default
  60. spotLight.shadow.camera.far = 1000; // default
  61. spotLight.shadow.focus = 1;
  62. spotLight.shadow.bias = -0.000002;
  63. // gui.add(spotLight.position, 'x', -500, 500);
  64. // gui.add(spotLight.position, 'y', -500, 500);
  65. // gui.add(spotLight.position, 'z', -500, 500);
  66. // gui.add(spotLight, 'distance', 0, 1000);
  67. }
  68. // 设置模型位置
  69. setModalPosition() {
  70. this.group?.scale.set(22, 22, 22);
  71. this.group?.position.set(-10, 25, 20);
  72. }
  73. addMonitorText(selectData) {
  74. if (!this.group) {
  75. return;
  76. }
  77. const screenDownText = VENT_PARAM['modalText']
  78. ? // @ts-ignore
  79. VENT_PARAM['modalText']
  80. : // @ts-ignore
  81. History_Type['type'] == 'remote'
  82. ? `国能神东煤炭集团监制`
  83. : '煤科通安(北京)智控科技有限公司研制';
  84. const screenDownTextX = 80 - (screenDownText.length - 10) * 6;
  85. const { sysOrgCode } = useGlobSetting();
  86. const textArr = [
  87. {
  88. text: `${sysOrgCode == 'zlnxnywwek' ? selectData.strinstallpos : '测风实时数据'}`, //王洼二矿需要特殊处理
  89. font: 'normal 32px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: sysOrgCode == 'zlnxnywwek' ? (380 - selectData.strinstallpos.length * 22) / 2 : 120,
  93. y: 75,
  94. },
  95. {
  96. text: `风量(m³/min):`,
  97. font: 'normal 29px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 12,
  101. y: 133,
  102. },
  103. {
  104. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  105. font: 'normal 29px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 311,
  109. y: 133,
  110. },
  111. {
  112. text: `风速(m/s): `,
  113. font: 'normal 29px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 12,
  117. y: 192,
  118. },
  119. {
  120. text: `${selectData.va ? selectData.va : '-'}`,
  121. font: 'normal 29px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 310,
  125. y: 192,
  126. },
  127. {
  128. text: `断面积(㎡):`,
  129. font: 'normal 29px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: 12,
  133. y: 247,
  134. },
  135. {
  136. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  137. font: 'normal 29px Arial',
  138. color: '#009900',
  139. strokeStyle: '#002200',
  140. x: 310,
  141. y: 247,
  142. },
  143. {
  144. text: screenDownText,
  145. font: 'normal 28px Arial',
  146. color: '#009900',
  147. strokeStyle: '#002200',
  148. x: screenDownTextX,
  149. y: 325,
  150. },
  151. ];
  152. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  153. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  154. const textMaterial = new THREE.MeshBasicMaterial({
  155. map: textMap, // 设置纹理贴图
  156. transparent: true,
  157. side: THREE.DoubleSide, // 这里是双面渲染的意思
  158. });
  159. textMaterial.blending = THREE.CustomBlending;
  160. const monitorPlane = this.group?.getObjectByName('monitorText');
  161. if (monitorPlane) {
  162. monitorPlane.material = textMaterial;
  163. } else {
  164. const planeGeometry = new THREE.PlaneGeometry(5.6, 3.46); // 平面3维几何体PlaneGeometry
  165. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  166. planeMesh.name = 'monitorText';
  167. planeMesh.scale.set(0.26, 0.26, 0.26);
  168. planeMesh.position.set(-2.52, 0.038, -0.42);
  169. this.group?.add(planeMesh);
  170. }
  171. });
  172. }
  173. /* 风门动画 */
  174. render() {
  175. if (!this.model) {
  176. return;
  177. }
  178. }
  179. initAnimation() {
  180. const dsmove = this.group.getObjectByName('dsmove');
  181. const dsmoveLine = dsmove?.getObjectByName('probe');
  182. const line = dsmoveLine?.getObjectByName('line_');
  183. if (line) {
  184. line.material.side = THREE.DoubleSide;
  185. line.material.opacity = 1;
  186. line.material.depthWrite = true;
  187. this.lineLight = gsap.to(line.material, {
  188. opacity: 0,
  189. duration: 0.3,
  190. ease: 'easeQutQuad',
  191. repeat: -1,
  192. yoyo: true,
  193. paused: true,
  194. });
  195. this.lineLight.play();
  196. }
  197. const dsgdObj = this.group.getObjectByName('dsgd');
  198. const dsgdLine = dsgdObj?.getObjectByName('fout_ceasourment_01')?.getObjectByName('cea08');
  199. if (dsgdLine) {
  200. dsgdLine.material.side = THREE.DoubleSide;
  201. dsgdLine.material.opacity = 1;
  202. dsgdLine.material.depthWrite = true;
  203. const gdLine = gsap.to(dsgdLine.material, {
  204. opacity: 0,
  205. duration: 0.3,
  206. ease: 'easeQutQuad',
  207. repeat: -1,
  208. yoyo: true,
  209. paused: true,
  210. });
  211. gdLine.play();
  212. }
  213. }
  214. /* 点击风窗,风窗全屏 */
  215. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  216. if (this.animationTimer) {
  217. clearTimeout(this.animationTimer);
  218. this.animationTimer = null;
  219. }
  220. // 判断是否点击到视频
  221. intersects.find((intersect) => {
  222. const mesh = intersect.object;
  223. // if (mesh.name === 'player1') {
  224. // if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  225. // // 双击,视频放大
  226. // if (this.player1) {
  227. // this.player1.requestFullscreen();
  228. // }
  229. // }
  230. // this.playerStartClickTime1 = new Date().getTime();
  231. // return true;
  232. // }
  233. return false;
  234. });
  235. }
  236. mouseUpModel() {}
  237. resetModel() {
  238. clearTimeout(this.animationTimer);
  239. }
  240. // 播放动画
  241. play(flag, isDirect = false) {
  242. if (this.deviceType !== 'move') return;
  243. const dsmove = this.group?.getObjectByName('dsmove') as THREE.Object3D;
  244. const dsTanTou = dsmove?.getObjectByName('probe') as THREE.Object3D;
  245. if (!dsTanTou || (flag != 'start' && flag != 'moni' && flag != 'up' && flag != 'down')) return;
  246. dsTanTou.position.setY(0.45);
  247. if (flag == 'moni') {
  248. gsap.to(dsTanTou['position'], {
  249. y: -0.32,
  250. duration: Math.abs(dsTanTou['position']['y'] + 0.32) * 21,
  251. overwrite: true,
  252. onComplete: function () {
  253. setTimeout(() => {
  254. gsap.to(dsTanTou['position'], {
  255. y: 0.45,
  256. duration: 0.77 * 21,
  257. overwrite: true,
  258. // onComplete: function () {
  259. // _this.isRun = false;
  260. // },
  261. });
  262. }, 5000);
  263. },
  264. });
  265. // setTimeout(() => {
  266. // }, Math.abs(dsTanTou['position']['y'] - 0.32) * 21);
  267. } else {
  268. if (!isDirect) {
  269. if (this.isRun) return;
  270. if (flag == 'down') {
  271. dsTanTou.position.setY(0.45);
  272. this.isRun = true;
  273. gsap.to(dsTanTou['position'], {
  274. y: -0.32,
  275. duration: Math.abs(dsTanTou['position']['y'] + 0.32) * 31,
  276. overwrite: true,
  277. });
  278. } else if (flag == 'up') {
  279. new Promise((resove) => {
  280. gsap.to(dsTanTou['position'], {
  281. y: 0.45,
  282. duration: 0.77 * 31,
  283. overwrite: true,
  284. onComplete: function () {
  285. resove(null);
  286. },
  287. });
  288. }).then(() => {
  289. this.isRun = false;
  290. });
  291. }
  292. } else {
  293. if (!this.isRun) return;
  294. const dsDown = gsap.getById('dsDown');
  295. const dsUp = gsap.getById('dsUp');
  296. if (dsDown) {
  297. dsDown.pause();
  298. dsDown.kill();
  299. }
  300. if (dsUp) {
  301. dsUp.pause();
  302. dsUp.kill();
  303. }
  304. dsTanTou.position.setY(0);
  305. this.isRun = false;
  306. }
  307. }
  308. }
  309. async initCamera(dom1?) {
  310. const videoPlayer1 = dom1;
  311. let monitorPlane: THREE.Mesh | null = null;
  312. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  313. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  314. const textMaterial = new THREE.MeshBasicMaterial({
  315. map: textMap, // 设置纹理贴图
  316. transparent: true,
  317. side: THREE.DoubleSide, // 这里是双面渲染的意思
  318. });
  319. textMaterial.blending = THREE.CustomBlending;
  320. monitorPlane = this.group?.getObjectByName('noPlayer');
  321. if (monitorPlane) {
  322. monitorPlane.material = textMaterial;
  323. } else {
  324. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  325. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  326. textMaterial.dispose();
  327. planeGeometry.dispose();
  328. }
  329. const videoPlayer = this.group.getObjectByName('player1');
  330. if (videoPlayer) {
  331. this.model.clearMesh(videoPlayer);
  332. this.group.remove(videoPlayer);
  333. }
  334. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  335. if (noPlayer1) {
  336. this.model.clearMesh(noPlayer1);
  337. this.group.remove(noPlayer1);
  338. }
  339. if (!videoPlayer1 && videoPlayer1 === null) {
  340. monitorPlane.name = 'noPlayer1';
  341. monitorPlane.scale.set(0.0125, 0.007, 0.012);
  342. monitorPlane.position.set(2.69, 0.02, -0.39);
  343. this.group?.add(monitorPlane);
  344. } else if (videoPlayer1) {
  345. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  346. if (mesh) {
  347. mesh?.scale.set(0.042, 0.036, 1);
  348. mesh?.position.set(2.685, 0.016, -0.38);
  349. mesh.rotation.y = -Math.PI;
  350. this.group.add(mesh);
  351. }
  352. }
  353. }
  354. resetModal() {
  355. const fmThreeBase = this.group.getObjectByName('dscf');
  356. if (fmThreeBase) {
  357. const screenObj1 = fmThreeBase.getObjectByName('对象208');
  358. const screenObj2 = fmThreeBase.getObjectByName('Box542');
  359. if (screenObj1) screenObj1.visible = false;
  360. if (screenObj2) screenObj2.visible = false;
  361. }
  362. }
  363. mountedThree() {
  364. return new Promise((resolve) => {
  365. this.model
  366. .setGLTFModel([this.modelName, 'dsgd', 'dsmove'], this.group)
  367. .then((object) => {
  368. // this.group.name = this.modelName;
  369. this.resetModal();
  370. this.setModalPosition();
  371. this.initAnimation();
  372. this.addLight();
  373. if (this.deviceType) this.setModelType(this.deviceType);
  374. resolve(null);
  375. })
  376. .catch(() => {
  377. resolve(null);
  378. });
  379. });
  380. }
  381. destroy() {
  382. if (this.lineLight && this.lineLight.kill) this.lineLight.kill();
  383. if (this.group) {
  384. this.model.clearGroup(this.group);
  385. }
  386. this.model = null;
  387. this.group = null;
  388. }
  389. }
  390. export default dsWindRect;