sprite-tiling.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*!
  2. * @pixi/sprite-tiling - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/sprite-tiling is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. this.PIXI = this.PIXI || {};
  9. var _pixi_sprite_tiling = (function (exports, core, math, sprite, constants, utils) {
  10. 'use strict';
  11. /*! *****************************************************************************
  12. Copyright (c) Microsoft Corporation.
  13. Permission to use, copy, modify, and/or distribute this software for any
  14. purpose with or without fee is hereby granted.
  15. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  16. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  17. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  18. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  19. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22. ***************************************************************************** */
  23. /* global Reflect, Promise */
  24. var extendStatics = function(d, b) {
  25. extendStatics = Object.setPrototypeOf ||
  26. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  27. function (d, b) { for (var p in b) { if (b.hasOwnProperty(p)) { d[p] = b[p]; } } };
  28. return extendStatics(d, b);
  29. };
  30. function __extends(d, b) {
  31. extendStatics(d, b);
  32. function __() { this.constructor = d; }
  33. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  34. }
  35. var __assign = function() {
  36. __assign = Object.assign || function __assign(t) {
  37. var arguments$1 = arguments;
  38. for (var s, i = 1, n = arguments.length; i < n; i++) {
  39. s = arguments$1[i];
  40. for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) { t[p] = s[p]; } }
  41. }
  42. return t;
  43. };
  44. return __assign.apply(this, arguments);
  45. };
  46. function __rest(s, e) {
  47. var t = {};
  48. for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  49. { t[p] = s[p]; } }
  50. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  51. { for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  52. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  53. { t[p[i]] = s[p[i]]; }
  54. } }
  55. return t;
  56. }
  57. function __decorate(decorators, target, key, desc) {
  58. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  59. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") { r = Reflect.decorate(decorators, target, key, desc); }
  60. else { for (var i = decorators.length - 1; i >= 0; i--) { if (d = decorators[i]) { r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; } } }
  61. return c > 3 && r && Object.defineProperty(target, key, r), r;
  62. }
  63. function __param(paramIndex, decorator) {
  64. return function (target, key) { decorator(target, key, paramIndex); }
  65. }
  66. function __metadata(metadataKey, metadataValue) {
  67. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") { return Reflect.metadata(metadataKey, metadataValue); }
  68. }
  69. function __awaiter(thisArg, _arguments, P, generator) {
  70. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  71. return new (P || (P = Promise))(function (resolve, reject) {
  72. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  73. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  74. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  75. step((generator = generator.apply(thisArg, _arguments || [])).next());
  76. });
  77. }
  78. function __generator(thisArg, body) {
  79. var _ = { label: 0, sent: function() { if (t[0] & 1) { throw t[1]; } return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  80. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  81. function verb(n) { return function (v) { return step([n, v]); }; }
  82. function step(op) {
  83. if (f) { throw new TypeError("Generator is already executing."); }
  84. while (_) { try {
  85. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) { return t; }
  86. if (y = 0, t) { op = [op[0] & 2, t.value]; }
  87. switch (op[0]) {
  88. case 0: case 1: t = op; break;
  89. case 4: _.label++; return { value: op[1], done: false };
  90. case 5: _.label++; y = op[1]; op = [0]; continue;
  91. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  92. default:
  93. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  94. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  95. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  96. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  97. if (t[2]) { _.ops.pop(); }
  98. _.trys.pop(); continue;
  99. }
  100. op = body.call(thisArg, _);
  101. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } }
  102. if (op[0] & 5) { throw op[1]; } return { value: op[0] ? op[1] : void 0, done: true };
  103. }
  104. }
  105. function __createBinding(o, m, k, k2) {
  106. if (k2 === undefined) { k2 = k; }
  107. o[k2] = m[k];
  108. }
  109. function __exportStar(m, exports) {
  110. for (var p in m) { if (p !== "default" && !exports.hasOwnProperty(p)) { exports[p] = m[p]; } }
  111. }
  112. function __values(o) {
  113. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  114. if (m) { return m.call(o); }
  115. if (o && typeof o.length === "number") { return {
  116. next: function () {
  117. if (o && i >= o.length) { o = void 0; }
  118. return { value: o && o[i++], done: !o };
  119. }
  120. }; }
  121. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  122. }
  123. function __read(o, n) {
  124. var m = typeof Symbol === "function" && o[Symbol.iterator];
  125. if (!m) { return o; }
  126. var i = m.call(o), r, ar = [], e;
  127. try {
  128. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) { ar.push(r.value); }
  129. }
  130. catch (error) { e = { error: error }; }
  131. finally {
  132. try {
  133. if (r && !r.done && (m = i["return"])) { m.call(i); }
  134. }
  135. finally { if (e) { throw e.error; } }
  136. }
  137. return ar;
  138. }
  139. function __spread() {
  140. var arguments$1 = arguments;
  141. for (var ar = [], i = 0; i < arguments.length; i++)
  142. { ar = ar.concat(__read(arguments$1[i])); }
  143. return ar;
  144. }
  145. function __spreadArrays() {
  146. var arguments$1 = arguments;
  147. for (var s = 0, i = 0, il = arguments.length; i < il; i++) { s += arguments$1[i].length; }
  148. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  149. { for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  150. { r[k] = a[j]; } }
  151. return r;
  152. };
  153. function __await(v) {
  154. return this instanceof __await ? (this.v = v, this) : new __await(v);
  155. }
  156. function __asyncGenerator(thisArg, _arguments, generator) {
  157. if (!Symbol.asyncIterator) { throw new TypeError("Symbol.asyncIterator is not defined."); }
  158. var g = generator.apply(thisArg, _arguments || []), i, q = [];
  159. return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
  160. function verb(n) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } }
  161. function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
  162. function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
  163. function fulfill(value) { resume("next", value); }
  164. function reject(value) { resume("throw", value); }
  165. function settle(f, v) { if (f(v), q.shift(), q.length) { resume(q[0][0], q[0][1]); } }
  166. }
  167. function __asyncDelegator(o) {
  168. var i, p;
  169. return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
  170. function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
  171. }
  172. function __asyncValues(o) {
  173. if (!Symbol.asyncIterator) { throw new TypeError("Symbol.asyncIterator is not defined."); }
  174. var m = o[Symbol.asyncIterator], i;
  175. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  176. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  177. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  178. }
  179. function __makeTemplateObject(cooked, raw) {
  180. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  181. return cooked;
  182. };
  183. function __importStar(mod) {
  184. if (mod && mod.__esModule) { return mod; }
  185. var result = {};
  186. if (mod != null) { for (var k in mod) { if (Object.hasOwnProperty.call(mod, k)) { result[k] = mod[k]; } } }
  187. result.default = mod;
  188. return result;
  189. }
  190. function __importDefault(mod) {
  191. return (mod && mod.__esModule) ? mod : { default: mod };
  192. }
  193. function __classPrivateFieldGet(receiver, privateMap) {
  194. if (!privateMap.has(receiver)) {
  195. throw new TypeError("attempted to get private field on non-instance");
  196. }
  197. return privateMap.get(receiver);
  198. }
  199. function __classPrivateFieldSet(receiver, privateMap, value) {
  200. if (!privateMap.has(receiver)) {
  201. throw new TypeError("attempted to set private field on non-instance");
  202. }
  203. privateMap.set(receiver, value);
  204. return value;
  205. }
  206. var tempPoint = new math.Point();
  207. /**
  208. * A tiling sprite is a fast way of rendering a tiling image.
  209. * @memberof PIXI
  210. */
  211. var TilingSprite = /** @class */ (function (_super) {
  212. __extends(TilingSprite, _super);
  213. /**
  214. * @param texture - The texture of the tiling sprite.
  215. * @param width - The width of the tiling sprite.
  216. * @param height - The height of the tiling sprite.
  217. */
  218. function TilingSprite(texture, width, height) {
  219. if (width === void 0) { width = 100; }
  220. if (height === void 0) { height = 100; }
  221. var _this = _super.call(this, texture) || this;
  222. _this.tileTransform = new math.Transform();
  223. // The width of the tiling sprite
  224. _this._width = width;
  225. // The height of the tiling sprite
  226. _this._height = height;
  227. _this.uvMatrix = _this.texture.uvMatrix || new core.TextureMatrix(texture);
  228. /**
  229. * Plugin that is responsible for rendering this element.
  230. * Allows to customize the rendering process without overriding '_render' method.
  231. * @default 'tilingSprite'
  232. */
  233. _this.pluginName = 'tilingSprite';
  234. _this.uvRespectAnchor = false;
  235. return _this;
  236. }
  237. Object.defineProperty(TilingSprite.prototype, "clampMargin", {
  238. /**
  239. * Changes frame clamping in corresponding textureTransform, shortcut
  240. * Change to -0.5 to add a pixel to the edge, recommended for transparent trimmed textures in atlas
  241. * @default 0.5
  242. * @member {number}
  243. */
  244. get: function () {
  245. return this.uvMatrix.clampMargin;
  246. },
  247. set: function (value) {
  248. this.uvMatrix.clampMargin = value;
  249. this.uvMatrix.update(true);
  250. },
  251. enumerable: false,
  252. configurable: true
  253. });
  254. Object.defineProperty(TilingSprite.prototype, "tileScale", {
  255. /** The scaling of the image that is being tiled. */
  256. get: function () {
  257. return this.tileTransform.scale;
  258. },
  259. set: function (value) {
  260. this.tileTransform.scale.copyFrom(value);
  261. },
  262. enumerable: false,
  263. configurable: true
  264. });
  265. Object.defineProperty(TilingSprite.prototype, "tilePosition", {
  266. /** The offset of the image that is being tiled. */
  267. get: function () {
  268. return this.tileTransform.position;
  269. },
  270. set: function (value) {
  271. this.tileTransform.position.copyFrom(value);
  272. },
  273. enumerable: false,
  274. configurable: true
  275. });
  276. /**
  277. * @protected
  278. */
  279. TilingSprite.prototype._onTextureUpdate = function () {
  280. if (this.uvMatrix) {
  281. this.uvMatrix.texture = this._texture;
  282. }
  283. this._cachedTint = 0xFFFFFF;
  284. };
  285. /**
  286. * Renders the object using the WebGL renderer
  287. * @param renderer - The renderer
  288. */
  289. TilingSprite.prototype._render = function (renderer) {
  290. // tweak our texture temporarily..
  291. var texture = this._texture;
  292. if (!texture || !texture.valid) {
  293. return;
  294. }
  295. this.tileTransform.updateLocalTransform();
  296. this.uvMatrix.update();
  297. renderer.batch.setObjectRenderer(renderer.plugins[this.pluginName]);
  298. renderer.plugins[this.pluginName].render(this);
  299. };
  300. /** Updates the bounds of the tiling sprite. */
  301. TilingSprite.prototype._calculateBounds = function () {
  302. var minX = this._width * -this._anchor._x;
  303. var minY = this._height * -this._anchor._y;
  304. var maxX = this._width * (1 - this._anchor._x);
  305. var maxY = this._height * (1 - this._anchor._y);
  306. this._bounds.addFrame(this.transform, minX, minY, maxX, maxY);
  307. };
  308. /**
  309. * Gets the local bounds of the sprite object.
  310. * @param rect - Optional output rectangle.
  311. * @returns The bounds.
  312. */
  313. TilingSprite.prototype.getLocalBounds = function (rect) {
  314. // we can do a fast local bounds if the sprite has no children!
  315. if (this.children.length === 0) {
  316. this._bounds.minX = this._width * -this._anchor._x;
  317. this._bounds.minY = this._height * -this._anchor._y;
  318. this._bounds.maxX = this._width * (1 - this._anchor._x);
  319. this._bounds.maxY = this._height * (1 - this._anchor._y);
  320. if (!rect) {
  321. if (!this._localBoundsRect) {
  322. this._localBoundsRect = new math.Rectangle();
  323. }
  324. rect = this._localBoundsRect;
  325. }
  326. return this._bounds.getRectangle(rect);
  327. }
  328. return _super.prototype.getLocalBounds.call(this, rect);
  329. };
  330. /**
  331. * Checks if a point is inside this tiling sprite.
  332. * @param point - The point to check.
  333. * @returns Whether or not the sprite contains the point.
  334. */
  335. TilingSprite.prototype.containsPoint = function (point) {
  336. this.worldTransform.applyInverse(point, tempPoint);
  337. var width = this._width;
  338. var height = this._height;
  339. var x1 = -width * this.anchor._x;
  340. if (tempPoint.x >= x1 && tempPoint.x < x1 + width) {
  341. var y1 = -height * this.anchor._y;
  342. if (tempPoint.y >= y1 && tempPoint.y < y1 + height) {
  343. return true;
  344. }
  345. }
  346. return false;
  347. };
  348. /**
  349. * Destroys this sprite and optionally its texture and children
  350. * @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
  351. * have been set to that value
  352. * @param {boolean} [options.children=false] - if set to true, all the children will have their destroy
  353. * method called as well. 'options' will be passed on to those calls.
  354. * @param {boolean} [options.texture=false] - Should it destroy the current texture of the sprite as well
  355. * @param {boolean} [options.baseTexture=false] - Should it destroy the base texture of the sprite as well
  356. */
  357. TilingSprite.prototype.destroy = function (options) {
  358. _super.prototype.destroy.call(this, options);
  359. this.tileTransform = null;
  360. this.uvMatrix = null;
  361. };
  362. /**
  363. * Helper function that creates a new tiling sprite based on the source you provide.
  364. * The source can be - frame id, image url, video url, canvas element, video element, base texture
  365. * @static
  366. * @param {string|PIXI.Texture|HTMLCanvasElement|HTMLVideoElement} source - Source to create texture from
  367. * @param {object} options - See {@link PIXI.BaseTexture}'s constructor for options.
  368. * @param {number} options.width - required width of the tiling sprite
  369. * @param {number} options.height - required height of the tiling sprite
  370. * @returns {PIXI.TilingSprite} The newly created texture
  371. */
  372. TilingSprite.from = function (source, options) {
  373. var texture = (source instanceof core.Texture)
  374. ? source
  375. : core.Texture.from(source, options);
  376. return new TilingSprite(texture, options.width, options.height);
  377. };
  378. Object.defineProperty(TilingSprite.prototype, "width", {
  379. /** The width of the sprite, setting this will actually modify the scale to achieve the value set. */
  380. get: function () {
  381. return this._width;
  382. },
  383. set: function (value) {
  384. this._width = value;
  385. },
  386. enumerable: false,
  387. configurable: true
  388. });
  389. Object.defineProperty(TilingSprite.prototype, "height", {
  390. /** The height of the TilingSprite, setting this will actually modify the scale to achieve the value set. */
  391. get: function () {
  392. return this._height;
  393. },
  394. set: function (value) {
  395. this._height = value;
  396. },
  397. enumerable: false,
  398. configurable: true
  399. });
  400. return TilingSprite;
  401. }(sprite.Sprite));
  402. var fragmentSimpleSrc = "#version 100\n#define SHADER_NAME Tiling-Sprite-Simple-100\n\nprecision lowp float;\n\nvarying vec2 vTextureCoord;\n\nuniform sampler2D uSampler;\nuniform vec4 uColor;\n\nvoid main(void)\n{\n vec4 texSample = texture2D(uSampler, vTextureCoord);\n gl_FragColor = texSample * uColor;\n}\n";
  403. var gl1VertexSrc = "#version 100\n#define SHADER_NAME Tiling-Sprite-100\n\nprecision lowp float;\n\nattribute vec2 aVertexPosition;\nattribute vec2 aTextureCoord;\n\nuniform mat3 projectionMatrix;\nuniform mat3 translationMatrix;\nuniform mat3 uTransform;\n\nvarying vec2 vTextureCoord;\n\nvoid main(void)\n{\n gl_Position = vec4((projectionMatrix * translationMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);\n\n vTextureCoord = (uTransform * vec3(aTextureCoord, 1.0)).xy;\n}\n";
  404. var gl1FragmentSrc = "#version 100\n#ifdef GL_EXT_shader_texture_lod\n #extension GL_EXT_shader_texture_lod : enable\n#endif\n#define SHADER_NAME Tiling-Sprite-100\n\nprecision lowp float;\n\nvarying vec2 vTextureCoord;\n\nuniform sampler2D uSampler;\nuniform vec4 uColor;\nuniform mat3 uMapCoord;\nuniform vec4 uClampFrame;\nuniform vec2 uClampOffset;\n\nvoid main(void)\n{\n vec2 coord = vTextureCoord + ceil(uClampOffset - vTextureCoord);\n coord = (uMapCoord * vec3(coord, 1.0)).xy;\n vec2 unclamped = coord;\n coord = clamp(coord, uClampFrame.xy, uClampFrame.zw);\n\n #ifdef GL_EXT_shader_texture_lod\n vec4 texSample = unclamped == coord\n ? texture2D(uSampler, coord) \n : texture2DLodEXT(uSampler, coord, 0);\n #else\n vec4 texSample = texture2D(uSampler, coord);\n #endif\n\n gl_FragColor = texSample * uColor;\n}\n";
  405. var gl2VertexSrc = "#version 300 es\n#define SHADER_NAME Tiling-Sprite-300\n\nprecision lowp float;\n\nin vec2 aVertexPosition;\nin vec2 aTextureCoord;\n\nuniform mat3 projectionMatrix;\nuniform mat3 translationMatrix;\nuniform mat3 uTransform;\n\nout vec2 vTextureCoord;\n\nvoid main(void)\n{\n gl_Position = vec4((projectionMatrix * translationMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);\n\n vTextureCoord = (uTransform * vec3(aTextureCoord, 1.0)).xy;\n}\n";
  406. var gl2FragmentSrc = "#version 300 es\n#define SHADER_NAME Tiling-Sprite-100\n\nprecision lowp float;\n\nin vec2 vTextureCoord;\n\nout vec4 fragmentColor;\n\nuniform sampler2D uSampler;\nuniform vec4 uColor;\nuniform mat3 uMapCoord;\nuniform vec4 uClampFrame;\nuniform vec2 uClampOffset;\n\nvoid main(void)\n{\n vec2 coord = vTextureCoord + ceil(uClampOffset - vTextureCoord);\n coord = (uMapCoord * vec3(coord, 1.0)).xy;\n vec2 unclamped = coord;\n coord = clamp(coord, uClampFrame.xy, uClampFrame.zw);\n\n vec4 texSample = texture(uSampler, coord, unclamped == coord ? 0.0f : -32.0f);// lod-bias very negative to force lod 0\n\n fragmentColor = texSample * uColor;\n}\n";
  407. var tempMat = new math.Matrix();
  408. /**
  409. * WebGL renderer plugin for tiling sprites
  410. * @class
  411. * @memberof PIXI
  412. * @extends PIXI.ObjectRenderer
  413. */
  414. var TilingSpriteRenderer = /** @class */ (function (_super) {
  415. __extends(TilingSpriteRenderer, _super);
  416. /**
  417. * constructor for renderer
  418. * @param {PIXI.Renderer} renderer - The renderer this tiling awesomeness works for.
  419. */
  420. function TilingSpriteRenderer(renderer) {
  421. var _this = _super.call(this, renderer) || this;
  422. // WebGL version is not available during initialization!
  423. renderer.runners.contextChange.add(_this);
  424. _this.quad = new core.QuadUv();
  425. /**
  426. * The WebGL state in which this renderer will work.
  427. * @member {PIXI.State}
  428. * @readonly
  429. */
  430. _this.state = core.State.for2d();
  431. return _this;
  432. }
  433. /** Creates shaders when context is initialized. */
  434. TilingSpriteRenderer.prototype.contextChange = function () {
  435. var renderer = this.renderer;
  436. var uniforms = { globals: renderer.globalUniforms };
  437. this.simpleShader = core.Shader.from(gl1VertexSrc, fragmentSimpleSrc, uniforms);
  438. this.shader = renderer.context.webGLVersion > 1
  439. ? core.Shader.from(gl2VertexSrc, gl2FragmentSrc, uniforms)
  440. : core.Shader.from(gl1VertexSrc, gl1FragmentSrc, uniforms);
  441. };
  442. /**
  443. * @param {PIXI.TilingSprite} ts - tilingSprite to be rendered
  444. */
  445. TilingSpriteRenderer.prototype.render = function (ts) {
  446. var renderer = this.renderer;
  447. var quad = this.quad;
  448. var vertices = quad.vertices;
  449. vertices[0] = vertices[6] = (ts._width) * -ts.anchor.x;
  450. vertices[1] = vertices[3] = ts._height * -ts.anchor.y;
  451. vertices[2] = vertices[4] = (ts._width) * (1.0 - ts.anchor.x);
  452. vertices[5] = vertices[7] = ts._height * (1.0 - ts.anchor.y);
  453. var anchorX = ts.uvRespectAnchor ? ts.anchor.x : 0;
  454. var anchorY = ts.uvRespectAnchor ? ts.anchor.y : 0;
  455. vertices = quad.uvs;
  456. vertices[0] = vertices[6] = -anchorX;
  457. vertices[1] = vertices[3] = -anchorY;
  458. vertices[2] = vertices[4] = 1.0 - anchorX;
  459. vertices[5] = vertices[7] = 1.0 - anchorY;
  460. quad.invalidate();
  461. var tex = ts._texture;
  462. var baseTex = tex.baseTexture;
  463. var premultiplied = baseTex.alphaMode > 0;
  464. var lt = ts.tileTransform.localTransform;
  465. var uv = ts.uvMatrix;
  466. var isSimple = baseTex.isPowerOfTwo
  467. && tex.frame.width === baseTex.width && tex.frame.height === baseTex.height;
  468. // auto, force repeat wrapMode for big tiling textures
  469. if (isSimple) {
  470. if (!baseTex._glTextures[renderer.CONTEXT_UID]) {
  471. if (baseTex.wrapMode === constants.WRAP_MODES.CLAMP) {
  472. baseTex.wrapMode = constants.WRAP_MODES.REPEAT;
  473. }
  474. }
  475. else {
  476. isSimple = baseTex.wrapMode !== constants.WRAP_MODES.CLAMP;
  477. }
  478. }
  479. var shader = isSimple ? this.simpleShader : this.shader;
  480. var w = tex.width;
  481. var h = tex.height;
  482. var W = ts._width;
  483. var H = ts._height;
  484. tempMat.set(lt.a * w / W, lt.b * w / H, lt.c * h / W, lt.d * h / H, lt.tx / W, lt.ty / H);
  485. // that part is the same as above:
  486. // tempMat.identity();
  487. // tempMat.scale(tex.width, tex.height);
  488. // tempMat.prepend(lt);
  489. // tempMat.scale(1.0 / ts._width, 1.0 / ts._height);
  490. tempMat.invert();
  491. if (isSimple) {
  492. tempMat.prepend(uv.mapCoord);
  493. }
  494. else {
  495. shader.uniforms.uMapCoord = uv.mapCoord.toArray(true);
  496. shader.uniforms.uClampFrame = uv.uClampFrame;
  497. shader.uniforms.uClampOffset = uv.uClampOffset;
  498. }
  499. shader.uniforms.uTransform = tempMat.toArray(true);
  500. shader.uniforms.uColor = utils.premultiplyTintToRgba(ts.tint, ts.worldAlpha, shader.uniforms.uColor, premultiplied);
  501. shader.uniforms.translationMatrix = ts.transform.worldTransform.toArray(true);
  502. shader.uniforms.uSampler = tex;
  503. renderer.shader.bind(shader);
  504. renderer.geometry.bind(quad);
  505. this.state.blendMode = utils.correctBlendMode(ts.blendMode, premultiplied);
  506. renderer.state.set(this.state);
  507. renderer.geometry.draw(this.renderer.gl.TRIANGLES, 6, 0);
  508. };
  509. /** @ignore */
  510. TilingSpriteRenderer.extension = {
  511. name: 'tilingSprite',
  512. type: core.ExtensionType.RendererPlugin,
  513. };
  514. return TilingSpriteRenderer;
  515. }(core.ObjectRenderer));
  516. exports.TilingSprite = TilingSprite;
  517. exports.TilingSpriteRenderer = TilingSpriteRenderer;
  518. Object.defineProperty(exports, '__esModule', { value: true });
  519. return exports;
  520. })({}, PIXI, PIXI, PIXI, PIXI, PIXI.utils);
  521. Object.assign(this.PIXI, _pixi_sprite_tiling);
  522. //# sourceMappingURL=sprite-tiling.js.map