UNPKG

nidza

Version:

Nidza.js is ultimate canvas2d+3d solution (Easy port any webgl or webgl2 script). In context of 3d graphics it is used for GLSL manipulation. Objective is low CPU usage price.

136 lines (124 loc) 4.53 kB
export class BaseShader { constructor() { console.log("SHADER BASE CLASS"); } /** * Creates and compiles a shader. * * @param {!WebGLRenderingContext} gl The WebGL Context. * @param {string} shaderSource The GLSL source code for the shader. * @param {number} shaderType The type of shader, VERTEX_SHADER or * FRAGMENT_SHADER. * @return {!WebGLShader} The shader. */ compileShader(gl, shaderType, shaderSource) { // Create the shader object var shader = gl.createShader(shaderType); // Set the shader source code. gl.shaderSource(shader, shaderSource); // Compile the shader gl.compileShader(shader); // Check if it compiled var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); if (!success) { // Something went wrong during compilation; get the error throw "could not compile shader:" + gl.getShaderInfoLog(shader); } return shader; } /** * Creates a program from 2 shaders. * * @param {!WebGLRenderingContext) gl The WebGL context. * @param {!WebGLShader} vertexShader A vertex shader. * @param {!WebGLShader} fragmentShader A fragment shader. * @return {!WebGLProgram} A program. */ createProgram(gl, vertexShader, fragmentShader) { // create a program. var program = gl.createProgram(); // attach the shaders. gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); // link the program. gl.linkProgram(program); // Check if it linked. var success = gl.getProgramParameter(program, gl.LINK_STATUS); if (!success) { // something went wrong with the link throw "program failed to link:" + gl.getProgramInfoLog(program); } return program; } /** * Creates a shader from the content of a script tag. * * @param {!WebGLRenderingContext} gl The WebGL Context. * @param {string} scriptId The id of the script tag. * @param {string} opt_shaderType. The type of shader to create. * If not passed in will use the type attribute from the * script tag. * @return {!WebGLShader} A shader. */ createShaderFromScript(gl, scriptId, opt_shaderType) { // look up the script tag by id. var shaderScript = document.getElementById(scriptId); if (!shaderScript) { throw "*** Error: unknown script element" + scriptId; } // extract the contents of the script tag. var shaderSource = shaderScript.text; // If we didn't pass in a type, use the 'type' from // the script tag. if (!opt_shaderType) { if (shaderScript.type == "x-shader/x-vertex") { opt_shaderType = gl.VERTEX_SHADER; } else if (shaderScript.type == "x-shader/x-fragment") { opt_shaderType = gl.FRAGMENT_SHADER; } else if (!opt_shaderType) { throw "*** Error: shader type not set"; } } return compileShader(gl, shaderSource, opt_shaderType); } // init Shader PRogram from inline initShaderProgram(gl, vsSource, fsSource) { const vertexShader = this.compileShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = this.compileShader(gl, gl.FRAGMENT_SHADER, fsSource); // Create the shader program const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); // If creating the shader program failed, alert if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { alert( "Unable to initialize the shader program: " + gl.getProgramInfoLog(shaderProgram) ); return null; } return shaderProgram; } /** * Creates a program from 2 script tags. * @param {!WebGLRenderingContext} gl The WebGL Context. * @param {string[]} shaderScriptIds Array of ids of the script * tags for the shaders. The first is assumed to be the * vertex shader, the second the fragment shader. * @return {!WebGLProgram} A program */ createProgramFromScripts(gl, shaderScriptIds) { var vertexShader = createShaderFromScript( gl, shaderScriptIds[0], gl.VERTEX_SHADER ); var fragmentShader = createShaderFromScript( gl, shaderScriptIds[1], gl.FRAGMENT_SHADER ); return createProgram(gl, vertexShader, fragmentShader); } }