dandaoFcBet.threejs.ts 7.8 KB

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