dandaoFcYjl.threejs.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindowYjl {
  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, 1);
  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.03, 0.032, 0.03);
  138. planeMesh.position.set(42.41, 4.58, -2.67);
  139. this.group?.add(planeMesh);
  140. }
  141. });
  142. }
  143. /* 提取风门序列帧,初始化前后门动画 */
  144. initAnimation() {
  145. const sdFcModal = this.group.getObjectByName('ddFc-yjl');
  146. if (!sdFcModal) return;
  147. // 初始化窗的动画
  148. const fengchuang = sdFcModal?.getObjectByName('FengChuang');
  149. const meshArr01: THREE.Mesh[] = [];
  150. if (fengchuang) {
  151. fengchuang.getObjectByName('group4')?.children.forEach((item: THREE.Object3D) => {
  152. item.children.forEach((obj: THREE.Mesh) => {
  153. if (obj.name && obj.name.startsWith('ted__FCshanye')) {
  154. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  155. meshArr01.push(obj);
  156. }
  157. });
  158. });
  159. this.windowsActionArr.frontWindow = meshArr01;
  160. }
  161. }
  162. play(rotationParam, flag) {
  163. if (!this.windowsActionArr.frontWindow) {
  164. return;
  165. }
  166. if (flag === 1) {
  167. // 前风窗动画
  168. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  169. gsap.to(mesh.rotation, {
  170. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  171. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  172. overwrite: true,
  173. });
  174. });
  175. } else if (flag === 0) {
  176. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  177. gsap.to(mesh.rotation, {
  178. y: 0,
  179. overwrite: true,
  180. });
  181. });
  182. }
  183. }
  184. /* 点击风窗,风窗全屏 */
  185. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  186. this.isLRAnimation = false;
  187. if (this.animationTimer) {
  188. clearTimeout(this.animationTimer);
  189. this.animationTimer = null;
  190. }
  191. // 判断是否点击到视频
  192. intersects.find((intersect) => {
  193. const mesh = intersect.object;
  194. if (mesh.name === 'player1') {
  195. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  196. // 双击,视频放大
  197. if (this.player1) {
  198. this.player1.requestFullscreen();
  199. }
  200. }
  201. this.playerStartClickTime1 = new Date().getTime();
  202. return true;
  203. }
  204. return false;
  205. });
  206. }
  207. mouseUpModel() {
  208. // 10s后开始摆动
  209. if (!this.animationTimer && !this.isLRAnimation) {
  210. this.animationTimer = setTimeout(() => {
  211. this.isLRAnimation = true;
  212. }, 10000);
  213. }
  214. }
  215. /* 风门动画 */
  216. render() {
  217. if (!this.model) {
  218. return;
  219. }
  220. if (this.isLRAnimation && this.group) {
  221. // 左右摇摆动画
  222. if (Math.abs(this.group.rotation.y) >= 0.2) {
  223. this.direction = -this.direction;
  224. this.group.rotation.y += 0.00002 * 30 * this.direction;
  225. } else {
  226. this.group.rotation.y += 0.00002 * 30 * this.direction;
  227. }
  228. }
  229. }
  230. mountedThree() {
  231. return new Promise((resolve) => {
  232. this.model.setGLTFModel(['ddFc-yjl'], this.group).then(() => {
  233. this.setModalPosition();
  234. this.initAnimation();
  235. resolve(null);
  236. });
  237. });
  238. }
  239. destroy() {
  240. this.model.clearGroup(this.group);
  241. this.windowsActionArr.frontWindow = undefined;
  242. this.model = null;
  243. this.group = null;
  244. }
  245. }
  246. export default singleWindowYjl;