@awayjs/graphics
Version:
AwayJS graphics classes
122 lines (121 loc) • 4.66 kB
JavaScript
import { Point, ColorUtils } 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.clearAllMaterials = function () {
for (var key in TextureAtlas._allColors) {
TextureAtlas._allColors[key].material = null;
}
for (var key in TextureAtlas._allGradients) {
TextureAtlas._allGradients[key].material = null;
}
};
TextureAtlas.getTextureForColor = function (color, alpha) {
var hash = (color | 0).toString(16) + '#' + ((alpha * 255) | 0).toString(16);
if (hash in this._allColors) {
return this._allColors[hash];
}
// 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 newColorObj = {
colorPos: textureAtlas.addColor(color, alpha),
bitmap: textureAtlas.bitmap,
material: null,
uvRectangle: null
};
this._allColors[hash] = newColorObj;
return newColorObj;
};
TextureAtlas.getTextureForGradient = function (gradient) {
var hash = gradient.toString();
if (hash in this._allGradients) {
gradient.uvRectangle = this._allGradients[hash].uvRectangle;
return this._allGradients[hash];
}
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);
var newGradEntry = {
colorPos: null,
bitmap: textureAtlas.bitmap,
material: null,
uvRectangle: gradient.uvRectangle.clone()
};
this._allGradients[hash] = newGradEntry;
return newGradEntry;
};
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.addColor = function (color, alpha) {
if (alpha === void 0) { alpha = 1; }
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:', color.toString(16));
return null;
}
}
var argb = ColorUtils.float32ColorToARGB(color);
argb[0] = 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 };