UNPKG

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