dandaoFcBetZh.threejs.ts 9.0 KB

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