56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
export class Texture {
|
|
constructor(gl, width = 1, height = 1) {
|
|
this.gl = gl;
|
|
this.width = width;
|
|
this.height = height;
|
|
this.handle = gl.createTexture();
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, this.handle);
|
|
gl.texImage2D(
|
|
gl.TEXTURE_2D,
|
|
0,
|
|
gl.RGBA,
|
|
1,
|
|
1,
|
|
0,
|
|
gl.RGBA,
|
|
gl.UNSIGNED_BYTE,
|
|
new Uint8Array([255, 255, 255, 255]),
|
|
);
|
|
this._configureSampling();
|
|
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
}
|
|
|
|
uploadImage(image) {
|
|
const { gl } = this;
|
|
this.width = image.width;
|
|
this.height = image.height;
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, this.handle);
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
}
|
|
|
|
uploadCanvas(canvas) {
|
|
const { gl } = this;
|
|
this.width = canvas.width;
|
|
this.height = canvas.height;
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, this.handle);
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
|
|
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
}
|
|
|
|
_configureSampling() {
|
|
const { gl } = this;
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
}
|
|
|
|
dispose() {
|
|
this.gl.deleteTexture(this.handle);
|
|
this.handle = null;
|
|
}
|
|
} |