duisheFixed.threejs.ts 8.1 KB

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