ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
58 lines (57 loc) • 3.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Texture_1 = require("./Texture");
const TEMPORARY_TEXTURE_2D_NAME = "_TEMPORARY_TEXTURE_2D_";
class Texture2D extends Texture_1.Texture {
constructor(scene, url, pixelData) {
super(scene);
this.pipelineState.BindTexture(3553 /* Texture2D */, this, 0);
if (pixelData !== undefined) {
this.context.texImage2D(WebGL2RenderingContext.TEXTURE_2D, 0, WebGL2RenderingContext.RGBA, 1, 1, 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, pixelData);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR);
}
else if (url !== undefined) {
// Get temporary texture while loading
this.context.texImage2D(WebGL2RenderingContext.TEXTURE_2D, 0, WebGL2RenderingContext.RGBA, 1, 1, 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, new Uint8Array([255, 255, 255, 255]));
const image = new Image();
image.addEventListener("load", (ev) => {
this.pipelineState.BindTexture(3553 /* Texture2D */, this, 0);
this.context.texImage2D(WebGL2RenderingContext.TEXTURE_2D, 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, image);
if (this.IsPowerOf2(image.width) && this.IsPowerOf2(image.height)) {
this.context.generateMipmap(WebGL2RenderingContext.TEXTURE_2D);
}
else {
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_2D, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR);
}
});
image.src = url;
}
else {
throw new Error("Both url and pixelData parameters can't be undefined.");
}
}
// Texture Manager --------------------------------------------------------------------------------------------------------
static Get(scene, url) {
let key = url;
let texture = this.textures.get(key);
if (texture === undefined) {
texture = new Texture2D(scene, url, undefined);
this.textures.set(key, texture);
}
return texture;
}
static GetBlank(scene) {
let key = TEMPORARY_TEXTURE_2D_NAME;
let texture = this.textures.get(key);
if (texture === undefined) {
texture = new Texture2D(scene, undefined, new Uint8Array([255, 255, 255, 255]));
this.textures.set(key, texture);
}
return texture;
}
}
exports.Texture2D = Texture2D;