UNPKG

2.64 kBJavaScriptView Raw
1import { Matrix } from "@pixi/math";
2const tempMat = new Matrix();
3class TextureMatrix {
4 /**
5 * @param texture - observed texture
6 * @param clampMargin - Changes frame clamping, 0.5 by default. Use -0.5 for extra border.
7 */
8 constructor(texture, clampMargin) {
9 this._texture = texture, this.mapCoord = new Matrix(), this.uClampFrame = new Float32Array(4), this.uClampOffset = new Float32Array(2), this._textureID = -1, this._updateID = 0, this.clampOffset = 0, this.clampMargin = typeof clampMargin > "u" ? 0.5 : clampMargin, this.isSimple = !1;
10 }
11 /** Texture property. */
12 get texture() {
13 return this._texture;
14 }
15 set texture(value) {
16 this._texture = value, this._textureID = -1;
17 }
18 /**
19 * Multiplies uvs array to transform
20 * @param uvs - mesh uvs
21 * @param [out=uvs] - output
22 * @returns - output
23 */
24 multiplyUvs(uvs, out) {
25 out === void 0 && (out = uvs);
26 const mat = this.mapCoord;
27 for (let i = 0; i < uvs.length; i += 2) {
28 const x = uvs[i], y = uvs[i + 1];
29 out[i] = x * mat.a + y * mat.c + mat.tx, out[i + 1] = x * mat.b + y * mat.d + mat.ty;
30 }
31 return out;
32 }
33 /**
34 * Updates matrices if texture was changed.
35 * @param [forceUpdate=false] - if true, matrices will be updated any case
36 * @returns - Whether or not it was updated
37 */
38 update(forceUpdate) {
39 const tex = this._texture;
40 if (!tex || !tex.valid || !forceUpdate && this._textureID === tex._updateID)
41 return !1;
42 this._textureID = tex._updateID, this._updateID++;
43 const uvs = tex._uvs;
44 this.mapCoord.set(uvs.x1 - uvs.x0, uvs.y1 - uvs.y0, uvs.x3 - uvs.x0, uvs.y3 - uvs.y0, uvs.x0, uvs.y0);
45 const orig = tex.orig, trim = tex.trim;
46 trim && (tempMat.set(
47 orig.width / trim.width,
48 0,
49 0,
50 orig.height / trim.height,
51 -trim.x / trim.width,
52 -trim.y / trim.height
53 ), this.mapCoord.append(tempMat));
54 const texBase = tex.baseTexture, frame = this.uClampFrame, margin = this.clampMargin / texBase.resolution, offset = this.clampOffset;
55 return frame[0] = (tex._frame.x + margin + offset) / texBase.width, frame[1] = (tex._frame.y + margin + offset) / texBase.height, frame[2] = (tex._frame.x + tex._frame.width - margin + offset) / texBase.width, frame[3] = (tex._frame.y + tex._frame.height - margin + offset) / texBase.height, this.uClampOffset[0] = offset / texBase.realWidth, this.uClampOffset[1] = offset / texBase.realHeight, this.isSimple = tex._frame.width === texBase.width && tex._frame.height === texBase.height && tex.rotate === 0, !0;
56 }
57}
58export {
59 TextureMatrix
60};
61//# sourceMappingURL=TextureMatrix.mjs.map