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