|
@@ -12,8 +12,13 @@ export class ScreenShot extends Component {
|
|
|
|
|
|
_renderTex: RenderTexture | null = null;
|
|
|
|
|
|
- start() {
|
|
|
+ private _originalTargetTexture: RenderTexture | null = null;
|
|
|
+
|
|
|
+ async start() {
|
|
|
this.registerEvent();
|
|
|
+ //获取相机组件
|
|
|
+ const camera = await this.getSceneCamera();
|
|
|
+ this._originalTargetTexture = camera.targetTexture;
|
|
|
}
|
|
|
|
|
|
private registerEvent(){
|
|
@@ -26,12 +31,11 @@ export class ScreenShot extends Component {
|
|
|
|
|
|
//截图
|
|
|
public async screenShot() {
|
|
|
+ this.node.active = true;
|
|
|
//获取相机组件
|
|
|
const camera = await this.getSceneCamera();
|
|
|
- console.log(`相机组件:${camera}`);
|
|
|
//获取目标节点
|
|
|
const targetNode = await this.getTargetNode();
|
|
|
- console.log(`目标节点:${targetNode}`);
|
|
|
|
|
|
const spriteFrame = this.sprite.spriteFrame!;
|
|
|
const sp = new SpriteFrame();
|
|
@@ -51,28 +55,23 @@ export class ScreenShot extends Component {
|
|
|
width: 280, // 直接设置为最终大小
|
|
|
height: 180,
|
|
|
});
|
|
|
-
|
|
|
- // 保存相机原始状态
|
|
|
- const originalCameraPos = camera.node.position.clone();
|
|
|
- const originalTargetTexture = camera.targetTexture;
|
|
|
- // console.log(`相机原始Texture:${originalTargetTexture}`);
|
|
|
-
|
|
|
+
|
|
|
if (targetNode) {
|
|
|
// 设置渲染纹理
|
|
|
sp.texture = renderTex;
|
|
|
+ // 关键修改:通过设置flipUV来翻转图像
|
|
|
+ sp.flipUVY = true;
|
|
|
this.sprite.spriteFrame = sp;
|
|
|
-
|
|
|
+
|
|
|
// 移动相机对准目标节点
|
|
|
- const targetPos = targetNode.position.clone();
|
|
|
- camera.node.position = targetPos.add(new Vec3(0, 0, 5));
|
|
|
+ const targetPos = targetNode.worldPosition.clone();
|
|
|
+ camera.node.lookAt(targetPos, Vec3.UP);
|
|
|
camera.targetTexture = renderTex;
|
|
|
|
|
|
- console.log(`相机位置:${camera.node.position}`);
|
|
|
// 确保渲染完成
|
|
|
this.scheduleOnce(() => {
|
|
|
// 恢复相机状态
|
|
|
- camera.node.position = originalCameraPos;
|
|
|
- camera.targetTexture = originalTargetTexture;
|
|
|
+ camera.targetTexture = this._originalTargetTexture;
|
|
|
// 强制更新材质
|
|
|
this.sprite.markForUpdateRenderData();
|
|
|
}, 0.1); // 稍微延长等待时间确保渲染完成
|
|
@@ -83,10 +82,11 @@ export class ScreenShot extends Component {
|
|
|
private async getSceneCamera() :Promise<Camera>{
|
|
|
return new Promise<Camera>((resolve, reject) => {
|
|
|
const levelNode = AliensGlobalInstance.instance.levels.children[0];
|
|
|
+ if(!levelNode){return;}
|
|
|
+
|
|
|
const camera = levelNode.getComponentInChildren(Camera)!;
|
|
|
resolve(camera);
|
|
|
});
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//获取目标节点
|
|
@@ -94,7 +94,7 @@ export class ScreenShot extends Component {
|
|
|
return new Promise<Node>((resolve, reject) => {
|
|
|
const levelNode = AliensGlobalInstance.instance.levels.children[0];
|
|
|
const et = levelNode.getChildByName('et');
|
|
|
- resolve(et.children[2]);
|
|
|
+ resolve(et.children[0]);
|
|
|
});
|
|
|
}
|
|
|
|