Skip to main content

createShader()

function createShader(
gl,
type,
source):
| WebGLShader
| null;

Creates and compiles a WebGL shader from source code.

Parameters

ParameterTypeDescription
glWebGL2RenderingContextThe WebGL2 rendering context.
typenumberShader type (gl.VERTEX_SHADER or gl.FRAGMENT_SHADER).
sourcestringGLSL source code for the shader.

Returns

| WebGLShader | null

The compiled WebGLShader instance, or null if compilation failed.

Example

const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);