wggl
Version:
A friendly interface to shaders
58 lines (57 loc) • 2.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var primitives_1 = require("./primitives");
var Texture = /** @class */ (function () {
function Texture(canvas, width, height, pixels, props) {
if (pixels === void 0) { pixels = new Uint8Array(0); }
this.canvas = canvas;
this.width = width;
this.height = height;
this.pixels = pixels;
this.format = primitives_1.PixelFormat.RGBA;
this.type = primitives_1.PixelType.UNSIGNED_BYTE;
this.wrap = primitives_1.TextureWrap.CLAMP_TO_EDGE;
this.filter = primitives_1.TextureFilter.NEAREST;
if (props != null) {
this.format = props.format || this.format;
this.type = props.type || this.type;
this.wrap = props.wrap || this.wrap;
this.filter = props.filter || this.filter;
}
this.gl = canvas.getContext("webgl");
if (!this.gl) {
console.warn("No WebGL support");
return;
}
this.texture = createTexture(this.gl, this.wrap, this.filter);
this.from(this.pixels);
}
Texture.prototype.from = function (newPixels) {
if (newPixels === void 0) { newPixels = new Uint8Array(0); }
if (newPixels instanceof WebGLTexture) {
this.texture = newPixels;
return;
}
newPixels = this.pixels;
var gl = this.gl;
var format = gl[this.format];
var type = gl[this.type];
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, format, this.width, this.height, 0, format, type, null);
if (newPixels) {
gl.texImage2D(gl.TEXTURE_2D, 0, format, this.width, this.height, 0, format, type, newPixels);
}
};
return Texture;
}());
exports.Texture = Texture;
function createTexture(gl, wrap, filter) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl[wrap]);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl[wrap]);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl[filter]);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl[filter]);
return texture;
}