From 549fe29bbf43c95502207407419a039c806bc2d6 Mon Sep 17 00:00:00 2001 From: AZhan Date: Tue, 21 Mar 2023 15:50:56 +0800 Subject: [PATCH 1/4] fix: sprite bounding box error --- packages/core/src/2d/sprite/Sprite.ts | 8 ++++++-- packages/core/src/2d/sprite/SpriteRenderer.ts | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/core/src/2d/sprite/Sprite.ts b/packages/core/src/2d/sprite/Sprite.ts index c62212730b..41e54f56f9 100644 --- a/packages/core/src/2d/sprite/Sprite.ts +++ b/packages/core/src/2d/sprite/Sprite.ts @@ -47,6 +47,7 @@ export class Sprite extends RefObject { if (this._texture !== value) { this._texture = value; this._dispatchSpriteChange(SpriteModifyFlags.texture); + (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } } @@ -105,6 +106,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); this._atlasRegion.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.atlasRegion); + (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** @@ -119,6 +121,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); this._atlasRegionOffset.set(x, y, MathUtil.clamp(value.z, 0, 1 - x), MathUtil.clamp(value.w, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.atlasRegionOffset); + (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** @@ -134,6 +137,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); region.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.region); + (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** @@ -251,11 +255,11 @@ export class Sprite extends RefObject { if (this._texture) { const { _texture, _atlasRegion, _atlasRegionOffset, _region } = this; const pixelsPerUnitReciprocal = 1.0 / Engine._pixelsPerUnit; - this.width = + this._width = ((_texture.width * _atlasRegion.width) / (1 - _atlasRegionOffset.x - _atlasRegionOffset.z)) * _region.width * pixelsPerUnitReciprocal; - this.height = + this._height = ((_texture.height * _atlasRegion.height) / (1 - _atlasRegionOffset.y - _atlasRegionOffset.w)) * _region.height * pixelsPerUnitReciprocal; diff --git a/packages/core/src/2d/sprite/SpriteRenderer.ts b/packages/core/src/2d/sprite/SpriteRenderer.ts index e7b78082ec..ae8047614d 100644 --- a/packages/core/src/2d/sprite/SpriteRenderer.ts +++ b/packages/core/src/2d/sprite/SpriteRenderer.ts @@ -304,7 +304,11 @@ export class SpriteRenderer extends Renderer implements ICustomClone { this.shaderData.setTexture(SpriteRenderer._textureProperty, this.sprite.texture); break; case SpriteModifyFlags.size: - this._drawMode === SpriteDrawMode.Sliced && (this._dirtyUpdateFlag |= RendererUpdateFlags.WorldVolume); + // When the width and height of `SpriteRenderer` are `undefined`, + // the `size` of `Sprite` will affect the position of `SpriteRenderer`. + if (this._drawMode === SpriteDrawMode.Sliced || this._width === undefined || this._height === undefined) { + this._dirtyUpdateFlag |= RendererUpdateFlags.WorldVolume; + } break; case SpriteModifyFlags.border: this._drawMode === SpriteDrawMode.Sliced && (this._dirtyUpdateFlag |= SpriteRendererUpdateFlags.All); From a9c108220a182fcc03a1ba8240987423d87ac227 Mon Sep 17 00:00:00 2001 From: AZhan Date: Tue, 21 Mar 2023 17:05:01 +0800 Subject: [PATCH 2/4] fix: sprite bounding box error --- packages/core/src/2d/sprite/Sprite.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/2d/sprite/Sprite.ts b/packages/core/src/2d/sprite/Sprite.ts index 41e54f56f9..99cf55d50a 100644 --- a/packages/core/src/2d/sprite/Sprite.ts +++ b/packages/core/src/2d/sprite/Sprite.ts @@ -47,7 +47,7 @@ export class Sprite extends RefObject { if (this._texture !== value) { this._texture = value; this._dispatchSpriteChange(SpriteModifyFlags.texture); - (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); + (this._width === undefined || this._height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } } @@ -106,7 +106,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); this._atlasRegion.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.atlasRegion); - (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); + (this._width === undefined || this._height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** @@ -121,7 +121,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); this._atlasRegionOffset.set(x, y, MathUtil.clamp(value.z, 0, 1 - x), MathUtil.clamp(value.w, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.atlasRegionOffset); - (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); + (this._width === undefined || this._height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** @@ -137,7 +137,7 @@ export class Sprite extends RefObject { const y = MathUtil.clamp(value.y, 0, 1); region.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y)); this._dispatchSpriteChange(SpriteModifyFlags.region); - (this._width === undefined || this.height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); + (this._width === undefined || this._height === undefined) && this._dispatchSpriteChange(SpriteModifyFlags.size); } /** From 2df30eb7b268723f0e2a83baa1f75a78520ebbd5 Mon Sep 17 00:00:00 2001 From: AZhan Date: Tue, 21 Mar 2023 18:47:48 +0800 Subject: [PATCH 3/4] fix: sprite bounding box error --- packages/core/src/2d/sprite/SpriteRenderer.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/core/src/2d/sprite/SpriteRenderer.ts b/packages/core/src/2d/sprite/SpriteRenderer.ts index ae8047614d..b15acd5aaf 100644 --- a/packages/core/src/2d/sprite/SpriteRenderer.ts +++ b/packages/core/src/2d/sprite/SpriteRenderer.ts @@ -117,13 +117,13 @@ export class SpriteRenderer extends Renderer implements ICustomClone { * Render width. */ get width(): number { - if (this._width === undefined && this._sprite) { - this.width = this._sprite.width; - } + this._width === undefined && this._sprite && (this.width = this._sprite.width); return this._width; } set width(value: number) { + // Update width if undefined + this._width === undefined && this._sprite && (this._width = this._sprite.width); if (this._width !== value) { this._width = value; this._dirtyUpdateFlag |= RendererUpdateFlags.WorldVolume; @@ -134,13 +134,13 @@ export class SpriteRenderer extends Renderer implements ICustomClone { * Render height. */ get height(): number { - if (this._height === undefined && this._sprite) { - this.height = this._sprite.height; - } + this._height === undefined && this._sprite && (this.height = this._sprite.height); return this._height; } set height(value: number) { + // Update height if undefined + this._height === undefined && this._sprite && (this._height = this._sprite.height); if (this._height !== value) { this._height = value; this._dirtyUpdateFlag |= RendererUpdateFlags.WorldVolume; @@ -234,6 +234,7 @@ export class SpriteRenderer extends Renderer implements ICustomClone { * @override */ protected _updateBounds(worldBounds: BoundingBox): void { + console.log("_updateBounds"); if (!this.sprite?.texture || !this.width || !this.height) { worldBounds.min.set(0, 0, 0); worldBounds.max.set(0, 0, 0); From 806090547b8e12f2ec04dedcc4288fba66930d11 Mon Sep 17 00:00:00 2001 From: AZhan Date: Tue, 21 Mar 2023 18:49:21 +0800 Subject: [PATCH 4/4] fix: sprite bounding box error --- packages/core/src/2d/sprite/SpriteRenderer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/2d/sprite/SpriteRenderer.ts b/packages/core/src/2d/sprite/SpriteRenderer.ts index b15acd5aaf..11296760b2 100644 --- a/packages/core/src/2d/sprite/SpriteRenderer.ts +++ b/packages/core/src/2d/sprite/SpriteRenderer.ts @@ -234,7 +234,6 @@ export class SpriteRenderer extends Renderer implements ICustomClone { * @override */ protected _updateBounds(worldBounds: BoundingBox): void { - console.log("_updateBounds"); if (!this.sprite?.texture || !this.width || !this.height) { worldBounds.min.set(0, 0, 0); worldBounds.max.set(0, 0, 0);