1 | import { ALPHA_MODES } from '@pixi/constants';
|
2 | import { determineCrossOrigin } from '@pixi/utils';
|
3 | import { Resource } from './Resource.mjs';
|
4 |
|
5 | class BaseImageResource extends Resource {
|
6 | constructor(source) {
|
7 | const sourceAny = source;
|
8 | const width = sourceAny.naturalWidth || sourceAny.videoWidth || sourceAny.width;
|
9 | const height = sourceAny.naturalHeight || sourceAny.videoHeight || sourceAny.height;
|
10 | super(width, height);
|
11 | this.source = source;
|
12 | this.noSubImage = false;
|
13 | }
|
14 | static crossOrigin(element, url, crossorigin) {
|
15 | if (crossorigin === void 0 && !url.startsWith("data:")) {
|
16 | element.crossOrigin = determineCrossOrigin(url);
|
17 | } else if (crossorigin !== false) {
|
18 | element.crossOrigin = typeof crossorigin === "string" ? crossorigin : "anonymous";
|
19 | }
|
20 | }
|
21 | upload(renderer, baseTexture, glTexture, source) {
|
22 | const gl = renderer.gl;
|
23 | const width = baseTexture.realWidth;
|
24 | const height = baseTexture.realHeight;
|
25 | source = source || this.source;
|
26 | if (typeof HTMLImageElement !== "undefined" && source instanceof HTMLImageElement) {
|
27 | if (!source.complete || source.naturalWidth === 0) {
|
28 | return false;
|
29 | }
|
30 | } else if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) {
|
31 | if (source.readyState <= 1 && source.buffered.length === 0) {
|
32 | return false;
|
33 | }
|
34 | }
|
35 | gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.alphaMode === ALPHA_MODES.UNPACK);
|
36 | if (!this.noSubImage && baseTexture.target === gl.TEXTURE_2D && glTexture.width === width && glTexture.height === height) {
|
37 | gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, baseTexture.format, glTexture.type, source);
|
38 | } else {
|
39 | glTexture.width = width;
|
40 | glTexture.height = height;
|
41 | gl.texImage2D(baseTexture.target, 0, glTexture.internalFormat, baseTexture.format, glTexture.type, source);
|
42 | }
|
43 | return true;
|
44 | }
|
45 | update() {
|
46 | if (this.destroyed) {
|
47 | return;
|
48 | }
|
49 | const source = this.source;
|
50 | const width = source.naturalWidth || source.videoWidth || source.width;
|
51 | const height = source.naturalHeight || source.videoHeight || source.height;
|
52 | this.resize(width, height);
|
53 | super.update();
|
54 | }
|
55 | dispose() {
|
56 | this.source = null;
|
57 | }
|
58 | }
|
59 |
|
60 | export { BaseImageResource };
|
61 |
|