Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SpriteRenderer bounding box error #1425

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/core/src/2d/sprite/Sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down