zhedie.threejs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. import { log } from 'console';
  5. class zdWindRect {
  6. model;
  7. modelName = 'zdcf';
  8. group: THREE.Group | null = null;
  9. mixers: THREE.AnimationMixer[] = [];
  10. animations: THREE.AnimationClip[] = [];
  11. animationAction = null as unknown as THREE.AnimationAction;
  12. animationTimer;
  13. isLRAnimation = true;
  14. direction = 1;
  15. player1;
  16. playerStartClickTime1 = new Date().getTime();
  17. constructor(model, playerVal1) {
  18. this.model = model;
  19. this.player1 = playerVal1;
  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. this.model.scene.add(this.model);
  62. }
  63. // 设置模型位置
  64. setModalPosition() {
  65. this.group?.scale.set(22, 22, 22);
  66. this.group?.position.set(-35, 25, 15);
  67. }
  68. addFmText(selectData) {
  69. if (!this.group) {
  70. return;
  71. }
  72. const textArr = [
  73. {
  74. text: `煤矿巷道远程风窗系统`,
  75. font: 'normal 2.2rem Arial',
  76. color: '#009900',
  77. strokeStyle: '#002200',
  78. x: 90,
  79. y: 95,
  80. },
  81. {
  82. text: `过风量(m3/min):`,
  83. font: 'normal 30px Arial',
  84. color: '#009900',
  85. strokeStyle: '#002200',
  86. x: 5,
  87. y: 150,
  88. },
  89. {
  90. text: `${
  91. selectData.frontRearDifference && selectData.rearPresentValue
  92. ? Math.min(selectData.frontRearDifference, selectData.rearPresentValue)
  93. : selectData.frontRearDifference || selectData.rearPresentValue || '-'
  94. }`,
  95. font: 'normal 30px Arial',
  96. color: '#009900',
  97. strokeStyle: '#002200',
  98. x: 235,
  99. y: 150,
  100. },
  101. {
  102. text: `过风面积(m2): `,
  103. font: 'normal 30px Arial',
  104. color: '#009900',
  105. strokeStyle: '#002200',
  106. x: 5,
  107. y: 205,
  108. },
  109. {
  110. text: `${
  111. selectData.forntArea && selectData.rearArea
  112. ? Math.min(selectData.forntArea, selectData.rearArea)
  113. : selectData.forntArea || selectData.rearArea || '-'
  114. }`,
  115. font: 'normal 30px Arial',
  116. color: '#009900',
  117. strokeStyle: '#002200',
  118. x: 200,
  119. y: 205,
  120. },
  121. {
  122. text: `风窗压差(Pa):`,
  123. font: 'normal 30px Arial',
  124. color: '#009900',
  125. strokeStyle: '#002200',
  126. x: 5,
  127. y: 256,
  128. },
  129. {
  130. text: `${selectData.dataDh}`,
  131. font: 'normal 30px Arial',
  132. color: '#009900',
  133. strokeStyle: '#002200',
  134. x: 200,
  135. y: 256,
  136. },
  137. {
  138. text: `调节精度:`,
  139. font: 'normal 30px Arial',
  140. color: '#009900',
  141. strokeStyle: '#002200',
  142. x: 320,
  143. y: 150,
  144. },
  145. {
  146. text: `1% FS`,
  147. font: 'normal 30px Arial',
  148. color: '#009900',
  149. strokeStyle: '#002200',
  150. x: 460,
  151. y: 150,
  152. },
  153. {
  154. text: `调节范围:`,
  155. font: 'normal 30px Arial',
  156. color: '#009900',
  157. strokeStyle: '#002200',
  158. x: 320,
  159. y: 205,
  160. },
  161. {
  162. text: `${selectData.maxarea}`,
  163. font: 'normal 30px Arial',
  164. color: '#009900',
  165. strokeStyle: '#002200',
  166. x: 460,
  167. y: 205,
  168. },
  169. {
  170. text: `煤炭科学技术研究院有限公司研制`,
  171. font: 'normal 28px Arial',
  172. color: '#009900',
  173. strokeStyle: '#002200',
  174. x: 60,
  175. y: 302,
  176. },
  177. ];
  178. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  179. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  180. const textMaterial = new THREE.MeshBasicMaterial({
  181. map: textMap, // 设置纹理贴图
  182. transparent: true,
  183. side: THREE.DoubleSide, // 这里是双面渲染的意思
  184. });
  185. textMaterial.blending = THREE.CustomBlending;
  186. const monitorPlane = this.group?.getObjectByName('monitorText');
  187. if (monitorPlane) {
  188. monitorPlane.material = textMaterial;
  189. } else {
  190. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  191. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  192. planeMesh.name = 'monitorText';
  193. planeMesh.scale.set(0.002, 0.002, 0.002);
  194. planeMesh.position.set(-3.6, -0.123, -0.41);
  195. this.group?.add(planeMesh);
  196. }
  197. });
  198. }
  199. /* 风门动画 */
  200. render() {
  201. if (!this.model) {
  202. return;
  203. }
  204. if (this.isLRAnimation && this.group) {
  205. // 左右摇摆动画
  206. if (Math.abs(this.group.rotation.y) >= 0.2) {
  207. this.direction = -this.direction;
  208. this.group.rotation.y += 0.00002 * 30 * this.direction;
  209. } else {
  210. this.group.rotation.y += 0.00002 * 30 * this.direction;
  211. }
  212. }
  213. if (this.mixers[0]) this.mixers[0]?.update(1 / 25);
  214. }
  215. /* 提取风门序列帧,初始化前后门动画 */
  216. initAnimation() {
  217. this.animationAction = this.mixers[0].clipAction(this.animations[0]);
  218. this.animationAction.clampWhenFinished = true;
  219. this.animationAction.loop = THREE.LoopOnce;
  220. debugger;
  221. console.log('模型动画-------------->', this.animationAction);
  222. }
  223. /* 点击风窗,风窗全屏 */
  224. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  225. this.isLRAnimation = false;
  226. if (this.animationTimer) {
  227. clearTimeout(this.animationTimer);
  228. this.animationTimer = null;
  229. }
  230. // 判断是否点击到视频
  231. intersects.find((intersect) => {
  232. const mesh = intersect.object;
  233. if (mesh.name === 'player1') {
  234. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  235. // 双击,视频放大
  236. if (this.player1) {
  237. this.player1.requestFullscreen();
  238. }
  239. }
  240. this.playerStartClickTime1 = new Date().getTime();
  241. return true;
  242. }
  243. return false;
  244. });
  245. }
  246. mouseUpModel() {
  247. // 10s后开始摆动
  248. if (!this.animationTimer && !this.isLRAnimation) {
  249. this.animationTimer = setTimeout(() => {
  250. this.isLRAnimation = true;
  251. }, 10000);
  252. }
  253. }
  254. resetModel() {
  255. clearTimeout(this.animationTimer);
  256. this.isLRAnimation = false;
  257. }
  258. // 播放动画
  259. play(flag, isDirect = false) {
  260. if (flag === 'up' && this.animationAction.time <= 0) {
  261. this.animationAction?.reset();
  262. // @ts-ignore
  263. this.animationAction.time = 0;
  264. this.animations[0].duration = 200 / 25;
  265. this.mixers[0].timeScale = 0.15;
  266. this.animationAction?.play();
  267. } else if (flag === 'center' && this.animationAction.time <= 8) {
  268. this.animationAction?.reset();
  269. // @ts-ignore
  270. this.animationAction.time = 200 / 25; // 8
  271. this.animations[0].duration = 350 / 25;
  272. this.mixers[0].timeScale = 0.15;
  273. this.animationAction?.play();
  274. } else if (flag === 'down' && this.animationAction.time <= 14) {
  275. this.animationAction?.reset();
  276. // @ts-ignore
  277. this.animationAction.time = 350 / 25; // 14
  278. this.animations[0].duration = 530 / 25;
  279. this.mixers[0].timeScale = 0.12;
  280. this.animationAction?.play();
  281. }
  282. // else if ((flag === 'reset' && this.animationAction.time != 0 && this.animationAction.time <= 18) || (flag === 'up' && this.animationAction.time == 18)) {
  283. // debugger;
  284. // this.animationAction?.reset();
  285. // // @ts-ignore
  286. // this.animationAction.time = 450 / 25; //
  287. // this.animations[0].duration = 530 / 25;
  288. // this.mixers[0].timeScale = 0.15;
  289. // this.animationAction?.play();
  290. // }
  291. }
  292. mountedThree() {
  293. return new Promise((resolve) => {
  294. this.model.setModel(this.modelName).then((gltf) => {
  295. this.group = gltf.scene;
  296. if (gltf.animations && gltf.animations.length > 0) {
  297. console.log('[ 解析后的模型动画 ] >', gltf.animations);
  298. gltf.animations.forEach((animation) => {
  299. const mixer = new THREE.AnimationMixer(gltf.scene);
  300. this.mixers.push(mixer);
  301. this.animations.push(animation);
  302. });
  303. }
  304. this.setModalPosition();
  305. this.initAnimation();
  306. setTimeout(async () => {
  307. const videoPlayer1 = document.getElementById('cf-player1')?.getElementsByClassName('vjs-tech')[0];
  308. if (videoPlayer1) {
  309. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  310. mesh?.scale.set(0.0385, 0.028, 0.022);
  311. mesh?.position.set(4.792, -0.16, -0.4);
  312. this.group?.add(mesh);
  313. }
  314. resolve(null);
  315. }, 0);
  316. });
  317. });
  318. }
  319. destroy() {
  320. if (this.mixers[0] && this.group) {
  321. this.mixers[0].uncacheClip(this.animationAction.getClip());
  322. this.mixers[0].uncacheAction(this.animationAction.getClip(), this.group);
  323. this.mixers[0].uncacheRoot(this.group as THREE.Group);
  324. this.model.clearGroup(this.group);
  325. }
  326. this.model = null;
  327. this.group = null;
  328. }
  329. }
  330. export default zdWindRect;