dandaoFcBet.threejs.ts 8.3 KB

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