dandaoFcXk.threejs.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import * as THREE from 'three';
  2. import { getTextCanvas } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindowXk {
  5. model;
  6. modelName = 'xkFcGroup';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. };
  14. constructor(model) {
  15. this.model = model;
  16. this.group.name = 'xkFcGroup';
  17. }
  18. addLight = () => {
  19. if (!this.group || !this.group) return;
  20. const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
  21. directionalLight.position.set(-437, 61, 559);
  22. this.group.add(directionalLight);
  23. };
  24. // 设置模型位置
  25. setModalPosition() {
  26. this.group?.scale.set(22, 22, 22);
  27. this.group?.position.set(-35, 25, 15);
  28. }
  29. addMonitorText(selectData) {
  30. if (!this.group) {
  31. return;
  32. }
  33. const textArr = [
  34. {
  35. text: `远程定量调节自动风窗`,
  36. font: 'normal 30px Arial',
  37. color: '#009900',
  38. strokeStyle: '#002200',
  39. x: 100,
  40. y: 95,
  41. },
  42. {
  43. text: `${selectData.OpenDegree ? '开度值(%)' : selectData.forntArea ? '过风面积(m2)' : '过风面积(m2)'}:`,
  44. font: 'normal 30px Arial',
  45. color: '#009900',
  46. strokeStyle: '#002200',
  47. x: 5,
  48. y: 145,
  49. },
  50. {
  51. text: selectData.OpenDegree
  52. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  53. : selectData.forntArea
  54. ? Number(`${selectData.forntArea}`).toFixed(2)
  55. : '-',
  56. font: 'normal 30px Arial',
  57. color: '#009900',
  58. strokeStyle: '#002200',
  59. x: 330,
  60. y: 145,
  61. },
  62. {
  63. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态:'}:`,
  64. font: 'normal 30px Arial',
  65. color: '#009900',
  66. strokeStyle: '#002200',
  67. x: 5,
  68. y: 200,
  69. },
  70. {
  71. text: `${
  72. selectData.frontRearDP
  73. ? selectData.frontRearDP
  74. : selectData.windSpeed
  75. ? selectData.windSpeed
  76. : selectData.netStatus == '0'
  77. ? '断开'
  78. : '连接'
  79. }`,
  80. font: 'normal 30px Arial',
  81. color: '#009900',
  82. strokeStyle: '#002200',
  83. x: 330,
  84. y: 200,
  85. },
  86. {
  87. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  88. font: 'normal 30px Arial',
  89. color: '#009900',
  90. strokeStyle: '#002200',
  91. x: 5,
  92. y: 250,
  93. },
  94. {
  95. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  96. font: 'normal 30px Arial',
  97. color: '#009900',
  98. strokeStyle: '#002200',
  99. x: 330,
  100. y: 250,
  101. },
  102. {
  103. text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
  104. font: 'normal 28px Arial',
  105. color: '#009900',
  106. strokeStyle: '#002200',
  107. x: History_Type['type'] == 'remote' ? 90 : 30,
  108. y: 300,
  109. },
  110. ];
  111. getTextCanvas(750, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  112. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  113. const textMaterial = new THREE.MeshBasicMaterial({
  114. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  115. map: textMap, // 设置纹理贴图
  116. transparent: true,
  117. side: THREE.DoubleSide, // 这里是双面渲染的意思
  118. });
  119. textMap.dispose();
  120. textMaterial.blending = THREE.CustomBlending;
  121. const monitorPlane = this.group?.getObjectByName('monitorText');
  122. if (monitorPlane) {
  123. monitorPlane.material = textMaterial;
  124. } else {
  125. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  126. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  127. planeMesh.name = 'monitorText';
  128. planeMesh.scale.set(0.0038, 0.004, 0.0038);
  129. planeMesh.position.set(4.44, -0.165, -0.46);
  130. this.group?.add(planeMesh);
  131. }
  132. });
  133. }
  134. /* 提取风门序列帧,初始化前后门动画 */
  135. initAnimation() {}
  136. play(rotationParam, flag) {
  137. if (!this.windowsActionArr.frontWindow) {
  138. return;
  139. }
  140. if (flag === 1) {
  141. // 前风窗动画
  142. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  143. gsap.to(mesh.rotation, {
  144. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  145. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  146. overwrite: true,
  147. });
  148. });
  149. } else if (flag === 0) {
  150. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  151. gsap.to(mesh.rotation, {
  152. y: 0,
  153. overwrite: true,
  154. });
  155. });
  156. }
  157. }
  158. /* 点击风窗,风窗全屏 */
  159. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  160. this.isLRAnimation = false;
  161. if (this.animationTimer) {
  162. clearTimeout(this.animationTimer);
  163. this.animationTimer = null;
  164. }
  165. }
  166. mouseUpModel() {
  167. // 10s后开始摆动
  168. if (!this.animationTimer && !this.isLRAnimation) {
  169. this.animationTimer = setTimeout(() => {
  170. this.isLRAnimation = true;
  171. }, 10000);
  172. }
  173. }
  174. /* 风门动画 */
  175. render() {
  176. if (!this.model) {
  177. return;
  178. }
  179. if (this.isLRAnimation && this.group) {
  180. // 左右摇摆动画
  181. if (Math.abs(this.group.rotation.y) >= 0.2) {
  182. this.direction = -this.direction;
  183. this.group.rotation.y += 0.00002 * 30 * this.direction;
  184. } else {
  185. this.group.rotation.y += 0.00002 * 30 * this.direction;
  186. }
  187. }
  188. }
  189. mountedThree() {
  190. return new Promise((resolve) => {
  191. this.model.setGLTFModel(['Fc-Xk'], this.group).then(() => {
  192. debugger;
  193. this.setModalPosition();
  194. this.initAnimation();
  195. this.addLight();
  196. resolve(null);
  197. });
  198. });
  199. }
  200. destroy() {
  201. this.model.clearGroup(this.group);
  202. this.model = null;
  203. this.group = null;
  204. }
  205. }
  206. export default singleWindowXk;