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: added Texture Options to spritesheet loader #11163

Merged
merged 7 commits into from
Dec 22, 2024
Merged
Changes from 4 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
25 changes: 22 additions & 3 deletions src/spritesheet/spritesheetAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Spritesheet } from './Spritesheet';
import type { AssetExtensionAdvanced } from '../assets/AssetExtension';
import type { Loader } from '../assets/loader/Loader';
import type { ResolvedAsset } from '../assets/types';
import type { TextureSourceOptions } from '../rendering/renderers/shared/texture/sources/TextureSource';
import type { SpritesheetData } from './Spritesheet';

export interface SpriteSheetJson extends SpritesheetData
Expand Down Expand Up @@ -62,6 +63,9 @@ function getCacheableAssets(keys: string[], asset: Spritesheet, ignoreMultiPack:
* src: 'path/to/spritesheet.json',
* data: {
* ignoreMultiPack: true,
* textureOptions: {
* scaleMode: "nearest"
* }
* }
* })
* @type {AssetExtension}
Expand Down Expand Up @@ -122,13 +126,19 @@ export const spritesheetAsset = {

async parse(
asset: SpriteSheetJson,
options: ResolvedAsset<{texture?: Texture, imageFilename?: string, ignoreMultiPack?: boolean}>,
options: ResolvedAsset<{
texture?: Texture,
imageFilename?: string,
ignoreMultiPack?: boolean,
textureOptions?: TextureSourceOptions
}>,
loader?: Loader
): Promise<Spritesheet>
{
const {
texture: imageTexture, // if user need to use preloaded texture
imageFilename // if user need to use custom filename (not from jsonFile.meta.image)
imageFilename, // if user need to use custom filename (not from jsonFile.meta.image)
textureOptions // if user need to set texture options on texture
} = options?.data ?? {};

let basePath = path.dirname(options.src);
Expand All @@ -148,7 +158,16 @@ export const spritesheetAsset = {
{
const imagePath = copySearchParams(basePath + (imageFilename ?? asset.meta.image), options.src);

const assets = await loader.load<Texture>([imagePath]);
let assets;

if (textureOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we even need the if (textureOptions) check here as textureOptions can be null:

replacing the original line
assets = await loader.load<Texture>([imagePath]);

with
assets = await loader.load<Texture>([{ src: imagePath, data: textureOptions}]);

should work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like that check is still needed, removing it caused the SVG spritesheet test to fail.
I could modify the SVG script to fix that issue but I don't know if that's in the scope of this PR, what are your thoughts?

{
assets = await loader.load<Texture>([{ src: imagePath, data: textureOptions }]);
}
else
{
assets = await loader.load<Texture>([imagePath]);
}

texture = assets[imagePath];
}
Expand Down
Loading