- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
import { LoaderResource } from '@pixi/loaders';
import { url } from '@pixi/utils';
import { settings } from '@pixi/settings';
import type { Loader } from '@pixi/loaders';
import type { INTERNAL_FORMATS } from '../const';
import type { ExtensionMetadata } from '@pixi/core';
import { ExtensionType } from '@pixi/core';
/**
* Schema for compressed-texture manifests
* @ignore
* @see PIXI.CompressedTextureLoader
*/
export type CompressedTextureManifest = {
textures: Array<{ src: string, format?: keyof INTERNAL_FORMATS}>,
cacheID: string;
};
// Missing typings? - https://github.com/microsoft/TypeScript/issues/39655
/** Compressed texture extensions */
/* eslint-disable camelcase */
export type CompressedTextureExtensions = {
s3tc?: WEBGL_compressed_texture_s3tc,
s3tc_sRGB: WEBGL_compressed_texture_s3tc_srgb,
etc: any,
etc1: any,
pvrtc: any,
atc: any,
astc: WEBGL_compressed_texture_astc
};
export type CompressedTextureExtensionRef = keyof CompressedTextureExtensions;
/* eslint-enable camelcase */
/**
* Loader plugin for handling compressed textures for all platforms.
* @class
* @memberof PIXI
* @implements {PIXI.ILoaderPlugin}
*/
export class CompressedTextureLoader
{
/** @ignore */
static extension: ExtensionMetadata = ExtensionType.Loader;
/** Map of available texture extensions. */
private static _textureExtensions: Partial<CompressedTextureExtensions>;
/** Map of available texture formats. */
private static _textureFormats: { [P in keyof INTERNAL_FORMATS]?: number };
/**
* Called after a compressed-textures manifest is loaded.
*
* This will then load the correct compression format for the device. Your manifest should adhere
* to the following schema:
*
* ```js
* import { INTERNAL_FORMATS } from '@pixi/constants';
*
* type CompressedTextureManifest = {
* textures: Array<{ src: string, format?: keyof INTERNAL_FORMATS}>,
* cacheID: string;
* };
* ```
*
* This is an example of a .json manifest file
*
* ```json
* {
* "cacheID":"asset",
* "textures":[
* { "src":"asset.fallback.png" },
* { "format":"COMPRESSED_RGBA_S3TC_DXT5_EXT", "src":"asset.s3tc.ktx" },
* { "format":"COMPRESSED_RGBA8_ETC2_EAC", "src":"asset.etc.ktx" },
* { "format":"RGBA_PVRTC_4BPPV1_IMG", "src":"asset.pvrtc.ktx" }
* ]
* }
* ```
*/
static use(resource: LoaderResource, next: (...args: any[]) => void): void
{
const data: CompressedTextureManifest = resource.data;
const loader = this as unknown as Loader;
if (resource.type === LoaderResource.TYPE.JSON
&& data
&& data.cacheID
&& data.textures)
{
const textures = data.textures;
let textureURL: string;
let fallbackURL: string;
// Search for an extension that holds one the formats
for (let i = 0, j = textures.length; i < j; i++)
{
const texture = textures[i];
const url = texture.src;
const format = texture.format;
if (!format)
{
fallbackURL = url;
}
if (CompressedTextureLoader.textureFormats[format])
{
textureURL = url;
break;
}
}
textureURL = textureURL || fallbackURL;
// Make sure we have a URL
if (!textureURL)
{
next(new Error(`Cannot load compressed-textures in ${resource.url}, make sure you provide a fallback`));
return;
}
if (textureURL === resource.url)
{
// Prevent infinite loops
next(new Error('URL of compressed texture cannot be the same as the manifest\'s URL'));
return;
}
const loadOptions = {
crossOrigin: resource.crossOrigin,
metadata: resource.metadata.imageMetadata,
parentResource: resource
};
const resourcePath = url.resolve(resource.url.replace(loader.baseUrl, ''), textureURL);
const resourceName = data.cacheID;
// The appropriate loader should register the texture
loader.add(resourceName, resourcePath, loadOptions, (res: LoaderResource) =>
{
if (res.error)
{
next(res.error);
return;
}
const { texture = null, textures = {} } = res;
// Make sure texture/textures is assigned to parent resource
Object.assign(resource, { texture, textures });
// Pass along any error
next();
});
}
else
{
next();
}
}
/** Map of available texture extensions. */
public static get textureExtensions(): Partial<CompressedTextureExtensions>
{
if (!CompressedTextureLoader._textureExtensions)
{
// 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. Silently failing.');
// #endif
return {};
}
const extensions = {
s3tc: gl.getExtension('WEBGL_compressed_texture_s3tc'),
s3tc_sRGB: gl.getExtension('WEBGL_compressed_texture_s3tc_srgb'), /* eslint-disable-line camelcase */
etc: gl.getExtension('WEBGL_compressed_texture_etc'),
etc1: gl.getExtension('WEBGL_compressed_texture_etc1'),
pvrtc: gl.getExtension('WEBGL_compressed_texture_pvrtc')
|| gl.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc'),
atc: gl.getExtension('WEBGL_compressed_texture_atc'),
astc: gl.getExtension('WEBGL_compressed_texture_astc')
};
CompressedTextureLoader._textureExtensions = extensions;
}
return CompressedTextureLoader._textureExtensions;
}
/** Map of available texture formats. */
public static get textureFormats(): { [P in keyof INTERNAL_FORMATS]?: number }
{
if (!CompressedTextureLoader._textureFormats)
{
const extensions = CompressedTextureLoader.textureExtensions;
CompressedTextureLoader._textureFormats = {};
// Assign all available compressed-texture formats
for (const extensionName in extensions)
{
const extension = extensions[extensionName as CompressedTextureExtensionRef];
if (!extension)
{
continue;
}
Object.assign(
CompressedTextureLoader._textureFormats,
Object.getPrototypeOf(extension));
}
}
return CompressedTextureLoader._textureFormats;
}
}