dandaoFc.threejs.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindow {
  5. model;
  6. modelName = 'ddFc';
  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 = 'ddFc';
  20. }
  21. // // 重置摄像头
  22. // const resetCamera = () => {
  23. // this.model.camera.position.set(30.328, 58.993, 148.315);
  24. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  25. // this.model.orbitControls?.update();
  26. // this.model.camera.updateProjectionMatrix();
  27. // };
  28. // 设置模型位置
  29. setModalPosition() {
  30. this.group?.scale.set(22, 22, 22);
  31. this.group?.position.set(-35, 25, 15);
  32. }
  33. addMonitorText(selectData) {
  34. if (!this.group) {
  35. return;
  36. }
  37. const textArr = [
  38. {
  39. text: `远程定量调节自动风窗`,
  40. font: 'normal 30px Arial',
  41. color: '#009900',
  42. strokeStyle: '#002200',
  43. x: 110,
  44. y: 90,
  45. },
  46. {
  47. text: `过风量(m3/min):`,
  48. font: 'normal 30px Arial',
  49. color: '#009900',
  50. strokeStyle: '#002200',
  51. x: 5,
  52. y: 145,
  53. },
  54. {
  55. text: selectData.forntm3 ? Number(`${selectData.forntm3}`).toFixed(0) : '-',
  56. font: 'normal 30px Arial',
  57. color: '#009900',
  58. strokeStyle: '#002200',
  59. x: 225,
  60. y: 145,
  61. },
  62. {
  63. text: `过风面积(m2): `,
  64. font: 'normal 30px Arial',
  65. color: '#009900',
  66. strokeStyle: '#002200',
  67. x: 5,
  68. y: 200,
  69. },
  70. {
  71. text: `${selectData.forntArea ? selectData.forntArea : '-'}`,
  72. font: 'normal 30px Arial',
  73. color: '#009900',
  74. strokeStyle: '#002200',
  75. x: 200,
  76. y: 200,
  77. },
  78. {
  79. text: `风窗压差(Pa):`,
  80. font: 'normal 30px Arial',
  81. color: '#009900',
  82. strokeStyle: '#002200',
  83. x: 5,
  84. y: 256,
  85. },
  86. {
  87. text: `${selectData.frontRearDifference ? selectData.frontRearDifference : '-'}`,
  88. font: 'normal 30px Arial',
  89. color: '#009900',
  90. strokeStyle: '#002200',
  91. x: 200,
  92. y: 256,
  93. },
  94. {
  95. text: `调节精度:`,
  96. font: 'normal 30px Arial',
  97. color: '#009900',
  98. strokeStyle: '#002200',
  99. x: 330,
  100. y: 145,
  101. },
  102. {
  103. // text: `1% FS`,
  104. text: `-`,
  105. font: 'normal 30px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 440,
  109. y: 145,
  110. },
  111. {
  112. text: `调节范围:`,
  113. font: 'normal 30px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 330,
  117. y: 200,
  118. },
  119. {
  120. text: selectData.maxarea ? `0~${selectData.maxarea}` : '-',
  121. font: 'normal 30px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 470,
  125. y: 200,
  126. },
  127. {
  128. text: `煤炭科学技术研究院有限公司研制`,
  129. font: 'normal 28px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: 60,
  133. y: 302,
  134. },
  135. ];
  136. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  137. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  138. const textMaterial = new THREE.MeshBasicMaterial({
  139. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  140. map: textMap, // 设置纹理贴图
  141. transparent: true,
  142. side: THREE.DoubleSide, // 这里是双面渲染的意思
  143. });
  144. textMap.dispose();
  145. textMaterial.blending = THREE.CustomBlending;
  146. const monitorPlane = this.group?.getObjectByName('monitorText');
  147. if (monitorPlane) {
  148. monitorPlane.material = textMaterial;
  149. } else {
  150. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  151. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  152. planeMesh.name = 'monitorText';
  153. planeMesh.scale.set(0.0025, 0.003, 0.002);
  154. planeMesh.position.set(3.77, -0.042, -0.23);
  155. this.group?.add(planeMesh);
  156. }
  157. });
  158. }
  159. /* 提取风门序列帧,初始化前后门动画 */
  160. initAnimation() {
  161. const meshArr01: THREE.Object3D[] = [];
  162. this.group?.children.forEach((obj) => {
  163. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  164. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  165. meshArr01.push(obj);
  166. }
  167. });
  168. this.windowsActionArr.frontWindow = meshArr01;
  169. }
  170. play(rotationParam, flag) {
  171. if (!this.windowsActionArr.frontWindow) {
  172. return;
  173. }
  174. if (flag === 1) {
  175. // 前风窗动画
  176. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  177. gsap.to(mesh.rotation, {
  178. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  179. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  180. overwrite: true,
  181. });
  182. });
  183. } else if (flag === 0) {
  184. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  185. gsap.to(mesh.rotation, {
  186. y: 0,
  187. overwrite: true,
  188. });
  189. });
  190. }
  191. }
  192. /* 点击风窗,风窗全屏 */
  193. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  194. this.isLRAnimation = false;
  195. if (this.animationTimer) {
  196. clearTimeout(this.animationTimer);
  197. this.animationTimer = null;
  198. }
  199. // 判断是否点击到视频
  200. intersects.find((intersect) => {
  201. const mesh = intersect.object;
  202. if (mesh.name === 'player1') {
  203. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  204. // 双击,视频放大
  205. if (this.player1) {
  206. this.player1.requestFullscreen();
  207. }
  208. }
  209. this.playerStartClickTime1 = new Date().getTime();
  210. return true;
  211. }
  212. return false;
  213. });
  214. }
  215. mouseUpModel() {
  216. // 10s后开始摆动
  217. if (!this.animationTimer && !this.isLRAnimation) {
  218. this.animationTimer = setTimeout(() => {
  219. this.isLRAnimation = true;
  220. }, 10000);
  221. }
  222. }
  223. /* 风门动画 */
  224. render() {
  225. if (!this.model) {
  226. return;
  227. }
  228. if (this.isLRAnimation && this.group) {
  229. // 左右摇摆动画
  230. if (Math.abs(this.group.rotation.y) >= 0.2) {
  231. this.direction = -this.direction;
  232. this.group.rotation.y += 0.00002 * 30 * this.direction;
  233. } else {
  234. this.group.rotation.y += 0.00002 * 30 * this.direction;
  235. }
  236. }
  237. }
  238. async initCamera(dom1?) {
  239. const videoPlayer1 = dom1;
  240. let monitorPlane: THREE.Mesh | null = null;
  241. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  242. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  243. const textMaterial = new THREE.MeshBasicMaterial({
  244. map: textMap, // 设置纹理贴图
  245. transparent: true,
  246. side: THREE.DoubleSide, // 这里是双面渲染的意思
  247. });
  248. textMaterial.blending = THREE.CustomBlending;
  249. monitorPlane = this.group?.getObjectByName('noPlayer');
  250. if (monitorPlane) {
  251. monitorPlane.material = textMaterial;
  252. } else {
  253. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  254. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  255. textMaterial.dispose();
  256. planeGeometry.dispose();
  257. }
  258. const videoPlayer = this.group.getObjectByName('player1');
  259. if (videoPlayer) {
  260. this.model.clearMesh(videoPlayer);
  261. this.group.remove(videoPlayer);
  262. }
  263. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  264. if (noPlayer1) {
  265. this.model.clearMesh(noPlayer1);
  266. this.group.remove(noPlayer1);
  267. }
  268. if (!videoPlayer1 && videoPlayer1 === null) {
  269. monitorPlane.name = 'noPlayer1';
  270. monitorPlane.scale.set(0.011, 0.0055, 0.011);
  271. monitorPlane.position.set(-2.01, 0.15, -0.23);
  272. this.group?.add(monitorPlane);
  273. } else if (videoPlayer1) {
  274. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  275. if (mesh) {
  276. mesh?.scale.set(0.0382, 0.028, 0.022);
  277. mesh?.position.set(-2.008, 0.148, -0.22);
  278. this.group.add(mesh);
  279. }
  280. }
  281. }
  282. mountedThree() {
  283. return new Promise((resolve) => {
  284. this.model.setGLTFModel(['ddFc']).then((gltf) => {
  285. this.group = gltf[0];
  286. this.setModalPosition();
  287. this.initAnimation();
  288. resolve(null);
  289. });
  290. });
  291. }
  292. destroy() {
  293. this.model.clearGroup(this.group);
  294. this.windowsActionArr.frontWindow = undefined;
  295. this.model = null;
  296. this.group = null;
  297. }
  298. }
  299. export default singleWindow;