duishe.threejs.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 { sysOrgCode } = useGlobSetting();
  78. const textArr = [
  79. {
  80. text: `${sysOrgCode == 'zlnxnywwek' ? selectData.strinstallpos : '远程控制自动风门'}`, //王洼二矿需要特殊处理
  81. font: 'normal 32px Arial',
  82. color: '#009900',
  83. strokeStyle: '#002200',
  84. x: sysOrgCode == 'zlnxnywwek' ? (380 - selectData.strinstallpos.length * 22) / 2 : 120,
  85. y: 75,
  86. },
  87. {
  88. text: `风量(m³/min):`,
  89. font: 'normal 29px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 12,
  93. y: 133,
  94. },
  95. {
  96. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  97. font: 'normal 29px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 311,
  101. y: 133,
  102. },
  103. {
  104. text: `风速(m/s): `,
  105. font: 'normal 29px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 12,
  109. y: 192,
  110. },
  111. {
  112. text: `${selectData.va ? selectData.va : '-'}`,
  113. font: 'normal 29px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 310,
  117. y: 192,
  118. },
  119. {
  120. text: `断面积(㎡):`,
  121. font: 'normal 29px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 12,
  125. y: 247,
  126. },
  127. {
  128. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  129. font: 'normal 29px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: 310,
  133. y: 247,
  134. },
  135. {
  136. text: `煤炭科学技术研究院有限公司研制`,
  137. font: 'normal 28px Arial',
  138. color: '#009900',
  139. strokeStyle: '#002200',
  140. x: 50,
  141. y: 302,
  142. },
  143. ];
  144. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  145. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  146. const textMaterial = new THREE.MeshBasicMaterial({
  147. map: textMap, // 设置纹理贴图
  148. transparent: true,
  149. side: THREE.DoubleSide, // 这里是双面渲染的意思
  150. });
  151. textMaterial.blending = THREE.CustomBlending;
  152. const monitorPlane = this.group?.getObjectByName('monitorText');
  153. if (monitorPlane) {
  154. monitorPlane.material = textMaterial;
  155. } else {
  156. const planeGeometry = new THREE.PlaneGeometry(5.6, 3.46); // 平面3维几何体PlaneGeometry
  157. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  158. planeMesh.name = 'monitorText';
  159. planeMesh.scale.set(0.26, 0.26, 0.26);
  160. planeMesh.position.set(-2.52, 0.038, -0.42);
  161. this.group?.add(planeMesh);
  162. }
  163. });
  164. }
  165. /* 风门动画 */
  166. render() {
  167. if (!this.model) {
  168. return;
  169. }
  170. }
  171. initAnimation() {
  172. const dsmove = this.group.getObjectByName('dsmove');
  173. const dsmoveLine = dsmove?.getObjectByName('probe');
  174. const line = dsmoveLine?.getObjectByName('line_');
  175. if (line) {
  176. line.material.side = THREE.DoubleSide;
  177. line.material.opacity = 1;
  178. line.material.depthWrite = true;
  179. this.lineLight = gsap.to(line.material, {
  180. opacity: 0,
  181. duration: 0.3,
  182. ease: 'easeQutQuad',
  183. repeat: -1,
  184. yoyo: true,
  185. paused: true,
  186. });
  187. this.lineLight.play();
  188. }
  189. const dsgdObj = this.group.getObjectByName('dsgd');
  190. const dsgdLine = dsgdObj?.getObjectByName('fout_ceasourment_01')?.getObjectByName('cea08');
  191. if (dsgdLine) {
  192. dsgdLine.material.side = THREE.DoubleSide;
  193. dsgdLine.material.opacity = 1;
  194. dsgdLine.material.depthWrite = true;
  195. const gdLine = gsap.to(dsgdLine.material, {
  196. opacity: 0,
  197. duration: 0.3,
  198. ease: 'easeQutQuad',
  199. repeat: -1,
  200. yoyo: true,
  201. paused: true,
  202. });
  203. gdLine.play();
  204. }
  205. }
  206. /* 点击风窗,风窗全屏 */
  207. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  208. if (this.animationTimer) {
  209. clearTimeout(this.animationTimer);
  210. this.animationTimer = null;
  211. }
  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. }
  228. mouseUpModel() {}
  229. resetModel() {
  230. clearTimeout(this.animationTimer);
  231. }
  232. // 播放动画
  233. play(flag, isDirect = false) {
  234. if (this.deviceType !== 'move') return;
  235. const dsmove = this.group?.getObjectByName('dsmove') as THREE.Object3D;
  236. const dsTanTou = dsmove?.getObjectByName('probe') as THREE.Object3D;
  237. if (!dsTanTou || (flag != 'start' && flag != 'moni' && flag != 'up' && flag != 'down')) return;
  238. dsTanTou.position.setY(0.45);
  239. if (flag == 'moni') {
  240. gsap.to(dsTanTou['position'], {
  241. y: -0.32,
  242. duration: Math.abs(dsTanTou['position']['y'] + 0.32) * 21,
  243. overwrite: true,
  244. onComplete: function () {
  245. setTimeout(() => {
  246. gsap.to(dsTanTou['position'], {
  247. y: 0.45,
  248. duration: 0.77 * 21,
  249. overwrite: true,
  250. // onComplete: function () {
  251. // _this.isRun = false;
  252. // },
  253. });
  254. }, 5000);
  255. },
  256. });
  257. // setTimeout(() => {
  258. // }, Math.abs(dsTanTou['position']['y'] - 0.32) * 21);
  259. } else {
  260. if (!isDirect) {
  261. if (this.isRun) return;
  262. if (flag == 'down') {
  263. dsTanTou.position.setY(0.45);
  264. this.isRun = true;
  265. gsap.to(dsTanTou['position'], {
  266. y: -0.32,
  267. duration: Math.abs(dsTanTou['position']['y'] + 0.32) * 31,
  268. overwrite: true,
  269. });
  270. } else if (flag == 'up') {
  271. new Promise((resove) => {
  272. gsap.to(dsTanTou['position'], {
  273. y: 0.45,
  274. duration: 0.77 * 31,
  275. overwrite: true,
  276. onComplete: function () {
  277. resove(null);
  278. },
  279. });
  280. }).then(() => {
  281. this.isRun = false;
  282. });
  283. }
  284. } else {
  285. if (!this.isRun) return;
  286. const dsDown = gsap.getById('dsDown');
  287. const dsUp = gsap.getById('dsUp');
  288. if (dsDown) {
  289. dsDown.pause();
  290. dsDown.kill();
  291. }
  292. if (dsUp) {
  293. dsUp.pause();
  294. dsUp.kill();
  295. }
  296. dsTanTou.position.setY(0);
  297. this.isRun = false;
  298. }
  299. }
  300. }
  301. async initCamera(dom1?) {
  302. const videoPlayer1 = dom1;
  303. let monitorPlane: THREE.Mesh | null = null;
  304. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  305. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  306. const textMaterial = new THREE.MeshBasicMaterial({
  307. map: textMap, // 设置纹理贴图
  308. transparent: true,
  309. side: THREE.DoubleSide, // 这里是双面渲染的意思
  310. });
  311. textMaterial.blending = THREE.CustomBlending;
  312. monitorPlane = this.group?.getObjectByName('noPlayer');
  313. if (monitorPlane) {
  314. monitorPlane.material = textMaterial;
  315. } else {
  316. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  317. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  318. textMaterial.dispose();
  319. planeGeometry.dispose();
  320. }
  321. const videoPlayer = this.group.getObjectByName('player1');
  322. if (videoPlayer) {
  323. this.model.clearMesh(videoPlayer);
  324. this.group.remove(videoPlayer);
  325. }
  326. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  327. if (noPlayer1) {
  328. this.model.clearMesh(noPlayer1);
  329. this.group.remove(noPlayer1);
  330. }
  331. if (!videoPlayer1 && videoPlayer1 === null) {
  332. monitorPlane.name = 'noPlayer1';
  333. monitorPlane.scale.set(0.0125, 0.007, 0.012);
  334. monitorPlane.position.set(2.69, 0.02, -0.39);
  335. this.group?.add(monitorPlane);
  336. } else if (videoPlayer1) {
  337. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  338. if (mesh) {
  339. mesh?.scale.set(0.042, 0.036, 1);
  340. mesh?.position.set(2.685, 0.016, -0.38);
  341. mesh.rotation.y = -Math.PI;
  342. this.group.add(mesh);
  343. }
  344. }
  345. }
  346. resetModal() {
  347. const fmThreeBase = this.group.getObjectByName('dscf');
  348. if (fmThreeBase) {
  349. const screenObj1 = fmThreeBase.getObjectByName('对象208');
  350. const screenObj2 = fmThreeBase.getObjectByName('Box542');
  351. if (screenObj1) screenObj1.visible = false;
  352. if (screenObj2) screenObj2.visible = false;
  353. }
  354. }
  355. mountedThree() {
  356. return new Promise((resolve) => {
  357. this.model
  358. .setGLTFModel([this.modelName, 'dsgd', 'dsmove'], this.group)
  359. .then((object) => {
  360. // this.group.name = this.modelName;
  361. this.resetModal();
  362. this.setModalPosition();
  363. this.initAnimation();
  364. this.addLight();
  365. if (this.deviceType) this.setModelType(this.deviceType);
  366. resolve(null);
  367. })
  368. .catch(() => {
  369. resolve(null);
  370. });
  371. });
  372. }
  373. destroy() {
  374. if (this.lineLight && this.lineLight.kill) this.lineLight.kill();
  375. if (this.group) {
  376. this.model.clearGroup(this.group);
  377. }
  378. this.model = null;
  379. this.group = null;
  380. }
  381. }
  382. export default dsWindRect;