@awayjs/graphics
Version:
AwayJS graphics classes
111 lines (110 loc) • 4.3 kB
JavaScript
import { Point, ColorUtils, Matrix } from '@awayjs/core';
import { BitmapImage2D } from '@awayjs/stage';
var TextureAtlas = /** @class */ (function () {
function TextureAtlas() {
this.availableRows = 256;
this.gradientRow = -1;
// begin outside valid region, because will be invalid `availableRows`
this.colorRow = 256;
this.colorPosition = 0;
this.bitmap = new BitmapImage2D(256, 256, true, null);
this.availableRows = 256;
}
TextureAtlas.getTextureForColor = function (solid) {
var hash = solid.toString();
if (hash in this._allColors) {
solid.uvMatrix = this._allColors[hash].uvMatrix;
return this._allColors[hash].bitmap;
}
// find textureAtlas that has empty space:
var len = this._allTextureAtlas.length;
var textureAtlas;
for (var t = 0; t < len; t++) {
if (this._allTextureAtlas[t].fitColor()) {
textureAtlas = this._allTextureAtlas[t];
break;
}
}
if (!textureAtlas) {
textureAtlas = new TextureAtlas();
this._allTextureAtlas.push(textureAtlas);
}
var colorPos = textureAtlas.addSolid(solid);
solid.uvMatrix = new Matrix(0, 0, 0, 0, colorPos.x, colorPos.y);
this._allColors[hash] = {
uvMatrix: solid.uvMatrix,
bitmap: textureAtlas.bitmap,
uvRectangle: null
};
return textureAtlas.bitmap;
};
TextureAtlas.getTextureForGradient = function (gradient) {
var hash = gradient.toString();
if (hash in this._allGradients) {
gradient.uvRectangle = this._allGradients[hash].uvRectangle;
return this._allGradients[hash].bitmap;
}
var textureAtlas;
var len = this._allTextureAtlas.length;
for (var t = 0; t < len; t++) {
if (this._allTextureAtlas[t].fitGradient()) {
textureAtlas = this._allTextureAtlas[t];
break;
}
}
if (!textureAtlas) {
textureAtlas = new TextureAtlas();
this._allTextureAtlas.push(textureAtlas);
}
textureAtlas.addGradient(gradient);
this._allGradients[hash] = {
bitmap: textureAtlas.bitmap,
uvRectangle: gradient.uvRectangle
};
return textureAtlas.bitmap;
};
TextureAtlas.prototype.fitGradient = function () {
return (this.availableRows > 0);
};
TextureAtlas.prototype.fitColor = function () {
return this.availableRows > 0 || this.colorPosition > 0;
};
TextureAtlas.prototype.addGradient = function (gradient) {
if (this.availableRows <= 0) {
console.error('[TextureAtlass] There are not free space for gradient:', gradient);
return;
}
this.gradientRow++;
this.availableRows--;
for (var px = 0; px < 256; px++) {
this.bitmap.setPixelFromArray(px, this.gradientRow, gradient.getColorAtPosition(px));
}
gradient.uvRectangle.x = 1 / 512;
gradient.uvRectangle.y = 1 / 512 + (this.gradientRow / 256); //+1/512;
gradient.uvRectangle.width = 1 - 1 / 512;
gradient.uvRectangle.height = gradient.uvRectangle.y;
return this.availableRows;
};
TextureAtlas.prototype.addSolid = function (solid) {
this.colorPosition--;
if (this.colorPosition < 0) {
this.colorRow--;
this.availableRows--;
this.colorPosition = 255;
if (this.availableRows < 0) {
this.availableRows = 0;
console.error('[TextureAtlass] There are not free space for color:', solid.color.toString(16));
return null;
}
}
var argb = ColorUtils.float32ColorToARGB(solid.color);
argb[0] = solid.alpha;
this.bitmap.setPixelFromArray(this.colorPosition, this.colorRow, argb);
return new Point(1 / 512 + this.colorPosition / 256, 1 / 512 + this.colorRow / 256);
};
TextureAtlas._allTextureAtlas = [];
TextureAtlas._allGradients = {};
TextureAtlas._allColors = {};
return TextureAtlas;
}());
export { TextureAtlas };