Prechádzať zdrojové kódy

截图渲染 恢复相机touchMove的位置和旋转角度

woso_javan 1 mesiac pred
rodič
commit
08a56e7ac3

+ 13 - 3
assets/module_aliens/Script/Components/ScreenShotComponent.ts

@@ -4,8 +4,8 @@ import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDis
 import { GameEvent } from '../Enum/GameEvent';
 const { ccclass, property } = _decorator;
 /** 截图组件脚本*/
-@ccclass('ScreenShot')
-export class ScreenShot extends Component {
+@ccclass('ScreenShotComponent')
+export class ScreenShotComponent extends Component {
 
     @property(Sprite)
     public sprite: Sprite = null!;
@@ -13,6 +13,8 @@ export class ScreenShot extends Component {
     _renderTex: RenderTexture | null = null;
 
     private _originalTargetTexture: RenderTexture | null = null;
+    private _originalCameraPosition: Vec3 = new Vec3();
+    private _originalCameraRotation: Vec3 = new Vec3();
 
     async start() {
         this.registerEvent();
@@ -59,7 +61,7 @@ export class ScreenShot extends Component {
         if (targetNode) {
             // 设置渲染纹理
             sp.texture = renderTex;
-            // 关键修改:通过设置flipUV来翻转图像
+            // 设置flipUV来翻转图像
             sp.flipUVY = true;
             this.sprite.spriteFrame = sp;
 
@@ -72,6 +74,8 @@ export class ScreenShot extends Component {
             this.scheduleOnce(() => {
                 // 恢复相机状态
                 camera.targetTexture = this._originalTargetTexture;
+                camera.node.setWorldPosition(this._originalCameraPosition);
+                camera.node.setRotationFromEuler(this._originalCameraRotation);
                 // 强制更新材质
                 this.sprite.markForUpdateRenderData();
             }, 0.1);  // 稍微延长等待时间确保渲染完成
@@ -98,6 +102,12 @@ export class ScreenShot extends Component {
         });
     }
 
+    //更新相机最新的位置和旋转角度
+    public saveCameraState(pos:Vec3,rotation:Vec3){
+        this._originalCameraPosition = pos;
+        this._originalCameraRotation = rotation;
+    }
+
     protected onDestroy(): void {
         this.unregisterEvent();
     }

+ 11 - 2
assets/module_aliens/Script/LevelAction.ts

@@ -4,7 +4,7 @@ import { GameEvent } from './Enum/GameEvent';
 import { LineDrawer } from './LineDrawer';
 import { EnemyComponent } from './Components/EnemyComponent';
 import { AliensGlobalInstance } from './AliensGlobalInstance';
-import { ScreenShot } from './Components/ScreenShotComponent';
+import { ScreenShotComponent } from './Components/ScreenShotComponent';
 
 const { ccclass, property } = _decorator;
 
@@ -185,8 +185,17 @@ export class LevelAction extends Component {
         newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
         
         this.camera.node.setRotationFromEuler(newRotation);
+        this.saveCameraState();
     }
-    
+
+    //保存相机的位置和旋转角度
+    private saveCameraState() {
+        const cameraOriginalPos = this.camera.node.worldPosition.clone();
+        const originalRotation = this.camera.node.eulerAngles.clone(); 
+        const screenShot = AliensGlobalInstance.instance.renderNode.getComponent(ScreenShotComponent)!;
+        screenShot.saveCameraState(cameraOriginalPos,originalRotation);
+    }
+
     private _onTouchEnd() {
         this._isDragging = false;
         // 不再需要重置_cameraOriginalPos,保持当前相机位置