Skip to content

Commit

Permalink
Update atlas.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Jul 2, 2024
1 parent 86b840c commit f141514
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions cocos/2d/utils/dynamic-atlas/atlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { PixelFormat } from '../../../asset/assets/asset-enum';
import { ImageAsset } from '../../../asset/assets/image-asset';
import { Texture2D } from '../../../asset/assets/texture-2d';
import { BufferTextureCopy } from '../../../gfx';
import { BufferTextureCopy, TextureUsage } from '../../../gfx';
import { cclegacy, warn } from '../../../core';
import { SpriteFrame } from '../../assets/sprite-frame';

Expand Down Expand Up @@ -106,19 +106,19 @@ export class Atlas {
if (cclegacy.internal.dynamicAtlasManager.textureBleeding) {
// Smaller frame is more likely to be affected by linear filter
if (width <= 8 || height <= 8) {
this._texture.drawTextureAt(texture.image!, this._x - 1, this._y - 1);
this._texture.drawTextureAt(texture.image!, this._x - 1, this._y + 1);
this._texture.drawTextureAt(texture.image!, this._x + 1, this._y - 1);
this._texture.drawTextureAt(texture.image!, this._x + 1, this._y + 1);
this._texture.drawTextureAt(texture, this._x - 1, this._y - 1);
this._texture.drawTextureAt(texture, this._x - 1, this._y + 1);
this._texture.drawTextureAt(texture, this._x + 1, this._y - 1);
this._texture.drawTextureAt(texture, this._x + 1, this._y + 1);
}

this._texture.drawTextureAt(texture.image!, this._x - 1, this._y);
this._texture.drawTextureAt(texture.image!, this._x + 1, this._y);
this._texture.drawTextureAt(texture.image!, this._x, this._y - 1);
this._texture.drawTextureAt(texture.image!, this._x, this._y + 1);
this._texture.drawTextureAt(texture, this._x - 1, this._y);
this._texture.drawTextureAt(texture, this._x + 1, this._y);
this._texture.drawTextureAt(texture, this._x, this._y - 1);
this._texture.drawTextureAt(texture, this._x, this._y + 1);
}

this._texture.drawTextureAt(texture.image!, this._x, this._y);
this._texture.drawTextureAt(texture, this._x, this._y);

this._innerTextureInfos[texture.getId()] = {
x: this._x,
Expand Down Expand Up @@ -246,7 +246,18 @@ export class DynamicAtlasTexture extends Texture2D {
* @param {Number} x
* @param {Number} y
*/
public drawTextureAt (image: ImageAsset, x: number, y: number): void {
public drawTextureAt (texture: Texture2D, x: number, y: number): void;
public drawTextureAt (image: ImageAsset, x: number, y: number): void;

public drawTextureAt (imageOrTexture: ImageAsset | Texture2D, x: number, y: number): void {
if (imageOrTexture instanceof ImageAsset) {
this.drawTextureAtByImage(imageOrTexture, x, y);
} else {
this.drawTextureAtByTexture(imageOrTexture, x, y);
}
}

private drawTextureAtByImage (image: ImageAsset, x: number, y: number): void {
const gfxTexture = this.getGFXTexture();
if (!image || !gfxTexture) {
return;
Expand All @@ -265,4 +276,25 @@ export class DynamicAtlasTexture extends Texture2D {
region.texExtent.height = image.height;
gfxDevice.copyTexImagesToTexture([image.data as HTMLCanvasElement], gfxTexture, [region]);
}

private drawTextureAtByTexture (texture: Texture2D, x: number, y: number): void {
const dstDfxTexture = this.getGFXTexture();
if (!texture || !dstDfxTexture) {
return;
}

const gfxDevice = this._getGFXDevice();
if (!gfxDevice) {
warn('Unable to get device');
return;
}

const srcGfxTexture = texture.getGFXTexture();
if (!srcGfxTexture) {
warn('No gfx texture found');
return;
}

gfxDevice.copyTextureToTexture(srcGfxTexture, dstDfxTexture, x, y, null);
}
}

0 comments on commit f141514

Please sign in to comment.