dandaoFc.threejs.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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: 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}`,
  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}`,
  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. font: 'normal 30px Arial',
  105. color: '#009900',
  106. strokeStyle: '#002200',
  107. x: 440,
  108. y: 145,
  109. },
  110. {
  111. text: `调节范围:`,
  112. font: 'normal 30px Arial',
  113. color: '#009900',
  114. strokeStyle: '#002200',
  115. x: 330,
  116. y: 200,
  117. },
  118. {
  119. text: `0~6`,
  120. font: 'normal 30px Arial',
  121. color: '#009900',
  122. strokeStyle: '#002200',
  123. x: 470,
  124. y: 200,
  125. },
  126. {
  127. text: `煤炭科学技术研究院有限公司研制`,
  128. font: 'normal 28px Arial',
  129. color: '#009900',
  130. strokeStyle: '#002200',
  131. x: 60,
  132. y: 302,
  133. },
  134. ];
  135. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  136. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  137. const textMaterial = new THREE.MeshBasicMaterial({
  138. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  139. map: textMap, // 设置纹理贴图
  140. transparent: true,
  141. side: THREE.DoubleSide, // 这里是双面渲染的意思
  142. });
  143. textMap.dispose();
  144. textMaterial.blending = THREE.CustomBlending;
  145. const monitorPlane = this.group?.getObjectByName('monitorText');
  146. if (monitorPlane) {
  147. monitorPlane.material = textMaterial;
  148. } else {
  149. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  150. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  151. planeMesh.name = 'monitorText';
  152. planeMesh.scale.set(0.0026, 0.003, 0.002);
  153. planeMesh.position.set(3.76, -0.042, -0.23);
  154. this.group?.add(planeMesh);
  155. }
  156. });
  157. }
  158. /* 提取风门序列帧,初始化前后门动画 */
  159. initAnimation() {
  160. const meshArr01: THREE.Object3D[] = [];
  161. this.group?.children.forEach((obj) => {
  162. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  163. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  164. meshArr01.push(obj);
  165. }
  166. });
  167. this.windowsActionArr.frontWindow = meshArr01;
  168. }
  169. play(rotationParam, flag) {
  170. if (!this.windowsActionArr.frontWindow) {
  171. return;
  172. }
  173. if (flag === 1) {
  174. // 前风窗动画
  175. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  176. gsap.to(mesh.rotation, {
  177. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  178. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  179. overwrite: true,
  180. });
  181. });
  182. } else if (flag === 0) {
  183. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  184. gsap.to(mesh.rotation, {
  185. y: 0,
  186. overwrite: true,
  187. });
  188. });
  189. }
  190. }
  191. /* 点击风窗,风窗全屏 */
  192. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  193. this.isLRAnimation = false;
  194. if (this.animationTimer) {
  195. clearTimeout(this.animationTimer);
  196. this.animationTimer = null;
  197. }
  198. // 判断是否点击到视频
  199. intersects.find((intersect) => {
  200. const mesh = intersect.object;
  201. if (mesh.name === 'player1') {
  202. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  203. // 双击,视频放大
  204. if (this.player1) {
  205. this.player1.requestFullscreen();
  206. }
  207. }
  208. this.playerStartClickTime1 = new Date().getTime();
  209. return true;
  210. }
  211. return false;
  212. });
  213. }
  214. mouseUpModel() {
  215. // 10s后开始摆动
  216. if (!this.animationTimer && !this.isLRAnimation) {
  217. this.animationTimer = setTimeout(() => {
  218. this.isLRAnimation = true;
  219. }, 10000);
  220. }
  221. }
  222. /* 风门动画 */
  223. render() {
  224. if (!this.model) {
  225. return;
  226. }
  227. if (this.isLRAnimation && this.group) {
  228. // 左右摇摆动画
  229. if (Math.abs(this.group.rotation.y) >= 0.2) {
  230. this.direction = -this.direction;
  231. this.group.rotation.y += 0.00002 * 30 * this.direction;
  232. } else {
  233. this.group.rotation.y += 0.00002 * 30 * this.direction;
  234. }
  235. }
  236. }
  237. async initCamera(dom1?) {
  238. const videoPlayer1 = dom1;
  239. let monitorPlane: THREE.Mesh | null = null;
  240. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  241. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  242. const textMaterial = new THREE.MeshBasicMaterial({
  243. map: textMap, // 设置纹理贴图
  244. transparent: true,
  245. side: THREE.DoubleSide, // 这里是双面渲染的意思
  246. });
  247. textMaterial.blending = THREE.CustomBlending;
  248. monitorPlane = this.group?.getObjectByName('noPlayer');
  249. if (monitorPlane) {
  250. monitorPlane.material = textMaterial;
  251. } else {
  252. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  253. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  254. textMaterial.dispose();
  255. planeGeometry.dispose();
  256. }
  257. const videoPlayer = this.group.getObjectByName('player1');
  258. if (videoPlayer) {
  259. this.model.clearMesh(videoPlayer);
  260. this.group.remove(videoPlayer);
  261. }
  262. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  263. if (noPlayer1) {
  264. this.model.clearMesh(noPlayer1);
  265. this.group.remove(noPlayer1);
  266. }
  267. if (!videoPlayer1 && videoPlayer1 === null) {
  268. monitorPlane.name = 'noPlayer1';
  269. monitorPlane.scale.set(0.011, 0.0055, 0.011);
  270. monitorPlane.position.set(-2.01, 0.15, -0.23);
  271. this.group?.add(monitorPlane);
  272. } else if (videoPlayer1) {
  273. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  274. if (mesh) {
  275. mesh?.scale.set(0.0382, 0.028, 0.022);
  276. mesh?.position.set(-2.008, 0.148, -0.22);
  277. this.group.add(mesh);
  278. }
  279. }
  280. }
  281. mountedThree() {
  282. return new Promise((resolve) => {
  283. this.model.setGLTFModel(['ddFc']).then((gltf) => {
  284. this.group = gltf[0];
  285. this.setModalPosition();
  286. this.initAnimation();
  287. resolve(null);
  288. });
  289. });
  290. }
  291. destroy() {
  292. this.model.clearGroup(this.group);
  293. this.windowsActionArr.frontWindow = undefined;
  294. this.model = null;
  295. this.group = null;
  296. }
  297. }
  298. export default singleWindow;