Class GLSLShaderRenderer

The GLSLShaderRenderer class is responsible for managing WebGL rendering, including shader programs, textures, and render targets. It provides methods for adding and updating buffers, rendering scenes, and managing resources.

Constructors

  • Creates a new GLSLShaderRenderer.

    Parameters

    • canvas: HTMLCanvasElement

      The canvas element to render to.

    • v: string

      The vertex shader code.

    • f: string

      The fragment shader code.

    • cU: any = {}

      Optional custom uniforms.

    Returns GLSLShaderRenderer

Properties

buffer: WebGLBuffer
canvas: HTMLCanvasElement

The canvas element to render to.

cU: any = {}

Optional custom uniforms.

deltaTime: number = 0
entity: any
frameCount: number = 0
gl: WebGLRenderingContext
header: string = ...
mainProgram: WebGLProgram
mainUniforms: Map<string, WebGLUniformLocation>
programs: Map<string, {
    program: null | WebGLProgram;
    state: boolean;
}>
screenVertexPosition: number
surfaceBuffer: WebGLBuffer
targets: Map<string, RenderTarget>
textureCache: Map<string, ITexture>
vertexPosition: number = 0

Methods

  • Adds assets (textures) to the renderer.

    Parameters

    • assets: any

      An object containing texture data.

    • cb: ((r?: any) => void)

      A callback function to be called after the assets are loaded.

        • (r?): void
        • Parameters

          • Optionalr: any

          Returns void

    Returns this

    The GLSLShaderRenderer instance for chaining.

  • Adds a buffer (shader program) to the renderer.

    Parameters

    • name: string

      The name of the buffer.

    • vertex: string

      The vertex shader code.

    • fragment: string

      The fragment shader code.

    • Optionaltextures: string[]

      An optional array of texture names.

    • OptionalcustomUniforms: any

      An optional object containing custom uniform functions.

    Returns GLSLShaderRenderer

    The GLSLShaderRenderer instance for chaining.

  • Creates   a new WebGLProgram and adds it to the list of programs.

    Parameters

    • name: string

      The name of the program.

    Returns WebGLProgram

    The created WebGLProgram.

  • Creates a shader of the specified type and attaches it to the program.

    Parameters

    • program: WebGLProgram

      The WebGLProgram to attach the shader to.

    • type: number

      The type of shader (gl.VERTEX_SHADER or gl.FRAGMENT_SHADER).

    • source: string

      The shader source code.

    Returns void

    Error if the shader compilation fails.

  • Creates a render target.

    Parameters

    • width: number

      The width of the render target.

    • height: number

      The height of the render target.

    • textures: string[]

      An array of texture names to use in the render target.

    • customUniforms: any

      An object containing custom uniform functions.

    Returns RenderTarget

    The created RenderTarget object.

  • Creates a new WebGLTexture.

    Parameters

    • data: Uint8Array | HTMLImageElement

      The image or Uint8Array data for the texture.

    • d: number

      The texture unit index.

    Returns WebGLTexture

    The created WebGLTexture.

  • Creates a cube map texture.

    Parameters

    • sources: any[]

      An array of image sources for the cube map faces.

    • d: number

      The texture unit index.

    Returns WebGLTexture

    The created WebGLTexture.

  • Starts the rendering loop with a fixed timestep.

    Parameters

    • t: number

      The initial time in milliseconds.

    • fps: number

      The desired frames per second.

    Returns this

    The GLSLShaderRenderer instance for chaining.

  • Sets the entity associated with this renderer.

    Parameters

    • entity: any

      The entity to associate with the renderer.

    Returns void

  • Sets the state of a shader program.

    Parameters

    • key: string

      The name of the shader program.

    • state: boolean

      Whether the program should be enabled or disabled.

    Returns void

  • Updates the renderer and executes all shader programs.

    Parameters

    • time: number

      The current time in seconds.

    Returns void

  • Generate a GLSLShaderRenderer

    Parameters

    • mainVertex: string
    • mainFrag: string
    • textureVertex: string
    • textureFrag: string
    • w: number
    • h: number

    Returns HTMLCanvasElement

    {HTMLCanvasElement}

    GLSLShaderRenderer