Source: core/textures/TextureMatrix.js

core/textures/TextureMatrix.js

  1. import { default as Matrix } from '../math/Matrix';
  2. const tempMat = new Matrix();
  3. /**
  4. * Class controls uv transform and frame clamp for texture
  5. * Can be used in Texture "transform" field, or separately, you can use different clamp settings on the same texture.
  6. * If you want to add support for texture region of certain feature or filter, that's what you're looking for.
  7. *
  8. * @see PIXI.Texture
  9. * @see PIXI.mesh.Mesh
  10. * @see PIXI.extras.TilingSprite
  11. * @class
  12. * @memberof PIXI
  13. */
  14. export default class TextureMatrix
  15. {
  16. /**
  17. *
  18. * @param {PIXI.Texture} texture observed texture
  19. * @param {number} [clampMargin] Changes frame clamping, 0.5 by default. Use -0.5 for extra border.
  20. * @constructor
  21. */
  22. constructor(texture, clampMargin)
  23. {
  24. this._texture = texture;
  25. this.mapCoord = new Matrix();
  26. this.uClampFrame = new Float32Array(4);
  27. this.uClampOffset = new Float32Array(2);
  28. this._lastTextureID = -1;
  29. /**
  30. * Changes frame clamping
  31. * Works with TilingSprite and Mesh
  32. * Change to 1.5 if you texture has repeated right and bottom lines, that leads to smoother borders
  33. *
  34. * @default 0
  35. * @member {number}
  36. */
  37. this.clampOffset = 0;
  38. /**
  39. * Changes frame clamping
  40. * Works with TilingSprite and Mesh
  41. * Change to -0.5 to add a pixel to the edge, recommended for transparent trimmed textures in atlas
  42. *
  43. * @default 0.5
  44. * @member {number}
  45. */
  46. this.clampMargin = (typeof clampMargin === 'undefined') ? 0.5 : clampMargin;
  47. }
  48. /**
  49. * texture property
  50. * @member {PIXI.Texture}
  51. */
  52. get texture()
  53. {
  54. return this._texture;
  55. }
  56. set texture(value) // eslint-disable-line require-jsdoc
  57. {
  58. this._texture = value;
  59. this._lastTextureID = -1;
  60. }
  61. /**
  62. * Multiplies uvs array to transform
  63. * @param {Float32Array} uvs mesh uvs
  64. * @param {Float32Array} [out=uvs] output
  65. * @returns {Float32Array} output
  66. */
  67. multiplyUvs(uvs, out)
  68. {
  69. if (out === undefined)
  70. {
  71. out = uvs;
  72. }
  73. const mat = this.mapCoord;
  74. for (let i = 0; i < uvs.length; i += 2)
  75. {
  76. const x = uvs[i];
  77. const y = uvs[i + 1];
  78. out[i] = (x * mat.a) + (y * mat.c) + mat.tx;
  79. out[i + 1] = (x * mat.b) + (y * mat.d) + mat.ty;
  80. }
  81. return out;
  82. }
  83. /**
  84. * updates matrices if texture was changed
  85. * @param {boolean} forceUpdate if true, matrices will be updated any case
  86. * @returns {boolean} whether or not it was updated
  87. */
  88. update(forceUpdate)
  89. {
  90. const tex = this._texture;
  91. if (!tex || !tex.valid)
  92. {
  93. return false;
  94. }
  95. if (!forceUpdate
  96. && this._lastTextureID === tex._updateID)
  97. {
  98. return false;
  99. }
  100. this._lastTextureID = tex._updateID;
  101. const uvs = tex._uvs;
  102. this.mapCoord.set(uvs.x1 - uvs.x0, uvs.y1 - uvs.y0, uvs.x3 - uvs.x0, uvs.y3 - uvs.y0, uvs.x0, uvs.y0);
  103. const orig = tex.orig;
  104. const trim = tex.trim;
  105. if (trim)
  106. {
  107. tempMat.set(orig.width / trim.width, 0, 0, orig.height / trim.height,
  108. -trim.x / trim.width, -trim.y / trim.height);
  109. this.mapCoord.append(tempMat);
  110. }
  111. const texBase = tex.baseTexture;
  112. const frame = this.uClampFrame;
  113. const margin = this.clampMargin / texBase.resolution;
  114. const offset = this.clampOffset;
  115. frame[0] = (tex._frame.x + margin + offset) / texBase.width;
  116. frame[1] = (tex._frame.y + margin + offset) / texBase.height;
  117. frame[2] = (tex._frame.x + tex._frame.width - margin + offset) / texBase.width;
  118. frame[3] = (tex._frame.y + tex._frame.height - margin + offset) / texBase.height;
  119. this.uClampOffset[0] = offset / texBase.realWidth;
  120. this.uClampOffset[1] = offset / texBase.realHeight;
  121. return true;
  122. }
  123. }