diff --git a/packages/assetpack/src/image/mipmap.ts b/packages/assetpack/src/image/mipmap.ts index 19755a8d..2b4f6f84 100644 --- a/packages/assetpack/src/image/mipmap.ts +++ b/packages/assetpack/src/image/mipmap.ts @@ -9,7 +9,7 @@ import type { SharpProcessingOptions } from './types.js'; export interface MipmapOptions extends PluginOptions { /** A template for denoting the resolution of the images. */ - template?: string; + template?: string | ((resolution: number) => string); /** An object containing the resolutions that the images will be resized to. */ resolutions?: { [x: string]: number }; /** A resolution used if the fixed tag is applied. Resolution must match one found in resolutions. */ @@ -94,8 +94,13 @@ export function mipmap(defaultOptions: MipmapOptions = {}): AssetPipe).template.replace('%%', `${data.resolution}`); - resolution = data.resolution === 1 ? '' : resolution; + const template = (options as Required).template; + + if (typeof template === 'function') { + resolution = template(data.resolution); + } else if (data.resolution !== 1) { + resolution = template.replace('%%', `${data.resolution}`); + } } const end = `${resolution}${data.format}`; diff --git a/packages/assetpack/src/spine/spineAtlasMipmap.ts b/packages/assetpack/src/spine/spineAtlasMipmap.ts index a0c418ed..3604c713 100644 --- a/packages/assetpack/src/spine/spineAtlasMipmap.ts +++ b/packages/assetpack/src/spine/spineAtlasMipmap.ts @@ -37,9 +37,13 @@ export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe { const scale = resolution / largestResolution; - let resolutionLabel = options.template.replace('%%', resolution.toString()); + let resolutionLabel = ''; - resolutionLabel = resolution === 1 ? '' : resolutionLabel; + if (typeof options.template === 'function') { + resolutionLabel = options.template(resolution); + } else if (resolution !== 1) { + resolutionLabel = options.template.replace('%%', resolution.toString()); + } const outputName = asset.filename.replace(/(\.[\w\d_-]+)$/i, `${resolutionLabel}$1`); diff --git a/packages/assetpack/src/texture-packer/packer/createJsons.ts b/packages/assetpack/src/texture-packer/packer/createJsons.ts index 70769656..0fde767a 100644 --- a/packages/assetpack/src/texture-packer/packer/createJsons.ts +++ b/packages/assetpack/src/texture-packer/packer/createJsons.ts @@ -17,6 +17,7 @@ export function createJsons( options: { textureName: string; resolution: number; + resolutionTemplate: string | ((resolution: number) => string); textureFormat: 'png' | 'jpg'; nameStyle: 'short' | 'relative'; removeFileExtension: boolean; @@ -66,7 +67,14 @@ export function createJsons( json.meta = { app: 'http://github.com/pixijs/assetpack', version: '1.0', - image: createName(options.textureName, i, bins.length !== 1, options.resolution, options.textureFormat), + image: createName( + options.textureName, + i, + bins.length !== 1, + options.resolution, + options.textureFormat, + options.resolutionTemplate, + ), format: 'RGBA8888', size: { w: width, @@ -78,7 +86,14 @@ export function createJsons( }; jsons.push({ - name: createName(options.textureName, i, bins.length !== 1, options.resolution, 'json'), + name: createName( + options.textureName, + i, + bins.length !== 1, + options.resolution, + 'json', + options.resolutionTemplate, + ), json, }); } diff --git a/packages/assetpack/src/texture-packer/packer/createTextures.ts b/packages/assetpack/src/texture-packer/packer/createTextures.ts index b263a110..cce19db9 100644 --- a/packages/assetpack/src/texture-packer/packer/createTextures.ts +++ b/packages/assetpack/src/texture-packer/packer/createTextures.ts @@ -54,7 +54,14 @@ export async function createTextures( texturePromises.push( compositeTexture.toBuffer().then((buffer) => ({ - name: createName(options.textureName, i, bins.length !== 1, options.resolution, options.textureFormat), + name: createName( + options.textureName, + i, + bins.length !== 1, + options.resolution, + options.textureFormat, + options.resolutionTemplate, + ), buffer, })), ); @@ -63,9 +70,22 @@ export async function createTextures( return await Promise.all(texturePromises); } -export function createName(name: string, page: number, paginate: boolean, scale: number, format: string): string { +export function createName( + name: string, + page: number, + paginate: boolean, + scale: number, + format: string, + template: string | ((resolution: number) => string), +): string { const pageLabel = !paginate ? '' : `-${page}`; - const scaleLabel = scale !== 1 ? `@${scale}x` : ''; + let scaleLabel = ''; + + if (typeof template === 'function') { + scaleLabel = template(scale); + } else if (scale !== 1) { + scaleLabel = template.replace('%%', `${scale}`); + } return `${name}${pageLabel}${scaleLabel}.${format}`; } diff --git a/packages/assetpack/src/texture-packer/packer/packTextures.ts b/packages/assetpack/src/texture-packer/packer/packTextures.ts index ba7c7691..e0cb0bf3 100644 --- a/packages/assetpack/src/texture-packer/packer/packTextures.ts +++ b/packages/assetpack/src/texture-packer/packer/packTextures.ts @@ -45,6 +45,7 @@ export interface PackTexturesOptions { removeFileExtension?: boolean; autodetectAnimations?: boolean; sharpOptions?: SharpProcessingOptions; + resolutionTemplate?: string | ((resolution: number) => string); } interface PackTexturesResult { @@ -65,6 +66,7 @@ export async function packTextures(_options: PackTexturesOptions): Promise string); /** An object containing the resolutions that the images will be resized to. */ resolutions?: Record; /** A resolution used if the fixed tag is applied. Resolution must match one found in resolutions. */ @@ -82,7 +82,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipeDefaults to `@%%x`. | -| resolutions | `object` | An object containing the resolutions that the images will be resized to.
Defaults to `{ default: 1, low: 0.5 }`. | -| fixedResolution | `string` | A resolution used if the fix tag is applied e.g.
Resolution must match one found in resolutions.
Defaults to `default`. | -| sharpOptions | `object` | Options for the sharp library to use when generating the images. See [sharp resize](https://sharp.pixelplumbing.com/api-resize/#resize).
Defaults to `{ }`. | +| Option | Type | Description | +| --------------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| template | `string`, `(resolution: number) => string` | If a string:
a template for denoting the resolution of the images.

If a function:
a custom formatter for denoting the resolution of images.

Defaults to `@%%x`. | +| resolutions | `object` | An object containing the resolutions that the images will be resized to.
Defaults to `{ default: 1, low: 0.5 }`. | +| fixedResolution | `string` | A resolution used if the fix tag is applied e.g.
Resolution must match one found in resolutions.
Defaults to `default`. | +| sharpOptions | `object` | Options for the sharp library to use when generating the images. See [sharp resize](https://sharp.pixelplumbing.com/api-resize/#resize).
Defaults to `{ }`. | ## Tags diff --git a/packages/docs/docs/guide/pipes/texture-packer.mdx b/packages/docs/docs/guide/pipes/texture-packer.mdx index c909f7f6..9b67a7d4 100644 --- a/packages/docs/docs/guide/pipes/texture-packer.mdx +++ b/packages/docs/docs/guide/pipes/texture-packer.mdx @@ -45,32 +45,32 @@ export default { ## API -| Option | Type | Description | -| ------------------------------------ | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| texturePacker | `object` | Options for generating texture atlases. | -| texturePacker.textureFormat | `png\|jpg` | The format of the texture atlas file.
Defaults to `png`. | -| texturePacker.padding | `number` | The padding between sprites in the texture atlas.
Defaults to `2`. | -| texturePacker.fixedSize | `boolean` | Whether the texture atlas should be a fixed size.
Defaults to `false`. | -| texturePacker.powerOfTwo | `boolean` | Whether the texture atlas should be a power of two.
Defaults to `false`. | -| texturePacker.width | `number` | The width of the texture atlas.
Defaults to `1024`. | -| texturePacker.height | `number` | The height of the texture atlas.
Defaults to `1024`. | -| texturePacker.allowTrim | `boolean` | Whether the texture atlas should allow trimming.
Defaults to `true`. | -| texturePacker.allowRotation | `boolean` | Whether the texture atlas should allow rotation.
Defaults to `true`. | -| texturePacker.alphaThreshold | `number` | The alpha threshold for the texture atlas.
Defaults to `0.1`. | -| texturePacker.scale | `number` | The scale of the texture atlas.
Defaults to `1`. | -| texturePacker.resolution | `number` | The resolution of the texture atlas.
Defaults to `1`. | -| texturePacker.nameStyle | `short\|relative` | The name style of the texture atlas.
Defaults to `relative`. | -| texturePacker.removeFileExtension | `boolean` | Whether the file extension should be removed.
Defaults to `false`. | -| texturePacker.autodetectAnimations | `boolean` | Whether to group sprites with numeric suffixes as animations.
Defaults to `true`. | -| texturePacker.sharpOptions | `object` | Options for the sharp library to use when generate the texture atlas. See [sharp resize](https://sharp.pixelplumbing.com/api-resize/#resize).
Defaults to `{ }`. | -| | | | -| resolutionOptions | `object` | Options for generating resolutions. | -| resolutionOptions.template | `string` | A template for denoting the resolution of the images.
Defaults to `@%%x`. | -| resolutionOptions.resolutions | `object` | An object containing the resolutions that the images will be resized to.
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.
Defaults to `default`. | -| resolutionOptions.maximumTextureSize | `number` | The maximum size a sprite sheet can be before its split out.
Defaults to `4096`. | -| | | | -| addFrameNames | `boolean` | Whether to add frame names to the data in the manifest
Defaults to `false`. | +| Option | Type | Description | +| ------------------------------------ | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| texturePacker | `object` | Options for generating texture atlases. | +| texturePacker.textureFormat | `png\|jpg` | The format of the texture atlas file.
Defaults to `png`. | +| texturePacker.padding | `number` | The padding between sprites in the texture atlas.
Defaults to `2`. | +| texturePacker.fixedSize | `boolean` | Whether the texture atlas should be a fixed size.
Defaults to `false`. | +| texturePacker.powerOfTwo | `boolean` | Whether the texture atlas should be a power of two.
Defaults to `false`. | +| texturePacker.width | `number` | The width of the texture atlas.
Defaults to `1024`. | +| texturePacker.height | `number` | The height of the texture atlas.
Defaults to `1024`. | +| texturePacker.allowTrim | `boolean` | Whether the texture atlas should allow trimming.
Defaults to `true`. | +| texturePacker.allowRotation | `boolean` | Whether the texture atlas should allow rotation.
Defaults to `true`. | +| texturePacker.alphaThreshold | `number` | The alpha threshold for the texture atlas.
Defaults to `0.1`. | +| texturePacker.scale | `number` | The scale of the texture atlas.
Defaults to `1`. | +| texturePacker.resolution | `number` | The resolution of the texture atlas.
Defaults to `1`. | +| texturePacker.nameStyle | `short\|relative` | The name style of the texture atlas.
Defaults to `relative`. | +| texturePacker.removeFileExtension | `boolean` | Whether the file extension should be removed.
Defaults to `false`. | +| texturePacker.autodetectAnimations | `boolean` | Whether to group sprites with numeric suffixes as animations.
Defaults to `true`. | +| texturePacker.sharpOptions | `object` | Options for the sharp library to use when generate the texture atlas. See [sharp resize](https://sharp.pixelplumbing.com/api-resize/#resize).
Defaults to `{ }`. | +| | | | +| resolutionOptions | `object` | Options for generating resolutions. | +| resolutionOptions.template | `string`, `(resolution: number) => string` | If a string:
a template for denoting the resolution of the images.

If a function:
a custom formatter for denoting the resolution of images.

Defaults to `@%%x`. | +| resolutionOptions.resolutions | `object` | An object containing the resolutions that the images will be resized to.
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.
Defaults to `default`. | +| resolutionOptions.maximumTextureSize | `number` | The maximum size a sprite sheet can be before its split out.
Defaults to `4096`. | +| | | | +| addFrameNames | `boolean` | Whether to add frame names to the data in the manifest
Defaults to `false`. | ## Tags