mesh.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*!
  2. * @pixi/mesh - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/mesh is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. this.PIXI = this.PIXI || {};
  9. var _pixi_mesh = (function (exports, core, math, constants, display, settings, 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. /**
  207. * Class controls cache for UV mapping from Texture normal space to BaseTexture normal space.
  208. * @memberof PIXI
  209. */
  210. var MeshBatchUvs = /** @class */ (function () {
  211. /**
  212. * @param uvBuffer - Buffer with normalized uv's
  213. * @param uvMatrix - Material UV matrix
  214. */
  215. function MeshBatchUvs(uvBuffer, uvMatrix) {
  216. this.uvBuffer = uvBuffer;
  217. this.uvMatrix = uvMatrix;
  218. this.data = null;
  219. this._bufferUpdateId = -1;
  220. this._textureUpdateId = -1;
  221. this._updateID = 0;
  222. }
  223. /**
  224. * Updates
  225. * @param forceUpdate - force the update
  226. */
  227. MeshBatchUvs.prototype.update = function (forceUpdate) {
  228. if (!forceUpdate
  229. && this._bufferUpdateId === this.uvBuffer._updateID
  230. && this._textureUpdateId === this.uvMatrix._updateID) {
  231. return;
  232. }
  233. this._bufferUpdateId = this.uvBuffer._updateID;
  234. this._textureUpdateId = this.uvMatrix._updateID;
  235. var data = this.uvBuffer.data;
  236. if (!this.data || this.data.length !== data.length) {
  237. this.data = new Float32Array(data.length);
  238. }
  239. this.uvMatrix.multiplyUvs(data, this.data);
  240. this._updateID++;
  241. };
  242. return MeshBatchUvs;
  243. }());
  244. var tempPoint = new math.Point();
  245. var tempPolygon = new math.Polygon();
  246. /**
  247. * Base mesh class.
  248. *
  249. * This class empowers you to have maximum flexibility to render any kind of WebGL visuals you can think of.
  250. * This class assumes a certain level of WebGL knowledge.
  251. * If you know a bit this should abstract enough away to make your life easier!
  252. *
  253. * Pretty much ALL WebGL can be broken down into the following:
  254. * - Geometry - The structure and data for the mesh. This can include anything from positions, uvs, normals, colors etc..
  255. * - Shader - This is the shader that PixiJS will render the geometry with (attributes in the shader must match the geometry)
  256. * - State - This is the state of WebGL required to render the mesh.
  257. *
  258. * Through a combination of the above elements you can render anything you want, 2D or 3D!
  259. * @memberof PIXI
  260. */
  261. var Mesh = /** @class */ (function (_super) {
  262. __extends(Mesh, _super);
  263. /**
  264. * @param geometry - The geometry the mesh will use.
  265. * @param {PIXI.MeshMaterial} shader - The shader the mesh will use.
  266. * @param state - The state that the WebGL context is required to be in to render the mesh
  267. * if no state is provided, uses {@link PIXI.State.for2d} to create a 2D state for PixiJS.
  268. * @param drawMode - The drawMode, can be any of the {@link PIXI.DRAW_MODES} constants.
  269. */
  270. function Mesh(geometry, shader, state, drawMode) {
  271. if (drawMode === void 0) { drawMode = constants.DRAW_MODES.TRIANGLES; }
  272. var _this = _super.call(this) || this;
  273. _this.geometry = geometry;
  274. _this.shader = shader;
  275. _this.state = state || core.State.for2d();
  276. _this.drawMode = drawMode;
  277. _this.start = 0;
  278. _this.size = 0;
  279. _this.uvs = null;
  280. _this.indices = null;
  281. _this.vertexData = new Float32Array(1);
  282. _this.vertexDirty = -1;
  283. _this._transformID = -1;
  284. _this._roundPixels = settings.settings.ROUND_PIXELS;
  285. _this.batchUvs = null;
  286. return _this;
  287. }
  288. Object.defineProperty(Mesh.prototype, "geometry", {
  289. /**
  290. * Includes vertex positions, face indices, normals, colors, UVs, and
  291. * custom attributes within buffers, reducing the cost of passing all
  292. * this data to the GPU. Can be shared between multiple Mesh objects.
  293. */
  294. get: function () {
  295. return this._geometry;
  296. },
  297. set: function (value) {
  298. if (this._geometry === value) {
  299. return;
  300. }
  301. if (this._geometry) {
  302. this._geometry.refCount--;
  303. if (this._geometry.refCount === 0) {
  304. this._geometry.dispose();
  305. }
  306. }
  307. this._geometry = value;
  308. if (this._geometry) {
  309. this._geometry.refCount++;
  310. }
  311. this.vertexDirty = -1;
  312. },
  313. enumerable: false,
  314. configurable: true
  315. });
  316. Object.defineProperty(Mesh.prototype, "uvBuffer", {
  317. /**
  318. * To change mesh uv's, change its uvBuffer data and increment its _updateID.
  319. * @readonly
  320. */
  321. get: function () {
  322. return this.geometry.buffers[1];
  323. },
  324. enumerable: false,
  325. configurable: true
  326. });
  327. Object.defineProperty(Mesh.prototype, "verticesBuffer", {
  328. /**
  329. * To change mesh vertices, change its uvBuffer data and increment its _updateID.
  330. * Incrementing _updateID is optional because most of Mesh objects do it anyway.
  331. * @readonly
  332. */
  333. get: function () {
  334. return this.geometry.buffers[0];
  335. },
  336. enumerable: false,
  337. configurable: true
  338. });
  339. Object.defineProperty(Mesh.prototype, "material", {
  340. get: function () {
  341. return this.shader;
  342. },
  343. /** Alias for {@link PIXI.Mesh#shader}. */
  344. set: function (value) {
  345. this.shader = value;
  346. },
  347. enumerable: false,
  348. configurable: true
  349. });
  350. Object.defineProperty(Mesh.prototype, "blendMode", {
  351. get: function () {
  352. return this.state.blendMode;
  353. },
  354. /**
  355. * The blend mode to be applied to the Mesh. Apply a value of
  356. * `PIXI.BLEND_MODES.NORMAL` to reset the blend mode.
  357. * @default PIXI.BLEND_MODES.NORMAL;
  358. */
  359. set: function (value) {
  360. this.state.blendMode = value;
  361. },
  362. enumerable: false,
  363. configurable: true
  364. });
  365. Object.defineProperty(Mesh.prototype, "roundPixels", {
  366. get: function () {
  367. return this._roundPixels;
  368. },
  369. /**
  370. * If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation.
  371. * Advantages can include sharper image quality (like text) and faster rendering on canvas.
  372. * The main disadvantage is movement of objects may appear less smooth.
  373. * To set the global default, change {@link PIXI.settings.ROUND_PIXELS}
  374. * @default false
  375. */
  376. set: function (value) {
  377. if (this._roundPixels !== value) {
  378. this._transformID = -1;
  379. }
  380. this._roundPixels = value;
  381. },
  382. enumerable: false,
  383. configurable: true
  384. });
  385. Object.defineProperty(Mesh.prototype, "tint", {
  386. /**
  387. * The multiply tint applied to the Mesh. This is a hex value. A value of
  388. * `0xFFFFFF` will remove any tint effect.
  389. *
  390. * Null for non-MeshMaterial shaders
  391. * @default 0xFFFFFF
  392. */
  393. get: function () {
  394. return 'tint' in this.shader ? this.shader.tint : null;
  395. },
  396. set: function (value) {
  397. this.shader.tint = value;
  398. },
  399. enumerable: false,
  400. configurable: true
  401. });
  402. Object.defineProperty(Mesh.prototype, "texture", {
  403. /** The texture that the Mesh uses. Null for non-MeshMaterial shaders */
  404. get: function () {
  405. return 'texture' in this.shader ? this.shader.texture : null;
  406. },
  407. set: function (value) {
  408. this.shader.texture = value;
  409. },
  410. enumerable: false,
  411. configurable: true
  412. });
  413. /**
  414. * Standard renderer draw.
  415. * @param renderer - Instance to renderer.
  416. */
  417. Mesh.prototype._render = function (renderer) {
  418. // set properties for batching..
  419. // TODO could use a different way to grab verts?
  420. var vertices = this.geometry.buffers[0].data;
  421. var shader = this.shader;
  422. // TODO benchmark check for attribute size..
  423. if (shader.batchable
  424. && this.drawMode === constants.DRAW_MODES.TRIANGLES
  425. && vertices.length < Mesh.BATCHABLE_SIZE * 2) {
  426. this._renderToBatch(renderer);
  427. }
  428. else {
  429. this._renderDefault(renderer);
  430. }
  431. };
  432. /**
  433. * Standard non-batching way of rendering.
  434. * @param renderer - Instance to renderer.
  435. */
  436. Mesh.prototype._renderDefault = function (renderer) {
  437. var shader = this.shader;
  438. shader.alpha = this.worldAlpha;
  439. if (shader.update) {
  440. shader.update();
  441. }
  442. renderer.batch.flush();
  443. // bind and sync uniforms..
  444. shader.uniforms.translationMatrix = this.transform.worldTransform.toArray(true);
  445. renderer.shader.bind(shader);
  446. // set state..
  447. renderer.state.set(this.state);
  448. // bind the geometry...
  449. renderer.geometry.bind(this.geometry, shader);
  450. // then render it
  451. renderer.geometry.draw(this.drawMode, this.size, this.start, this.geometry.instanceCount);
  452. };
  453. /**
  454. * Rendering by using the Batch system.
  455. * @param renderer - Instance to renderer.
  456. */
  457. Mesh.prototype._renderToBatch = function (renderer) {
  458. var geometry = this.geometry;
  459. var shader = this.shader;
  460. if (shader.uvMatrix) {
  461. shader.uvMatrix.update();
  462. this.calculateUvs();
  463. }
  464. // set properties for batching..
  465. this.calculateVertices();
  466. this.indices = geometry.indexBuffer.data;
  467. this._tintRGB = shader._tintRGB;
  468. this._texture = shader.texture;
  469. var pluginName = this.material.pluginName;
  470. renderer.batch.setObjectRenderer(renderer.plugins[pluginName]);
  471. renderer.plugins[pluginName].render(this);
  472. };
  473. /** Updates vertexData field based on transform and vertices. */
  474. Mesh.prototype.calculateVertices = function () {
  475. var geometry = this.geometry;
  476. var verticesBuffer = geometry.buffers[0];
  477. var vertices = verticesBuffer.data;
  478. var vertexDirtyId = verticesBuffer._updateID;
  479. if (vertexDirtyId === this.vertexDirty && this._transformID === this.transform._worldID) {
  480. return;
  481. }
  482. this._transformID = this.transform._worldID;
  483. if (this.vertexData.length !== vertices.length) {
  484. this.vertexData = new Float32Array(vertices.length);
  485. }
  486. var wt = this.transform.worldTransform;
  487. var a = wt.a;
  488. var b = wt.b;
  489. var c = wt.c;
  490. var d = wt.d;
  491. var tx = wt.tx;
  492. var ty = wt.ty;
  493. var vertexData = this.vertexData;
  494. for (var i = 0; i < vertexData.length / 2; i++) {
  495. var x = vertices[(i * 2)];
  496. var y = vertices[(i * 2) + 1];
  497. vertexData[(i * 2)] = (a * x) + (c * y) + tx;
  498. vertexData[(i * 2) + 1] = (b * x) + (d * y) + ty;
  499. }
  500. if (this._roundPixels) {
  501. var resolution = settings.settings.RESOLUTION;
  502. for (var i = 0; i < vertexData.length; ++i) {
  503. vertexData[i] = Math.round((vertexData[i] * resolution | 0) / resolution);
  504. }
  505. }
  506. this.vertexDirty = vertexDirtyId;
  507. };
  508. /** Updates uv field based on from geometry uv's or batchUvs. */
  509. Mesh.prototype.calculateUvs = function () {
  510. var geomUvs = this.geometry.buffers[1];
  511. var shader = this.shader;
  512. if (!shader.uvMatrix.isSimple) {
  513. if (!this.batchUvs) {
  514. this.batchUvs = new MeshBatchUvs(geomUvs, shader.uvMatrix);
  515. }
  516. this.batchUvs.update();
  517. this.uvs = this.batchUvs.data;
  518. }
  519. else {
  520. this.uvs = geomUvs.data;
  521. }
  522. };
  523. /**
  524. * Updates the bounds of the mesh as a rectangle. The bounds calculation takes the worldTransform into account.
  525. * there must be a aVertexPosition attribute present in the geometry for bounds to be calculated correctly.
  526. */
  527. Mesh.prototype._calculateBounds = function () {
  528. this.calculateVertices();
  529. this._bounds.addVertexData(this.vertexData, 0, this.vertexData.length);
  530. };
  531. /**
  532. * Tests if a point is inside this mesh. Works only for PIXI.DRAW_MODES.TRIANGLES.
  533. * @param point - The point to test.
  534. * @returns - The result of the test.
  535. */
  536. Mesh.prototype.containsPoint = function (point) {
  537. if (!this.getBounds().contains(point.x, point.y)) {
  538. return false;
  539. }
  540. this.worldTransform.applyInverse(point, tempPoint);
  541. var vertices = this.geometry.getBuffer('aVertexPosition').data;
  542. var points = tempPolygon.points;
  543. var indices = this.geometry.getIndex().data;
  544. var len = indices.length;
  545. var step = this.drawMode === 4 ? 3 : 1;
  546. for (var i = 0; i + 2 < len; i += step) {
  547. var ind0 = indices[i] * 2;
  548. var ind1 = indices[i + 1] * 2;
  549. var ind2 = indices[i + 2] * 2;
  550. points[0] = vertices[ind0];
  551. points[1] = vertices[ind0 + 1];
  552. points[2] = vertices[ind1];
  553. points[3] = vertices[ind1 + 1];
  554. points[4] = vertices[ind2];
  555. points[5] = vertices[ind2 + 1];
  556. if (tempPolygon.contains(tempPoint.x, tempPoint.y)) {
  557. return true;
  558. }
  559. }
  560. return false;
  561. };
  562. Mesh.prototype.destroy = function (options) {
  563. _super.prototype.destroy.call(this, options);
  564. if (this._cachedTexture) {
  565. this._cachedTexture.destroy();
  566. this._cachedTexture = null;
  567. }
  568. this.geometry = null;
  569. this.shader = null;
  570. this.state = null;
  571. this.uvs = null;
  572. this.indices = null;
  573. this.vertexData = null;
  574. };
  575. /** The maximum number of vertices to consider batchable. Generally, the complexity of the geometry. */
  576. Mesh.BATCHABLE_SIZE = 100;
  577. return Mesh;
  578. }(display.Container));
  579. var fragment = "varying vec2 vTextureCoord;\nuniform vec4 uColor;\n\nuniform sampler2D uSampler;\n\nvoid main(void)\n{\n gl_FragColor = texture2D(uSampler, vTextureCoord) * uColor;\n}\n";
  580. var vertex = "attribute vec2 aVertexPosition;\nattribute vec2 aTextureCoord;\n\nuniform mat3 projectionMatrix;\nuniform mat3 translationMatrix;\nuniform mat3 uTextureMatrix;\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 = (uTextureMatrix * vec3(aTextureCoord, 1.0)).xy;\n}\n";
  581. /**
  582. * Slightly opinionated default shader for PixiJS 2D objects.
  583. * @memberof PIXI
  584. */
  585. var MeshMaterial = /** @class */ (function (_super) {
  586. __extends(MeshMaterial, _super);
  587. /**
  588. * @param uSampler - Texture that material uses to render.
  589. * @param options - Additional options
  590. * @param {number} [options.alpha=1] - Default alpha.
  591. * @param {number} [options.tint=0xFFFFFF] - Default tint.
  592. * @param {string} [options.pluginName='batch'] - Renderer plugin for batching.
  593. * @param {PIXI.Program} [options.program=0xFFFFFF] - Custom program.
  594. * @param {object} [options.uniforms] - Custom uniforms.
  595. */
  596. function MeshMaterial(uSampler, options) {
  597. var _this = this;
  598. var uniforms = {
  599. uSampler: uSampler,
  600. alpha: 1,
  601. uTextureMatrix: math.Matrix.IDENTITY,
  602. uColor: new Float32Array([1, 1, 1, 1]),
  603. };
  604. // Set defaults
  605. options = Object.assign({
  606. tint: 0xFFFFFF,
  607. alpha: 1,
  608. pluginName: 'batch',
  609. }, options);
  610. if (options.uniforms) {
  611. Object.assign(uniforms, options.uniforms);
  612. }
  613. _this = _super.call(this, options.program || core.Program.from(vertex, fragment), uniforms) || this;
  614. _this._colorDirty = false;
  615. _this.uvMatrix = new core.TextureMatrix(uSampler);
  616. _this.batchable = options.program === undefined;
  617. _this.pluginName = options.pluginName;
  618. _this.tint = options.tint;
  619. _this.alpha = options.alpha;
  620. return _this;
  621. }
  622. Object.defineProperty(MeshMaterial.prototype, "texture", {
  623. /** Reference to the texture being rendered. */
  624. get: function () {
  625. return this.uniforms.uSampler;
  626. },
  627. set: function (value) {
  628. if (this.uniforms.uSampler !== value) {
  629. if (!this.uniforms.uSampler.baseTexture.alphaMode !== !value.baseTexture.alphaMode) {
  630. this._colorDirty = true;
  631. }
  632. this.uniforms.uSampler = value;
  633. this.uvMatrix.texture = value;
  634. }
  635. },
  636. enumerable: false,
  637. configurable: true
  638. });
  639. Object.defineProperty(MeshMaterial.prototype, "alpha", {
  640. get: function () {
  641. return this._alpha;
  642. },
  643. /**
  644. * This gets automatically set by the object using this.
  645. * @default 1
  646. */
  647. set: function (value) {
  648. if (value === this._alpha)
  649. { return; }
  650. this._alpha = value;
  651. this._colorDirty = true;
  652. },
  653. enumerable: false,
  654. configurable: true
  655. });
  656. Object.defineProperty(MeshMaterial.prototype, "tint", {
  657. get: function () {
  658. return this._tint;
  659. },
  660. /**
  661. * Multiply tint for the material.
  662. * @default 0xFFFFFF
  663. */
  664. set: function (value) {
  665. if (value === this._tint)
  666. { return; }
  667. this._tint = value;
  668. this._tintRGB = (value >> 16) + (value & 0xff00) + ((value & 0xff) << 16);
  669. this._colorDirty = true;
  670. },
  671. enumerable: false,
  672. configurable: true
  673. });
  674. /** Gets called automatically by the Mesh. Intended to be overridden for custom {@link MeshMaterial} objects. */
  675. MeshMaterial.prototype.update = function () {
  676. if (this._colorDirty) {
  677. this._colorDirty = false;
  678. var baseTexture = this.texture.baseTexture;
  679. utils.premultiplyTintToRgba(this._tint, this._alpha, this.uniforms.uColor, baseTexture.alphaMode);
  680. }
  681. if (this.uvMatrix.update()) {
  682. this.uniforms.uTextureMatrix = this.uvMatrix.mapCoord;
  683. }
  684. };
  685. return MeshMaterial;
  686. }(core.Shader));
  687. /**
  688. * Standard 2D geometry used in PixiJS.
  689. *
  690. * Geometry can be defined without passing in a style or data if required.
  691. *
  692. * ```js
  693. * const geometry = new PIXI.Geometry();
  694. *
  695. * geometry.addAttribute('positions', [0, 0, 100, 0, 100, 100, 0, 100], 2);
  696. * geometry.addAttribute('uvs', [0,0,1,0,1,1,0,1], 2);
  697. * geometry.addIndex([0,1,2,1,3,2]);
  698. *
  699. * ```
  700. * @memberof PIXI
  701. */
  702. var MeshGeometry = /** @class */ (function (_super) {
  703. __extends(MeshGeometry, _super);
  704. /**
  705. * @param {Float32Array|number[]} [vertices] - Positional data on geometry.
  706. * @param {Float32Array|number[]} [uvs] - Texture UVs.
  707. * @param {Uint16Array|number[]} [index] - IndexBuffer
  708. */
  709. function MeshGeometry(vertices, uvs, index) {
  710. var _this = _super.call(this) || this;
  711. var verticesBuffer = new core.Buffer(vertices);
  712. var uvsBuffer = new core.Buffer(uvs, true);
  713. var indexBuffer = new core.Buffer(index, true, true);
  714. _this.addAttribute('aVertexPosition', verticesBuffer, 2, false, constants.TYPES.FLOAT)
  715. .addAttribute('aTextureCoord', uvsBuffer, 2, false, constants.TYPES.FLOAT)
  716. .addIndex(indexBuffer);
  717. _this._updateId = -1;
  718. return _this;
  719. }
  720. Object.defineProperty(MeshGeometry.prototype, "vertexDirtyId", {
  721. /**
  722. * If the vertex position is updated.
  723. * @readonly
  724. * @private
  725. */
  726. get: function () {
  727. return this.buffers[0]._updateID;
  728. },
  729. enumerable: false,
  730. configurable: true
  731. });
  732. return MeshGeometry;
  733. }(core.Geometry));
  734. exports.Mesh = Mesh;
  735. exports.MeshBatchUvs = MeshBatchUvs;
  736. exports.MeshGeometry = MeshGeometry;
  737. exports.MeshMaterial = MeshMaterial;
  738. Object.defineProperty(exports, '__esModule', { value: true });
  739. return exports;
  740. })({}, PIXI, PIXI, PIXI, PIXI, PIXI, PIXI.utils);
  741. Object.assign(this.PIXI, _pixi_mesh);
  742. //# sourceMappingURL=mesh.js.map