From c426b601a60cb8eb54916b896701720889ef61d9 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 27 Apr 2022 14:57:47 +0800 Subject: [PATCH 1/7] fix(bg): add export for BackgroundTextureFillMode --- packages/core/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 11bd6d0b2e..c8e6031e54 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -42,6 +42,7 @@ export { Background } from "./Background"; export { BackgroundMode } from "./enums/BackgroundMode"; export { CameraClearFlags } from "./enums/CameraClearFlags"; export { ColorSpace } from "./enums/ColorSpace"; +export { BackgroundTextureFillMode } from "./enums/BackgroundTextureFillMode"; export * from "./input/index"; export * from "./lighting/index"; export * from "./material/index"; From 56508447fd657dd33835f624065db13890cea6c3 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Thu, 28 Apr 2022 18:52:24 +0800 Subject: [PATCH 2/7] fix(text): fix horizontal alignment error --- packages/core/src/2d/sprite/Sprite.ts | 3 +-- packages/core/src/2d/text/TextRenderer.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/core/src/2d/sprite/Sprite.ts b/packages/core/src/2d/sprite/Sprite.ts index ed24a791e1..97f40d690c 100644 --- a/packages/core/src/2d/sprite/Sprite.ts +++ b/packages/core/src/2d/sprite/Sprite.ts @@ -112,8 +112,7 @@ export class Sprite extends RefObject { set pivot(value: Vector2) { const pivot = this._pivot; - const x = MathUtil.clamp(value.x, 0, 1); - const y = MathUtil.clamp(value.y, 0, 1); + const { x, y } = value; if (pivot === value || pivot.x !== x || pivot.y !== y) { pivot.setValue(x, y); this._setDirtyFlagTrue(DirtyFlag.positions); diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index d54b446855..cfd34fbdd5 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -378,7 +378,21 @@ export class TextRenderer extends Renderer { const { width, height } = trimData; const canvas = TextUtils.updateCanvas(width, height, trimData.data); this._clearTexture(); - const { _sprite: sprite } = this; + const { _sprite: sprite, horizontalAlignment } = this; + + // Handle the case that width of text is larger than real width. + const originWidthInPixel = this.width * sprite.pixelsPerUnit; + if (originWidthInPixel > width && horizontalAlignment !== TextHorizontalAlignment.Center) { + const diffWidth = (originWidthInPixel - width) * 0.5; + const pivot = sprite.pivot; + if (horizontalAlignment === TextHorizontalAlignment.Left) { + pivot.x = 0.5 + diffWidth / width; + } else { + pivot.x = 0.5 - diffWidth / width; + } + sprite.pivot = pivot; + } + // If add fail, set texture for sprite. if (!this.engine._dynamicTextAtlasManager.addSprite(sprite, canvas)) { const texture = new Texture2D(this.engine, width, height); From 88d52e9de36a3b648e91e879e7a68c86b0736ad8 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Fri, 29 Apr 2022 10:29:41 +0800 Subject: [PATCH 3/7] fix(text): fix pivot --- packages/core/src/2d/text/TextRenderer.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index cfd34fbdd5..2074b38fc9 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -382,16 +382,18 @@ export class TextRenderer extends Renderer { // Handle the case that width of text is larger than real width. const originWidthInPixel = this.width * sprite.pixelsPerUnit; + const pivot = sprite.pivot; if (originWidthInPixel > width && horizontalAlignment !== TextHorizontalAlignment.Center) { const diffWidth = (originWidthInPixel - width) * 0.5; - const pivot = sprite.pivot; if (horizontalAlignment === TextHorizontalAlignment.Left) { pivot.x = 0.5 + diffWidth / width; } else { pivot.x = 0.5 - diffWidth / width; } - sprite.pivot = pivot; + } else { + pivot.x = 0.5; } + sprite.pivot = pivot; // If add fail, set texture for sprite. if (!this.engine._dynamicTextAtlasManager.addSprite(sprite, canvas)) { From 3df662bdebaf2bf7f189f1872a989a06b684af84 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Sat, 7 May 2022 23:43:12 +0800 Subject: [PATCH 4/7] fix(text): fix vertical alignment error --- packages/core/src/2d/text/TextRenderer.ts | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index 2074b38fc9..dae1549164 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -378,20 +378,20 @@ export class TextRenderer extends Renderer { const { width, height } = trimData; const canvas = TextUtils.updateCanvas(width, height, trimData.data); this._clearTexture(); - const { _sprite: sprite, horizontalAlignment } = this; + const { _sprite: sprite, horizontalAlignment, verticalAlignment } = this; - // Handle the case that width of text is larger than real width. - const originWidthInPixel = this.width * sprite.pixelsPerUnit; - const pivot = sprite.pivot; + // Handle the case that width or height of text is larger than real width or height. + const { pixelsPerUnit, pivot } = sprite; + const originWidthInPixel = this.width * pixelsPerUnit; + const originHeightInPixel = this.height * pixelsPerUnit; + pivot.setValue(0.5, 0.5); if (originWidthInPixel > width && horizontalAlignment !== TextHorizontalAlignment.Center) { - const diffWidth = (originWidthInPixel - width) * 0.5; - if (horizontalAlignment === TextHorizontalAlignment.Left) { - pivot.x = 0.5 + diffWidth / width; - } else { - pivot.x = 0.5 - diffWidth / width; - } - } else { - pivot.x = 0.5; + const diffPivotX = ((originWidthInPixel - width) * 0.5) / width; + pivot.x = 0.5 + diffPivotX * (horizontalAlignment === TextHorizontalAlignment.Left ? 1 : -1); + } + if (originHeightInPixel > height && verticalAlignment !== TextVerticalAlignment.Center) { + const diffPivotY = ((originHeightInPixel - height) * 0.5) / height; + pivot.y = 0.5 + diffPivotY * (verticalAlignment === TextVerticalAlignment.Top ? -1 : 1); } sprite.pivot = pivot; From 61884c9a8a1655be0ce7010cd9d67faafcef29fb Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 1 Jun 2022 11:30:36 +0800 Subject: [PATCH 5/7] fix(text): fix bounds error --- packages/core/src/2d/text/TextRenderer.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index dae1549164..ccd47dd791 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -17,6 +17,7 @@ import { TextUtils } from "./TextUtils"; */ export class TextRenderer extends Renderer { private static _tempVec3: Vector3 = new Vector3(); + private static _tempBox: BoundingBox = new BoundingBox(); /** @internal temp solution. */ @ignoreClone @@ -329,19 +330,14 @@ export class TextRenderer extends Renderer { * @override */ protected _updateBounds(worldBounds: BoundingBox): void { - const sprite = this._sprite; - if (sprite && sprite.texture) { - if (this._customLocalBounds && this._customRootEntity) { - const worldMatrix = this._customRootEntity.transform.worldMatrix; - BoundingBox.transform(this._customLocalBounds, worldMatrix, worldBounds); - } else { - const localBounds = sprite.bounds; - const worldMatrix = this._entity.transform.worldMatrix; - BoundingBox.transform(localBounds, worldMatrix, worldBounds); - } + if (this._customLocalBounds && this._customRootEntity) { + const worldMatrix = this._customRootEntity.transform.worldMatrix; + BoundingBox.transform(this._customLocalBounds, worldMatrix, worldBounds); } else { - worldBounds.min.setValue(0, 0, 0); - worldBounds.max.setValue(0, 0, 0); + const sprite = this._sprite; + const localBounds = sprite && sprite.texture ? sprite.bounds : TextRenderer._tempBox; + const worldMatrix = this._entity.transform.worldMatrix; + BoundingBox.transform(localBounds, worldMatrix, worldBounds); } } From 66afe8d3e10a970c79f173adcba4097720b1a510 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 1 Jun 2022 12:04:04 +0800 Subject: [PATCH 6/7] fix(text): opt code --- packages/core/src/2d/text/TextRenderer.ts | 29 ++++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index ccd47dd791..f544a2b7f0 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -378,16 +378,27 @@ export class TextRenderer extends Renderer { // Handle the case that width or height of text is larger than real width or height. const { pixelsPerUnit, pivot } = sprite; - const originWidthInPixel = this.width * pixelsPerUnit; - const originHeightInPixel = this.height * pixelsPerUnit; - pivot.setValue(0.5, 0.5); - if (originWidthInPixel > width && horizontalAlignment !== TextHorizontalAlignment.Center) { - const diffPivotX = ((originWidthInPixel - width) * 0.5) / width; - pivot.x = 0.5 + diffPivotX * (horizontalAlignment === TextHorizontalAlignment.Left ? 1 : -1); + switch (horizontalAlignment) { + case TextHorizontalAlignment.Left: + pivot.x = (this.width * pixelsPerUnit) / width * 0.5; + break; + case TextHorizontalAlignment.Right: + pivot.x = 1 - (this.width * pixelsPerUnit) / width * 0.5; + break; + case TextHorizontalAlignment.Center: + pivot.x = 0.5; + break; } - if (originHeightInPixel > height && verticalAlignment !== TextVerticalAlignment.Center) { - const diffPivotY = ((originHeightInPixel - height) * 0.5) / height; - pivot.y = 0.5 + diffPivotY * (verticalAlignment === TextVerticalAlignment.Top ? -1 : 1); + switch (verticalAlignment) { + case TextVerticalAlignment.Top: + pivot.y = 1 - (this.height * pixelsPerUnit) / height * 0.5; + break; + case TextVerticalAlignment.Bottom: + pivot.y = (this.height * pixelsPerUnit) / height * 0.5; + break; + case TextVerticalAlignment.Center: + pivot.y = 0.5; + break; } sprite.pivot = pivot; From 142ebf1e4e9649b26962ec2640b1518f816bbb26 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 1 Jun 2022 14:18:56 +0800 Subject: [PATCH 7/7] fix(text): opt code --- packages/core/src/2d/text/TextRenderer.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/core/src/2d/text/TextRenderer.ts b/packages/core/src/2d/text/TextRenderer.ts index f544a2b7f0..2e54ab06c7 100644 --- a/packages/core/src/2d/text/TextRenderer.ts +++ b/packages/core/src/2d/text/TextRenderer.ts @@ -17,7 +17,6 @@ import { TextUtils } from "./TextUtils"; */ export class TextRenderer extends Renderer { private static _tempVec3: Vector3 = new Vector3(); - private static _tempBox: BoundingBox = new BoundingBox(); /** @internal temp solution. */ @ignoreClone @@ -334,10 +333,8 @@ export class TextRenderer extends Renderer { const worldMatrix = this._customRootEntity.transform.worldMatrix; BoundingBox.transform(this._customLocalBounds, worldMatrix, worldBounds); } else { - const sprite = this._sprite; - const localBounds = sprite && sprite.texture ? sprite.bounds : TextRenderer._tempBox; const worldMatrix = this._entity.transform.worldMatrix; - BoundingBox.transform(localBounds, worldMatrix, worldBounds); + BoundingBox.transform(this._sprite.bounds, worldMatrix, worldBounds); } }