ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
98 lines (97 loc) • 3.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const gl_matrix_1 = require("gl-matrix");
exports.TEXTURE_UNIT_AMOUNT = 16;
class PipelineState {
constructor(context) {
this.clearColor = gl_matrix_1.vec4.fromValues(0, 0, 0, 0);
this.clearDepth = 1;
this.depthTest = false;
this.depthFunction = 513 /* Less */;
this.faceCulling = false;
this.faceCullingMode = 1029 /* Back */;
this.currentVAO = null;
this.currentTextureUnit = 0;
this.textureUnits = new Array(exports.TEXTURE_UNIT_AMOUNT);
this.context = context;
}
get ClearColor() { return this.clearColor; }
set ClearColor(clearColor) {
if (gl_matrix_1.vec4.exactEquals(this.clearColor, clearColor))
return;
this.clearColor = clearColor;
this.context.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
}
get ClearDepth() { return this.clearDepth; }
set ClearDepth(clearDepth) {
if (this.clearDepth === clearDepth)
return;
this.clearDepth = clearDepth;
this.context.clearDepth(clearDepth);
}
get DepthTest() { return this.depthTest; }
set DepthTest(depthTest) {
if (this.depthTest === depthTest)
return;
this.depthTest = depthTest;
if (depthTest)
this.context.enable(this.context.DEPTH_TEST);
else
this.context.disable(this.context.DEPTH_TEST);
}
get DepthFunction() { return this.depthFunction; }
set DepthFunction(depthFunction) {
if (this.depthFunction === depthFunction)
return;
this.depthFunction = depthFunction;
this.context.depthFunc(depthFunction);
}
get FaceCulling() { return this.faceCulling; }
set FaceCulling(faceCulling) {
if (this.faceCulling === faceCulling)
return;
this.faceCulling = faceCulling;
if (faceCulling)
this.context.enable(this.context.CULL_FACE);
else
this.context.disable(this.context.CULL_FACE);
}
get FaceCullingMode() { return this.faceCullingMode; }
set FaceCullingMode(faceCullingMode) {
if (this.faceCullingMode === faceCullingMode)
return;
this.faceCullingMode = faceCullingMode;
this.context.cullFace(faceCullingMode);
}
get CurrentShader() { return this.currentShader; }
set CurrentShader(shader) {
if (this.currentShader === shader)
return;
this.currentShader = shader;
if (shader)
this.context.useProgram(shader.Program);
else
this.context.useProgram(null);
}
get CurrentVAO() { return this.currentVAO; }
set CurrentVAO(vao) {
if (this.currentVAO === vao)
return;
this.currentVAO = vao;
this.context.bindVertexArray(vao);
}
SetCurrentTextureUnit(textureUnit) {
if (this.currentTextureUnit === textureUnit)
return;
this.currentTextureUnit = textureUnit;
this.context.activeTexture(WebGL2RenderingContext.TEXTURE0 + textureUnit);
}
BindTexture(type, texture, textureUnit) {
if (this.textureUnits[textureUnit] === texture)
return;
this.textureUnits[textureUnit] = texture;
this.SetCurrentTextureUnit(textureUnit);
this.context.bindTexture(type, texture.Texture);
}
}
exports.PipelineState = PipelineState;