Skip to content
Open
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
11 changes: 8 additions & 3 deletions packages/assetpack/src/image/mipmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -94,8 +94,13 @@ export function mipmap(defaultOptions: MipmapOptions = {}): AssetPipe<MipmapOpti
let resolution = '';

if (options) {
resolution = (options as Required<MipmapOptions>).template.replace('%%', `${data.resolution}`);
resolution = data.resolution === 1 ? '' : resolution;
const template = (options as Required<MipmapOptions>).template;

if (typeof template === 'function') {
resolution = template(data.resolution);
} else if (data.resolution !== 1) {
resolution = template.replace('%%', `${data.resolution}`);
}
}

const end = `${resolution}${data.format}`;
Expand Down
8 changes: 6 additions & 2 deletions packages/assetpack/src/spine/spineAtlasMipmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe<SpineOption
// loop through each resolution and pack the images
const assets = Object.values(resolutionHash).map((resolution) => {
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`);

Expand Down
19 changes: 17 additions & 2 deletions packages/assetpack/src/texture-packer/packer/createJsons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
}
Expand Down
26 changes: 23 additions & 3 deletions packages/assetpack/src/texture-packer/packer/createTextures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),
);
Expand All @@ -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}`;
}
2 changes: 2 additions & 0 deletions packages/assetpack/src/texture-packer/packer/packTextures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface PackTexturesOptions {
removeFileExtension?: boolean;
autodetectAnimations?: boolean;
sharpOptions?: SharpProcessingOptions;
resolutionTemplate?: string | ((resolution: number) => string);
}

interface PackTexturesResult {
Expand All @@ -65,6 +66,7 @@ export async function packTextures(_options: PackTexturesOptions): Promise<PackT
textureFormat: 'png',
scale: 1,
resolution: 1,
resolutionTemplate: '@%%x',
nameStyle: 'relative',
removeFileExtension: false,
autodetectAnimations: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/assetpack/src/texture-packer/texturePacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface TexturePackerOptions extends PluginOptions {
/** Options for creating different resolutions of the sprite sheet. */
resolutionOptions?: {
/** 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?: Record<string, number>;
/** A resolution used if the fixed tag is applied. Resolution must match one found in resolutions. */
Expand Down Expand Up @@ -82,7 +82,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te

async transform(asset: Asset, options) {
const { resolutionOptions, texturePacker } = options;
const { resolutions, fixedResolution } = resolutionOptions!;
const { resolutions, fixedResolution, template: resolutionTemplate } = resolutionOptions!;

const fixedResolutions = { [fixedResolution]: resolutions[fixedResolution] };

Expand Down Expand Up @@ -147,6 +147,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
texturesToPack,
scale,
resolution,
resolutionTemplate,
});

if (options.addFrameNames) {
Expand Down
12 changes: 6 additions & 6 deletions packages/docs/docs/guide/pipes/mipmap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export default {

## API

| Option | Type | Description |
| --------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| template | `string` | A template for denoting the resolution of the images.<br />Defaults to `@%%x`. |
| resolutions | `object` | An object containing the resolutions that the images will be resized to.<br />Defaults to `{ default: 1, low: 0.5 }`. |
| fixedResolution | `string` | A resolution used if the fix tag is applied e.g.<br />Resolution must match one found in resolutions.<br />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).<br />Defaults to `{ }`. |
| Option | Type | Description |
| --------------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| template | `string`, `(resolution: number) => string` | If a string:<br />a template for denoting the resolution of the images.<br /><br />If a function:<br />a custom formatter for denoting the resolution of images.<br /><br />Defaults to `@%%x`. |
| resolutions | `object` | An object containing the resolutions that the images will be resized to.<br />Defaults to `{ default: 1, low: 0.5 }`. |
| fixedResolution | `string` | A resolution used if the fix tag is applied e.g.<br />Resolution must match one found in resolutions.<br />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).<br />Defaults to `{ }`. |

## Tags

Expand Down
Loading
Loading