shuangdaoFcSw.threejs.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class doubleWindow {
  5. model;
  6. modelName = 'sdFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: <THREE.Mesh[]>[],
  13. backWindow: <THREE.Mesh[]>[],
  14. };
  15. constructor(model) {
  16. this.model = model;
  17. // this.group.name = 'ddFc';
  18. }
  19. // // 重置摄像头
  20. // const resetCamera = () => {
  21. // this.model.camera.position.set(30.328, 58.993, 148.315);
  22. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  23. // this.model.orbitControls?.update();
  24. // this.model.camera.updateProjectionMatrix();
  25. // };
  26. addLight = () => {
  27. if (!this.model) return;
  28. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  29. directionalLight.position.set(-110, 150, 647);
  30. this.group?.add(directionalLight);
  31. // directionalLight.target = group;
  32. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  33. pointLight2.position.set(-101, 34, 16);
  34. pointLight2.shadow.bias = 0.05;
  35. this.group.add(pointLight2);
  36. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  37. pointLight3.position.set(19, 25, -7);
  38. pointLight3.shadow.bias = 0.05;
  39. this.group.add(pointLight3);
  40. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  41. pointLight6.position.set(51, 51, 9);
  42. pointLight6.shadow.bias = 0.05;
  43. this.group.add(pointLight6);
  44. };
  45. // 设置模型位置
  46. setModalPosition() {
  47. this.group?.scale.set(22, 22, 22);
  48. this.group?.position.set(-35, 25, 15);
  49. }
  50. addMonitorText(selectData) {
  51. if (!this.group) {
  52. return;
  53. }
  54. const screenDownText = VENT_PARAM['modalText']
  55. ? VENT_PARAM['modalText']
  56. : History_Type['type'] == 'remote'
  57. ? `国能神东煤炭集团监制`
  58. : '煤炭科学技术研究院有限公司研制';
  59. const screenDownTextX = 120 - (screenDownText.length - 10) * 6;
  60. const textArr = [
  61. {
  62. text: `远程定量调节自动风窗`,
  63. font: 'normal 30px Arial',
  64. color: '#009900',
  65. strokeStyle: '#002200',
  66. x: 110,
  67. y: 90,
  68. },
  69. {
  70. text: `${selectData.OpenDegree ? '开度值(%)' : selectData.forntArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  71. font: 'normal 30px Arial',
  72. color: '#009900',
  73. strokeStyle: '#002200',
  74. x: 5,
  75. y: 145,
  76. },
  77. {
  78. text: selectData.OpenDegree
  79. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  80. : selectData.forntArea
  81. ? Number(`${selectData.forntArea}`).toFixed(2)
  82. : '-',
  83. font: 'normal 30px Arial',
  84. color: '#009900',
  85. strokeStyle: '#002200',
  86. x: 330,
  87. y: 145,
  88. },
  89. {
  90. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  91. font: 'normal 30px Arial',
  92. color: '#009900',
  93. strokeStyle: '#002200',
  94. x: 5,
  95. y: 200,
  96. },
  97. {
  98. text: `${
  99. selectData.frontRearDP
  100. ? selectData.frontRearDP
  101. : selectData.windSpeed
  102. ? selectData.windSpeed
  103. : selectData.netStatus == '0'
  104. ? '断开'
  105. : '连接'
  106. }`,
  107. font: 'normal 30px Arial',
  108. color: '#009900',
  109. strokeStyle: '#002200',
  110. x: 330,
  111. y: 200,
  112. },
  113. {
  114. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  115. font: 'normal 30px Arial',
  116. color: '#009900',
  117. strokeStyle: '#002200',
  118. x: 5,
  119. y: 256,
  120. },
  121. {
  122. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  123. font: 'normal 30px Arial',
  124. color: '#009900',
  125. strokeStyle: '#002200',
  126. x: 330,
  127. y: 256,
  128. },
  129. {
  130. text: screenDownText,
  131. font: 'normal 28px Arial',
  132. color: '#009900',
  133. strokeStyle: '#002200',
  134. x: screenDownTextX,
  135. y: 302,
  136. },
  137. ];
  138. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  139. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  140. const textMaterial = new THREE.MeshBasicMaterial({
  141. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  142. map: textMap, // 设置纹理贴图
  143. transparent: true,
  144. side: THREE.DoubleSide, // 这里是双面渲染的意思
  145. });
  146. textMap.dispose();
  147. textMaterial.blending = THREE.CustomBlending;
  148. const monitorPlane = this.group?.getObjectByName('monitorText');
  149. if (monitorPlane) {
  150. monitorPlane.material = textMaterial;
  151. } else {
  152. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  153. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  154. planeMesh.name = 'monitorText';
  155. planeMesh.scale.set(0.0025, 0.003, 0.002);
  156. planeMesh.position.set(3.71, -0.042, -0.23);
  157. this.group?.add(planeMesh);
  158. }
  159. });
  160. }
  161. /* 提取风门序列帧,初始化前后门动画 */
  162. initAnimation() {
  163. const meshArr01: THREE.Object3D[] = [];
  164. const meshArr02: THREE.Object3D[] = [];
  165. const windowGroup = new THREE.Group();
  166. const fcObj = this.group.getObjectByName('ddFc');
  167. windowGroup.name = 'hiddenGroup';
  168. const leftObjNames = ['FCshanye03', 'FCshanye01', 'FCshanye02', 'FCshanye12', 'FCshanye11', 'FCshanye10'];
  169. const rightObjNames = ['FCshanye04', 'FCshanye05', 'FCshanye06', 'FCshanye07', 'FCshanye08', 'FCshanye09'];
  170. leftObjNames.filter((name) => {
  171. const obj = fcObj?.getObjectByName(name);
  172. if (obj) {
  173. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  174. meshArr01.push(obj);
  175. }
  176. });
  177. rightObjNames.filter((name) => {
  178. const obj = fcObj?.getObjectByName(name);
  179. if (obj) {
  180. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  181. meshArr02.push(obj);
  182. }
  183. });
  184. this.windowsActionArr.frontWindow = meshArr01;
  185. this.windowsActionArr.backWindow = meshArr02;
  186. this.group?.add(windowGroup);
  187. }
  188. play(rotationParam, flag) {
  189. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  190. return;
  191. }
  192. if (flag === 1) {
  193. // 前风窗动画
  194. this.windowsActionArr.frontWindow.forEach((mesh) => {
  195. gsap.to(mesh.rotation, {
  196. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  197. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  198. overwrite: true,
  199. });
  200. });
  201. } else if (flag === 2) {
  202. // 后风窗动画
  203. this.windowsActionArr.backWindow.forEach((mesh) => {
  204. gsap.to(mesh.rotation, {
  205. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  206. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  207. overwrite: true,
  208. });
  209. });
  210. } else if (flag === 0) {
  211. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  212. gsap.to(mesh.rotation, {
  213. y: 0,
  214. overwrite: true,
  215. });
  216. });
  217. }
  218. }
  219. /* 点击风窗,风窗全屏 */
  220. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  221. this.isLRAnimation = false;
  222. if (this.animationTimer) {
  223. clearTimeout(this.animationTimer);
  224. this.animationTimer = null;
  225. }
  226. // 判断是否点击到视频
  227. intersects.find((intersect) => {
  228. const mesh = intersect.object;
  229. if (mesh.name === 'player1') {
  230. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  231. // 双击,视频放大
  232. if (this.player1) {
  233. this.player1.requestFullscreen();
  234. }
  235. }
  236. this.playerStartClickTime1 = new Date().getTime();
  237. return true;
  238. }
  239. return false;
  240. });
  241. }
  242. mouseUpModel() {
  243. // 10s后开始摆动
  244. if (!this.animationTimer && !this.isLRAnimation) {
  245. this.animationTimer = setTimeout(() => {
  246. this.isLRAnimation = true;
  247. }, 10000);
  248. }
  249. }
  250. /* 风门动画 */
  251. render() {
  252. if (!this.model) {
  253. return;
  254. }
  255. if (this.isLRAnimation && this.group) {
  256. // 左右摇摆动画
  257. if (Math.abs(this.group.rotation.y) >= 0.2) {
  258. this.direction = -this.direction;
  259. this.group.rotation.y += 0.00002 * 30 * this.direction;
  260. } else {
  261. this.group.rotation.y += 0.00002 * 30 * this.direction;
  262. }
  263. }
  264. }
  265. mountedThree(playerDom) {
  266. return new Promise((resolve) => {
  267. this.model.setGLTFModel('ddFc').then((gltf) => {
  268. const fcModal = gltf[0];
  269. fcModal.name = 'ddFc';
  270. this.group?.add(fcModal);
  271. this.setModalPosition();
  272. this.initAnimation();
  273. this.addLight();
  274. resolve(null);
  275. });
  276. });
  277. }
  278. destroy() {
  279. this.model.clearGroup(this.group);
  280. this.windowsActionArr.frontWindow = undefined;
  281. this.model = null;
  282. this.group = null;
  283. }
  284. }
  285. export default doubleWindow;