Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nomip tag to avoid mipmaps being generated #120

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/assetpack/src/image/mipmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const defaultMipmapOptions: Required<MipmapOptions> = {
fixedResolution: 'default',
};

export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions, 'fix'>
export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions, 'fix' | 'nomip'>
Zyie marked this conversation as resolved.
Show resolved Hide resolved
{
const mipmap = resolveOptions(_options, defaultMipmapOptions);

Expand All @@ -34,14 +34,15 @@ export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions, '
},
tags: {
fix: 'fix',
nomip: 'nomip',
},
test(asset: Asset, options)
{
return options && checkExt(asset.path, '.png', '.jpg', '.jpeg');
return options && checkExt(asset.path, '.png', '.jpg', '.jpeg') && !asset.allMetaData[this.tags!.nomip];
},
async transform(asset: Asset, options)
{
const shouldMipmap = mipmap && !asset.metaData[this.tags!.fix];
const shouldMipmap = mipmap && !asset.allMetaData[this.tags!.fix];

let processedImages: CompressImageData[];

Expand Down
6 changes: 4 additions & 2 deletions packages/assetpack/src/texture-packer/texturePacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function checkForTexturePackerShortcutClashes(
}
}

export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<TexturePackerOptions, 'tps' | 'fix' | 'jpg'>
export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<TexturePackerOptions, 'tps' | 'fix' | 'jpg' | 'nomip'>
{
let shortcutClash: Record<string, boolean> = {};

Expand All @@ -75,6 +75,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
tps: 'tps',
fix: 'fix',
jpg: 'jpg',
nomip: 'nomip',
},
test(asset: Asset)
{
Expand Down Expand Up @@ -183,7 +184,8 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te

jsonAsset.buffer = Buffer.from(JSON.stringify(json, null, 2));

textureAsset.metaData[this.tags!.fix] = true;
// don't mipmap the texture again, we have already done that
textureAsset.metaData[this.tags!.nomip] = true;

jsonAsset.transformData.page = i;

Expand Down
8 changes: 4 additions & 4 deletions packages/assetpack/src/webfont/sdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface SDFFontOptions extends PluginOptions

function signedFont(
defaultOptions: SDFFontOptions
): AssetPipe<SDFFontOptions, 'font' | 'nc' | 'fix'>
): AssetPipe<SDFFontOptions, 'font' | 'nc' | 'nomip'>
{
return {
folder: false,
Expand All @@ -23,7 +23,7 @@ function signedFont(
tags: {
font: 'font',
nc: 'nc',
fix: 'fix',
nomip: 'nomip',
},
test(asset: Asset)
{
Expand Down Expand Up @@ -51,9 +51,9 @@ function signedFont(

const newTextureAsset = createNewAssetAt(asset, newTextureName);

// don't compress!
// don't compress or mipmap the font texture, we can't resize or update image paths
newTextureAsset.metaData[this.tags!.nc] = true;
newTextureAsset.metaData[this.tags!.fix] = true;
newTextureAsset.metaData[this.tags!.nomip] = true;
newTextureAsset.metaData.mIgnore = true;

assets.push(newTextureAsset);
Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/test/core/AssetWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('AssetWatcher', () =>

it('should have correct file state with a cache', async () =>
{
const testName = 'asset-watcher';
const testName = 'asset-watcher-file';
const inputDir = getInputDir(pkg, testName);

createFolder(
Expand Down
40 changes: 39 additions & 1 deletion packages/assetpack/test/image/Mipmap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,45 @@ describe('Mipmap', () =>
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
pipes: [mipmap()]
pipes: [mipmap({ fixedResolution: 'low' })]
});

await assetpack.run();

expect(existsSync(`${outputDir}/assets/test.png`)).toBe(false);
expect(existsSync(`${outputDir}/assets/[email protected]`)).toBe(true);
});

it('should prevent mipmaps', async () =>
{
const testName = 'mip-nomip';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [],
folders: [
{
name: 'assets{nomip}',
files: [
{
name: 'test.png',
content: assetPath('image/png-1.png'),
},
],
folders: [],
},
],
});

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
pipes: [mipmap({ fixedResolution: 'low' })]
});

await assetpack.run();
Expand Down
12 changes: 9 additions & 3 deletions packages/assetpack/test/texture-packer/texturePacker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ describe('Texture Packer', () =>
output: outputDir,
cache: false,
pipes: [
texturePacker()
texturePacker({ resolutionOptions: { fixedResolution: 'low' } }),
]
});

Expand All @@ -373,10 +373,16 @@ describe('Texture Packer', () =>
const sheet1 = existsSync(`${outputDir}/sprites/sprites.json`);
const sheet2 = existsSync(`${outputDir}/sprites/[email protected]`);
const sheet3 = existsSync(`${outputDir}/sprites/[email protected]`);
const img = existsSync(`${outputDir}/sprites/sprites.png`);
const img2 = existsSync(`${outputDir}/sprites/[email protected]`);
const img3 = existsSync(`${outputDir}/sprites/[email protected]`);

expect(sheet1).toBe(true);
expect(sheet2).toBe(false);
expect(sheet1).toBe(false);
expect(sheet2).toBe(true);
expect(sheet3).toBe(false);
expect(img).toBe(false);
expect(img2).toBe(true);
expect(img3).toBe(false);
});

it('should create jpg spritesheets', async () =>
Expand Down
40 changes: 40 additions & 0 deletions packages/assetpack/test/webfont/Webfont.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,46 @@ describe('Webfont', () =>
expect(existsSync(`${outputDir}/sdf.png`)).toBe(true);
});

it('should generate a sdf font from ttf file at 1 resolution', async () =>
{
const testName = 'sdf-resolution';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [
{
name: 'sdf{sdf}.ttf',
content: assetPath('font/Roboto-Regular.ttf'),
},
],
folders: [],
}
);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
pipes: [
sdfFont(),
mipmap({
fixedResolution: 'high',
resolutions: { high: 2, default: 1 },
})
]
});

await assetpack.run();

// expect webfont file to be generated
expect(existsSync(`${outputDir}/sdf.fnt`)).toBe(true);
expect(existsSync(`${outputDir}/sdf.png`)).toBe(true);
});

it('should generate a split sdf font from ttf file', async () =>
{
const testName = 'sdf-split';
Expand Down
7 changes: 4 additions & 3 deletions packages/docs/docs/guide/pipes/mipmap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ export default {

## Tags

| Tag | Folder/File | Description |
| ----- | ----------- | -------------------------------------------------------------------------------------- |
| `fix` | `both` | If present the `fixedResolution` will be used. No other resolutions will be generated. |
| Tag | Folder/File | Description |
| ------- | ----------- | -------------------------------------------------------------------------------------- |
| `fix` | `both` | If present the `fixedResolution` will be used. No other resolutions will be generated. |
| `nomip` | `both` | If present mipmaps will not be generated. |

### Example: fixed resolution

Expand Down