dandaoFcBd2.threejs.ts 7.8 KB

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