trtc-electron-sdk
Version:
trtc electron sdk
168 lines (167 loc) • 6.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = __importDefault(require("../../logger"));
const vertex = [
-1, -1, 0,
1, -1, 0.0,
1, 1, 0.0,
-1, 1, 0.0
];
const vertexIndice = [
0, 1, 2,
0, 2, 3
];
const triangleTexCoords = [
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
];
const createGL = function (canvas) {
const errMsg = 'Create WebGL context failed';
let isSuccess = true;
let gl = null;
try {
gl = canvas.getContext('webgl', { alpha: true, premultipliedAlpha: false })
|| canvas.getContext("experimental-webgl", { alpha: true, premultipliedAlpha: false });
}
catch (err) {
logger_1.default.warn('createGL error:', err);
isSuccess = false;
}
if (!isSuccess || !gl) {
throw new Error(errMsg);
}
return gl;
};
const createShader = function (gl, shaderSource, shaderType) {
//compile the vertex shader
const shader = gl.createShader(shaderType);
if (shader) {
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(`Compile shader failed: ${gl.getShaderInfoLog(shader)}`);
}
return shader;
}
else {
throw new Error('create Shader failed');
}
};
const initShaders = function (gl) {
const VSHADER_SOURCE = 'attribute vec3 aPos; ' +
'attribute vec2 aVertexTextureCoord; ' +
'varying mediump vec2 vTextureCoord; ' +
'void main(void){ ' +
' gl_Position = vec4(aPos, 1); ' +
' vTextureCoord = aVertexTextureCoord;' +
'}';
const FSHADER_SOURCE = 'varying mediump vec2 vTextureCoord; ' +
'uniform sampler2D uSampler; ' +
'void main(void) { ' +
' gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)); ' +
'}';
// compile shaders
const vertexShader = createShader(gl, VSHADER_SOURCE, gl.VERTEX_SHADER);
const fragmentShader = createShader(gl, FSHADER_SOURCE, gl.FRAGMENT_SHADER);
const vShader = vertexShader;
const fShader = fragmentShader;
if (!vertexShader || !fragmentShader) {
throw new Error('initialize shader failed');
}
// create program
const glProgram = gl.createProgram();
if (glProgram) {
// attach and link shaders to the program
gl.attachShader(glProgram, vertexShader);
gl.attachShader(glProgram, fragmentShader);
gl.linkProgram(glProgram);
if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)) {
throw new Error('initialize shader program failed');
}
else {
gl.useProgram(glProgram);
return {
vShader,
fShader,
glProgram,
};
}
}
else {
throw new Error('create shader program failed');
}
};
const initUniforms = function (gl, glProgram) {
return gl.getUniformLocation(glProgram, 'uSampler');
};
const initTexture = function (gl) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// c++ addon 层拷贝数据时翻转,这里不做翻转
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
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);
};
const initTextureAttr = function (gl, glProgram) {
// vertex data
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertex), gl.STATIC_DRAW);
// indice data
const vertexIndiceBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndiceBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(vertexIndice), gl.STATIC_DRAW);
// set position attribute
const aVertexPosition = gl.getAttribLocation(glProgram, 'aPos');
gl.vertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aVertexPosition);
// texture coordinate data
const trianglesTexCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, trianglesTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleTexCoords), gl.STATIC_DRAW);
// set texture coordinate attribute
const vertexTexCoordAttribute = gl.getAttribLocation(glProgram, 'aVertexTextureCoord');
gl.enableVertexAttribArray(vertexTexCoordAttribute);
gl.vertexAttribPointer(vertexTexCoordAttribute, 2, gl.FLOAT, false, 0, 0);
};
class RGBACanvas3DRenderer {
constructor(canvas) {
try {
this.gl = createGL(canvas);
const { vShader, fShader, glProgram } = initShaders(this.gl);
const samplerUniform = initUniforms(this.gl, glProgram);
initTextureAttr(this.gl, glProgram);
initTexture(this.gl);
}
catch (err) {
logger_1.default.warn('RGBARenderContextCreator.create() failed.', err);
throw new Error('create WebGL context failed');
}
}
draw(data, width, height) {
try {
if (this.gl) {
this.gl.viewport(0, 0, width, height);
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, width, height, 0, this.gl.RGBA, this.gl.UNSIGNED_BYTE, data);
this.gl.drawElements(this.gl.TRIANGLES, 6, this.gl.UNSIGNED_SHORT, 0);
}
else {
throw new Error('WebGL context is null');
}
}
catch (err) {
logger_1.default.warn('RGBACanvas3DRenderer.draw error:', err);
}
}
destroy() {
this.gl = null;
}
}
exports.default = RGBACanvas3DRenderer;