@awayjs/stage
Version:
Stage for AwayJS
160 lines (159 loc) • 5.8 kB
JavaScript
import { __extends } from "tslib";
import { Image2D, _Stage_Image2D } from '../image/Image2D';
import { FilterUtils } from './FilterUtils';
var GradientAtlas = /** @class */ (function (_super) {
__extends(GradientAtlas, _super);
function GradientAtlas() {
var _this = _super.call(this, 256, 256, true) || this;
/* internal*/ _this._data = new Uint8Array(4 * 256 * 256);
_this._gradMap = {};
_this._count = 0;
return _this;
}
Object.defineProperty(GradientAtlas.prototype, "assetType", {
get: function () {
return GradientAtlas.assetType;
},
enumerable: false,
configurable: true
});
GradientAtlas.getAtlassForHash = function (hash, findEmpty) {
if (findEmpty === void 0) { findEmpty = true; }
var atlass;
for (var _i = 0, _a = this.gradientAtlases; _i < _a.length; _i++) {
var at = _a[_i];
if (at.hasGradient(hash)) {
atlass = at;
break;
}
}
if (atlass) {
return atlass;
}
if (!findEmpty) {
return null;
}
for (var _b = 0, _c = this.gradientAtlases; _b < _c.length; _b++) {
var at = _c[_b];
if (!at.full) {
return at;
}
}
atlass = new GradientAtlas();
this.gradientAtlases.push(atlass);
return atlass;
};
GradientAtlas.computeHash = function (colors, alphas, ratios) {
return colors.join('') + alphas.map(function (e) { return e * 0xff | 0; }) + ratios.join('');
};
Object.defineProperty(GradientAtlas.prototype, "full", {
get: function () {
return this._count === 256;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GradientAtlas.prototype, "length", {
get: function () {
return this._count;
},
enumerable: false,
configurable: true
});
GradientAtlas.prototype.hasGradient = function (hash) {
return (hash in this._gradMap);
};
GradientAtlas.prototype.getGradient = function (hash) {
return this._gradMap[hash];
};
GradientAtlas.prototype.setGradient = function (colors, alphas, ratios) {
if (!colors || !alphas || !ratios) {
return null;
}
// simple validation for invalid ratios
if (ratios.length === 0) {
return;
}
var hash = GradientAtlas.computeHash(colors, alphas, ratios);
if (hash in this._gradMap) {
return this._gradMap[hash];
}
if (this._count === 256) {
// overflow
return null;
}
var index = this._count;
this._count++;
this.fillRow(colors, alphas, ratios, index);
return this._gradMap[hash] = {
index: index,
hash: hash
};
};
GradientAtlas.prototype.fillRow = function (colors, alphas, ratios, index) {
if (ratios[0] !== 0 || ratios[ratios.length] !== 255) {
colors = colors.slice();
ratios = ratios.slice();
alphas = alphas.slice();
if (ratios[0] !== 0) {
ratios.unshift(0);
colors.unshift(colors[0]);
alphas.unshift(alphas[0]);
}
if (ratios[ratios.length - 1] !== 255) {
ratios.push(255);
colors.push(colors[colors.length - 1]);
alphas.push(alphas[alphas.length - 1]);
}
}
var len = ratios.length;
var colorA = [0, 0, 0, 0];
var colorB = [0, 0, 0, 0];
var buff = new Uint8Array(this._data.buffer, 256 * 4 * index, 256 * 4);
buff.fill(0);
for (var i = 0; i < len; i++) {
// we should end pallete, because ratios can not end with 255
FilterUtils.colorToU8(colors[i + 0], alphas[i + 0], colorA);
FilterUtils.colorToU8(colors[i + 1], alphas[i + 1], colorB);
var from = ratios[i + 0];
var to = ratios[i + 1];
for (var j = from; j <= to; j++) {
var factor = (j - from) / (to - from);
var k = j * 4;
// interpolate each color
buff[k + 0] = colorA[0] + (colorB[0] - colorA[0]) * factor | 0;
buff[k + 1] = colorA[1] + (colorB[1] - colorA[1]) * factor | 0;
buff[k + 2] = colorA[2] + (colorB[2] - colorA[2]) * factor | 0;
buff[k + 3] = colorA[3] + (colorB[3] - colorA[3]) * factor | 0;
var a = buff[k + 3] / 0xff;
// PMA
buff[k + 0] = buff[k + 0] * a | 0;
buff[k + 1] = buff[k + 1] * a | 0;
buff[k + 2] = buff[k + 2] * a | 0;
}
}
this.invalidate();
};
GradientAtlas.assetType = '[image GradientAtlas]';
GradientAtlas.gradientAtlases = [];
return GradientAtlas;
}(Image2D));
export { GradientAtlas };
var _Stage_GradientAtlass = /** @class */ (function (_super) {
__extends(_Stage_GradientAtlass, _super);
function _Stage_GradientAtlass() {
return _super !== null && _super.apply(this, arguments) || this;
}
_Stage_GradientAtlass.prototype.getTexture = function () {
_super.prototype.getTexture.call(this);
if (this._invalid) {
this._invalid = false;
this._texture.uploadFromArray(this.image._data, 0, false);
}
return this._texture;
};
return _Stage_GradientAtlass;
}(_Stage_Image2D));
export { _Stage_GradientAtlass };
// MOVED TO LIB INDEX
// Stage.registerAbstraction(_Stage_GradientAtlass, GradientAtlass);