ScreenShotComponent.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { _decorator, Camera, Component, director, Node, Quat, RenderTexture, Sprite, SpriteFrame, Vec3 } from 'cc';
  2. import { AliensGlobalInstance } from '../AliensGlobalInstance';
  3. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  4. import { GameEvent } from '../Enum/GameEvent';
  5. import { GameUtil } from '../GameUtil';
  6. import { tgxUITips } from 'db://assets/core_tgx/tgx';
  7. const { ccclass, property } = _decorator;
  8. /** 截图组件脚本*/
  9. @ccclass('ScreenShotComponent')
  10. export class ScreenShotComponent extends Component {
  11. @property(Sprite)
  12. public sprite: Sprite = null!;
  13. _renderTex: RenderTexture | null = null;
  14. private _originalTargetTexture: RenderTexture | null = null;
  15. private _originalCameraPosition: Vec3 = new Vec3();
  16. // private _originalCameraRotation: Vec3 = new Vec3();
  17. private _originalCameraRotation: Quat = new Quat();
  18. private _shouldFlipImage: boolean = true;
  19. private _index: number = 0;
  20. //渲染的目标节点
  21. private _targetNode: Node = null!;
  22. protected onLoad(): void {
  23. this.registerEvent();
  24. }
  25. private registerEvent() {
  26. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT, this.screenShot, this);
  27. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT_ENEMY, this.shootEnemy, this);
  28. }
  29. private unregisterEvent() {
  30. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT, this.screenShot, this);
  31. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT_ENEMY, this.shootEnemy, this);
  32. }
  33. //截图
  34. public async screenShot() {
  35. this.node.active = true;
  36. const camera = await this.getSceneCamera();
  37. this._targetNode = await this.getTargetNode();
  38. if (!this._targetNode) return;
  39. // 创建新的RenderTexture
  40. const renderTex = new RenderTexture();
  41. renderTex.reset({
  42. width: 150,
  43. height: 110,
  44. });
  45. // 保存原始相机状态
  46. this._originalTargetTexture = camera.targetTexture;
  47. camera.node.getWorldPosition(this._originalCameraPosition);
  48. camera.node.getWorldRotation(this._originalCameraRotation);
  49. // 设置相机位置和朝向
  50. const targetPos = this._targetNode.worldPosition.clone();
  51. const cameraPos = targetPos.add(new Vec3(0, 0, 5));
  52. camera.node.setWorldPosition(cameraPos);
  53. camera.node.lookAt(targetPos, Vec3.UP);
  54. camera.targetTexture = renderTex;
  55. // 添加临时光源确保材质正确渲染
  56. const tempLight = new Node();
  57. tempLight.addComponent(Component).scheduleOnce(() => {
  58. tempLight.destroy();
  59. }, 0.2);
  60. camera.node.addChild(tempLight);
  61. // 等待两帧确保渲染完成
  62. await new Promise(resolve => this.scheduleOnce(resolve, 0.2));
  63. // 创建新的SpriteFrame并设置翻转
  64. const newSpriteFrame = new SpriteFrame();
  65. newSpriteFrame.texture = renderTex;
  66. newSpriteFrame.flipUVY = this._shouldFlipImage;
  67. // 更新Sprite显示
  68. this.sprite.spriteFrame = newSpriteFrame;
  69. this.sprite.markForUpdateRenderData(true);
  70. // 恢复相机状态
  71. camera.targetTexture = this._originalTargetTexture;
  72. camera.node.setWorldPosition(this._originalCameraPosition);
  73. camera.node.setRotation(this._originalCameraRotation);
  74. }
  75. private _shootCount: number = 0;
  76. //击杀了场景怪物 隐藏侦探节点
  77. private shootEnemy(enemy: Node) {
  78. if (!this.node.active || !this._targetNode) return;
  79. if (enemy == this._targetNode) {
  80. this.scheduleOnce(() => {
  81. tgxUITips.show('击杀的怪物是侦探上的!');
  82. this._shootCount++; // 增加计数
  83. if (this._shootCount > 0) {
  84. this._shouldFlipImage = false;
  85. }
  86. this.node.active = false;
  87. }, 1);
  88. }
  89. }
  90. //获取场景相机
  91. private async getSceneCamera(): Promise<Camera> {
  92. return new Promise<Camera>((resolve, reject) => {
  93. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  94. if (!levelNode) { return; }
  95. const camera = levelNode.getComponentInChildren(Camera)!;
  96. resolve(camera);
  97. });
  98. }
  99. //获取目标节点
  100. private async getTargetNode(): Promise<Node> {
  101. return new Promise<Node>((resolve, reject) => {
  102. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  103. const et = levelNode.getChildByName('Ets');
  104. this._index++;
  105. if (this._index >= et.children.length) {
  106. this._index = 0;
  107. }
  108. resolve(et.children[this._index]);
  109. });
  110. }
  111. //更新相机最新的位置和旋转角度
  112. public saveCameraState(pos: Vec3, rotation: Vec3) {
  113. this._originalCameraPosition = pos;
  114. // 将Vec3欧拉角转换为Quat
  115. const quat = new Quat();
  116. Quat.fromEuler(quat, rotation.x, rotation.y, rotation.z);
  117. this._originalCameraRotation.set(quat);
  118. }
  119. protected onDestroy(): void {
  120. this.unregisterEvent();
  121. }
  122. }