melonJS
    Preparing search index...

    Class SwayEffect

    A shader effect that leans a sprite from side to side, as if a breeze went through it: each row is drawn a little to the left or right by a sine of the time, and by more the further above its anchor row it is, so the top sways most and everything from the anchor down stays put. For grass, trees, reeds or a whole background layer: anything planted at its bottom.

    Everything is measured across the sprite itself: its strength is a fraction of the sprite's width and its anchor a row of the sprite, so a sprite sways the same whether it has a texture of its own or is cut from an atlas, and whatever else it draws through.

    It is a WaveEffect with an anchor and sway-sized defaults, so it shares its shader rather than adding one. The other difference is the time: SwayEffect#setTime wraps the phase to one turn on the CPU before the shader sees it. A raw clock grows without bound, and a medium-precision float, the default on most phones, runs out of digits a few minutes into a session and the sway starts to stutter; a wrapped phase never grows.

    // a tree whose trunk is planted at 90% of its height
    const sway = new SwayEffect(renderer, { strength: 0.02, anchor: 0.9 });
    tree.addPostEffect(sway);
    // each frame
    sway.setTime(timer.getTime() / 1000);

    Hierarchy (View Summary)

    Index
    • Parameters

      • renderer: Renderer

        the current renderer instance

      • Optionaloptions: { anchor?: number; frequency?: number; speed?: number; strength?: number } = {}

        effect options

        • Optionalanchor?: number

          the row of the sprite that stays put, as a fraction of its height from the top (1 = the bottom row)

        • Optionalfrequency?: number

          how much the sway bends along the height: low values lean the whole sprite as one, high ones ripple it

        • Optionalspeed?: number

          how fast it sways, in radians per second

        • Optionalstrength?: number

          how far the top moves each way, as a fraction of the sprite's width

      Returns SwayEffect

    destroyed: boolean = false

    true once destroy has been called. Distinct from enabled — which also toggles transiently across a context lost / restored cycle — to give callers a stable signal for "this effect has been explicitly released."

    enabled: boolean = false

    whether this effect is active (false in Canvas mode, false after destroy, and false while the WebGL context is suspended between an ONCONTEXT_LOST and the matching ONCONTEXT_RESTORED event).

    shared: boolean = false

    When true, a renderable will NOT auto-destroy this effect when it is removed from its postEffects (via the shader setter, Renderable#removePostEffect, Renderable#clearPostEffects) or when the renderable itself is destroyed. Set this on an effect 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
    
    speed: number

    how fast it sways, in radians per second

    • Bind an extra texture to a named sampler2D uniform in this shader, so a custom effect can read a second texture — a noise map, mask, gradient, flow/lookup table — besides the sprite/target it post-processes (uSampler). The engine uploads, caches, and re-binds it to a reserved texture unit each time the effect draws, and points the sampler uniform at it — no raw WebGL texture-unit juggling.

      Declare the sampler in your fragment (uniform sampler2D <name>;) and pass that name here. Any engine texture works — a Texture2d asset (NoiseTexture2d, TextureAtlas, …) can be passed directly, or a raw drawable source. No-op in Canvas mode.

      Parameters

      • name: string

        the sampler2D uniform name declared in the fragment

      • image: HTMLCanvasElement | HTMLImageElement | OffscreenCanvas | ImageBitmap | Texture2d

        the texture: an engine texture asset, or a raw drawable source

      • Optionalrepeat: "repeat" | "no-repeat" | "repeat-x" | "repeat-y" = "no-repeat"

        wrap mode; use "repeat" for a tiled/scrolled texture

      Returns ShaderEffect

      this effect for chaining

      // "water": distort the sprite by a static noise texture scrolled over time
      const noise = new me.NoiseTexture2d({ width: 256, height: 256, seamless: true });
      const water = new me.ShaderEffect(renderer, `
      uniform sampler2D uNoise;
      uniform float uTime;
      vec4 apply(vec4 color, vec2 uv) {
      vec2 flow = texture2D(uNoise, uv + uTime * 0.03).rg - 0.5;
      return texture2D(uSampler, uv + flow * 0.02);
      }`);
      water.setTexture("uNoise", noise, "repeat");
      waterSprite.addPostEffect(water);
      // each frame, in your Stage's update(dt):
      water.setTime(me.timer.getTime() / 1000);
    • Set the uniform to the given value

      Parameters

      • name: string

        the uniform name

      • value: number | boolean | object | number[] | Float32Array<ArrayBufferLike>

        the value to assign to that uniform. Scalars (float, int, bool) take a number or a boolean; vectors and matrices take an array, a Float32Array, or any object exposing toArray() — which is every Vector2d, Vector3d, Color and Matrix3d.

        Sprites drawn one after the other through the same effect share one draw call. A new value draws the sprites queued before it first, so each sprite draws with the values it was given; the value it already has changes nothing, and keeps the draw shared.

      Returns void

      // a scalar the body declares as `uniform float uStrength;`
      fx.setUniform("uStrength", 0.5);
      // a vec3 — an array, or anything with toArray()
      fx.setUniform("uTint", [1.0, 0.82, 0.55]);
      fx.setUniform("uOrigin", new me.Vector2d(0.5, 0.5));
      // one outline shared by every enemy: drawn in a single call. Marked
      // shared, so the first enemy destroyed does not destroy it for the rest
      const outline = new me.OutlineEffect(renderer, { color: [1, 1, 1] });
      outline.shared = true;
      for (const enemy of enemies) {
      enemy.addPostEffect(outline);
      }