UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

156 lines (130 loc) 3.34 kB
import AABB2 from "../../../../core/geom/2d/aabb/AABB2.js"; import Rectangle from "../../../../core/geom/2d/Rectangle.js"; import Vector2 from "../../../../core/geom/Vector2.js"; export class AtlasPatch { /** * Position in the atlas where the raw patch data is found, excludes padding * @readonly * @type {Vector2} */ position = new Vector2(0, 0); /** * Size in pixels of the patch, excludes padding * @readonly * @type {Vector2} */ size = new Vector2(0, 0); /** * Number of pixels to add to patch size to avoid sampling error at the edges * @type {number} */ padding = 4; /** * Managed by TextureAtlas * @type {number} */ id = -1; /** * Texture data of the patch, this is what will be stenciled into the atlas * @type {Sampler2D|null} */ sampler = null; /** * @readonly * @type {Rectangle} */ uv = new Rectangle(0, 0, 0, 0); /** * Area in the atlas where patch along with the padding is packed into. * Used for packing inside TextureAtlas. * Do not modify manually * @readonly * @type {AABB2} */ packing = new AABB2(); /** * bitfield * @type {number|AtlasPatchFlag} */ flags = 0; /** * Version of associated sampler that was last written. * Used to track and signal when sampler needs to be re-painted. * @type {number} */ last_painted_version = -1; /** * * @param {number} canvasWidth * @param {number} canvasHeight */ updateUV(canvasWidth, canvasHeight) { const position = this.position; const size = this.size; const uv = this.uv; const x0 = position.x; const y0 = position.y; const sx = size.x; const sy = size.y; uv.set( x0 / canvasWidth, y0 / canvasHeight, sx / canvasWidth, sy / canvasHeight ); } /** * * @param {number} canvasWidth * @param {number} canvasHeight */ updatePositionFromPacking(canvasWidth, canvasHeight) { const patch = this; const box = this.packing; const x = box.x0 + patch.padding; const y = box.y0 + patch.padding; patch.position.set(x, y); this.updateUV(canvasWidth, canvasHeight) } /** * * @param {number|AtlasPatchFlag} flag * @returns {void} */ setFlag(flag) { this.flags |= flag; } /** * * @param {number|AtlasPatchFlag} flag * @returns {void} */ clearFlag(flag) { this.flags &= ~flag; } /** * * @param {number|AtlasPatchFlag} flag * @param {boolean} value */ writeFlag(flag, value) { if (value) { this.setFlag(flag); } else { this.clearFlag(flag); } } /** * * @param {number|AtlasPatchFlag} flag * @returns {boolean} */ getFlag(flag) { return (this.flags & flag) === flag; } } /** * @readonly * @type {boolean} */ AtlasPatch.prototype.isAtlasPatch = true;