dandaoFc.threejs.ts 9.0 KB

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