fanLocal.threejs.single.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import * as THREE from 'three';
  2. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer';
  3. import { getTextCanvas, setModalCenter } from '/@/utils/threejs/util';
  4. import Smoke from '/@/views/vent/comment/threejs/Smoke';
  5. import gsap from 'gsap';
  6. // 本模型的上下文对象,用于实现本模型的特定功能,代码参考了旧有的 fanLocal.three.ts
  7. class ModelContext {
  8. model;
  9. modelName = 'jbfj-single';
  10. group: THREE.Object3D | null = null;
  11. fanType?: string;
  12. topSmoke?: Smoke;
  13. downSmoke?: Smoke;
  14. returnSmoke?: Smoke;
  15. topLife?: number;
  16. downLife?: number;
  17. constructor(model) {
  18. this.model = model;
  19. }
  20. addLight() {
  21. // optional implementation
  22. }
  23. mountedThree() {
  24. return new Promise((resolve) => {
  25. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  26. this.group = gltf[0];
  27. if (this.group) {
  28. setModalCenter(this.group);
  29. this.addLight();
  30. this.initFly();
  31. this.setModalPosition();
  32. resolve(null);
  33. }
  34. });
  35. });
  36. }
  37. destroy() {
  38. if (this.model) {
  39. this.model.isRender = false;
  40. this.clearFly();
  41. this.topSmoke = undefined;
  42. this.downSmoke = undefined;
  43. this.returnSmoke = undefined;
  44. // @ts-ignore
  45. this.group = undefined;
  46. this.model.destroy();
  47. }
  48. }
  49. async initFly() {
  50. const topCurve = [
  51. {
  52. path0: new THREE.Vector3(7.698, 0.398, 0.19),
  53. path1: new THREE.Vector3(-0.65, 0.398, 0.19),
  54. isSpread: true,
  55. spreadDirection: 0, //
  56. },
  57. {
  58. path0: new THREE.Vector3(-0.65, 0.398, 0.19),
  59. path1: new THREE.Vector3(-7.599, 0.398, 0.19),
  60. isSpread: true,
  61. spreadRang: 1,
  62. spreadDirection: 1, //
  63. },
  64. ];
  65. if (!this.topSmoke) {
  66. this.topSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 2, 0.35, 3.7, 100);
  67. this.topSmoke.setPath(topCurve);
  68. await this.topSmoke.setPoints();
  69. this.group?.add(this.topSmoke.points);
  70. }
  71. }
  72. playSmoke(selectData) {
  73. // debugger;
  74. if (selectData['Fan1StartStatus'] == '1') {
  75. // 主风机打开
  76. this.setSmokeFrequency('top', 40);
  77. this.runFly('top', 'open');
  78. } else {
  79. // 备风机关闭
  80. this.runFly('top', 'close');
  81. }
  82. if (selectData['Fan1StartStatus'] != '1') {
  83. this.runFly('all', 'close');
  84. }
  85. }
  86. runFly(deviceType, state) {
  87. if (state === 'open') {
  88. if (deviceType === 'top') {
  89. if (this.downSmoke && this.downSmoke.frameId) {
  90. this.downSmoke.stopSmoke();
  91. }
  92. if (this.topSmoke && !this.topSmoke.frameId) {
  93. this.topSmoke.startSmoke();
  94. }
  95. }
  96. } else {
  97. if (deviceType === 'top') {
  98. if (this.topSmoke && this.topSmoke.frameId) {
  99. this.topSmoke.stopSmoke();
  100. }
  101. }
  102. }
  103. if (deviceType === 'all' && state === 'close') {
  104. this.returnSmoke?.stopSmoke();
  105. }
  106. }
  107. setSmokeFrequency(deviceType, frequency) {
  108. const life = (frequency - 30) * 25;
  109. let duration = 0;
  110. let smoke;
  111. if (deviceType === 'top') {
  112. if (this.topLife == life) {
  113. return;
  114. }
  115. this.topLife = life;
  116. smoke = this.topSmoke;
  117. duration = (Math.abs(life - smoke.life) / 500) * 25;
  118. }
  119. if (smoke) {
  120. gsap.to(smoke, {
  121. life: life,
  122. duration: duration,
  123. ease: 'easeInCubic',
  124. overwrite: true,
  125. });
  126. }
  127. }
  128. addText(selectData) {
  129. if (!this.group) {
  130. return;
  131. }
  132. // @ts-ignore
  133. const screenDownText = VENT_PARAM['modalText']
  134. ? // @ts-ignore
  135. VENT_PARAM['modalText']
  136. : // @ts-ignore
  137. History_Type['type'] == 'remote'
  138. ? `国能神东煤炭集团监制`
  139. : '煤炭科学技术研究院有限公司研制';
  140. const screenDownTextX = 80 - (screenDownText.length - 10) * 6;
  141. const textArr = [
  142. {
  143. text: `智能局部通风机监测与控制系统`,
  144. font: 'normal 30px Arial',
  145. color: '#009900',
  146. strokeStyle: '#002200',
  147. x: 20,
  148. y: 108,
  149. },
  150. {
  151. text: `供风距离(m):`,
  152. font: 'normal 30px Arial',
  153. color: '#009900',
  154. strokeStyle: '#002200',
  155. x: 0,
  156. y: 152,
  157. },
  158. {
  159. text: `${
  160. selectData.airSupplyDistence_merge
  161. ? selectData.airSupplyDistence_merge
  162. : selectData.fchimenylength
  163. ? selectData.fchimenylength
  164. : selectData.airSupplyDistence_merge
  165. ? selectData.airSupplyDistence_merge
  166. : '-'
  167. }`,
  168. font: 'normal 30px Arial',
  169. color: '#009900',
  170. strokeStyle: '#002200',
  171. x: 228,
  172. y: 152,
  173. },
  174. {
  175. text: `风筒直径(mm): `,
  176. font: 'normal 30px Arial',
  177. color: '#009900',
  178. strokeStyle: '#002200',
  179. x: 0,
  180. y: 200,
  181. },
  182. {
  183. text: ` ${selectData.fchimenydiamlimit ? selectData.fchimenydiamlimit : selectData.ductDiameter_merge ? selectData.ductDiameter_merge : '-'}`,
  184. font: 'normal 30px Arial',
  185. color: '#009900',
  186. strokeStyle: '#002200',
  187. x: 220,
  188. y: 200,
  189. },
  190. {
  191. text: `故障诊断:`,
  192. font: 'normal 30px Arial',
  193. color: '#009900',
  194. strokeStyle: '#002200',
  195. x: 0,
  196. y: 245,
  197. },
  198. {
  199. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  200. font: 'normal 30px Arial',
  201. color: '#009900',
  202. strokeStyle: '#002200',
  203. x: 220,
  204. y: 245,
  205. },
  206. {
  207. text: `型号功率:`,
  208. font: 'normal 30px Arial',
  209. color: '#009900',
  210. strokeStyle: '#002200',
  211. x: 0,
  212. y: 285,
  213. },
  214. {
  215. text: `${selectData.model_Power_merge ? selectData.model_Power_merge : '-'}`,
  216. font: 'normal 26px Arial',
  217. color: '#009900',
  218. strokeStyle: '#002200',
  219. x: 220,
  220. y: 285,
  221. },
  222. {
  223. text: screenDownText,
  224. font: 'normal 28px Arial',
  225. color: '#009900',
  226. strokeStyle: '#002200',
  227. x: screenDownTextX,
  228. y: 325,
  229. },
  230. ];
  231. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  232. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  233. const textMaterial = new THREE.MeshBasicMaterial({
  234. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  235. map: textMap, // 设置纹理贴图
  236. transparent: true,
  237. side: THREE.FrontSide, // 这里是双面渲染的意思
  238. });
  239. textMaterial.blending = THREE.CustomBlending;
  240. const monitorPlane = this.group?.getObjectByName('monitorText');
  241. if (monitorPlane) {
  242. // @ts-ignore-next-line
  243. monitorPlane.material = textMaterial;
  244. } else {
  245. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  246. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  247. planeMesh.name = 'monitorText';
  248. planeMesh.scale.set(0.0135, 0.0135, 0.0135);
  249. planeMesh.rotation.y = -Math.PI / 2;
  250. planeMesh.position.set(-84.79, 0.82, 17.0);
  251. this.group?.add(planeMesh);
  252. }
  253. });
  254. }
  255. addCssText() {
  256. if (!this.group) return;
  257. if (!this.group.getObjectByName('text1')) {
  258. const element = document.getElementById('inputBox') as HTMLElement;
  259. if (element) {
  260. const fanLocalCSS3D = new CSS3DObject(element);
  261. fanLocalCSS3D.name = 'text1';
  262. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  263. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  264. fanLocalCSS3D.position.set(-85.68, 5.97, -3.39);
  265. this.group.add(fanLocalCSS3D);
  266. }
  267. }
  268. if (!this.group.getObjectByName('text2')) {
  269. const element = document.getElementById('outBox') as HTMLElement;
  270. if (element) {
  271. const fanLocalCSS3D = new CSS3DObject(element);
  272. fanLocalCSS3D.name = 'text2';
  273. fanLocalCSS3D.scale.set(0.15, 0.15, 0.15);
  274. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  275. fanLocalCSS3D.position.set(85.62, 17.65, 7.71);
  276. this.group.add(fanLocalCSS3D);
  277. }
  278. }
  279. if (!this.group.getObjectByName('text3')) {
  280. const element = document.getElementById('returnBox') as HTMLElement;
  281. if (element) {
  282. const fanLocalCSS3D = new CSS3DObject(element);
  283. fanLocalCSS3D.name = 'text3';
  284. fanLocalCSS3D.scale.set(0.07, 0.07, 0.07);
  285. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  286. fanLocalCSS3D.position.set(-25.97, 9.3, -15.09);
  287. this.group.add(fanLocalCSS3D);
  288. }
  289. }
  290. if (!this.group.getObjectByName('text4')) {
  291. const element = document.getElementById('gateBox') as HTMLElement;
  292. if (element) {
  293. // element.innerHTML = '';
  294. const fanLocalCSS3D = new CSS3DObject(element);
  295. fanLocalCSS3D.name = 'text4';
  296. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  297. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  298. fanLocalCSS3D.position.set(-73.13, 8.44, -23.52);
  299. this.group.add(fanLocalCSS3D);
  300. }
  301. }
  302. if (!this.group.getObjectByName('text5')) {
  303. const element = document.getElementById('windownBox') as HTMLElement;
  304. if (element) {
  305. // element.innerHTML = '';
  306. const fanLocalCSS3D = new CSS3DObject(element);
  307. fanLocalCSS3D.name = 'text5';
  308. fanLocalCSS3D.scale.set(0.07, 0.07, 0.07);
  309. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  310. fanLocalCSS3D.position.set(-28.44, 9.78, -40.42);
  311. this.group.add(fanLocalCSS3D);
  312. }
  313. }
  314. if (!this.group.getObjectByName('text7')) {
  315. const element = document.getElementById('inputBox0') as HTMLElement;
  316. if (element) {
  317. const fanLocalCSS3D = new CSS3DObject(element);
  318. fanLocalCSS3D.name = 'text7';
  319. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  320. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  321. fanLocalCSS3D.position.set(-84.23, 4.97, -18.92);
  322. this.group.add(fanLocalCSS3D);
  323. }
  324. }
  325. if (!this.group.getObjectByName('text6')) {
  326. const element = document.getElementById('inputBox1') as HTMLElement;
  327. if (element) {
  328. const fanLocalCSS3D = new CSS3DObject(element);
  329. fanLocalCSS3D.name = 'text6';
  330. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  331. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  332. fanLocalCSS3D.position.set(-84.47, 6.56, -19.47);
  333. this.group.add(fanLocalCSS3D);
  334. }
  335. }
  336. if (!this.group.getObjectByName('text8')) {
  337. const element = document.getElementById('gasBox3') as HTMLElement;
  338. if (element) {
  339. const fanLocalCSS3D = new CSS3DObject(element);
  340. fanLocalCSS3D.name = 'text8';
  341. fanLocalCSS3D.scale.set(0.03, 0.03, 0.03);
  342. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  343. fanLocalCSS3D.position.set(-90.04, 6, 5);
  344. this.group.add(fanLocalCSS3D);
  345. }
  346. }
  347. if (!this.group.getObjectByName('text9')) {
  348. const element = document.getElementById('gasBox2') as HTMLElement;
  349. if (element) {
  350. const fanLocalCSS3D = new CSS3DObject(element);
  351. fanLocalCSS3D.name = 'text9';
  352. fanLocalCSS3D.scale.set(0.07, 0.07, 0.07);
  353. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  354. fanLocalCSS3D.position.set(-8, 7.46, -35.28);
  355. this.group.add(fanLocalCSS3D);
  356. }
  357. }
  358. if (!this.group.getObjectByName('text10')) {
  359. const element = document.getElementById('gasBox1') as HTMLElement;
  360. if (element) {
  361. const fanLocalCSS3D = new CSS3DObject(element);
  362. fanLocalCSS3D.name = 'text10';
  363. fanLocalCSS3D.scale.set(0.1, 0.1, 0.1);
  364. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  365. fanLocalCSS3D.position.set(80, 9, -43);
  366. this.group.add(fanLocalCSS3D);
  367. }
  368. }
  369. }
  370. clearFly() {
  371. if (this.topSmoke) this.topSmoke.clearSmoke();
  372. if (this.downSmoke) this.downSmoke.clearSmoke();
  373. if (this.returnSmoke) this.returnSmoke.clearSmoke();
  374. }
  375. // 设置模型位置
  376. setModalPosition() {
  377. if (!this.group) return;
  378. this.group.scale.set(13, 13, 13);
  379. this.group.position.set(0, -10, -50);
  380. }
  381. }
  382. export default ModelContext;