shuangdaoFcSw.threejs.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.OpenDegree1 ? '前窗开度值(°)' : selectData.forntArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  71. font: 'normal 30px Arial',
  72. color: '#009900',
  73. strokeStyle: '#002200',
  74. x: 5,
  75. y: 145,
  76. },
  77. {
  78. text: selectData.OpenDegree1
  79. ? Number(`${selectData.OpenDegree1}`).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.OpenDegree2 ? '后窗开度值(°)' : selectData.forntArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  91. font: 'normal 30px Arial',
  92. color: '#009900',
  93. strokeStyle: '#002200',
  94. x: 5,
  95. y: 200,
  96. },
  97. {
  98. text: selectData.OpenDegree2
  99. ? Number(`${selectData.OpenDegree2}`).toFixed(2)
  100. : selectData.rearArea
  101. ? Number(`${selectData.rearArea}`).toFixed(2)
  102. : '-',
  103. font: 'normal 30px Arial',
  104. color: '#009900',
  105. strokeStyle: '#002200',
  106. x: 330,
  107. y: 200,
  108. },
  109. {
  110. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  111. font: 'normal 30px Arial',
  112. color: '#009900',
  113. strokeStyle: '#002200',
  114. x: 5,
  115. y: 256,
  116. },
  117. {
  118. text: `${
  119. selectData.frontRearDP
  120. ? selectData.frontRearDP
  121. : selectData.windSpeed
  122. ? selectData.windSpeed
  123. : selectData.netStatus == '0'
  124. ? '断开'
  125. : '连接'
  126. }`,
  127. font: 'normal 30px Arial',
  128. color: '#009900',
  129. strokeStyle: '#002200',
  130. x: 330,
  131. y: 256,
  132. },
  133. {
  134. text: screenDownText,
  135. font: 'normal 28px Arial',
  136. color: '#009900',
  137. strokeStyle: '#002200',
  138. x: screenDownTextX,
  139. y: 302,
  140. },
  141. ];
  142. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  143. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  144. const textMaterial = new THREE.MeshBasicMaterial({
  145. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  146. map: textMap, // 设置纹理贴图
  147. transparent: true,
  148. side: THREE.DoubleSide, // 这里是双面渲染的意思
  149. });
  150. textMap.dispose();
  151. textMaterial.blending = THREE.CustomBlending;
  152. const monitorPlane = this.group?.getObjectByName('monitorText');
  153. if (monitorPlane) {
  154. monitorPlane.material = textMaterial;
  155. } else {
  156. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  157. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  158. planeMesh.name = 'monitorText';
  159. planeMesh.scale.set(0.0025, 0.003, 0.002);
  160. planeMesh.position.set(3.71, -0.042, -0.23);
  161. this.group?.add(planeMesh);
  162. }
  163. });
  164. }
  165. /* 提取风门序列帧,初始化前后门动画 */
  166. initAnimation() {
  167. const meshArr01: THREE.Object3D[] = [];
  168. const meshArr02: THREE.Object3D[] = [];
  169. const windowGroup = new THREE.Group();
  170. const fcObj = this.group.getObjectByName('ddFc');
  171. windowGroup.name = 'hiddenGroup';
  172. const leftObjNames = ['FCshanye03', 'FCshanye01', 'FCshanye02', 'FCshanye12', 'FCshanye11', 'FCshanye10'];
  173. const rightObjNames = ['FCshanye04', 'FCshanye05', 'FCshanye06', 'FCshanye07', 'FCshanye08', 'FCshanye09'];
  174. leftObjNames.filter((name) => {
  175. const obj = fcObj?.getObjectByName(name);
  176. if (obj) {
  177. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  178. meshArr01.push(obj);
  179. }
  180. });
  181. rightObjNames.filter((name) => {
  182. const obj = fcObj?.getObjectByName(name);
  183. if (obj) {
  184. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  185. meshArr02.push(obj);
  186. }
  187. });
  188. this.windowsActionArr.frontWindow = meshArr01;
  189. this.windowsActionArr.backWindow = meshArr02;
  190. this.group?.add(windowGroup);
  191. }
  192. play(rotationParam, flag) {
  193. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  194. return;
  195. }
  196. if (flag === 1) {
  197. // 前风窗动画
  198. this.windowsActionArr.frontWindow.forEach((mesh) => {
  199. gsap.to(mesh.rotation, {
  200. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  201. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  202. overwrite: true,
  203. });
  204. });
  205. } else if (flag === 2) {
  206. // 后风窗动画
  207. this.windowsActionArr.backWindow.forEach((mesh) => {
  208. gsap.to(mesh.rotation, {
  209. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  210. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  211. overwrite: true,
  212. });
  213. });
  214. } else if (flag === 0) {
  215. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  216. gsap.to(mesh.rotation, {
  217. y: 0,
  218. overwrite: true,
  219. });
  220. });
  221. }
  222. }
  223. /* 点击风窗,风窗全屏 */
  224. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  225. this.isLRAnimation = false;
  226. if (this.animationTimer) {
  227. clearTimeout(this.animationTimer);
  228. this.animationTimer = null;
  229. }
  230. // 判断是否点击到视频
  231. intersects.find((intersect) => {
  232. const mesh = intersect.object;
  233. if (mesh.name === 'player1') {
  234. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  235. // 双击,视频放大
  236. if (this.player1) {
  237. this.player1.requestFullscreen();
  238. }
  239. }
  240. this.playerStartClickTime1 = new Date().getTime();
  241. return true;
  242. }
  243. return false;
  244. });
  245. }
  246. mouseUpModel() {
  247. // 10s后开始摆动
  248. if (!this.animationTimer && !this.isLRAnimation) {
  249. this.animationTimer = setTimeout(() => {
  250. this.isLRAnimation = true;
  251. }, 10000);
  252. }
  253. }
  254. /* 风门动画 */
  255. render() {
  256. if (!this.model) {
  257. return;
  258. }
  259. if (this.isLRAnimation && this.group) {
  260. // 左右摇摆动画
  261. if (Math.abs(this.group.rotation.y) >= 0.2) {
  262. this.direction = -this.direction;
  263. this.group.rotation.y += 0.00002 * 30 * this.direction;
  264. } else {
  265. this.group.rotation.y += 0.00002 * 30 * this.direction;
  266. }
  267. }
  268. }
  269. mountedThree(playerDom) {
  270. return new Promise((resolve) => {
  271. this.model.setGLTFModel('ddFc').then((gltf) => {
  272. const fcModal = gltf[0];
  273. fcModal.name = 'ddFc';
  274. this.group?.add(fcModal);
  275. this.setModalPosition();
  276. this.initAnimation();
  277. this.addLight();
  278. resolve(null);
  279. });
  280. });
  281. }
  282. destroy() {
  283. this.model.clearGroup(this.group);
  284. this.windowsActionArr.frontWindow = undefined;
  285. this.model = null;
  286. this.group = null;
  287. }
  288. }
  289. export default doubleWindow;