forked from pixijs/pixijs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add detection parsers to Assets (pixijs#8482)
- Loading branch information
Showing
20 changed files
with
327 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { ExtensionMetadata } from '@pixi/core'; | ||
|
||
export interface FormatDetectionParser | ||
{ | ||
extension?: ExtensionMetadata; | ||
test: () => Promise<boolean>, | ||
add: (formats: string[]) => Promise<string[]>, | ||
remove: (formats: string[]) => Promise<string[]>, | ||
} | ||
|
||
export * from './parsers'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ExtensionType } from '@pixi/core'; | ||
import { settings } from '@pixi/settings'; | ||
import type { FormatDetectionParser } from '..'; | ||
import { addFormats, removeFormats } from '../utils/detectUtils'; | ||
|
||
export const detectAvif: FormatDetectionParser = { | ||
extension: ExtensionType.DetectionParser, | ||
test: async (): Promise<boolean> => | ||
{ | ||
if (!globalThis.createImageBitmap) return false; | ||
|
||
// eslint-disable-next-line max-len | ||
const avifData = 'data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A='; | ||
const blob = await settings.ADAPTER.fetch(avifData).then((r) => r.blob()); | ||
|
||
return createImageBitmap(blob).then(() => true, () => false); | ||
}, | ||
add: addFormats('avif'), | ||
remove: removeFormats('avif') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { BasisParser } from '@pixi/basis'; | ||
import { ExtensionType } from '@pixi/core'; | ||
import type { FormatDetectionParser } from '..'; | ||
import { addFormats, removeFormats } from '../utils/detectUtils'; | ||
|
||
export const detectBasis = { | ||
extension: ExtensionType.DetectionParser, | ||
test: async (): Promise<boolean> => !!(BasisParser.basisBinding && BasisParser.TranscoderWorker.wasmSource), | ||
add: addFormats('basis'), | ||
remove: removeFormats('basis') | ||
} as FormatDetectionParser; |
73 changes: 73 additions & 0 deletions
73
packages/assets/src/detections/parsers/detectCompressedTextures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { CompressedTextureExtensionRef, CompressedTextureExtensions } from '@pixi/compressed-textures'; | ||
import { ExtensionType } from '@pixi/core'; | ||
import { settings } from '@pixi/settings'; | ||
import type { FormatDetectionParser } from '..'; | ||
|
||
let storedGl: WebGLRenderingContext; | ||
let extensions: Partial<CompressedTextureExtensions>; | ||
|
||
function getCompressedTextureExtensions() | ||
{ | ||
extensions = { | ||
s3tc: storedGl.getExtension('WEBGL_compressed_texture_s3tc'), | ||
s3tc_sRGB: storedGl.getExtension('WEBGL_compressed_texture_s3tc_srgb'), /* eslint-disable-line camelcase */ | ||
etc: storedGl.getExtension('WEBGL_compressed_texture_etc'), | ||
etc1: storedGl.getExtension('WEBGL_compressed_texture_etc1'), | ||
pvrtc: storedGl.getExtension('WEBGL_compressed_texture_pvrtc') | ||
|| storedGl.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc'), | ||
atc: storedGl.getExtension('WEBGL_compressed_texture_atc'), | ||
astc: storedGl.getExtension('WEBGL_compressed_texture_astc') | ||
} as Partial<CompressedTextureExtensions>; | ||
} | ||
|
||
export const detectCompressedTextures = { | ||
extension: ExtensionType.DetectionParser, | ||
test: async (): Promise<boolean> => | ||
{ | ||
// Auto-detect WebGL compressed-texture extensions | ||
const canvas = settings.ADAPTER.createCanvas(); | ||
const gl = canvas.getContext('webgl'); | ||
|
||
if (!gl) | ||
{ | ||
// #if _DEBUG | ||
console.warn('WebGL not available for compressed textures.'); | ||
// #endif | ||
|
||
return false; | ||
} | ||
|
||
storedGl = gl; | ||
|
||
return true; | ||
}, | ||
add: async (formats: string[]): Promise<string[]> => | ||
{ | ||
if (!extensions) getCompressedTextureExtensions(); | ||
|
||
const textureFormats = []; | ||
|
||
// Assign all available compressed-texture formats | ||
for (const extensionName in extensions) | ||
{ | ||
const extension = extensions[extensionName as CompressedTextureExtensionRef]; | ||
|
||
if (!extension) | ||
{ | ||
continue; | ||
} | ||
|
||
textureFormats.push(extensionName); | ||
} | ||
|
||
formats.unshift(...textureFormats); | ||
|
||
return formats; | ||
}, | ||
remove: async (formats: string[]): Promise<string[]> => | ||
{ | ||
if (!extensions) getCompressedTextureExtensions(); | ||
|
||
return formats.filter((f) => !(f in extensions)); | ||
}, | ||
} as FormatDetectionParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtensionType } from '@pixi/core'; | ||
import { settings } from '@pixi/settings'; | ||
import type { FormatDetectionParser } from '..'; | ||
import { addFormats, removeFormats } from '../utils/detectUtils'; | ||
|
||
export const detectWebp = { | ||
extension: ExtensionType.DetectionParser, | ||
test: async (): Promise<boolean> => | ||
{ | ||
if (!globalThis.createImageBitmap) return false; | ||
|
||
const webpData = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA='; | ||
const blob = await settings.ADAPTER.fetch(webpData).then((r) => r.blob()); | ||
|
||
return createImageBitmap(blob).then(() => true, () => false); | ||
}, | ||
add: addFormats('webp'), | ||
remove: removeFormats('webp') | ||
} as FormatDetectionParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './detectAvif'; | ||
export * from './detectCompressedTextures'; | ||
export * from './detectBasis'; | ||
export * from './detectWebp'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function addFormats(...format: string[]): (formats: string[]) => Promise<string[]> | ||
{ | ||
return async (formats: string[]) => | ||
{ | ||
formats.unshift(...format); | ||
|
||
return formats; | ||
}; | ||
} | ||
export function removeFormats(...format: string[]): (formats: string[]) => Promise<string[]> | ||
{ | ||
return async (formats: string[]) => | ||
{ | ||
for (const f of format) | ||
{ | ||
const index = formats.indexOf(f); | ||
|
||
if (index !== -1) | ||
{ | ||
formats.splice(index, 1); | ||
} | ||
} | ||
|
||
return formats; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './detectUtils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './resolveSpriteSheetUrl'; | ||
export * from './resolveTextureUrl'; | ||
export * from './resolveCompressedTextureUrl'; |
49 changes: 49 additions & 0 deletions
49
packages/assets/src/resolver/parsers/resolveCompressedTextureUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ExtensionType } from '@pixi/core'; | ||
import { settings } from '@pixi/settings'; | ||
|
||
import type { ResolveAsset, ResolveURLParser } from '../types'; | ||
|
||
export const resolveCompressedTextureUrl = { | ||
extension: ExtensionType.ResolveParser, | ||
test: (value: string) => | ||
{ | ||
const temp = value.split('?')[0]; | ||
const extension = temp.split('.').pop(); | ||
|
||
return ['basis', 'ktx', 'dds'].includes(extension); | ||
}, | ||
parse: (value: string): ResolveAsset => | ||
{ | ||
const temp = value.split('?')[0]; | ||
const extension = temp.split('.').pop(); | ||
|
||
if (extension === 'ktx') | ||
{ | ||
const extensions = [ | ||
'.s3tc.ktx', | ||
'.s3tc_sRGB.ktx', | ||
'.etc.ktx', | ||
'.etc1.ktx', | ||
'.pvrt.ktx', | ||
'.atc.ktx', | ||
'.astc.ktx' | ||
]; | ||
|
||
// check if value ends with one of the extensions | ||
if (extensions.some((ext) => value.endsWith(ext))) | ||
{ | ||
return { | ||
resolution: parseFloat(settings.RETINA_PREFIX.exec(value)?.[1] ?? '1'), | ||
format: extensions.find((ext) => value.endsWith(ext)), | ||
src: value, | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
resolution: parseFloat(settings.RETINA_PREFIX.exec(value)?.[1] ?? '1'), | ||
format: value.split('.').pop(), | ||
src: value, | ||
}; | ||
}, | ||
} as ResolveURLParser; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.