duishe.threejs.ts 12 KB

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