diff --git a/packages/texture-packer/src/packer/createTextureData.ts b/packages/texture-packer/src/packer/createTextureData.ts index f7e424b..7a2c99e 100644 --- a/packages/texture-packer/src/packer/createTextureData.ts +++ b/packages/texture-packer/src/packer/createTextureData.ts @@ -29,6 +29,8 @@ export async function createTextureData(options: Required) 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 @@ -37,13 +39,13 @@ export async function createTextureData(options: Required) height: newHeight, }); - if (options.allowTrim) + if (allowTrim) { sharpImage = sharp(await sharpImage.toBuffer()); } } - if (options.allowTrim) + if (allowTrim) { sharpImage = sharpImage .trim({ diff --git a/packages/texture-packer/test/resources/2x2-small-empty-texture.png b/packages/texture-packer/test/resources/2x2-small-empty-texture.png new file mode 100644 index 0000000..4b859dc Binary files /dev/null and b/packages/texture-packer/test/resources/2x2-small-empty-texture.png differ diff --git a/packages/texture-packer/test/texturePacker.test.ts b/packages/texture-packer/test/texturePacker.test.ts index 8f5ef84..44c023d 100644 --- a/packages/texture-packer/test/texturePacker.test.ts +++ b/packages/texture-packer/test/texturePacker.test.ts @@ -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 + } + }, + ); + }); }); +