melonJS
    Preparing search index...

    Function getShader

    • Return the precompiled ShaderEffect for the given "shader" asset — compiled once during preloading, ready to assign to a renderable or camera shader property.

      This returns a SHARED instance: the same ShaderEffect object on every call, owned by the loader (its shared flag is true). That means:

      • it is safe to assign to any number of renderables — none of their cleanup paths will auto-destroy it, only loader.unload / loader.unloadAll free it (and its GL program);
      • all of them share ONE set of uniform values — setUniform on it affects every renderable using the shader.

      When a renderable needs its own uniform values, make a private, caller-owned copy with ShaderEffect.clone() — the clone's shared flag is reset to false, so it is auto-destroyed with the renderable it is assigned to, like any hand-constructed effect.

      A shader asset declared as a {vertex, fragment} program pair (see the example) compiles into a raw GLShader instead — the type the advanced paths take directly (a Mesh custom shader, renderer.customShader, a custom batcher). Same shared-instance semantics, and GLShader.clone() likewise yields a caller-owned copy.

      Shader assets are WebGL-only: under the Canvas renderer a fragment-body asset returns an inert ShaderEffect stub (same behavior as constructing one directly), and a program-pair asset returns null (a raw GL program has no canvas analog). Note that shader assets require video.init() to have been called — an inherent precondition of the preload flow, since the loading screen itself needs the renderer.

      Parameters

      • elt: string

        name of the shader asset (as specified in the preload list)

      Returns any

      the shared, precompiled shader, or null if not found

      loader

      me.loader.preload([
      // from a file (or data: URI)
      { name: "waterRipple", type: "shader", src: "shaders/waterRipple.frag" },
      // or inline GLSL via the `data` field
      { name: "flash", type: "shader", data: `
      uniform float uIntensity;
      vec4 apply(vec4 color, vec2 uv) { return mix(color, vec4(1.0), uIntensity); }
      ` },
      // or a complete {vertex, fragment} program pair → a raw GLShader
      { name: "toonMesh", type: "shader", src: {
      vertex: "shaders/toon.vert",
      fragment: "shaders/toon.frag",
      } },
      ], () => {
      // one shared program — same uniform state for every user
      mySprite.shader = me.loader.getShader("waterRipple");
      // private copy with its own uniforms (caller-owned, shared = false)
      boss.shader = me.loader.getShader("flash").clone();
      // a program pair comes back as a GLShader, e.g. for a custom Mesh shader
      renderer.drawMesh(myMesh, { shader: me.loader.getShader("toonMesh") });
      });