UNPKG

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