Source: mesh/Plane.js

mesh/Plane.js

  1. import Mesh from './Mesh';
  2. /**
  3. * The Plane allows you to draw a texture across several points and them manipulate these points
  4. *
  5. *```js
  6. * for (let i = 0; i < 20; i++) {
  7. * points.push(new PIXI.Point(i * 50, 0));
  8. * };
  9. * let Plane = new PIXI.Plane(PIXI.Texture.fromImage("snake.png"), points);
  10. * ```
  11. *
  12. * @class
  13. * @extends PIXI.mesh.Mesh
  14. * @memberof PIXI.mesh
  15. *
  16. */
  17. export default class Plane extends Mesh
  18. {
  19. /**
  20. * @param {PIXI.Texture} texture - The texture to use on the Plane.
  21. * @param {number} [verticesX=10] - The number of vertices in the x-axis
  22. * @param {number} [verticesY=10] - The number of vertices in the y-axis
  23. */
  24. constructor(texture, verticesX, verticesY)
  25. {
  26. super(texture);
  27. /**
  28. * Tracker for if the Plane is ready to be drawn. Needed because Mesh ctor can
  29. * call _onTextureUpdated which could call refresh too early.
  30. *
  31. * @member {boolean}
  32. * @private
  33. */
  34. this._ready = true;
  35. this.verticesX = verticesX || 10;
  36. this.verticesY = verticesY || 10;
  37. this.drawMode = Mesh.DRAW_MODES.TRIANGLES;
  38. this.refresh();
  39. }
  40. /**
  41. * Refreshes plane coordinates
  42. *
  43. */
  44. _refresh()
  45. {
  46. const texture = this._texture;
  47. const total = this.verticesX * this.verticesY;
  48. const verts = [];
  49. const colors = [];
  50. const uvs = [];
  51. const indices = [];
  52. const segmentsX = this.verticesX - 1;
  53. const segmentsY = this.verticesY - 1;
  54. const sizeX = texture.width / segmentsX;
  55. const sizeY = texture.height / segmentsY;
  56. for (let i = 0; i < total; i++)
  57. {
  58. const x = (i % this.verticesX);
  59. const y = ((i / this.verticesX) | 0);
  60. verts.push(x * sizeX, y * sizeY);
  61. uvs.push(x / segmentsX, y / segmentsY);
  62. }
  63. // cons
  64. const totalSub = segmentsX * segmentsY;
  65. for (let i = 0; i < totalSub; i++)
  66. {
  67. const xpos = i % segmentsX;
  68. const ypos = (i / segmentsX) | 0;
  69. const value = (ypos * this.verticesX) + xpos;
  70. const value2 = (ypos * this.verticesX) + xpos + 1;
  71. const value3 = ((ypos + 1) * this.verticesX) + xpos;
  72. const value4 = ((ypos + 1) * this.verticesX) + xpos + 1;
  73. indices.push(value, value2, value3);
  74. indices.push(value2, value4, value3);
  75. }
  76. // console.log(indices)
  77. this.vertices = new Float32Array(verts);
  78. this.uvs = new Float32Array(uvs);
  79. this.colors = new Float32Array(colors);
  80. this.indices = new Uint16Array(indices);
  81. this.dirty++;
  82. this.indexDirty++;
  83. this.multiplyUvs();
  84. }
  85. /**
  86. * Clear texture UVs when new texture is set
  87. *
  88. * @private
  89. */
  90. _onTextureUpdate()
  91. {
  92. Mesh.prototype._onTextureUpdate.call(this);
  93. // wait for the Plane ctor to finish before calling refresh
  94. if (this._ready)
  95. {
  96. this.refresh();
  97. }
  98. }
  99. }