mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
Webworker support for SharedAssetManger
This commit is contained in:
parent
d43b7b0a9f
commit
93f1e546bb
12
spine-ts/build/spine-webgl.d.ts
vendored
12
spine-ts/build/spine-webgl.d.ts
vendored
@ -636,7 +636,7 @@ declare module spine {
|
||||
private queueAsset;
|
||||
loadText(clientId: string, path: string): void;
|
||||
loadJson(clientId: string, path: string): void;
|
||||
loadTexture(clientId: string, textureLoader: (image: HTMLImageElement) => any, path: string): void;
|
||||
loadTexture(clientId: string, textureLoader: (image: HTMLImageElement | ImageBitmap) => any, path: string): void;
|
||||
get(clientId: string, path: string): any;
|
||||
private updateClientAssets;
|
||||
isLoadingComplete(clientId: string): boolean;
|
||||
@ -881,9 +881,9 @@ declare module spine {
|
||||
}
|
||||
declare module spine {
|
||||
abstract class Texture {
|
||||
protected _image: HTMLImageElement;
|
||||
constructor(image: HTMLImageElement);
|
||||
getImage(): HTMLImageElement;
|
||||
protected _image: HTMLImageElement | ImageBitmap;
|
||||
constructor(image: HTMLImageElement | ImageBitmap);
|
||||
getImage(): HTMLImageElement | ImageBitmap;
|
||||
abstract setFilters(minFilter: TextureFilter, magFilter: TextureFilter): void;
|
||||
abstract setWraps(uWrap: TextureWrap, vWrap: TextureWrap): void;
|
||||
abstract dispose(): void;
|
||||
@ -1369,7 +1369,7 @@ declare module spine.webgl {
|
||||
private boundUnit;
|
||||
private useMipMaps;
|
||||
static DISABLE_UNPACK_PREMULTIPLIED_ALPHA_WEBGL: boolean;
|
||||
constructor(context: ManagedWebGLRenderingContext | WebGLRenderingContext, image: HTMLImageElement, useMipMaps?: boolean);
|
||||
constructor(context: ManagedWebGLRenderingContext | WebGLRenderingContext, image: HTMLImageElement | ImageBitmap, useMipMaps?: boolean);
|
||||
setFilters(minFilter: TextureFilter, magFilter: TextureFilter): void;
|
||||
static validateMagFilter(magFilter: TextureFilter): TextureFilter.Nearest | TextureFilter.Linear | TextureFilter.Linear;
|
||||
setWraps(uWrap: TextureWrap, vWrap: TextureWrap): void;
|
||||
@ -1755,7 +1755,7 @@ declare module spine.webgl {
|
||||
canvas: HTMLCanvasElement | OffscreenCanvas;
|
||||
gl: WebGLRenderingContext;
|
||||
private restorables;
|
||||
constructor(canvasOrContext: HTMLCanvasElement | WebGLRenderingContext, contextConfig?: any);
|
||||
constructor(canvasOrContext: HTMLCanvasElement | WebGLRenderingContext | OffscreenCanvas | WebGL2RenderingContext, contextConfig?: any);
|
||||
addRestorable(restorable: Restorable): void;
|
||||
removeRestorable(restorable: Restorable): void;
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
@ -3572,15 +3572,35 @@ var spine;
|
||||
path = this.pathPrefix + path;
|
||||
if (!this.queueAsset(clientId, textureLoader, path))
|
||||
return;
|
||||
var img = new Image();
|
||||
img.crossOrigin = "anonymous";
|
||||
img.onload = function (ev) {
|
||||
_this.rawAssets[path] = img;
|
||||
var isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document);
|
||||
var isWebWorker = !isBrowser && typeof importScripts !== 'undefined';
|
||||
if (isWebWorker) {
|
||||
var options = { mode: "cors" };
|
||||
fetch(path, options).then(function (response) {
|
||||
if (!response.ok) {
|
||||
_this.errors[path] = "Couldn't load image " + path;
|
||||
}
|
||||
return response.blob();
|
||||
}).then(function (blob) {
|
||||
return createImageBitmap(blob, {
|
||||
premultiplyAlpha: 'none',
|
||||
colorSpaceConversion: 'none'
|
||||
});
|
||||
}).then(function (bitmap) {
|
||||
_this.rawAssets[path] = bitmap;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var img_1 = new Image();
|
||||
img_1.crossOrigin = "anonymous";
|
||||
img_1.onload = function (ev) {
|
||||
_this.rawAssets[path] = img_1;
|
||||
};
|
||||
img.onerror = function (ev) {
|
||||
img_1.onerror = function (ev) {
|
||||
_this.errors[path] = "Couldn't load image " + path;
|
||||
};
|
||||
img.src = path;
|
||||
img_1.src = path;
|
||||
}
|
||||
};
|
||||
SharedAssetManager.prototype.get = function (clientId, path) {
|
||||
path = this.pathPrefix + path;
|
||||
@ -3590,6 +3610,8 @@ var spine;
|
||||
return clientAssets.assets[path];
|
||||
};
|
||||
SharedAssetManager.prototype.updateClientAssets = function (clientAssets) {
|
||||
var isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document);
|
||||
var isWebWorker = !isBrowser && typeof importScripts !== 'undefined';
|
||||
for (var i = 0; i < clientAssets.toLoad.length; i++) {
|
||||
var path = clientAssets.toLoad[i];
|
||||
var asset = clientAssets.assets[path];
|
||||
@ -3597,6 +3619,15 @@ var spine;
|
||||
var rawAsset = this.rawAssets[path];
|
||||
if (rawAsset === null || rawAsset === undefined)
|
||||
continue;
|
||||
if (isWebWorker) {
|
||||
if (rawAsset instanceof ImageBitmap) {
|
||||
clientAssets.assets[path] = clientAssets.textureLoader(rawAsset);
|
||||
}
|
||||
else {
|
||||
clientAssets.assets[path] = rawAsset;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (rawAsset instanceof HTMLImageElement) {
|
||||
clientAssets.assets[path] = clientAssets.textureLoader(rawAsset);
|
||||
}
|
||||
@ -3605,6 +3636,7 @@ var spine;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
SharedAssetManager.prototype.isLoadingComplete = function (clientId) {
|
||||
var clientAssets = this.clientAssets[clientId];
|
||||
@ -10732,7 +10764,7 @@ var spine;
|
||||
var _this = this;
|
||||
if (contextConfig === void 0) { contextConfig = { alpha: "true" }; }
|
||||
this.restorables = new Array();
|
||||
if (canvasOrContext instanceof HTMLCanvasElement) {
|
||||
if (!((canvasOrContext instanceof WebGLRenderingContext) || (canvasOrContext instanceof WebGL2RenderingContext))) {
|
||||
var canvas = canvasOrContext;
|
||||
this.gl = (canvas.getContext("webgl2", contextConfig) || canvas.getContext("webgl", contextConfig));
|
||||
this.canvas = canvas;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -32,7 +32,7 @@ module spine {
|
||||
clientId: string;
|
||||
toLoad = new Array<string>();
|
||||
assets: Map<any> = {};
|
||||
textureLoader: (image: HTMLImageElement) => any;
|
||||
textureLoader: (image: HTMLImageElement | ImageBitmap) => any;
|
||||
|
||||
constructor(clientId: string) {
|
||||
this.clientId = clientId;
|
||||
@ -56,7 +56,7 @@ module spine {
|
||||
this.pathPrefix = pathPrefix;
|
||||
}
|
||||
|
||||
private queueAsset(clientId: string, textureLoader: (image: HTMLImageElement) => any, path: string): boolean {
|
||||
private queueAsset(clientId: string, textureLoader: (image: HTMLImageElement | ImageBitmap) => any, path: string): boolean {
|
||||
let clientAssets = this.clientAssets[clientId];
|
||||
if (clientAssets === null || clientAssets === undefined) {
|
||||
clientAssets = new Assets(clientId);
|
||||
@ -111,10 +111,30 @@ module spine {
|
||||
request.send();
|
||||
}
|
||||
|
||||
loadTexture (clientId: string, textureLoader: (image: HTMLImageElement) => any, path: string) {
|
||||
loadTexture (clientId: string, textureLoader: (image: HTMLImageElement | ImageBitmap) => any, path: string) {
|
||||
path = this.pathPrefix + path;
|
||||
if (!this.queueAsset(clientId, textureLoader, path)) return;
|
||||
|
||||
let isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document);
|
||||
let isWebWorker = !isBrowser && typeof importScripts !== 'undefined';
|
||||
|
||||
if (isWebWorker) {
|
||||
// For webworker use fetch instead of Image()
|
||||
const options = {mode: <RequestMode>"cors"};
|
||||
fetch(path, options).then( (response) => {
|
||||
if (!response.ok) {
|
||||
this.errors[path] = "Couldn't load image " + path;
|
||||
}
|
||||
return response.blob();
|
||||
}).then( (blob) => {
|
||||
return createImageBitmap(blob, {
|
||||
premultiplyAlpha: 'none',
|
||||
colorSpaceConversion: 'none',
|
||||
});
|
||||
}).then( (bitmap) => {
|
||||
this.rawAssets[path] = bitmap;
|
||||
});
|
||||
} else {
|
||||
let img = new Image();
|
||||
img.crossOrigin = "anonymous";
|
||||
img.onload = (ev) => {
|
||||
@ -125,6 +145,7 @@ module spine {
|
||||
}
|
||||
img.src = path;
|
||||
}
|
||||
}
|
||||
|
||||
get (clientId: string, path: string) {
|
||||
path = this.pathPrefix + path;
|
||||
@ -134,12 +155,24 @@ module spine {
|
||||
}
|
||||
|
||||
private updateClientAssets(clientAssets: Assets): void {
|
||||
let isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document);
|
||||
let isWebWorker = !isBrowser && typeof importScripts !== 'undefined';
|
||||
|
||||
for (let i = 0; i < clientAssets.toLoad.length; i++) {
|
||||
let path = clientAssets.toLoad[i];
|
||||
let asset = clientAssets.assets[path];
|
||||
if (asset === null || asset === undefined) {
|
||||
let rawAsset = this.rawAssets[path];
|
||||
if (rawAsset === null || rawAsset === undefined) continue;
|
||||
|
||||
if (isWebWorker)
|
||||
{
|
||||
if (rawAsset instanceof ImageBitmap) {
|
||||
clientAssets.assets[path] = clientAssets.textureLoader(<ImageBitmap>rawAsset);
|
||||
} else {
|
||||
clientAssets.assets[path] = rawAsset;
|
||||
}
|
||||
} else {
|
||||
if (rawAsset instanceof HTMLImageElement) {
|
||||
clientAssets.assets[path] = clientAssets.textureLoader(<HTMLImageElement>rawAsset);
|
||||
} else {
|
||||
@ -148,6 +181,7 @@ module spine {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isLoadingComplete (clientId: string): boolean {
|
||||
let clientAssets = this.clientAssets[clientId];
|
||||
|
||||
@ -29,13 +29,13 @@
|
||||
|
||||
module spine {
|
||||
export abstract class Texture {
|
||||
protected _image: HTMLImageElement;
|
||||
protected _image: HTMLImageElement | ImageBitmap;
|
||||
|
||||
constructor (image: HTMLImageElement) {
|
||||
constructor (image: HTMLImageElement | ImageBitmap) {
|
||||
this._image = image;
|
||||
}
|
||||
|
||||
getImage (): HTMLImageElement {
|
||||
getImage (): HTMLImageElement | ImageBitmap {
|
||||
return this._image;
|
||||
}
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@
|
||||
module spine.webgl {
|
||||
export class AssetManager extends spine.AssetManager {
|
||||
constructor (context: ManagedWebGLRenderingContext | WebGLRenderingContext, pathPrefix: string = "") {
|
||||
super((image: HTMLImageElement) => {
|
||||
super((image: HTMLImageElement | ImageBitmap) => {
|
||||
return new spine.webgl.GLTexture(context, image);
|
||||
}, pathPrefix);
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ module spine.webgl {
|
||||
|
||||
public static DISABLE_UNPACK_PREMULTIPLIED_ALPHA_WEBGL = false;
|
||||
|
||||
constructor (context: ManagedWebGLRenderingContext | WebGLRenderingContext, image: HTMLImageElement, useMipMaps: boolean = false) {
|
||||
constructor (context: ManagedWebGLRenderingContext | WebGLRenderingContext, image: HTMLImageElement | ImageBitmap, useMipMaps: boolean = false) {
|
||||
super(image);
|
||||
this.context = context instanceof ManagedWebGLRenderingContext? context : new ManagedWebGLRenderingContext(context);
|
||||
this.useMipMaps = useMipMaps;
|
||||
|
||||
@ -33,8 +33,8 @@ module spine.webgl {
|
||||
public gl: WebGLRenderingContext;
|
||||
private restorables = new Array<Restorable>();
|
||||
|
||||
constructor(canvasOrContext: HTMLCanvasElement | WebGLRenderingContext, contextConfig: any = { alpha: "true" }) {
|
||||
if (canvasOrContext instanceof HTMLCanvasElement) {
|
||||
constructor(canvasOrContext: HTMLCanvasElement | WebGLRenderingContext | OffscreenCanvas | WebGL2RenderingContext, contextConfig: any = { alpha: "true" }) {
|
||||
if (!((canvasOrContext instanceof WebGLRenderingContext) || (canvasOrContext instanceof WebGL2RenderingContext))) {
|
||||
let canvas = canvasOrContext;
|
||||
this.gl = <WebGLRenderingContext> (canvas.getContext("webgl2", contextConfig) || canvas.getContext("webgl", contextConfig));
|
||||
this.canvas = canvas;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user