dandaoFcBetZh.threejs.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.frontArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  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.frontArea
  64. ? Number(`${selectData.frontArea}`).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. if (this.animationTimer) {
  199. clearTimeout(this.animationTimer);
  200. this.animationTimer = null;
  201. }
  202. // 判断是否点击到视频
  203. intersects.find((intersect) => {
  204. const mesh = intersect.object;
  205. if (mesh.name === 'player1') {
  206. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  207. // 双击,视频放大
  208. if (this.player1) {
  209. this.player1.requestFullscreen();
  210. }
  211. }
  212. this.playerStartClickTime1 = new Date().getTime();
  213. return true;
  214. }
  215. return false;
  216. });
  217. }
  218. mouseUpModel() {}
  219. /* 风门动画 */
  220. render() {
  221. if (!this.model) {
  222. return;
  223. }
  224. }
  225. async initCamera(dom1?) {
  226. const videoPlayer1 = dom1;
  227. let monitorPlane: THREE.Mesh | null = null;
  228. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  229. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  230. const textMaterial = new THREE.MeshBasicMaterial({
  231. map: textMap, // 设置纹理贴图
  232. transparent: true,
  233. side: THREE.DoubleSide, // 这里是双面渲染的意思
  234. });
  235. textMaterial.blending = THREE.CustomBlending;
  236. monitorPlane = this.group?.getObjectByName('noPlayer');
  237. if (monitorPlane) {
  238. monitorPlane.material = textMaterial;
  239. } else {
  240. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  241. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  242. textMaterial.dispose();
  243. planeGeometry.dispose();
  244. }
  245. const videoPlayer = this.group.getObjectByName('player1');
  246. if (videoPlayer) {
  247. this.model.clearMesh(videoPlayer);
  248. this.group.remove(videoPlayer);
  249. }
  250. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  251. if (noPlayer1) {
  252. this.model.clearMesh(noPlayer1);
  253. this.group.remove(noPlayer1);
  254. }
  255. if (!videoPlayer1 && videoPlayer1 === null) {
  256. monitorPlane.name = 'noPlayer1';
  257. monitorPlane.scale.set(0.015, 0.007, 0.011);
  258. monitorPlane.position.set(4.04, 0.02, -0.46);
  259. this.group?.add(monitorPlane);
  260. } else if (videoPlayer1) {
  261. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  262. if (mesh) {
  263. mesh?.scale.set(-0.038, 0.029, 1);
  264. mesh?.position.set(-4.302, 0.15, -0.23);
  265. mesh.rotation.y = -Math.PI;
  266. this.group.add(mesh);
  267. }
  268. }
  269. }
  270. mountedThree() {
  271. return new Promise((resolve) => {
  272. this.model.setGLTFModel(['ddFc-bet-zh'], this.group).then(() => {
  273. this.setModalPosition();
  274. this.initAnimation();
  275. this.addLight();
  276. resolve(null);
  277. });
  278. });
  279. }
  280. destroy() {
  281. this.model.clearGroup(this.group);
  282. this.windowsActionArr.frontWindow = undefined;
  283. this.model = null;
  284. this.group = null;
  285. }
  286. }
  287. export default singleWindowBetZh;