From 0ec1916baa1a1a508036e290f791164a54d3a80a Mon Sep 17 00:00:00 2001 From: Mat Groves Date: Tue, 30 Apr 2024 13:04:13 +0100 Subject: [PATCH] cache bust at start of file --- .../src/packer/createTextureData.ts | 6 +- .../resources/2x2-small-empty-texture.png | Bin 0 -> 71 bytes .../texture-packer/test/texturePacker.test.ts | 73 ++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 packages/texture-packer/test/resources/2x2-small-empty-texture.png 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 0000000000000000000000000000000000000000..4b859dce28fc257ac6ff73107a31ebb78f2c1456 GIT binary patch literal 71 zcmeAS@N?(olHy`uVBq!ia0vp^Od!kwBL7~QRScx~JY5_^IA$jQ;Ai6z;9>~g#r-w^ Sje;RihQZU-&t;ucLK6Vp^bgzs literal 0 HcmV?d00001 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 + } + }, + ); + }); }); +