melonJS
    Preparing search index...

    Class GLShader

    a base GL Shader object

    Index

    Constructors

    • Parameters

      • gl: WebGLRenderingContext

        the current WebGL rendering context

      • vertex: string

        a string containing the GLSL source code to set

      • fragment: string

        a string containing the GLSL source code to set

      • Optionalprecision: string

        float precision ('lowp', 'mediump' or 'highp').

      Returns GLShader

      // create a basic shader
      let myShader = new me.GLShader(
      // WebGL rendering context
      gl,
      // vertex shader
      [
      "void main() {",
      " gl_Position = doMathToMakeClipspaceCoordinates;",
      "}"
      ].join("\n"),
      // fragment shader
      [
      "void main() {",
      " gl_FragColor = doMathToMakeAColor;",
      "}"
      ].join("\n")
      )
      // use the shader
      myShader.bind();

    Properties

    _precision: string | undefined
    _sourceFragment: string
    _sourceVertex: string
    _uniformCache: any
    attributes: number[]

    the location attributes of the shader

    destroyed: boolean

    true once destroy has been called. After this flag is true, every method on the shader is a silent no-op — callers holding a stale reference (e.g. a still-registered update loop) do not crash the frame.

    fragment: any
    gl: WebGLRenderingContext

    the active gl rendering context

    program: WebGLProgram

    a reference to the shader program (once compiled)

    shared: boolean

    When true, a renderable will NOT auto-destroy this shader when it is removed from its postEffects (via the shader setter, removePostEffect, clearPostEffects) or when the renderable itself is destroyed. Set this on a shader shared across several renderables so one of them going away doesn't free the GL program still used by the others — you then own its lifecycle and call destroy yourself.

    false
    
    suspended: boolean

    true while the WebGL context is lost (and until it's restored). The GL program/attributes/uniforms are released during the suspended window but the shader source code is preserved so _onContextRestored can rebuild the program against the new context. User code generally doesn't read this — methods short-circuit internally — but it's available for diagnostic / debug-plugin tooling.

    uniforms: object

    the uniforms of the shader

    vertex: any

    Methods

    • Create an independent copy of this shader, compiled as its own GL program from the same vertex + fragment sources (and precision), with every uniform value set so far replayed onto the copy. Use it when several renderables need the same custom shader with different uniform values — a single instance has a single set of uniforms.

      Ownership and lifecycle state do NOT carry over: the clone's shared flag is always reset to false, even when cloning a shared shader — the clone is caller-owned and will be auto-destroyed by the renderable it is assigned to, unless you explicitly set shared = true on it yourself. Note that ShaderEffect (which wraps a GLShader by composition) has its own clone() with the same semantics, which additionally copies its effect-level state (extra setTexture bindings).

      Returns GLShader

      a new, caller-owned shader (shared === false)

    • destroy this shader objects resources (program, attributes, uniforms). Idempotent — calling destroy twice (or after a context-lost suspend) is safe. Unsubscribes from the renderer's context lost / restored events so a destroyed shader is never automatically resurrected.

      Returns void

    • returns the location of an attribute variable in this shader program

      Parameters

      • name: string

        the name of the attribute variable whose location to get.

      Returns number

      number indicating the location of the variable name if found. Returns -1 otherwise

    • Set the uniform to the given value

      Parameters

      • name: string

        the uniform name

      • value: object | Float32Array<ArrayBufferLike>

        the value to assign to that uniform

      Returns void

      myShader.setUniform("uProjectionMatrix", this.projectionMatrix);
      
    • activate the given vertex attribute for this shader

      Parameters

      • gl: WebGLRenderingContext

        the current WebGL rendering context

      • attributes: object[]

        an array of vertex attributes

      • stride: number

        the size of a single vertex in bytes

      Returns void