melonJS
    Preparing search index...

    Function getShader

    • Get the shader compiled from a loaded "shader" asset — compiled once, at load time, ready to add to a renderable or a camera as a post effect.

      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 complete program — a {vertex, fragment} GLSL pair and/or a full wgsl module (see the example) — compiles into a raw GLShader instead, carrying one realization per GPU backend (isWebGL / isWebGPU): the type the hosted 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.

      Unloading the asset destroys the shared instance, under whatever still draws with it: with bundles, an effect that outlives a level (a HUD's, a camera's) belongs in a bundle that outlives that level, such as "common". A clone() is the caller's own and is not destroyed with it.

      Degradation is never fatal: a fragment-body asset without a body in the active renderer's language (or on Canvas) is an inert ShaderEffect stub, and a complete-program asset without a realization for the active backend is an inert GLShader — assigning either just keeps the built-in rendering. Note that shader assets require an initialized Application (await app.init()) — an inherent precondition of the preload flow, since the loading screen itself needs the renderer.

      Parameters

      • elt: string

        the asset name

      Returns ShaderEffect | GLShader | null

      the shared, precompiled shader, or null if none is loaded under that name

      await loader.preload([
      // from a file (or a data: URI)
      { name: "waterRipple", type: "shader", src: "shaders/waterRipple.frag" },
      // or inline GLSL, through 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 program: a {vertex, fragment} GLSL pair and/or a full
      // WGSL module, compiled into one GLShader; the active renderer hosts
      // the one it speaks
      { name: "toonMesh", type: "shader", src: {
      vertex: "shaders/toon.vert",
      fragment: "shaders/toon.frag",
      wgsl: "shaders/toon.wgsl",
      } },
      ]);

      // one shared program: the same uniform values for every user
      mySprite.addPostEffect(loader.getShader("waterRipple"));
      // a private copy with its own uniforms (caller-owned, shared = false)
      boss.addPostEffect(loader.getShader("flash").clone());
      // a complete program hosts on a mesh, replacing the built-in shading
      myMesh.addPostEffect(loader.getShader("toonMesh"));