duishe.threejs.ts 12 KB

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