Skip to content

Commit

Permalink
feat: add frame names to spritesheet manifest data (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie authored Sep 3, 2024
1 parent 4c98058 commit 2f76270
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
17 changes: 17 additions & 0 deletions packages/assetpack/src/texture-packer/texturePacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface TexturePackerOptions extends PluginOptions
/** The maximum size a sprite sheet can be before its split out */
maximumTextureSize?: number;
}
/** If true, the frame names for the sprite sheet will be added to the asset meta data. */
addFrameNames?: boolean;
}

function checkForTexturePackerShortcutClashes(
Expand Down Expand Up @@ -67,6 +69,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
nameStyle: 'relative',
..._options.texturePacker,
},
addFrameNames: _options.addFrameNames ?? false,
},
tags: {
tps: 'tps',
Expand Down Expand Up @@ -130,6 +133,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
const assets: Asset[] = [];

let checkedForClashes = false;
const imageNames = new Set();

Object.values(resolutionHash).sort((a, b) => b - a).forEach((resolution) =>
{
Expand All @@ -147,6 +151,14 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
resolution,
});

if (options.addFrameNames)
{
out.jsons.forEach(({ json }) =>
{
Object.keys(json.frames).forEach((frame) => imageNames.add(frame));
});
}

const outPromises: Promise<void>[] = [];

for (let i = 0; i < out.textures.length; i++)
Expand Down Expand Up @@ -184,6 +196,11 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te

await Promise.all(promises);

if (options.addFrameNames)
{
asset.metaData.frameNames = Array.from(imageNames);
}

return assets;
},

Expand Down
29 changes: 27 additions & 2 deletions packages/assetpack/test/manifest/Manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('Manifest', () =>
resolutionOptions: {
maximumTextureSize: 512,
},
addFrameNames: true,
}),
mipmap(),
compress({
Expand Down Expand Up @@ -190,10 +191,34 @@ describe('Manifest', () =>
],
data: {
tags: {
tps: true,
m: true,
tps: true,
frameNames: [
'sprite9.png',
'sprite8.png',
'sprite7.png',
'sprite6.png',
'sprite5.png',
'sprite4.png',
'sprite3.png',
'sprite2.png',
'sprite1.png',
'sprite0.png'
]
},
},
frameNames: [
'sprite9.png',
'sprite8.png',
'sprite7.png',
'sprite6.png',
'sprite5.png',
'sprite4.png',
'sprite3.png',
'sprite2.png',
'sprite1.png',
'sprite0.png'
]
}
},
],
});
Expand Down
2 changes: 2 additions & 0 deletions packages/docs/docs/guide/pipes/texture-packer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export default {
| resolutionOptions.resolutions | `object` | An object containing the resolutions that the images will be resized to.<br />Defaults to `{ default: 1, low: 0.5 }`. |
| resolutionOptions.fixedResolution | `string` | A resolution used if the fix tag is applied. Resolution must match one found in resolutions.<br />Defaults to `default`. |
| resolutionOptions.maximumTextureSize | `number` | The maximum size a sprite sheet can be before its split out.<br />Defaults to `4096`. |
| | | |
| addFrameNames | `boolean` | Whether to add frame names to the data in the manifest<br />Defaults to `false`. |

## Tags

Expand Down

0 comments on commit 2f76270

Please sign in to comment.