shuangdaoFc.threejs.ts 12 KB

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