zhedie.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. // import * as dat from 'dat.gui';
  4. // const gui = new dat.GUI();
  5. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  6. class zdWindRect {
  7. model;
  8. modelName = 'zdcf';
  9. group: THREE.Object3D = new THREE.Object3D();
  10. mixers: THREE.AnimationMixer[] = [];
  11. animations: THREE.AnimationClip[] = [];
  12. animationAction = null as unknown as THREE.AnimationAction;
  13. animationTimer;
  14. isLRAnimation = true;
  15. direction = 1;
  16. player1;
  17. playerStartClickTime1 = new Date().getTime();
  18. constructor(model, playerVal1) {
  19. this.model = model;
  20. this.player1 = playerVal1;
  21. this.group.name = this.modelName;
  22. }
  23. addLight() {
  24. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  25. directionalLight.position.set(42.4, -250, 53);
  26. this.group.add(directionalLight);
  27. directionalLight.target = this.group;
  28. // gui.add(directionalLight.position, 'x', -500, 500);
  29. // gui.add(directionalLight.position, 'y', -500, 500);
  30. // gui.add(directionalLight.position, 'z', -500, 500);
  31. const spotLight = new THREE.SpotLight();
  32. spotLight.angle = Math.PI / 8;
  33. spotLight.penumbra = 0;
  34. spotLight.castShadow = true;
  35. spotLight.distance = 0;
  36. spotLight.position.set(-470, -500, 500);
  37. spotLight.target = this.group;
  38. this.group.add(spotLight);
  39. spotLight.shadow.camera.near = 0.5; // default
  40. spotLight.shadow.camera.far = 1000; // default
  41. spotLight.shadow.focus = 1;
  42. spotLight.shadow.bias = -0.000002;
  43. }
  44. // 设置模型位置
  45. setModalPosition() {
  46. this.group?.scale.set(22, 22, 22);
  47. this.group?.position.set(-35, 25, 15);
  48. }
  49. addMonitorText(selectData) {
  50. if (!this.group) {
  51. return;
  52. }
  53. const textArr = [
  54. {
  55. text: `折叠式测风装置`,
  56. font: 'normal 32px Arial',
  57. color: '#009900',
  58. strokeStyle: '#002200',
  59. x: 140,
  60. y: 90,
  61. },
  62. {
  63. text: `风量(m3/min):`,
  64. font: 'normal 29px Arial',
  65. color: '#009900',
  66. strokeStyle: '#002200',
  67. x: 2,
  68. y: 145,
  69. },
  70. {
  71. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  72. font: 'normal 29px Arial',
  73. color: '#009900',
  74. strokeStyle: '#002200',
  75. x: 200,
  76. y: 145,
  77. },
  78. {
  79. text: `气源压力(MPa): `,
  80. font: 'normal 29px Arial',
  81. color: '#009900',
  82. strokeStyle: '#002200',
  83. x: 2,
  84. y: 202,
  85. },
  86. {
  87. text: `${selectData.temperature ? selectData.temperature : '-'}`,
  88. font: 'normal 29px Arial',
  89. color: '#009900',
  90. strokeStyle: '#002200',
  91. x: 215,
  92. y: 202,
  93. },
  94. {
  95. text: `Va(m/s):`,
  96. font: 'normal 29px Arial',
  97. color: '#009900',
  98. strokeStyle: '#002200',
  99. x: 2,
  100. y: 255,
  101. },
  102. {
  103. text: `${selectData.va ? selectData.va : '-'}`,
  104. font: 'normal 29px Arial',
  105. color: '#009900',
  106. strokeStyle: '#002200',
  107. x: 130,
  108. y: 255,
  109. },
  110. {
  111. text: `V1(m/s):`,
  112. font: 'normal 28px Arial',
  113. color: '#009900',
  114. strokeStyle: '#002200',
  115. x: 321,
  116. y: 145,
  117. },
  118. {
  119. text: `${selectData.incipientWindSpeed1 ? selectData.incipientWindSpeed1 : '-'}`,
  120. font: 'normal 28px Arial',
  121. color: '#009900',
  122. strokeStyle: '#002200',
  123. x: 445,
  124. y: 145,
  125. },
  126. {
  127. text: `V2(m/s):`,
  128. font: 'normal 28px Arial',
  129. color: '#009900',
  130. strokeStyle: '#002200',
  131. x: 320,
  132. y: 200,
  133. },
  134. {
  135. text: `${selectData.incipientWindSpeed2 ? selectData.incipientWindSpeed2 : '-'}`,
  136. font: 'normal 28px Arial',
  137. color: '#009900',
  138. strokeStyle: '#002200',
  139. x: 442,
  140. y: 200,
  141. },
  142. {
  143. text: `V3(m/s):`,
  144. font: 'normal 28px Arial',
  145. color: '#009900',
  146. strokeStyle: '#002200',
  147. x: 320,
  148. y: 252,
  149. },
  150. {
  151. text: `${selectData.incipientWindSpeed3 ? selectData.incipientWindSpeed3 : '-'}`,
  152. font: 'normal 28px Arial',
  153. color: '#009900',
  154. strokeStyle: '#002200',
  155. x: 442,
  156. y: 252,
  157. },
  158. {
  159. text: `煤炭科学技术研究院有限公司研制`,
  160. font: 'normal 28px Arial',
  161. color: '#009900',
  162. strokeStyle: '#002200',
  163. x: 60,
  164. y: 302,
  165. },
  166. ];
  167. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  168. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  169. const textMaterial = new THREE.MeshBasicMaterial({
  170. map: textMap, // 设置纹理贴图
  171. transparent: true,
  172. side: THREE.DoubleSide, // 这里是双面渲染的意思
  173. });
  174. textMaterial.blending = THREE.CustomBlending;
  175. const monitorPlane = this.group?.getObjectByName('monitorText');
  176. if (monitorPlane) {
  177. monitorPlane.material = textMaterial;
  178. } else {
  179. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  180. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  181. planeMesh.name = 'monitorText';
  182. planeMesh.scale.set(0.002, 0.002, 0.002);
  183. planeMesh.position.set(-3.6, -0.123, -0.41);
  184. this.group?.add(planeMesh);
  185. }
  186. });
  187. }
  188. /* 风门动画 */
  189. render() {
  190. if (!this.model) {
  191. return;
  192. }
  193. if (this.isLRAnimation && this.group) {
  194. // 左右摇摆动画
  195. if (Math.abs(this.group.rotation.y) >= 0.2) {
  196. this.direction = -this.direction;
  197. this.group.rotation.y += 0.00002 * 30 * this.direction;
  198. } else {
  199. this.group.rotation.y += 0.00002 * 30 * this.direction;
  200. }
  201. }
  202. if (this.mixers[0]) this.mixers[0]?.update(1 / 25);
  203. }
  204. /* 提取风门序列帧,初始化前后门动画 */
  205. initAnimation() {
  206. if (this.group) {
  207. if (this.group.animations && this.group.animations.length > 0) {
  208. this.group.animations.forEach((animation) => {
  209. const mixer = new THREE.AnimationMixer(this.group);
  210. this.mixers.push(mixer);
  211. this.animations.push(animation);
  212. });
  213. }
  214. this.animationAction = this.mixers[0].clipAction(this.animations[0]);
  215. this.animationAction.clampWhenFinished = true;
  216. this.animationAction.loop = THREE.LoopOnce;
  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. resetModel() {
  251. clearTimeout(this.animationTimer);
  252. this.isLRAnimation = false;
  253. }
  254. // 播放动画
  255. play(flag, isDirect = false) {
  256. if (flag === 'up' && this.animationAction.time <= 0) {
  257. this.animationAction?.reset();
  258. // @ts-ignore
  259. this.animationAction.time = 0;
  260. this.animations[0].duration = 200 / 25;
  261. this.mixers[0].timeScale = 0.15;
  262. this.animationAction?.play();
  263. } else if (flag === 'center' && this.animationAction.time <= 8) {
  264. this.animationAction?.reset();
  265. // @ts-ignore
  266. this.animationAction.time = 200 / 25; // 8
  267. this.animations[0].duration = 350 / 25;
  268. this.mixers[0].timeScale = 0.15;
  269. this.animationAction?.play();
  270. } else if (flag === 'down' && this.animationAction.time <= 14) {
  271. this.animationAction?.reset();
  272. // @ts-ignore
  273. this.animationAction.time = 350 / 25; // 14
  274. this.animations[0].duration = 530 / 25;
  275. this.mixers[0].timeScale = 0.12;
  276. this.animationAction?.play();
  277. }
  278. // else if ((flag === 'reset' && this.animationAction.time != 0 && this.animationAction.time <= 18) || (flag === 'up' && this.animationAction.time == 18)) {
  279. // debugger;
  280. // this.animationAction?.reset();
  281. // // @ts-ignore
  282. // this.animationAction.time = 450 / 25; //
  283. // this.animations[0].duration = 530 / 25;
  284. // this.mixers[0].timeScale = 0.15;
  285. // this.animationAction?.play();
  286. // }
  287. }
  288. mountedThree() {
  289. return new Promise((resolve) => {
  290. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  291. this.group = gltf[0];
  292. this.setModalPosition();
  293. this.initAnimation();
  294. this.addLight();
  295. setTimeout(async () => {
  296. const videoPlayer1 = document.getElementById('cf-player1')?.getElementsByClassName('vjs-tech')[0];
  297. if (videoPlayer1) {
  298. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  299. mesh?.scale.set(0.0385, 0.028, 0.022);
  300. mesh?.position.set(4.792, -0.16, -0.4);
  301. if (mesh) this.group?.add(mesh);
  302. } else {
  303. const textArr = [
  304. {
  305. text: `无信号输入`,
  306. font: 'normal 40px Arial',
  307. color: '#009900',
  308. strokeStyle: '#002200',
  309. x: 170,
  310. y: 40,
  311. },
  312. ];
  313. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  314. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  315. const textMaterial = new THREE.MeshBasicMaterial({
  316. map: textMap, // 设置纹理贴图
  317. transparent: true,
  318. side: THREE.DoubleSide, // 这里是双面渲染的意思
  319. });
  320. textMaterial.blending = THREE.CustomBlending;
  321. const monitorPlane = this.group?.getObjectByName('noPlayer');
  322. if (monitorPlane) {
  323. monitorPlane.material = textMaterial;
  324. } else {
  325. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  326. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  327. planeMesh.name = 'noPlayer';
  328. planeMesh.scale.set(0.012, 0.009, 0.012);
  329. planeMesh.position.set(4.83, -0.5, -0.39);
  330. this.group?.add(planeMesh);
  331. }
  332. });
  333. }
  334. resolve(null);
  335. }, 0);
  336. });
  337. });
  338. }
  339. destroy() {
  340. if (this.mixers[0] && this.group) {
  341. this.mixers[0].uncacheClip(this.animationAction.getClip());
  342. this.mixers[0].uncacheAction(this.animationAction.getClip(), this.group);
  343. this.mixers[0].uncacheRoot(this.group as THREE.Group);
  344. this.model.clearGroup(this.group);
  345. }
  346. this.model = null;
  347. this.group = null;
  348. }
  349. }
  350. export default zdWindRect;