ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
66 lines (65 loc) • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Texture_1 = require("./Texture");
const TEMPORARY_TEXTURE_CUBE_NAME = "_TEMPORARY_TEXTURE_CUBE_";
const CUBE_TEXTURES = 6;
const CUBE_MAP_FACES = [
WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X, WebGL2RenderingContext.TEXTURE_CUBE_MAP_NEGATIVE_X,
WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_Y, WebGL2RenderingContext.TEXTURE_CUBE_MAP_NEGATIVE_Y,
WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_Z, WebGL2RenderingContext.TEXTURE_CUBE_MAP_NEGATIVE_Z
];
class TextureCube extends Texture_1.Texture {
constructor(scene, urls, pixelData) {
super(scene);
this.pipelineState.BindTexture(34067 /* TextureCubeMap */, this, 0);
if (pixelData !== undefined) {
for (let f = 0; f < CUBE_TEXTURES; f++) {
this.context.texImage2D(CUBE_MAP_FACES[f], 0, WebGL2RenderingContext.RGBA, 1, 1, 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, pixelData);
}
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.NEAREST);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.NEAREST);
}
else if (urls !== undefined) {
const u = [urls.positiveX, urls.negativeX, urls.positiveY, urls.negativeY, urls.positiveZ, urls.negativeZ];
for (let f = 0; f < CUBE_TEXTURES; f++) {
this.context.texImage2D(CUBE_MAP_FACES[f], 0, WebGL2RenderingContext.RGBA, 1, 1, 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, new Uint8Array([255, 0, 255, 255]));
const image = new Image();
image.addEventListener("load", (ev) => {
this.pipelineState.BindTexture(34067 /* TextureCubeMap */, this, 0);
this.context.texImage2D(CUBE_MAP_FACES[f], 0, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.RGBA, WebGL2RenderingContext.UNSIGNED_BYTE, image);
//this.context.generateMipmap(WebGL2RenderingContext.TEXTURE_CUBE_MAP);
});
image.src = u[f];
}
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR);
this.context.texParameteri(WebGL2RenderingContext.TEXTURE_CUBE_MAP, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.LINEAR);
}
else {
throw new Error("Both urls and pixelData parameters can't be undefined.");
}
}
// Texture Manager --------------------------------------------------------------------------------------------------------
static Get(scene, urls) {
let key = urls.positiveX + urls.negativeX + urls.positiveY + urls.negativeY + urls.positiveZ + urls.negativeZ;
let texture = this.textures.get(key);
if (texture === undefined) {
texture = new TextureCube(scene, urls, undefined);
this.textures.set(key, texture);
}
return texture;
}
static GetBlank(scene) {
let key = TEMPORARY_TEXTURE_CUBE_NAME;
let texture = this.textures.get(key);
if (texture === undefined) {
texture = new TextureCube(scene, undefined, new Uint8Array([255, 255, 255, 255]));
this.textures.set(key, texture);
}
return texture;
}
}
exports.TextureCube = TextureCube;