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:opt atlas code. #420

Merged
merged 25 commits into from
Aug 6, 2021
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
45 changes: 19 additions & 26 deletions packages/core/src/2d/sprite/Sprite.ts
Original file line number Diff line number Diff line change
@@ -64,12 +64,9 @@ export class Sprite extends RefObject {

set atlasRegion(value: Rect) {
const atlasRegion = this._atlasRegion;
atlasRegion.setValue(
MathUtil.clamp(value.x, 0, 1),
MathUtil.clamp(value.y, 0, 1),
MathUtil.clamp(value.width, 0, 1 - atlasRegion.x),
MathUtil.clamp(value.height, 0, 1 - atlasRegion.y)
);
const x = MathUtil.clamp(value.x, 0, 1);
const y = MathUtil.clamp(value.y, 0, 1);
atlasRegion.setValue(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
this._setDirtyFlagTrue(DirtyFlag.positions);
}

@@ -106,12 +103,9 @@ export class Sprite extends RefObject {

set region(value: Rect) {
const region = this._region;
region.setValue(
MathUtil.clamp(value.x, 0, 1),
MathUtil.clamp(value.y, 0, 1),
MathUtil.clamp(value.width, 0, 1 - region.x),
MathUtil.clamp(value.height, 0, 1 - region.y)
);
const x = MathUtil.clamp(value.x, 0, 1);
const y = MathUtil.clamp(value.y, 0, 1);
region.setValue(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
this._setDirtyFlagTrue(DirtyFlag.positions | DirtyFlag.uv);
}

@@ -182,24 +176,24 @@ export class Sprite extends RefObject {

// Get the distance between the anchor point and the four sides.
const lx = (-pivot.x + atlasRegionOffset.x) * unitWidth;
const ty = (-pivot.y + atlasRegionOffset.y) * unitHeight;
const by = (-pivot.y + atlasRegionOffset.y) * unitHeight;
const rx = unitWidth + lx;
const by = unitHeight + ty;
const ty = unitHeight + by;

// Assign values ​​to _positions
const positions = this._positions;
// Top-left.
positions[0].setValue(lx, by);
positions[0].setValue(lx, ty);
// Top-right.
positions[1].setValue(rx, by);
positions[1].setValue(rx, ty);
// Bottom-right.
positions[2].setValue(rx, ty);
positions[2].setValue(rx, by);
// Bottom-left.
positions[3].setValue(lx, ty);
positions[3].setValue(lx, by);

// Update bounds.
bounds.min.setValue(lx, ty, 0);
bounds.max.setValue(rx, by, 0);
bounds.min.setValue(lx, by, 0);
bounds.max.setValue(rx, ty, 0);
} else {
// Update bounds.
bounds.min.setValue(0, 0, 0);
@@ -217,12 +211,11 @@ export class Sprite extends RefObject {

if (this._isContainDirtyFlag(DirtyFlag.uv)) {
const { _region: region, _atlasRegion: atlasRegion, _uv: uv } = this;
const realWidth = atlasRegion.width * region.width;
const realHeight = atlasRegion.height * region.height;
const left = atlasRegion.x + realWidth * region.x;
const top = atlasRegion.y + realHeight * region.y;
const right = left + realWidth;
const bottom = top + realHeight;
const { width: atlasRegionWidth, height: atlasRegionHeight } = atlasRegion;
const left = atlasRegion.x + atlasRegionWidth * region.x;
const top = atlasRegion.y + atlasRegionHeight * region.y;
const right = left + atlasRegionWidth * region.width;
const bottom = top + atlasRegionHeight * region.height;

// Top-left.
uv[0].setValue(left, top);
12 changes: 4 additions & 8 deletions packages/loader/src/SpriteAtlasLoader.ts
Original file line number Diff line number Diff line change
@@ -51,16 +51,12 @@ class SpriteAtlasLoader extends Loader<SpriteAtlas> {
for (let j = sprites.length - 1; j >= 0; j--) {
const atlasSprite = sprites[j];
const { region, pivot, atlasRegionOffset, atlasRegion } = atlasSprite;

tempRect.setValue(region.x, region.y, region.w, region.h);
tempPivot.setValue(pivot.x, pivot.y);

const sprite = new Sprite(
engine,
texture,
tempRect,
tempPivot,
atlasSprite.pixelsPerUnit,
region ? tempRect.setValue(region.x, region.y, region.w, region.h) : undefined,
pivot ? tempPivot.setValue(pivot.x, pivot.y) : undefined,
atlasSprite.pixelsPerUnit || undefined,
atlasSprite.name
);
sprite.atlasRegion.setValue(
@@ -69,7 +65,7 @@ class SpriteAtlasLoader extends Loader<SpriteAtlas> {
atlasRegion.w * sourceWidthReciprocal,
atlasRegion.h * sourceHeightReciprocal
);
sprite.atlasRegionOffset.setValue(atlasRegionOffset.x, atlasRegionOffset.y);
atlasRegionOffset && sprite.atlasRegionOffset.setValue(atlasRegionOffset.x, atlasRegionOffset.y);
/** @ts-ignore */
spriteAtlas._addSprite(sprite);
}