dandaoFcBetZh.threejs.ts 9.4 KB

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