UNPKG

2.43 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var math = require('@pixi/math');
6
7const tempMat = new math.Matrix();
8class TextureMatrix {
9 constructor(texture, clampMargin) {
10 this._texture = texture;
11 this.mapCoord = new math.Matrix();
12 this.uClampFrame = new Float32Array(4);
13 this.uClampOffset = new Float32Array(2);
14 this._textureID = -1;
15 this._updateID = 0;
16 this.clampOffset = 0;
17 this.clampMargin = typeof clampMargin === "undefined" ? 0.5 : clampMargin;
18 this.isSimple = false;
19 }
20 get texture() {
21 return this._texture;
22 }
23 set texture(value) {
24 this._texture = value;
25 this._textureID = -1;
26 }
27 multiplyUvs(uvs, out) {
28 if (out === void 0) {
29 out = uvs;
30 }
31 const mat = this.mapCoord;
32 for (let i = 0; i < uvs.length; i += 2) {
33 const x = uvs[i];
34 const y = uvs[i + 1];
35 out[i] = x * mat.a + y * mat.c + mat.tx;
36 out[i + 1] = x * mat.b + y * mat.d + mat.ty;
37 }
38 return out;
39 }
40 update(forceUpdate) {
41 const tex = this._texture;
42 if (!tex || !tex.valid) {
43 return false;
44 }
45 if (!forceUpdate && this._textureID === tex._updateID) {
46 return false;
47 }
48 this._textureID = tex._updateID;
49 this._updateID++;
50 const uvs = tex._uvs;
51 this.mapCoord.set(uvs.x1 - uvs.x0, uvs.y1 - uvs.y0, uvs.x3 - uvs.x0, uvs.y3 - uvs.y0, uvs.x0, uvs.y0);
52 const orig = tex.orig;
53 const trim = tex.trim;
54 if (trim) {
55 tempMat.set(orig.width / trim.width, 0, 0, orig.height / trim.height, -trim.x / trim.width, -trim.y / trim.height);
56 this.mapCoord.append(tempMat);
57 }
58 const texBase = tex.baseTexture;
59 const frame = this.uClampFrame;
60 const margin = this.clampMargin / texBase.resolution;
61 const offset = this.clampOffset;
62 frame[0] = (tex._frame.x + margin + offset) / texBase.width;
63 frame[1] = (tex._frame.y + margin + offset) / texBase.height;
64 frame[2] = (tex._frame.x + tex._frame.width - margin + offset) / texBase.width;
65 frame[3] = (tex._frame.y + tex._frame.height - margin + offset) / texBase.height;
66 this.uClampOffset[0] = offset / texBase.realWidth;
67 this.uClampOffset[1] = offset / texBase.realHeight;
68 this.isSimple = tex._frame.width === texBase.width && tex._frame.height === texBase.height && tex.rotate === 0;
69 return true;
70 }
71}
72
73exports.TextureMatrix = TextureMatrix;
74//# sourceMappingURL=TextureMatrix.js.map