@spearwolf/twopoint5d
Version:
Create 2.5D realtime graphics and pixelart with WebGL and three.js
124 lines • 3.84 kB
JavaScript
const minCoord = (current, scalarKey, sizeKey) => {
let texCoords = current;
let scalar = 0;
while (texCoords.parent != null) {
scalar += texCoords[scalarKey];
texCoords = texCoords.parent;
}
return scalar / texCoords[sizeKey];
};
const maxCoord = (current, scalarKey, sizeKey) => {
let texCoords = current;
let coord = current[sizeKey];
while (texCoords.parent != null) {
coord += texCoords[scalarKey];
texCoords = texCoords.parent;
}
return coord / texCoords[sizeKey];
};
export class TextureCoords {
static { this.FLIP_HORIZONTAL = 1; }
static { this.FLIP_VERTICAL = 2; }
static { this.FLIP_DIAGONAL = 4; }
constructor(...args) {
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
this.flip = 0;
if (args[0] instanceof TextureCoords) {
this.parent = args[0];
this.x = args[1] ?? 0;
this.y = args[2] ?? 0;
this.width = args[3] ?? 0;
this.height = args[4] ?? 0;
}
else if (args?.length) {
this.x = args[0] ?? 0;
this.y = args[1] ?? 0;
this.width = args[2] ?? 0;
this.height = args[3] ?? 0;
}
}
clone() {
const texCoords = new TextureCoords();
texCoords.parent = this.parent;
texCoords.x = this.x;
texCoords.y = this.y;
texCoords.width = this.width;
texCoords.height = this.height;
texCoords.flip = this.flip;
return texCoords;
}
get root() {
let root = this;
while (root.parent) {
root = root.parent;
}
return root;
}
get flipH() {
return (this.flip & TextureCoords.FLIP_HORIZONTAL) > 0;
}
set flipH(flip) {
this.flip =
(flip ? TextureCoords.FLIP_HORIZONTAL : 0) | (this.flip & (TextureCoords.FLIP_VERTICAL | TextureCoords.FLIP_DIAGONAL));
}
get flipV() {
return (this.flip & TextureCoords.FLIP_VERTICAL) > 0;
}
set flipV(flip) {
this.flip =
(flip ? TextureCoords.FLIP_VERTICAL : 0) | (this.flip & (TextureCoords.FLIP_HORIZONTAL | TextureCoords.FLIP_DIAGONAL));
}
get flipD() {
return (this.flip & TextureCoords.FLIP_DIAGONAL) > 0;
}
set flipD(flip) {
this.flip =
(flip ? TextureCoords.FLIP_DIAGONAL : 0) | (this.flip & (TextureCoords.FLIP_VERTICAL | TextureCoords.FLIP_HORIZONTAL));
}
flipHorizontal() {
this.flipH = !this.flipH;
return this;
}
flipVertical() {
this.flipV = !this.flipV;
return this;
}
flipDiagonal() {
this.flipD = !this.flipD;
return this;
}
get s() {
const { flipD } = this;
return this.flipH
? maxCoord(this, flipD ? 'y' : 'x', flipD ? 'height' : 'width')
: minCoord(this, flipD ? 'y' : 'x', flipD ? 'height' : 'width');
}
get t() {
const { flipD } = this;
return this.flipV
? maxCoord(this, flipD ? 'x' : 'y', flipD ? 'width' : 'height')
: minCoord(this, flipD ? 'x' : 'y', flipD ? 'width' : 'height');
}
get s1() {
const { flipD } = this;
return this.flipH
? minCoord(this, flipD ? 'y' : 'x', flipD ? 'height' : 'width')
: maxCoord(this, flipD ? 'y' : 'x', flipD ? 'height' : 'width');
}
get t1() {
const { flipD } = this;
return this.flipV
? minCoord(this, flipD ? 'x' : 'y', flipD ? 'width' : 'height')
: maxCoord(this, flipD ? 'x' : 'y', flipD ? 'width' : 'height');
}
get u() {
return this.s1 - this.s;
}
get v() {
return this.t1 - this.t;
}
}
//# sourceMappingURL=TextureCoords.js.map