dandaoFc.threejs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.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.forntArea
  83. ? Number(`${selectData.forntArea}`).toFixed(2)
  84. : '-',
  85. font: 'normal 30px Arial',
  86. color: '#009900',
  87. strokeStyle: '#002200',
  88. x: 330,
  89. y: 145,
  90. },
  91. {
  92. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  93. font: 'normal 30px Arial',
  94. color: '#009900',
  95. strokeStyle: '#002200',
  96. x: 5,
  97. y: 200,
  98. },
  99. {
  100. text: `${
  101. selectData.frontRearDP
  102. ? selectData.frontRearDP
  103. : selectData.windSpeed
  104. ? selectData.windSpeed
  105. : selectData.netStatus == '0'
  106. ? '断开'
  107. : '连接'
  108. }`,
  109. font: 'normal 30px Arial',
  110. color: '#009900',
  111. strokeStyle: '#002200',
  112. x: 330,
  113. y: 200,
  114. },
  115. {
  116. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  117. font: 'normal 30px Arial',
  118. color: '#009900',
  119. strokeStyle: '#002200',
  120. x: 5,
  121. y: 256,
  122. },
  123. {
  124. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  125. font: 'normal 30px Arial',
  126. color: '#009900',
  127. strokeStyle: '#002200',
  128. x: 330,
  129. y: 256,
  130. },
  131. {
  132. text: screenDownText,
  133. font: 'normal 28px Arial',
  134. color: '#009900',
  135. strokeStyle: '#002200',
  136. x: screenDownTextX,
  137. y: 302,
  138. },
  139. ];
  140. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  141. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  142. const textMaterial = new THREE.MeshBasicMaterial({
  143. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  144. map: textMap, // 设置纹理贴图
  145. transparent: true,
  146. side: THREE.DoubleSide, // 这里是双面渲染的意思
  147. });
  148. textMap.dispose();
  149. textMaterial.blending = THREE.CustomBlending;
  150. const monitorPlane = this.group?.getObjectByName('monitorText');
  151. if (monitorPlane) {
  152. monitorPlane.material = textMaterial;
  153. } else {
  154. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  155. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  156. planeMesh.name = 'monitorText';
  157. planeMesh.scale.set(0.0025, 0.003, 0.002);
  158. planeMesh.position.set(3.71, -0.042, -0.23);
  159. this.group?.add(planeMesh);
  160. }
  161. });
  162. }
  163. /* 提取风门序列帧,初始化前后门动画 */
  164. initAnimation() {
  165. const meshArr01: THREE.Object3D[] = [];
  166. if (this.group) {
  167. this.group.getObjectByName('ddFc')?.children.forEach((obj) => {
  168. if (obj.type === 'Mesh' && ((obj.name && obj.name.startsWith('shanye')) || obj.name.startsWith('FCshanye'))) {
  169. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  170. meshArr01.push(obj);
  171. }
  172. });
  173. }
  174. this.windowsActionArr.frontWindow = meshArr01;
  175. }
  176. play(rotationParam, flag) {
  177. if (this.windowsActionArr.frontWindow.length <= 0) {
  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.00002 * 30 * this.direction;
  239. } else {
  240. this.group.rotation.y += 0.00002 * 30 * this.direction;
  241. }
  242. }
  243. }
  244. async initCamera(dom1) {
  245. const videoPlayer1 = dom1;
  246. let monitorPlane: THREE.Mesh | null = null;
  247. const textArr = [
  248. {
  249. text: `无信号输入`,
  250. font: 'normal 40px Arial',
  251. color: '#009900',
  252. strokeStyle: '#002200',
  253. x: 170,
  254. y: 40,
  255. },
  256. ];
  257. const canvas = await getTextCanvas(320, 180, textArr, null);
  258. if (canvas) {
  259. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  260. const textMaterial = new THREE.MeshBasicMaterial({
  261. map: textMap, // 设置纹理贴图
  262. transparent: true,
  263. side: THREE.DoubleSide, // 这里是双面渲染的意思
  264. });
  265. textMaterial.blending = THREE.CustomBlending;
  266. monitorPlane = this.group?.getObjectByName('noPlayer');
  267. if (monitorPlane) {
  268. monitorPlane.material = textMaterial;
  269. } else {
  270. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  271. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  272. textMaterial.dispose();
  273. planeGeometry.dispose();
  274. }
  275. }
  276. const videoPlayer = this.group.getObjectByName('player1');
  277. if (videoPlayer) {
  278. this.model.clearMesh(videoPlayer);
  279. this.group.remove(videoPlayer);
  280. }
  281. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  282. if (noPlayer1) {
  283. this.model.clearMesh(noPlayer1);
  284. this.group.remove(noPlayer1);
  285. }
  286. if (!videoPlayer1 && videoPlayer1 === null) {
  287. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  288. const planeMesh = monitorPlane.clone();
  289. planeMesh.name = 'noPlayer1';
  290. planeMesh.scale.set(0.011, 0.0053, 0.012);
  291. planeMesh.position.set(-4.3, 0.13, -0.23);
  292. this.group?.add(planeMesh.clone());
  293. }
  294. } else if (videoPlayer1) {
  295. try {
  296. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  297. if (mesh) {
  298. mesh?.scale.set(0.0382, 0.028, 0.022);
  299. mesh?.position.set(-3.008, 0.148, -0.22);
  300. this.group.add(mesh);
  301. }
  302. } catch (error) {
  303. console.log('视频信号异常');
  304. }
  305. }
  306. }
  307. mountedThree(playerDom) {
  308. return new Promise((resolve) => {
  309. this.model.setGLTFModel('ddFc').then((gltf) => {
  310. const fcModal = gltf[0];
  311. fcModal.name = 'ddFc';
  312. this.group?.add(fcModal);
  313. this.setModalPosition();
  314. this.initAnimation();
  315. this.addLight();
  316. resolve(null);
  317. });
  318. });
  319. }
  320. destroy() {
  321. this.model.clearGroup(this.group);
  322. this.windowsActionArr.frontWindow = undefined;
  323. this.model = null;
  324. this.group = null;
  325. }
  326. }
  327. export default singleWindow;