dandaoFcBet.threejs.ts 8.8 KB

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