zhedie.threejs.ts 11 KB

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