dandaoFc.threejs.ts 10 KB

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