dandaoFc.threejs.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindow {
  5. model;
  6. modelName = 'ddFc';
  7. group: THREE.Group | null = null;
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. };
  14. player1;
  15. player2;
  16. playerStartClickTime1 = new Date().getTime();
  17. constructor(model, playerVal1) {
  18. this.model = model;
  19. this.player1 = playerVal1;
  20. }
  21. // // 重置摄像头
  22. // const resetCamera = () => {
  23. // this.model.camera.position.set(30.328, 58.993, 148.315);
  24. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  25. // this.model.orbitControls?.update();
  26. // this.model.camera.updateProjectionMatrix();
  27. // };
  28. // 设置模型位置
  29. setModalPosition() {
  30. this.group?.scale.set(22, 22, 22);
  31. this.group?.position.set(-35, 25, 15);
  32. }
  33. addFmText(selectData) {
  34. if (!this.group) {
  35. return;
  36. }
  37. const textArr = [
  38. {
  39. text: `煤矿巷道远程风窗系统`,
  40. font: 'normal 2.2rem Arial',
  41. color: '#009900',
  42. strokeStyle: '#002200',
  43. x: 90,
  44. y: 95,
  45. },
  46. {
  47. text: `过风量(m3/min):`,
  48. font: 'normal 30px Arial',
  49. color: '#009900',
  50. strokeStyle: '#002200',
  51. x: 5,
  52. y: 150,
  53. },
  54. {
  55. text: `${
  56. selectData.frontRearDifference && selectData.rearPresentValue
  57. ? Math.min(selectData.frontRearDifference, selectData.rearPresentValue)
  58. : selectData.frontRearDifference || selectData.rearPresentValue || '-'
  59. }`,
  60. font: 'normal 30px Arial',
  61. color: '#009900',
  62. strokeStyle: '#002200',
  63. x: 235,
  64. y: 150,
  65. },
  66. {
  67. text: `过风面积(m2): `,
  68. font: 'normal 30px Arial',
  69. color: '#009900',
  70. strokeStyle: '#002200',
  71. x: 5,
  72. y: 205,
  73. },
  74. {
  75. text: `${
  76. selectData.forntArea && selectData.rearArea
  77. ? Math.min(selectData.forntArea, selectData.rearArea)
  78. : selectData.forntArea || selectData.rearArea || '-'
  79. }`,
  80. font: 'normal 30px Arial',
  81. color: '#009900',
  82. strokeStyle: '#002200',
  83. x: 200,
  84. y: 205,
  85. },
  86. {
  87. text: `风窗压差(Pa):`,
  88. font: 'normal 30px Arial',
  89. color: '#009900',
  90. strokeStyle: '#002200',
  91. x: 5,
  92. y: 256,
  93. },
  94. {
  95. text: `${selectData.dataDh}`,
  96. font: 'normal 30px Arial',
  97. color: '#009900',
  98. strokeStyle: '#002200',
  99. x: 200,
  100. y: 256,
  101. },
  102. {
  103. text: `调节精度:`,
  104. font: 'normal 30px Arial',
  105. color: '#009900',
  106. strokeStyle: '#002200',
  107. x: 320,
  108. y: 150,
  109. },
  110. {
  111. text: `1% FS`,
  112. font: 'normal 30px Arial',
  113. color: '#009900',
  114. strokeStyle: '#002200',
  115. x: 460,
  116. y: 150,
  117. },
  118. {
  119. text: `调节范围:`,
  120. font: 'normal 30px Arial',
  121. color: '#009900',
  122. strokeStyle: '#002200',
  123. x: 320,
  124. y: 205,
  125. },
  126. {
  127. text: `${selectData.maxarea}`,
  128. font: 'normal 30px Arial',
  129. color: '#009900',
  130. strokeStyle: '#002200',
  131. x: 460,
  132. y: 205,
  133. },
  134. {
  135. text: `煤炭科学技术研究院有限公司研制`,
  136. font: 'normal 28px Arial',
  137. color: '#009900',
  138. strokeStyle: '#002200',
  139. x: 60,
  140. y: 302,
  141. },
  142. ];
  143. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  144. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  145. const textMaterial = new THREE.MeshBasicMaterial({
  146. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  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(526, 346); // 平面3维几何体PlaneGeometry
  157. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  158. planeMesh.name = 'monitorText';
  159. planeMesh.scale.set(0.002, 0.002, 0.002);
  160. planeMesh.position.set(3.61, 0.158, -0.23);
  161. this.group?.add(planeMesh);
  162. }
  163. });
  164. }
  165. /* 提取风门序列帧,初始化前后门动画 */
  166. initAnimation() {
  167. const meshArr01: THREE.Object3D[] = [];
  168. this.group?.children.forEach((obj) => {
  169. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  170. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  171. meshArr01.push(obj);
  172. }
  173. });
  174. this.windowsActionArr.frontWindow = meshArr01;
  175. }
  176. play(rotationParam, flag) {
  177. if (!this.windowsActionArr.frontWindow) {
  178. return;
  179. }
  180. if (flag === 1) {
  181. // 前风窗动画
  182. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  183. gsap.to(mesh.rotation, {
  184. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  185. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  186. overwrite: true,
  187. });
  188. });
  189. } else if (flag === 0) {
  190. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  191. gsap.to(mesh.rotation, {
  192. y: 0,
  193. overwrite: true,
  194. });
  195. });
  196. }
  197. }
  198. /* 点击风窗,风窗全屏 */
  199. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  200. this.isLRAnimation = false;
  201. if (this.animationTimer) {
  202. clearTimeout(this.animationTimer);
  203. this.animationTimer = null;
  204. }
  205. // 判断是否点击到视频
  206. intersects.find((intersect) => {
  207. const mesh = intersect.object;
  208. if (mesh.name === 'player1') {
  209. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  210. // 双击,视频放大
  211. if (this.player1) {
  212. this.player1.requestFullscreen();
  213. }
  214. }
  215. this.playerStartClickTime1 = new Date().getTime();
  216. return true;
  217. }
  218. return false;
  219. });
  220. }
  221. mouseUpModel() {
  222. // 10s后开始摆动
  223. if (!this.animationTimer && !this.isLRAnimation) {
  224. this.animationTimer = setTimeout(() => {
  225. this.isLRAnimation = true;
  226. }, 10000);
  227. }
  228. }
  229. /* 风门动画 */
  230. render() {
  231. if (!this.model) {
  232. return;
  233. }
  234. if (this.isLRAnimation && this.group) {
  235. // 左右摇摆动画
  236. if (Math.abs(this.group.rotation.y) >= 0.2) {
  237. this.direction = -this.direction;
  238. this.group.rotation.y += 0.00005 * 30 * this.direction;
  239. } else {
  240. this.group.rotation.y += 0.00005 * 30 * this.direction;
  241. }
  242. }
  243. }
  244. mountedThree() {
  245. return new Promise((resolve) => {
  246. this.model.setModel(this.modelName).then((gltf) => {
  247. this.group = gltf.scene;
  248. this.setModalPosition();
  249. this.initAnimation();
  250. setTimeout(async () => {
  251. const videoPlayer1 = document.getElementById('fc-player1')?.getElementsByClassName('vjs-tech')[0];
  252. if (videoPlayer1) {
  253. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  254. mesh?.scale.set(0.0382, 0.028, 0.022);
  255. mesh?.position.set(-2.008, 0.148, -0.22);
  256. this.group?.add(mesh);
  257. }
  258. resolve(null);
  259. }, 0);
  260. });
  261. });
  262. }
  263. destroy() {
  264. this.model.clearGroup(this.group);
  265. this.windowsActionArr.frontWindow = undefined;
  266. this.model = null;
  267. this.group = null;
  268. }
  269. }
  270. export default singleWindow;