shuangdaoFc.threejs.ts 11 KB

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