1 | {"version":3,"file":"TextureMatrix.mjs","sources":["../../src/textures/TextureMatrix.ts"],"sourcesContent":["import { Matrix } from '@pixi/math';\n\nimport type { Texture } from './Texture';\n\nconst tempMat = new Matrix();\n\n/**\n * Class controls uv mapping from Texture normal space to BaseTexture normal space.\n *\n * Takes `trim` and `rotate` into account. May contain clamp settings for Meshes and TilingSprite.\n *\n * Can be used in Texture `uvMatrix` field, or separately, you can use different clamp settings on the same texture.\n * If you want to add support for texture region of certain feature or filter, that's what you're looking for.\n *\n * Takes track of Texture changes through `_lastTextureID` private field.\n * Use `update()` method call to track it from outside.\n * @see PIXI.Texture\n * @see PIXI.Mesh\n * @see PIXI.TilingSprite\n * @memberof PIXI\n */\nexport class TextureMatrix\n{\n /**\n * Matrix operation that converts texture region coords to texture coords\n * @readonly\n */\n public mapCoord: Matrix;\n\n /**\n * Changes frame clamping\n * Works with TilingSprite and Mesh\n * Change to 1.5 if you texture has repeated right and bottom lines, that leads to smoother borders\n * @default 0\n */\n public clampOffset: number;\n\n /**\n * Changes frame clamping\n * Works with TilingSprite and Mesh\n * Change to -0.5 to add a pixel to the edge, recommended for transparent trimmed textures in atlas\n * @default 0.5\n */\n public clampMargin: number;\n\n /**\n * Clamp region for normalized coords, left-top pixel center in xy , bottom-right in zw.\n * Calculated based on clampOffset.\n */\n readonly uClampFrame: Float32Array;\n\n /** Normalized clamp offset. Calculated based on clampOffset. */\n readonly uClampOffset: Float32Array;\n\n /**\n * Tracks Texture frame changes.\n * @protected\n */\n _textureID: number;\n\n /**\n * Tracks Texture frame changes.\n * @protected\n */\n _updateID: number;\n _texture: Texture;\n\n /**\n * If texture size is the same as baseTexture.\n * @default false\n * @readonly\n */\n isSimple: boolean;\n\n /**\n * @param texture - observed texture\n * @param clampMargin - Changes frame clamping, 0.5 by default. Use -0.5 for extra border.\n */\n constructor(texture: Texture, clampMargin?: number)\n {\n this._texture = texture;\n\n this.mapCoord = new Matrix();\n this.uClampFrame = new Float32Array(4);\n this.uClampOffset = new Float32Array(2);\n this._textureID = -1;\n this._updateID = 0;\n\n this.clampOffset = 0;\n this.clampMargin = (typeof clampMargin === 'undefined') ? 0.5 : clampMargin;\n this.isSimple = false;\n }\n\n /** Texture property. */\n get texture(): Texture\n {\n return this._texture;\n }\n\n set texture(value: Texture)\n {\n this._texture = value;\n this._textureID = -1;\n }\n\n /**\n * Multiplies uvs array to transform\n * @param uvs - mesh uvs\n * @param [out=uvs] - output\n * @returns - output\n */\n multiplyUvs(uvs: Float32Array, out?: Float32Array): Float32Array\n {\n if (out === undefined)\n {\n out = uvs;\n }\n\n const mat = this.mapCoord;\n\n for (let i = 0; i < uvs.length; i += 2)\n {\n const x = uvs[i];\n const y = uvs[i + 1];\n\n out[i] = (x * mat.a) + (y * mat.c) + mat.tx;\n out[i + 1] = (x * mat.b) + (y * mat.d) + mat.ty;\n }\n\n return out;\n }\n\n /**\n * Updates matrices if texture was changed.\n * @param [forceUpdate=false] - if true, matrices will be updated any case\n * @returns - Whether or not it was updated\n */\n update(forceUpdate?: boolean): boolean\n {\n const tex = this._texture;\n\n if (!tex || !tex.valid)\n {\n return false;\n }\n\n if (!forceUpdate\n && this._textureID === tex._updateID)\n {\n return false;\n }\n\n this._textureID = tex._updateID;\n this._updateID++;\n\n const uvs = tex._uvs;\n\n this.mapCoord.set(uvs.x1 - uvs.x0, uvs.y1 - uvs.y0, uvs.x3 - uvs.x0, uvs.y3 - uvs.y0, uvs.x0, uvs.y0);\n\n const orig = tex.orig;\n const trim = tex.trim;\n\n if (trim)\n {\n tempMat.set(orig.width / trim.width, 0, 0, orig.height / trim.height,\n -trim.x / trim.width, -trim.y / trim.height);\n this.mapCoord.append(tempMat);\n }\n\n const texBase = tex.baseTexture;\n const frame = this.uClampFrame;\n const margin = this.clampMargin / texBase.resolution;\n const offset = this.clampOffset;\n\n frame[0] = (tex._frame.x + margin + offset) / texBase.width;\n frame[1] = (tex._frame.y + margin + offset) / texBase.height;\n frame[2] = (tex._frame.x + tex._frame.width - margin + offset) / texBase.width;\n frame[3] = (tex._frame.y + tex._frame.height - margin + offset) / texBase.height;\n this.uClampOffset[0] = offset / texBase.realWidth;\n this.uClampOffset[1] = offset / texBase.realHeight;\n\n this.isSimple = tex._frame.width === texBase.width\n && tex._frame.height === texBase.height\n && tex.rotate === 0;\n\n return true;\n }\n}\n"],"names":[],"mappings":";AAIA,MAAM,UAAU,IAAI;AAiBb,MAAM,cACb;AAAA;AAAA;AAAA;AAAA;AAAA,EAwDI,YAAY,SAAkB,aAC9B;AACI,SAAK,WAAW,SAEhB,KAAK,WAAW,IAAI,OACpB,GAAA,KAAK,cAAc,IAAI,aAAa,CAAC,GACrC,KAAK,eAAe,IAAI,aAAa,CAAC,GACtC,KAAK,aAAa,IAClB,KAAK,YAAY,GAEjB,KAAK,cAAc,GACnB,KAAK,cAAe,OAAO,cAAgB,MAAe,MAAM,aAChE,KAAK,WAAW;AAAA,EACpB;AAAA;AAAA,EAGA,IAAI,UACJ;AACI,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,QAAQ,OACZ;AACS,SAAA,WAAW,OAChB,KAAK,aAAa;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAY,KAAmB,KAC/B;AACQ,YAAQ,WAER,MAAM;AAGV,UAAM,MAAM,KAAK;AAEjB,aAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK,GACrC;AACI,YAAM,IAAI,IAAI,CAAC,GACT,IAAI,IAAI,IAAI,CAAC;AAEf,UAAA,CAAC,IAAK,IAAI,IAAI,IAAM,IAAI,IAAI,IAAK,IAAI,IACzC,IAAI,IAAI,CAAC,IAAK,IAAI,IAAI,IAAM,IAAI,IAAI,IAAK,IAAI;AAAA,IACjD;AAEO,WAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,aACP;AACI,UAAM,MAAM,KAAK;AAEb,QAAA,CAAC,OAAO,CAAC,IAAI,SAKb,CAAC,e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