dandaoFc.threejs.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.Object3D = new THREE.Object3D();
  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) {
  18. this.model = model;
  19. // this.group.name = 'ddFc';
  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. addMonitorText(selectData) {
  34. if (!this.group) {
  35. return;
  36. }
  37. const textArr = [
  38. {
  39. text: `远程定量调节自动风窗`,
  40. font: 'normal 30px Arial',
  41. color: '#009900',
  42. strokeStyle: '#002200',
  43. x: 110,
  44. y: 90,
  45. },
  46. {
  47. text: `${selectData.OpenDegree ? '开度值' : selectData.forntArea ? '过风面积(m2)' : '过风面积(m2)'}:`,
  48. font: 'normal 30px Arial',
  49. color: '#009900',
  50. strokeStyle: '#002200',
  51. x: 5,
  52. y: 145,
  53. },
  54. {
  55. text: selectData.OpenDegree
  56. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  57. : selectData.forntArea
  58. ? Number(`${selectData.forntArea}`).toFixed(2)
  59. : '-',
  60. font: 'normal 30px Arial',
  61. color: '#009900',
  62. strokeStyle: '#002200',
  63. x: 330,
  64. y: 145,
  65. },
  66. {
  67. text: `${selectData.frontRearDP ? '风窗压差(Pa):' : selectData.windSpeed ? '风速:' : '通信状态:'}:`,
  68. font: 'normal 30px Arial',
  69. color: '#009900',
  70. strokeStyle: '#002200',
  71. x: 5,
  72. y: 200,
  73. },
  74. {
  75. text: `${
  76. selectData.frontRearDP
  77. ? selectData.frontRearDP
  78. : selectData.windSpeed
  79. ? selectData.windSpeed
  80. : selectData.netStatus == '0'
  81. ? '断开'
  82. : '连接'
  83. }`,
  84. font: 'normal 30px Arial',
  85. color: '#009900',
  86. strokeStyle: '#002200',
  87. x: 330,
  88. y: 200,
  89. },
  90. {
  91. text: `${selectData.fWindowM3 ? '过风量(m³/min):' : '风窗道数:'} `,
  92. font: 'normal 30px Arial',
  93. color: '#009900',
  94. strokeStyle: '#002200',
  95. x: 5,
  96. y: 256,
  97. },
  98. {
  99. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  100. font: 'normal 30px Arial',
  101. color: '#009900',
  102. strokeStyle: '#002200',
  103. x: 330,
  104. y: 256,
  105. },
  106. {
  107. text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
  108. font: 'normal 28px Arial',
  109. color: '#009900',
  110. strokeStyle: '#002200',
  111. x: History_Type['type'] == 'remote' ? 120 : 60,
  112. y: 302,
  113. },
  114. ];
  115. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  116. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  117. const textMaterial = new THREE.MeshBasicMaterial({
  118. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  119. map: textMap, // 设置纹理贴图
  120. transparent: true,
  121. side: THREE.DoubleSide, // 这里是双面渲染的意思
  122. });
  123. textMap.dispose();
  124. textMaterial.blending = THREE.CustomBlending;
  125. const monitorPlane = this.group?.getObjectByName('monitorText');
  126. if (monitorPlane) {
  127. monitorPlane.material = textMaterial;
  128. } else {
  129. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  130. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  131. planeMesh.name = 'monitorText';
  132. planeMesh.scale.set(0.0025, 0.003, 0.002);
  133. planeMesh.position.set(3.71, -0.042, -0.23);
  134. this.group?.add(planeMesh);
  135. }
  136. });
  137. }
  138. /* 提取风门序列帧,初始化前后门动画 */
  139. initAnimation() {
  140. const meshArr01: THREE.Object3D[] = [];
  141. this.group.getObjectByName('ddFc')?.children.forEach((obj) => {
  142. if (obj.type === 'Mesh' && ((obj.name && obj.name.startsWith('shanye')) || obj.name.startsWith('FCshanye'))) {
  143. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  144. meshArr01.push(obj);
  145. }
  146. });
  147. this.windowsActionArr.frontWindow = meshArr01;
  148. }
  149. play(rotationParam, flag) {
  150. if (this.windowsActionArr.frontWindow.length <= 0) {
  151. return;
  152. }
  153. if (flag === 1) {
  154. // 前风窗动画
  155. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  156. gsap.to(mesh.rotation, {
  157. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  158. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  159. overwrite: true,
  160. });
  161. });
  162. } else if (flag === 0) {
  163. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  164. gsap.to(mesh.rotation, {
  165. y: 0,
  166. overwrite: true,
  167. });
  168. });
  169. }
  170. }
  171. /* 点击风窗,风窗全屏 */
  172. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  173. this.isLRAnimation = false;
  174. if (this.animationTimer) {
  175. clearTimeout(this.animationTimer);
  176. this.animationTimer = null;
  177. }
  178. // 判断是否点击到视频
  179. intersects.find((intersect) => {
  180. const mesh = intersect.object;
  181. if (mesh.name === 'player1') {
  182. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  183. // 双击,视频放大
  184. if (this.player1) {
  185. this.player1.requestFullscreen();
  186. }
  187. }
  188. this.playerStartClickTime1 = new Date().getTime();
  189. return true;
  190. }
  191. return false;
  192. });
  193. }
  194. mouseUpModel() {
  195. // 10s后开始摆动
  196. if (!this.animationTimer && !this.isLRAnimation) {
  197. this.animationTimer = setTimeout(() => {
  198. this.isLRAnimation = true;
  199. }, 10000);
  200. }
  201. }
  202. /* 风门动画 */
  203. render() {
  204. if (!this.model) {
  205. return;
  206. }
  207. if (this.isLRAnimation && this.group) {
  208. // 左右摇摆动画
  209. if (Math.abs(this.group.rotation.y) >= 0.2) {
  210. this.direction = -this.direction;
  211. this.group.rotation.y += 0.00002 * 30 * this.direction;
  212. } else {
  213. this.group.rotation.y += 0.00002 * 30 * this.direction;
  214. }
  215. }
  216. }
  217. async initCamera(dom1) {
  218. const videoPlayer1 = dom1;
  219. let monitorPlane: THREE.Mesh | null = null;
  220. const textArr = [
  221. {
  222. text: `无信号输入`,
  223. font: 'normal 40px Arial',
  224. color: '#009900',
  225. strokeStyle: '#002200',
  226. x: 170,
  227. y: 40,
  228. },
  229. ];
  230. const canvas = await getTextCanvas(320, 180, textArr, null);
  231. if (canvas) {
  232. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  233. const textMaterial = new THREE.MeshBasicMaterial({
  234. map: textMap, // 设置纹理贴图
  235. transparent: true,
  236. side: THREE.DoubleSide, // 这里是双面渲染的意思
  237. });
  238. textMaterial.blending = THREE.CustomBlending;
  239. monitorPlane = this.group?.getObjectByName('noPlayer');
  240. if (monitorPlane) {
  241. monitorPlane.material = textMaterial;
  242. } else {
  243. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  244. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  245. textMaterial.dispose();
  246. planeGeometry.dispose();
  247. }
  248. }
  249. const videoPlayer = this.group.getObjectByName('player1');
  250. if (videoPlayer) {
  251. this.model.clearMesh(videoPlayer);
  252. this.group.remove(videoPlayer);
  253. }
  254. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  255. if (noPlayer1) {
  256. this.model.clearMesh(noPlayer1);
  257. this.group.remove(noPlayer1);
  258. }
  259. if (!videoPlayer1 && videoPlayer1 === null) {
  260. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  261. const planeMesh = monitorPlane.clone();
  262. planeMesh.name = 'noPlayer1';
  263. planeMesh.scale.set(0.011, 0.0053, 0.012);
  264. planeMesh.position.set(-4.3, 0.13, -0.23);
  265. this.group?.add(planeMesh.clone());
  266. }
  267. } else if (videoPlayer1) {
  268. try {
  269. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  270. if (mesh) {
  271. mesh?.scale.set(0.0382, 0.028, 0.022);
  272. mesh?.position.set(-3.008, 0.148, -0.22);
  273. this.group.add(mesh);
  274. }
  275. } catch (error) {
  276. console.log('视频信号异常');
  277. }
  278. }
  279. }
  280. mountedThree(playerDom) {
  281. return new Promise((resolve) => {
  282. this.model.setGLTFModel(['ddFc'], this.group).then(() => {
  283. this.setModalPosition();
  284. this.initAnimation();
  285. resolve(null);
  286. });
  287. });
  288. }
  289. destroy() {
  290. this.model.clearGroup(this.group);
  291. this.windowsActionArr.frontWindow = undefined;
  292. this.model = null;
  293. this.group = null;
  294. }
  295. }
  296. export default singleWindow;