dandaoFc.threejs.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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, playerVal1) {
  18. this.model = model;
  19. this.player1 = playerVal1;
  20. this.group.name = 'ddFc';
  21. }
  22. // // 重置摄像头
  23. // const resetCamera = () => {
  24. // this.model.camera.position.set(30.328, 58.993, 148.315);
  25. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  26. // this.model.orbitControls?.update();
  27. // this.model.camera.updateProjectionMatrix();
  28. // };
  29. // 设置模型位置
  30. setModalPosition() {
  31. this.group?.scale.set(22, 22, 22);
  32. this.group?.position.set(-35, 25, 15);
  33. }
  34. addMonitorText(selectData) {
  35. if (!this.group) {
  36. return;
  37. }
  38. const textArr = [
  39. {
  40. text: `远程定量调节自动风窗`,
  41. font: 'normal 30px Arial',
  42. color: '#009900',
  43. strokeStyle: '#002200',
  44. x: 110,
  45. y: 94,
  46. },
  47. {
  48. text: `过风量(m3/min):`,
  49. font: 'normal 30px Arial',
  50. color: '#009900',
  51. strokeStyle: '#002200',
  52. x: 5,
  53. y: 150,
  54. },
  55. {
  56. text: `${selectData.forntm3}`,
  57. font: 'normal 30px Arial',
  58. color: '#009900',
  59. strokeStyle: '#002200',
  60. x: 235,
  61. y: 150,
  62. },
  63. {
  64. text: `过风面积(m2): `,
  65. font: 'normal 30px Arial',
  66. color: '#009900',
  67. strokeStyle: '#002200',
  68. x: 5,
  69. y: 205,
  70. },
  71. {
  72. text: `${selectData.forntArea}`,
  73. font: 'normal 30px Arial',
  74. color: '#009900',
  75. strokeStyle: '#002200',
  76. x: 200,
  77. y: 205,
  78. },
  79. {
  80. text: `风窗压差(Pa):`,
  81. font: 'normal 30px Arial',
  82. color: '#009900',
  83. strokeStyle: '#002200',
  84. x: 5,
  85. y: 256,
  86. },
  87. {
  88. text: `${selectData.frontRearDifference}`,
  89. font: 'normal 30px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 200,
  93. y: 256,
  94. },
  95. {
  96. text: `调节精度:`,
  97. font: 'normal 30px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 320,
  101. y: 150,
  102. },
  103. {
  104. text: `1% FS`,
  105. font: 'normal 30px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 460,
  109. y: 150,
  110. },
  111. {
  112. text: `调节范围:`,
  113. font: 'normal 30px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 320,
  117. y: 205,
  118. },
  119. {
  120. text: `0~6`,
  121. font: 'normal 30px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 460,
  125. y: 205,
  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(526, 346, 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. 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.002, 0.002, 0.002);
  153. planeMesh.position.set(3.61, 0.158, -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. mountedThree() {
  238. return new Promise((resolve) => {
  239. this.model.setGLTFModel(['ddFc']).then((gltf) => {
  240. this.group = gltf[0];
  241. this.setModalPosition();
  242. this.initAnimation();
  243. setTimeout(async () => {
  244. const videoPlayer1 = document.getElementById('fc-player1')?.getElementsByClassName('vjs-tech')[0];
  245. if (videoPlayer1) {
  246. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  247. mesh?.scale.set(0.0382, 0.028, 0.022);
  248. mesh?.position.set(-2.008, 0.148, -0.22);
  249. this.group?.add(mesh);
  250. } else {
  251. const textArr = [
  252. {
  253. text: `无信号输入`,
  254. font: 'normal 40px Arial',
  255. color: '#009900',
  256. strokeStyle: '#002200',
  257. x: 170,
  258. y: 40,
  259. },
  260. ];
  261. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  262. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  263. const textMaterial = new THREE.MeshBasicMaterial({
  264. map: textMap, // 设置纹理贴图
  265. transparent: true,
  266. side: THREE.DoubleSide, // 这里是双面渲染的意思
  267. });
  268. textMaterial.blending = THREE.CustomBlending;
  269. const monitorPlane = this.group?.getObjectByName('noPlayer');
  270. if (monitorPlane) {
  271. monitorPlane.material = textMaterial;
  272. } else {
  273. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  274. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  275. planeMesh.name = 'noPlayer';
  276. planeMesh.scale.set(0.011, 0.008, 0.011);
  277. planeMesh.position.set(-1.99, -0.18, -0.23);
  278. this.group?.add(planeMesh.clone());
  279. textMaterial.dispose();
  280. planeGeometry.dispose();
  281. }
  282. });
  283. }
  284. resolve(null);
  285. }, 0);
  286. });
  287. });
  288. }
  289. destroy() {
  290. this.model.clearGroup(this.group);
  291. this.windowsActionArr.frontWindow = undefined;
  292. this.model = null;
  293. this.group = null;
  294. }
  295. }
  296. export default singleWindow;