duisheFixed.threejs.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. // import * as dat from 'dat.gui';
  5. // const gui = new dat.GUI();
  6. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  7. class fixedWindRect {
  8. model;
  9. modelName = 'fixedCf';
  10. group: THREE.Object3D = new THREE.Object3D();
  11. animationTimer;
  12. isLRAnimation = true;
  13. direction = 1;
  14. lineLight;
  15. constructor(model) {
  16. this.model = model;
  17. this.group.name = this.modelName;
  18. }
  19. addLight() {
  20. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  21. directionalLight.position.set(64, 215, 500);
  22. directionalLight.target = this.group;
  23. this.group.add(directionalLight);
  24. const pointLight2 = new THREE.PointLight(0xffffff, 2, 300);
  25. pointLight2.position.set(-113, 29, 10);
  26. // light2.castShadow = true
  27. pointLight2.shadow.bias = -0.05;
  28. this.group.add(pointLight2);
  29. // gui.add(pointLight2.position, 'x', -1000, 1000);
  30. // gui.add(pointLight2.position, 'y', -1000, 1000);
  31. // gui.add(pointLight2.position, 'z', -1000, 1000);
  32. // gui.add(spotLight.position, 'x', -500, 500);
  33. // gui.add(spotLight.position, 'y', -500, 500);
  34. // gui.add(spotLight.position, 'z', -500, 500);
  35. // gui.add(spotLight, 'distance', 0, 1000);
  36. }
  37. // 设置模型位置
  38. setModalPosition() {
  39. this.group?.scale.set(22, 22, 22);
  40. this.group?.position.set(-10, 25, 20);
  41. }
  42. addMonitorText(selectData) {
  43. if (!this.group) {
  44. return;
  45. }
  46. const screenDownText = VENT_PARAM['modalText']
  47. ? VENT_PARAM['modalText']
  48. : History_Type['type'] == 'remote'
  49. ? `国能神东煤炭集团监制`
  50. : '煤炭科学技术研究院有限公司研制';
  51. const screenDownTextX = 110 - (screenDownText.length - 10) * 6;
  52. const textArr = [
  53. {
  54. text: `测风实时数据`,
  55. font: 'normal 32px Arial',
  56. color: '#009900',
  57. strokeStyle: '#002200',
  58. x: 160,
  59. y: 75,
  60. },
  61. {
  62. text: `风量(m³/min):`,
  63. font: 'normal 29px Arial',
  64. color: '#009900',
  65. strokeStyle: '#002200',
  66. x: 10,
  67. y: 140,
  68. },
  69. {
  70. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  71. font: 'normal 29px Arial',
  72. color: '#009900',
  73. strokeStyle: '#002200',
  74. x: 361,
  75. y: 140,
  76. },
  77. {
  78. text: `风速(m/s): `,
  79. font: 'normal 29px Arial',
  80. color: '#009900',
  81. strokeStyle: '#002200',
  82. x: 10,
  83. y: 202,
  84. },
  85. {
  86. text: `${selectData.va ? selectData.va : '-'}`,
  87. font: 'normal 29px Arial',
  88. color: '#009900',
  89. strokeStyle: '#002200',
  90. x: 360,
  91. y: 202,
  92. },
  93. {
  94. text: `断面积(㎡):`,
  95. font: 'normal 29px Arial',
  96. color: '#009900',
  97. strokeStyle: '#002200',
  98. x: 10,
  99. y: 272,
  100. },
  101. {
  102. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  103. font: 'normal 29px Arial',
  104. color: '#009900',
  105. strokeStyle: '#002200',
  106. x: 360,
  107. y: 272,
  108. },
  109. {
  110. text: screenDownText,
  111. font: 'normal 28px Arial',
  112. color: '#009900',
  113. strokeStyle: '#002200',
  114. x: screenDownTextX,
  115. y: 328,
  116. },
  117. ];
  118. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  119. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  120. const textMaterial = new THREE.MeshBasicMaterial({
  121. map: textMap, // 设置纹理贴图
  122. transparent: true,
  123. side: THREE.DoubleSide, // 这里是双面渲染的意思
  124. });
  125. textMaterial.blending = THREE.CustomBlending;
  126. const monitorPlane = this.group?.getObjectByName('monitorText');
  127. if (monitorPlane) {
  128. monitorPlane.material = textMaterial;
  129. } else {
  130. const planeGeometry = new THREE.PlaneGeometry(5.6, 3.46); // 平面3维几何体PlaneGeometry
  131. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  132. planeMesh.name = 'monitorText';
  133. planeMesh.scale.set(0.23, 0.23, 0.23);
  134. planeMesh.position.set(-2.38, 0.068, -0.4);
  135. this.group?.add(planeMesh);
  136. }
  137. });
  138. }
  139. initAnimation() {
  140. const line = this.group?.getObjectByName('line_');
  141. if (line) {
  142. line.material.side = THREE.DoubleSide;
  143. line.material.opacity = 1;
  144. line.material.depthWrite = true;
  145. this.lineLight = gsap.to(line.material, {
  146. opacity: 0,
  147. duration: 0.3,
  148. ease: 'easeQutQuad',
  149. repeat: -1,
  150. yoyo: true,
  151. paused: true,
  152. });
  153. this.lineLight.play();
  154. }
  155. }
  156. async initCamera(dom1?) {
  157. const videoPlayer1 = dom1;
  158. let monitorPlane: THREE.Mesh | null = null;
  159. const textArr = [
  160. {
  161. text: `无信号输入`,
  162. font: 'normal 40px Arial',
  163. color: '#009900',
  164. strokeStyle: '#002200',
  165. x: 170,
  166. y: 40,
  167. },
  168. ];
  169. const canvas = await getTextCanvas(320, 180, textArr, null);
  170. if (canvas) {
  171. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  172. const textMaterial = new THREE.MeshBasicMaterial({
  173. map: textMap, // 设置纹理贴图
  174. transparent: true,
  175. side: THREE.DoubleSide, // 这里是双面渲染的意思
  176. });
  177. textMaterial.blending = THREE.CustomBlending;
  178. monitorPlane = this.group?.getObjectByName('noPlayer');
  179. if (monitorPlane) {
  180. monitorPlane.material = textMaterial;
  181. } else {
  182. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  183. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  184. textMaterial.dispose();
  185. planeGeometry.dispose();
  186. }
  187. }
  188. const videoPlayer = this.group.getObjectByName('player1');
  189. if (videoPlayer) {
  190. this.model.clearMesh(videoPlayer);
  191. this.group.remove(videoPlayer);
  192. }
  193. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  194. if (noPlayer1) {
  195. this.model.clearMesh(noPlayer1);
  196. this.group.remove(noPlayer1);
  197. }
  198. if (!videoPlayer1 && videoPlayer1 === null) {
  199. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  200. const planeMesh = monitorPlane.clone();
  201. planeMesh.name = 'noPlayer1';
  202. planeMesh.scale.set(0.011, 0.0053, 0.012);
  203. planeMesh.position.set(-4.3, 0.13, -0.23);
  204. this.group?.add(planeMesh.clone());
  205. }
  206. } else if (videoPlayer1) {
  207. try {
  208. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  209. if (mesh) {
  210. mesh?.scale.set(0.042, 0.036, 0.022);
  211. mesh?.position.set(2.552, 0.018, -0.4);
  212. this.group.add(mesh);
  213. }
  214. } catch (error) {
  215. console.log('视频信号异常');
  216. }
  217. }
  218. }
  219. /* 风门动画 */
  220. render() {
  221. if (!this.model) {
  222. return;
  223. }
  224. }
  225. /* 点击风窗,风窗全屏 */
  226. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  227. if (this.animationTimer) {
  228. clearTimeout(this.animationTimer);
  229. this.animationTimer = null;
  230. }
  231. // 判断是否点击到视频
  232. intersects.find((intersect) => {
  233. intersect;
  234. return false;
  235. });
  236. }
  237. mouseUpModel() {}
  238. resetModel() {
  239. clearTimeout(this.animationTimer);
  240. }
  241. mountedThree(playerDom) {
  242. return new Promise((resolve) => {
  243. this.model
  244. .setGLTFModel([this.modelName], this.group)
  245. .then(() => {
  246. this.setModalPosition();
  247. this.addLight();
  248. this.initAnimation();
  249. // this.initCamera(playerDom);
  250. resolve(null);
  251. })
  252. .catch(() => {
  253. resolve(null);
  254. });
  255. });
  256. }
  257. destroy() {
  258. if (this.group) {
  259. this.model.clearGroup(this.group);
  260. }
  261. this.model = null;
  262. this.group = null;
  263. }
  264. }
  265. export default fixedWindRect;