UNPKG

2.33 kBJavaScriptView Raw
1import { ALPHA_MODES } from "@pixi/constants";
2import { Resource } from "./Resource.mjs";
3class BufferResource extends Resource {
4 /**
5 * @param source - Source buffer
6 * @param options - Options
7 * @param {number} options.width - Width of the texture
8 * @param {number} options.height - Height of the texture
9 * @param {1|2|4|8} [options.unpackAlignment=4] - The alignment of the pixel rows.
10 */
11 constructor(source, options) {
12 const { width, height } = options || {};
13 if (!width || !height)
14 throw new Error("BufferResource width or height invalid");
15 super(width, height), this.data = source, this.unpackAlignment = options.unpackAlignment ?? 4;
16 }
17 /**
18 * Upload the texture to the GPU.
19 * @param renderer - Upload to the renderer
20 * @param baseTexture - Reference to parent texture
21 * @param glTexture - glTexture
22 * @returns - true is success
23 */
24 upload(renderer, baseTexture, glTexture) {
25 const gl = renderer.gl;
26 gl.pixelStorei(gl.UNPACK_ALIGNMENT, this.unpackAlignment), gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.alphaMode === ALPHA_MODES.UNPACK);
27 const width = baseTexture.realWidth, height = baseTexture.realHeight;
28 return glTexture.width === width && glTexture.height === height ? gl.texSubImage2D(
29 baseTexture.target,
30 0,
31 0,
32 0,
33 width,
34 height,
35 baseTexture.format,
36 glTexture.type,
37 this.data
38 ) : (glTexture.width = width, glTexture.height = height, gl.texImage2D(
39 baseTexture.target,
40 0,
41 glTexture.internalFormat,
42 width,
43 height,
44 0,
45 baseTexture.format,
46 glTexture.type,
47 this.data
48 )), !0;
49 }
50 /** Destroy and don't use after this. */
51 dispose() {
52 this.data = null;
53 }
54 /**
55 * Used to auto-detect the type of resource.
56 * @param {*} source - The source object
57 * @returns {boolean} `true` if buffer source
58 */
59 static test(source) {
60 return source === null || source instanceof Int8Array || source instanceof Uint8Array || source instanceof Uint8ClampedArray || source instanceof Int16Array || source instanceof Uint16Array || source instanceof Int32Array || source instanceof Uint32Array || source instanceof Float32Array;
61 }
62}
63export {
64 BufferResource
65};
66//# sourceMappingURL=BufferResource.mjs.map