dandaoFcBet.threejs.ts 8.7 KB

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