- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
import { Matrix } from '@pixi/math';
import type { Texture } from './Texture';
const tempMat = new Matrix();
/**
* Class controls uv mapping from Texture normal space to BaseTexture normal space.
*
* Takes `trim` and `rotate` into account. May contain clamp settings for Meshes and TilingSprite.
*
* Can be used in Texture `uvMatrix` field, or separately, you can use different clamp settings on the same texture.
* If you want to add support for texture region of certain feature or filter, that's what you're looking for.
*
* Takes track of Texture changes through `_lastTextureID` private field.
* Use `update()` method call to track it from outside.
* @see PIXI.Texture
* @see PIXI.Mesh
* @see PIXI.TilingSprite
* @memberof PIXI
*/
export class TextureMatrix
{
/**
* Matrix operation that converts texture region coords to texture coords
* @readonly
*/
public mapCoord: Matrix;
/**
* Changes frame clamping
* Works with TilingSprite and Mesh
* Change to 1.5 if you texture has repeated right and bottom lines, that leads to smoother borders
* @default 0
*/
public clampOffset: number;
/**
* Changes frame clamping
* Works with TilingSprite and Mesh
* Change to -0.5 to add a pixel to the edge, recommended for transparent trimmed textures in atlas
* @default 0.5
*/
public clampMargin: number;
/**
* Clamp region for normalized coords, left-top pixel center in xy , bottom-right in zw.
* Calculated based on clampOffset.
*/
readonly uClampFrame: Float32Array;
/** Normalized clamp offset. Calculated based on clampOffset. */
readonly uClampOffset: Float32Array;
/**
* Tracks Texture frame changes.
* @protected
*/
_textureID: number;
/**
* Tracks Texture frame changes.
* @protected
*/
_updateID: number;
_texture: Texture;
/**
* If texture size is the same as baseTexture.
* @default false
* @readonly
*/
isSimple: boolean;
/**
* @param texture - observed texture
* @param clampMargin - Changes frame clamping, 0.5 by default. Use -0.5 for extra border.
*/
constructor(texture: Texture, clampMargin?: number)
{
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 === 'undefined') ? 0.5 : clampMargin;
this.isSimple = false;
}
/** Texture property. */
get texture(): Texture
{
return this._texture;
}
set texture(value: Texture)
{
this._texture = value;
this._textureID = -1;
}
/**
* Multiplies uvs array to transform
* @param uvs - mesh uvs
* @param [out=uvs] - output
* @returns - output
*/
multiplyUvs(uvs: Float32Array, out?: Float32Array): Float32Array
{
if (out === undefined)
{
out = uvs;
}
const mat = this.mapCoord;
for (let i = 0; i < uvs.length; i += 2)
{
const x = uvs[i];
const y = uvs[i + 1];
out[i] = (x * mat.a) + (y * mat.c) + mat.tx;
out[i + 1] = (x * mat.b) + (y * mat.d) + mat.ty;
}
return out;
}
/**
* Updates matrices if texture was changed.
* @param [forceUpdate=false] - if true, matrices will be updated any case
* @returns - Whether or not it was updated
*/
update(forceUpdate?: boolean): boolean
{
const tex = this._texture;
if (!tex || !tex.valid)
{
return false;
}
if (!forceUpdate
&& this._textureID === tex._updateID)
{
return false;
}
this._textureID = tex._updateID;
this._updateID++;
const uvs = tex._uvs;
this.mapCoord.set(uvs.x1 - uvs.x0, uvs.y1 - uvs.y0, uvs.x3 - uvs.x0, uvs.y3 - uvs.y0, uvs.x0, uvs.y0);
const orig = tex.orig;
const trim = tex.trim;
if (trim)
{
tempMat.set(orig.width / trim.width, 0, 0, orig.height / trim.height,
-trim.x / trim.width, -trim.y / trim.height);
this.mapCoord.append(tempMat);
}
const texBase = tex.baseTexture;
const frame = this.uClampFrame;
const margin = this.clampMargin / texBase.resolution;
const offset = this.clampOffset;
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;
return true;
}
}