gate.threejs.one.sp.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import * as THREE from 'three';
  2. import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
  3. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  4. import { drawHot } from '/@/utils/threejs/util';
  5. import { useAppStore } from '/@/store/modules/app';
  6. // import * as dat from 'dat.gui';
  7. // const gui = new dat.GUI();
  8. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  9. class FmSp1 {
  10. modelName = 'fmSp1';
  11. model; //
  12. group;
  13. isLRAnimation = true; // 是否开启左右摇摆动画
  14. direction = 1; // 摇摆方向
  15. animationTimer: NodeJS.Timeout | null = null; // 摇摆开启定时器
  16. player1;
  17. player2;
  18. deviceDetailCSS3D;
  19. playerStartClickTime1 = new Date().getTime();
  20. playerStartClickTime2 = new Date().getTime();
  21. fmClock = new THREE.Clock();
  22. mixers: THREE.AnimationMixer | undefined;
  23. appStore = useAppStore();
  24. backDamperOpenMesh;
  25. backDamperClosedMesh;
  26. frontDamperOpenMesh;
  27. frontDamperClosedMesh;
  28. clipActionArr = {
  29. frontDoor: null as unknown as THREE.AnimationAction,
  30. backDoor: null as unknown as THREE.AnimationAction,
  31. };
  32. constructor(model) {
  33. this.model = model;
  34. }
  35. addLight() {
  36. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5);
  37. directionalLight.position.set(344, 690, 344);
  38. this.group?.add(directionalLight);
  39. directionalLight.target = this.group as THREE.Object3D;
  40. const pointLight2 = new THREE.PointLight(0xffeeee, 1, 300);
  41. pointLight2.position.set(-4, 10, 1.8);
  42. pointLight2.shadow.bias = 0.05;
  43. this.group?.add(pointLight2);
  44. const pointLight3 = new THREE.PointLight(0xffeeee, 1, 200);
  45. pointLight3.position.set(-0.5, -0.5, 0.75);
  46. pointLight3.shadow.bias = 0.05;
  47. this.group?.add(pointLight3);
  48. }
  49. // 重置摄像头
  50. resetCamera() {
  51. this.model.camera.far = 274;
  52. this.model.orbitControls?.update();
  53. this.model.camera.updateProjectionMatrix();
  54. }
  55. // 设置模型位置
  56. setModalPosition() {
  57. this.group?.scale.set(22, 22, 22);
  58. this.group?.position.set(-20, 20, 9);
  59. }
  60. /* 添加监控数据 */
  61. addMonitorText(selectData) {
  62. if (!this.group) {
  63. return;
  64. }
  65. const screenDownText = VENT_PARAM['modalText']
  66. ? VENT_PARAM['modalText']
  67. : History_Type['type'] == 'remote'
  68. ? `国能神东煤炭集团监制`
  69. : '煤炭科学技术研究院有限公司研制';
  70. const screenDownTextX = 90 - (screenDownText.length - 11) * 6;
  71. const textArr = [
  72. {
  73. text: `远程控制自动风门`,
  74. font: 'normal 30px Arial',
  75. color: '#00FF00',
  76. strokeStyle: '#007400',
  77. x: 120,
  78. y: 100,
  79. },
  80. {
  81. text: `净通行高度(m):`,
  82. font: 'normal 30px Arial',
  83. color: '#00FF00',
  84. strokeStyle: '#007400',
  85. x: 0,
  86. y: 155,
  87. },
  88. {
  89. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  90. font: 'normal 30px Arial',
  91. color: '#00FF00',
  92. strokeStyle: '#007400',
  93. x: 330,
  94. y: 155,
  95. },
  96. {
  97. text: `净通行宽度(m): `,
  98. font: 'normal 30px Arial',
  99. color: '#00FF00',
  100. strokeStyle: '#007400',
  101. x: 0,
  102. y: 215,
  103. },
  104. {
  105. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  106. font: 'normal 30px Arial',
  107. color: '#00FF00',
  108. strokeStyle: '#007400',
  109. x: 320,
  110. y: 215,
  111. },
  112. {
  113. text: `故障诊断:`,
  114. font: 'normal 30px Arial',
  115. color: '#00FF00',
  116. strokeStyle: '#007400',
  117. x: 0,
  118. y: 275,
  119. },
  120. {
  121. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  122. font: 'normal 30px Arial',
  123. color: '#00FF00',
  124. strokeStyle: '#007400',
  125. x: 320,
  126. y: 275,
  127. },
  128. {
  129. text: screenDownText,
  130. font: 'normal 28px Arial',
  131. color: '#00FF00',
  132. strokeStyle: '#007400',
  133. x: screenDownTextX,
  134. y: 325,
  135. },
  136. ];
  137. //
  138. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  139. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  140. textMap.colorSpace = THREE.SRGBColorSpace;
  141. const textMaterial = new THREE.MeshBasicMaterial({
  142. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  143. map: textMap, // 设置纹理贴图
  144. transparent: true,
  145. side: THREE.FrontSide, // 这里是双面渲染的意思
  146. });
  147. textMaterial.blending = THREE.CustomBlending;
  148. const monitorPlane = this.group.getObjectByName('monitorText');
  149. if (monitorPlane) {
  150. monitorPlane.material = textMaterial;
  151. } else {
  152. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  153. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  154. planeMesh.name = 'monitorText';
  155. planeMesh.scale.set(0.002, 0.002, 0.002);
  156. planeMesh.position.set(3.995, 0.67, -0.27);
  157. this.group.add(planeMesh);
  158. }
  159. textMap.dispose();
  160. });
  161. }
  162. /* 风门动画 */
  163. render() {
  164. if (!this.model) {
  165. return;
  166. }
  167. if (this.mixers && this.fmClock.running) {
  168. this.mixers.update(2);
  169. }
  170. }
  171. mouseUpModel() {}
  172. /* 提取风门序列帧,初始化前后门动画 */
  173. initAnimation() {
  174. debugger;
  175. const fmSp1 = this.group.getObjectByName('FengMen_SiShanLing').getObjectByName('men1');
  176. const tracks = this.group.animations[0].tracks;
  177. const fontTracks: any[] = [];
  178. for (let i = 0; i < tracks.length; i++) {
  179. const track = tracks[i];
  180. fontTracks.push(track);
  181. }
  182. this.mixers = new THREE.AnimationMixer(fmSp1);
  183. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  184. const frontClipAction = this.mixers.clipAction(frontDoor, fmSp1);
  185. frontClipAction.clampWhenFinished = true;
  186. frontClipAction.loop = THREE.LoopOnce;
  187. this.clipActionArr.frontDoor = frontClipAction;
  188. }
  189. // 播放动画
  190. play(handlerState, timeScale = 0.05) {
  191. if (this.clipActionArr.frontDoor) {
  192. let handler = () => {};
  193. switch (handlerState) {
  194. case 1: // 打开前门
  195. handler = () => {
  196. this.clipActionArr.frontDoor.paused = true;
  197. this.clipActionArr.frontDoor.reset();
  198. this.clipActionArr.frontDoor.time = 0;
  199. this.clipActionArr.frontDoor.timeScale = timeScale;
  200. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  201. this.clipActionArr.frontDoor.play();
  202. this.fmClock.start();
  203. // 显示打开前门文字
  204. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  205. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  206. };
  207. break;
  208. case 2: // 关闭前门
  209. handler = () => {
  210. this.clipActionArr.frontDoor.paused = true;
  211. this.clipActionArr.frontDoor.reset(); //
  212. this.clipActionArr.frontDoor.time = 1.5;
  213. this.clipActionArr.frontDoor.timeScale = -timeScale;
  214. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  215. this.clipActionArr.frontDoor.play();
  216. this.fmClock.start();
  217. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  218. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  219. };
  220. break;
  221. default:
  222. }
  223. handler();
  224. }
  225. }
  226. mountedThree(playerDom) {
  227. this.group = new THREE.Object3D();
  228. this.group.name = this.modelName;
  229. return new Promise((resolve) => {
  230. if (!this.model) {
  231. resolve(null);
  232. }
  233. this.model.setGLTFModel('fmSp1').then((gltf) => {
  234. this.group = gltf[0];
  235. this.group.name = 'fmSp1';
  236. console.log('fmSp1', this.group);
  237. this.setModalPosition();
  238. this.initAnimation();
  239. this.addLight();
  240. this.model.animate();
  241. resolve(this.model);
  242. });
  243. });
  244. }
  245. destroy() {
  246. if (this.model) {
  247. if (this.mixers) {
  248. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  249. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  250. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  251. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  252. this.mixers.uncacheRoot(this.group);
  253. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  254. }
  255. this.model.clearGroup(this.group);
  256. this.clipActionArr.backDoor = undefined;
  257. this.clipActionArr.frontDoor = undefined;
  258. this.mixers = undefined;
  259. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  260. }
  261. }
  262. }
  263. export default FmSp1;