UNPKG

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