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(texture-packer): skip trim for any files smaller than 3x3 #22

Merged
merged 1 commit into from
Apr 30, 2024
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
6 changes: 4 additions & 2 deletions packages/texture-packer/src/packer/createTextureData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export async function createTextureData(options: Required<PackTexturesOptions>)
const newWidth = Math.ceil(metaData.width * scale);
const newHeight = Math.ceil(metaData.height * scale);

const allowTrim = options.allowTrim && newWidth >= 3 && newHeight >= 3;

if (scale < 1)
{
sharpImage = sharpImage
Expand All @@ -37,13 +39,13 @@ export async function createTextureData(options: Required<PackTexturesOptions>)
height: newHeight,
});

if (options.allowTrim)
if (allowTrim)
{
sharpImage = sharp(await sharpImage.toBuffer());
}
}

if (options.allowTrim)
if (allowTrim)
{
sharpImage = sharpImage
.trim({
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions packages/texture-packer/test/texturePacker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,77 @@ describe('Texture Packer', () =>
// Restore console.warn
mockWarn.mockRestore();
});

it.only('should handle smaller than 3x3 textures if trimming is enabled', async () =>
{
const testName = 'tp-small-trim';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

const sprites: File[] = [];

sprites.push({
name: `sprite.png`,
content: assetPath(pkg, `sp-1.png`),
});

sprites.push({
name: `empty2x2.png`,
content: assetPath(pkg, `2x2-small-empty-texture.png`),
});

createFolder(
pkg,
{
name: testName,
files: [],
folders: [
{
name: 'sprites{tps}',
files: sprites,
folders: [],
},
],
});

const assetpack = new AssetPack({
entry: inputDir,
output: outputDir,
cache: false,
pipes: [
texturePacker({
resolutionOptions: { resolutions: { default: 1 } },
}),
]
});

// Mock console.warn

await assetpack.run();

const sheet1 = readJSONSync(`${outputDir}/sprites.json`);

expect(sheet1.frames['empty2x2.png']).toEqual({
frame: {
x: 2,
y: 2,
w: 2,
h: 2
},
rotated: false,
trimmed: false,
spriteSourceSize: {
x: 0,
y: 0,
w: 2,
h: 2
},
sourceSize: {
w: 2,
h: 2
}
},
);
});
});

Loading