1 | "use strict";
|
2 | var constants = require("@pixi/constants"), settings = require("@pixi/settings"), utils = require("@pixi/utils"), autoDetectResource = require("./resources/autoDetectResource.js"), BufferResource = require("./resources/BufferResource.js"), Resource = require("./resources/Resource.js");
|
3 | const defaultBufferOptions = {
|
4 | scaleMode: constants.SCALE_MODES.NEAREST,
|
5 | alphaMode: constants.ALPHA_MODES.NPM
|
6 | }, _BaseTexture = class _BaseTexture2 extends utils.EventEmitter {
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | constructor(resource = null, options = null) {
|
27 | super(), options = Object.assign({}, _BaseTexture2.defaultOptions, options);
|
28 | const {
|
29 | alphaMode,
|
30 | mipmap,
|
31 | anisotropicLevel,
|
32 | scaleMode,
|
33 | width,
|
34 | height,
|
35 | wrapMode,
|
36 | format,
|
37 | type,
|
38 | target,
|
39 | resolution,
|
40 | resourceOptions
|
41 | } = options;
|
42 | resource && !(resource instanceof Resource.Resource) && (resource = autoDetectResource.autoDetectResource(resource, resourceOptions), resource.internal = !0), this.resolution = resolution || settings.settings.RESOLUTION, this.width = Math.round((width || 0) * this.resolution) / this.resolution, this.height = Math.round((height || 0) * this.resolution) / this.resolution, this._mipmap = mipmap, this.anisotropicLevel = anisotropicLevel, this._wrapMode = wrapMode, this._scaleMode = scaleMode, this.format = format, this.type = type, this.target = target, this.alphaMode = alphaMode, this.uid = utils.uid(), this.touched = 0, this.isPowerOfTwo = !1, this._refreshPOT(), this._glTextures = {}, this.dirtyId = 0, this.dirtyStyleId = 0, this.cacheId = null, this.valid = width > 0 && height > 0, this.textureCacheIds = [], this.destroyed = !1, this.resource = null, this._batchEnabled = 0, this._batchLocation = 0, this.parentTextureArray = null, this.setResource(resource);
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 | get realWidth() {
|
49 | return Math.round(this.width * this.resolution);
|
50 | }
|
51 | |
52 |
|
53 |
|
54 |
|
55 | get realHeight() {
|
56 | return Math.round(this.height * this.resolution);
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 | get mipmap() {
|
63 | return this._mipmap;
|
64 | }
|
65 | set mipmap(value) {
|
66 | this._mipmap !== value && (this._mipmap = value, this.dirtyStyleId++);
|
67 | }
|
68 | |
69 |
|
70 |
|
71 |
|
72 | get scaleMode() {
|
73 | return this._scaleMode;
|
74 | }
|
75 | set scaleMode(value) {
|
76 | this._scaleMode !== value && (this._scaleMode = value, this.dirtyStyleId++);
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 | get wrapMode() {
|
83 | return this._wrapMode;
|
84 | }
|
85 | set wrapMode(value) {
|
86 | this._wrapMode !== value && (this._wrapMode = value, this.dirtyStyleId++);
|
87 | }
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | setStyle(scaleMode, mipmap) {
|
95 | let dirty;
|
96 | return scaleMode !== void 0 && scaleMode !== this.scaleMode && (this.scaleMode = scaleMode, dirty = !0), mipmap !== void 0 && mipmap !== this.mipmap && (this.mipmap = mipmap, dirty = !0), dirty && this.dirtyStyleId++, this;
|
97 | }
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | setSize(desiredWidth, desiredHeight, resolution) {
|
106 | return resolution = resolution || this.resolution, this.setRealSize(desiredWidth * resolution, desiredHeight * resolution, resolution);
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | setRealSize(realWidth, realHeight, resolution) {
|
116 | return this.resolution = resolution || this.resolution, this.width = Math.round(realWidth) / this.resolution, this.height = Math.round(realHeight) / this.resolution, this._refreshPOT(), this.update(), this;
|
117 | }
|
118 | |
119 |
|
120 |
|
121 |
|
122 | _refreshPOT() {
|
123 | this.isPowerOfTwo = utils.isPow2(this.realWidth) && utils.isPow2(this.realHeight);
|
124 | }
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 | setResolution(resolution) {
|
131 | const oldResolution = this.resolution;
|
132 | return oldResolution === resolution ? this : (this.resolution = resolution, this.valid && (this.width = Math.round(this.width * oldResolution) / resolution, this.height = Math.round(this.height * oldResolution) / resolution, this.emit("update", this)), this._refreshPOT(), this);
|
133 | }
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 | setResource(resource) {
|
140 | if (this.resource === resource)
|
141 | return this;
|
142 | if (this.resource)
|
143 | throw new Error("Resource can be set only once");
|
144 | return resource.bind(this), this.resource = resource, this;
|
145 | }
|
146 |
|
147 | update() {
|
148 | this.valid ? (this.dirtyId++, this.dirtyStyleId++, this.emit("update", this)) : this.width > 0 && this.height > 0 && (this.valid = !0, this.emit("loaded", this), this.emit("update", this));
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | onError(event) {
|
156 | this.emit("error", this, event);
|
157 | }
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | destroy() {
|
165 | this.resource && (this.resource.unbind(this), this.resource.internal && this.resource.destroy(), this.resource = null), this.cacheId && (delete utils.BaseTextureCache[this.cacheId], delete utils.TextureCache[this.cacheId], this.cacheId = null), this.valid = !1, this.dispose(), _BaseTexture2.removeFromCache(this), this.textureCacheIds = null, this.destroyed = !0, this.emit("destroyed", this), this.removeAllListeners();
|
166 | }
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | dispose() {
|
174 | this.emit("dispose", this);
|
175 | }
|
176 |
|
177 | castToBaseTexture() {
|
178 | return this;
|
179 | }
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | static from(source, options, strict = settings.settings.STRICT_TEXTURE_CACHE) {
|
194 | const isFrame = typeof source == "string";
|
195 | let cacheId = null;
|
196 | if (isFrame)
|
197 | cacheId = source;
|
198 | else {
|
199 | if (!source._pixiId) {
|
200 | const prefix = options?.pixiIdPrefix || "pixiid";
|
201 | source._pixiId = `${prefix}_${utils.uid()}`;
|
202 | }
|
203 | cacheId = source._pixiId;
|
204 | }
|
205 | let baseTexture = utils.BaseTextureCache[cacheId];
|
206 | if (isFrame && strict && !baseTexture)
|
207 | throw new Error(`The cacheId "${cacheId}" does not exist in BaseTextureCache.`);
|
208 | return baseTexture || (baseTexture = new _BaseTexture2(source, options), baseTexture.cacheId = cacheId, _BaseTexture2.addToCache(baseTexture, cacheId)), baseTexture;
|
209 | }
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | static fromBuffer(buffer, width, height, options) {
|
229 | buffer = buffer || new Float32Array(width * height * 4);
|
230 | const resource = new BufferResource.BufferResource(buffer, { width, height, ...options?.resourceOptions });
|
231 | let format, type;
|
232 | return buffer instanceof Float32Array ? (format = constants.FORMATS.RGBA, type = constants.TYPES.FLOAT) : buffer instanceof Int32Array ? (format = constants.FORMATS.RGBA_INTEGER, type = constants.TYPES.INT) : buffer instanceof Uint32Array ? (format = constants.FORMATS.RGBA_INTEGER, type = constants.TYPES.UNSIGNED_INT) : buffer instanceof Int16Array ? (format = constants.FORMATS.RGBA_INTEGER, type = constants.TYPES.SHORT) : buffer instanceof Uint16Array ? (format = constants.FORMATS.RGBA_INTEGER, type = constants.TYPES.UNSIGNED_SHORT) : buffer instanceof Int8Array ? (format = constants.FORMATS.RGBA, type = constants.TYPES.BYTE) : (format = constants.FORMATS.RGBA, type = constants.TYPES.UNSIGNED_BYTE), resource.internal = !0, new _BaseTexture2(resource, Object.assign({}, defaultBufferOptions, { type, format }, options));
|
233 | }
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 | static addToCache(baseTexture, id) {
|
240 | id && (baseTexture.textureCacheIds.includes(id) || baseTexture.textureCacheIds.push(id), utils.BaseTextureCache[id] && utils.BaseTextureCache[id] !== baseTexture && console.warn(`BaseTexture added to the cache with an id [${id}] that already had an entry`), utils.BaseTextureCache[id] = baseTexture);
|
241 | }
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | static removeFromCache(baseTexture) {
|
248 | if (typeof baseTexture == "string") {
|
249 | const baseTextureFromCache = utils.BaseTextureCache[baseTexture];
|
250 | if (baseTextureFromCache) {
|
251 | const index = baseTextureFromCache.textureCacheIds.indexOf(baseTexture);
|
252 | return index > -1 && baseTextureFromCache.textureCacheIds.splice(index, 1), delete utils.BaseTextureCache[baseTexture], baseTextureFromCache;
|
253 | }
|
254 | } else if (baseTexture?.textureCacheIds) {
|
255 | for (let i = 0; i < baseTexture.textureCacheIds.length; ++i)
|
256 | delete utils.BaseTextureCache[baseTexture.textureCacheIds[i]];
|
257 | return baseTexture.textureCacheIds.length = 0, baseTexture;
|
258 | }
|
259 | return null;
|
260 | }
|
261 | };
|
262 | _BaseTexture.defaultOptions = {
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 | mipmap: constants.MIPMAP_MODES.POW2,
|
269 |
|
270 | anisotropicLevel: 0,
|
271 | |
272 |
|
273 |
|
274 |
|
275 |
|
276 | scaleMode: constants.SCALE_MODES.LINEAR,
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 | wrapMode: constants.WRAP_MODES.CLAMP,
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 | alphaMode: constants.ALPHA_MODES.UNPACK,
|
289 | |
290 |
|
291 |
|
292 |
|
293 |
|
294 | target: constants.TARGETS.TEXTURE_2D,
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 | format: constants.FORMATS.RGBA,
|
301 | |
302 |
|
303 |
|
304 |
|
305 |
|
306 | type: constants.TYPES.UNSIGNED_BYTE
|
307 | },
|
308 | _BaseTexture._globalBatch = 0;
|
309 | let BaseTexture = _BaseTexture;
|
310 | exports.BaseTexture = BaseTexture;
|
311 |
|