From 8d916b9816ef25b7ddf5fb515fefeac951fb4757 Mon Sep 17 00:00:00 2001 From: ChiaNing Date: Mon, 11 Oct 2021 11:29:52 +0800 Subject: [PATCH] fix rotation with anchor (#9453) --- cocos/2d/framework/ui-transform.ts | 14 +++++++------- cocos/2d/renderer/draw-batch-gpu.ts | 15 ++++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cocos/2d/framework/ui-transform.ts b/cocos/2d/framework/ui-transform.ts index 1b87afd24b..05549cb47f 100644 --- a/cocos/2d/framework/ui-transform.ts +++ b/cocos/2d/framework/ui-transform.ts @@ -43,6 +43,7 @@ import { NodeEventProcessor } from '../../core/scene-graph/node-event-processor' const _vec2a = new Vec2(); const _vec2b = new Vec2(); +const _vec3a = new Vec3(); const _mat4_temp = new Mat4(); const _matrix = new Mat4(); const _worldMatrix = new Mat4(); @@ -70,7 +71,7 @@ export class UITransform extends Component { * 内容尺寸。 */ @displayOrder(0) - @tooltip('i18n:ui_transform.conten_size') + @tooltip('i18n:ui_transform.content_size') // @constget get contentSize (): Readonly { return this._contentSize; @@ -256,14 +257,14 @@ export class UITransform extends Component { // macro.UI_GPU_DRIVEN declare public _rectDirty: boolean; declare public _rectWithScale: Vec3; - declare public _anchorCache: Vec2; + declare public _anchorCache: Vec3; constructor () { super(); if (UI_GPU_DRIVEN) { this._rectDirty = true; this._rectWithScale = new Vec3(); - this._anchorCache = new Vec2(); + this._anchorCache = new Vec3(); } } @@ -684,16 +685,15 @@ export class UITransform extends Component { } } - public checkAndUpdateRect (scale: Vec3) { + public checkAndUpdateRect (rot: Quat, scale: Vec3) { if (this._rectDirty) { this._rectWithScale.x = scale.x * this.width; this._rectWithScale.y = scale.y * this.height; this._rectWithScale.z = scale.z; - const eulerZ = this.node.angle / 180 * Math.PI; + const lenX = (0.5 - this.anchorPoint.x) * this.width * scale.x; const lenY = (0.5 - this.anchorPoint.y) * this.height * scale.y; - this._anchorCache.x = (lenX) * Math.cos(eulerZ) - (lenY) * Math.sin(eulerZ); - this._anchorCache.y = (lenX) * Math.sin(eulerZ) + (lenY) * Math.cos(eulerZ); + Vec3.transformQuat(this._anchorCache, _vec3a.set(lenX, lenY, 0), rot); } } diff --git a/cocos/2d/renderer/draw-batch-gpu.ts b/cocos/2d/renderer/draw-batch-gpu.ts index 232d60983c..eb77e1c7c1 100644 --- a/cocos/2d/renderer/draw-batch-gpu.ts +++ b/cocos/2d/renderer/draw-batch-gpu.ts @@ -76,12 +76,8 @@ export class DrawBatch2DGPU extends DrawBatch2D { // @ts-expect-error using private members const { _pos: t, _rot: r, _scale: s } = renderComp.node; DrawBatch2DGPU._tempRect = renderComp.node._uiProps.uiTransformComp!; - DrawBatch2DGPU._tempRect.checkAndUpdateRect(s); - - DrawBatch2DGPU._tempAnchor.set(DrawBatch2DGPU._tempRect._anchorCache); - DrawBatch2DGPU._tempPosition.x = t.x + DrawBatch2DGPU._tempAnchor.x; - DrawBatch2DGPU._tempPosition.y = t.y + DrawBatch2DGPU._tempAnchor.y; - DrawBatch2DGPU._tempPosition.z = t.z; + DrawBatch2DGPU._tempRect.checkAndUpdateRect(r, s); + Vec3.add(DrawBatch2DGPU._tempPosition, t, DrawBatch2DGPU._tempRect._anchorCache); let mode = 0; let fillType = 0; @@ -167,11 +163,8 @@ export class DrawBatch2DGPU extends DrawBatch2D { DrawBatch2DGPU._tempRect = node._uiProps.uiTransformComp!; // @ts-expect-error using private members const { _pos: t, _rot: r, _scale: s } = renderComp.node; - DrawBatch2DGPU._tempRect.checkAndUpdateRect(s); - DrawBatch2DGPU._tempAnchor.set(DrawBatch2DGPU._tempRect._anchorCache); - DrawBatch2DGPU._tempPosition.x = t.x + DrawBatch2DGPU._tempAnchor.x; - DrawBatch2DGPU._tempPosition.y = t.y + DrawBatch2DGPU._tempAnchor.y; - DrawBatch2DGPU._tempPosition.z = t.z; + DrawBatch2DGPU._tempRect.checkAndUpdateRect(r, s); + Vec3.add(DrawBatch2DGPU._tempPosition, t, DrawBatch2DGPU._tempRect._anchorCache); if (node.hasChangedFlags & TransformBit.RS || DrawBatch2DGPU._tempRect._rectDirty) { localBuffer.updateDataTRSByDirty(bufferInfo.instanceID, bufferInfo.UBOIndex,