Class ScreenEffects

java.lang.Object
net.ixdarklord.coolcatcanvas.api.client.effect.ScreenEffects

public final class ScreenEffects extends Object
Full-screen post-processing effects: color grading, distortion, blur, and whatever else a fragment shader can do to the finished frame, with uniforms that can change every frame. Every effect is a ScreenEffect: it fades in and out, can be pulsed, driven by a condition or by the server, and stacks with any number of others.

A tint, with no assets

The one effect CoolCatLib ships. Call tint(Identifier, int) right where it's needed:
ScreenEffects.tint(MyMod.id("hurt"), 0x59FF0000).enableFor(5);    // a red flash
ScreenEffects.tint(MyMod.id("frost"), 0x593366FF).enable();       // stays until disable()

A vanilla effect

Vanilla's post effects register by their id. They ignore the effect's strength, so autoBlend fades them:
ScreenEffect invert = ScreenEffects.register(MyMod.id("invert"), Identifier.withDefaultNamespace("invert")).autoBlend(true);
invert.enableFor(40);

Your own shader

A mod brings its own post_effect JSON and fragment shader (see ScreenEffectDefinition). Register it once, from client code (a ClientModConstructor is a good place), then turn it on and off as the game goes, or let it run itself:
// assets/mymod/post_effect/desaturate.json, drawing assets/mymod/shaders/post/desaturate.fsh
ScreenEffect insanity = ScreenEffects.register(MyMod.id("insanity"), MyMod.id("desaturate"))
        .fade(40)
        .activeWhen(context -> context.inWorld() && Sanity.of(context.player()) > 0.4F)
        .strength(context -> Mth.inverseLerp(Sanity.of(context.player()), 0.4F, 0.8F))
        .setUniform("Contrast", 1.4F);
Effects start disabled, and draw over the world unless moved to ScreenEffectScope.SCREEN. The server can drive a player's effects too, tints included, through ScreenEffectControl.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final net.minecraft.resources.Identifier
    The built-in bloom definition: bright parts of the frame glow into what is around them.
    static final String
    How strong the glow is (0.9 by default).
    static final String
    How far the glow spreads, in pixels a blur step (2 by default).
    static final String
    How bright a pixel must be to glow, 0 to 1 (0.72 by default): lower lights up more of the frame.
    static final net.minecraft.resources.Identifier
    The built-in tint definition: washes the frame in its Color uniform, a vec4 whose alpha is how far the frame is mixed towards the color (scaled by the effect's strength).
    static final String
    The TINT definition's color uniform.
  • Method Summary

    Modifier and Type
    Method
    Description
    all()
     
    bloom(net.minecraft.resources.Identifier id, float intensity)
    Bloom over the world, with nothing to set up: the first call for an id makes it, later ones set its strength and hand back the same effect.
    static net.minecraft.client.gui.screens.Screen
    createScreen(@Nullable net.minecraft.client.gui.screens.Screen parent)
    The effects screen: every selectable effect with a switch, the active ones as reorderable layers with a strength slider each, previewed live over the game.
    static void
    Hides every effect at once, e.g.
    get(net.minecraft.resources.Identifier id)
     
    The order effects are drawn in.
    static void
    Opens createScreen(Screen) over the current screen.
    register(net.minecraft.resources.Identifier id, ScreenEffectDefinition definition)
    Registers an effect from a definition built in code.
    register(net.minecraft.resources.Identifier id, net.minecraft.resources.Identifier definition)
    Registers an effect loaded from assets/<namespace>/post_effect/<path>.json, a vanilla one included.
    tint(net.minecraft.resources.Identifier id, int argb)
    A tint over the screen, with nothing to set up: the first call for an id makes the tint, later ones recolor it and hand back the same effect, so this can be called right where the tint is needed.
    static boolean
    unregister(net.minecraft.resources.Identifier id)
    Removes an effect and frees its GPU resources.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TINT

      public static final net.minecraft.resources.Identifier TINT
      The built-in tint definition: washes the frame in its Color uniform, a vec4 whose alpha is how far the frame is mixed towards the color (scaled by the effect's strength). See tint(Identifier, int).
    • TINT_COLOR

      public static final String TINT_COLOR
      The TINT definition's color uniform.
      See Also:
    • BLOOM

      public static final net.minecraft.resources.Identifier BLOOM
      The built-in bloom definition: bright parts of the frame glow into what is around them. See bloom(Identifier, float).
    • BLOOM_THRESHOLD

      public static final String BLOOM_THRESHOLD
      How bright a pixel must be to glow, 0 to 1 (0.72 by default): lower lights up more of the frame.
      See Also:
    • BLOOM_INTENSITY

      public static final String BLOOM_INTENSITY
      How strong the glow is (0.9 by default).
      See Also:
    • BLOOM_RADIUS

      public static final String BLOOM_RADIUS
      How far the glow spreads, in pixels a blur step (2 by default).
      See Also:
  • Method Details

    • bloom

      public static ScreenEffect bloom(net.minecraft.resources.Identifier id, float intensity)
      Bloom over the world, with nothing to set up: the first call for an id makes it, later ones set its strength and hand back the same effect. World effects, fire, lava, the sun and anything else bright glow softly.
      ScreenEffects.bloom(MyMod.id("glow"), 0.9F).enable();                       // on until disable()
      ScreenEffects.bloom(MyMod.id("blast"), 2.5F).fade(10).enableFor(4);        // a surge of light that dies away
      ScreenEffects.bloom(MyMod.id("glow"), 1.2F).setUniform(ScreenEffects.BLOOM_THRESHOLD, 0.55F);
      
      It is a ScreenEffect like any other. Bloom costs six full-screen passes while it is visible, so leave it off when nothing needs it.
      Parameters:
      intensity - how strong the glow is; around 1 is gentle, 2 and up is a haze
      Throws:
      IllegalArgumentException - if the id belongs to an effect that isn't a bloom
    • tint

      public static ScreenEffect tint(net.minecraft.resources.Identifier id, int argb)
      A tint over the screen, with nothing to set up: the first call for an id makes the tint, later ones recolor it and hand back the same effect, so this can be called right where the tint is needed.
      ScreenEffects.tint(MyMod.id("hurt"), 0x59FF0000).enableFor(5);    // a red flash
      ScreenEffects.tint(MyMod.id("frost"), 0x593366FF).enable();       // stays until disable()
      
      // Over the HUD and menus too, with a slow fade.
      ScreenEffects.tint(MyMod.id("blackout"), 0xE6000000).scope(ScreenEffectScope.SCREEN).fade(40).enable();
      
      // Set up once, then it runs by itself: redder the lower the player's health.
      ScreenEffects.tint(MyMod.id("low_health"), 0x66FF0000)
              .activeWhen(context -> context.inWorld() && context.player().getHealth() < 8.0F)
              .strength(context -> 1.0F - context.player().getHealth() / 8.0F);
      
      Each id is its own effect, with its own color and state. It's a ScreenEffect like any other (fades, strength, conditions), drawn over the world unless moved to ScreenEffectScope.SCREEN. Unlike registered effects it starts out of the effects screen; selectable(true) lists it. To change the color smoothly instead of at once, animate its TINT_COLOR uniform.
      Parameters:
      argb - the color; its alpha is how strong the tint is at full strength
      Throws:
      IllegalArgumentException - if the id belongs to an effect that isn't a tint
    • register

      public static ScreenEffect register(net.minecraft.resources.Identifier id, net.minecraft.resources.Identifier definition)
      Registers an effect loaded from assets/<namespace>/post_effect/<path>.json, a vanilla one included. It's loaded with the game's resources, and reloaded with them.
      Throws:
      IllegalArgumentException - if the id is taken
    • register

      public static ScreenEffect register(net.minecraft.resources.Identifier id, ScreenEffectDefinition definition)
      Registers an effect from a definition built in code. Its shaders still come from resources.
      Throws:
      IllegalArgumentException - if the id is taken
    • get

      public static Optional<ScreenEffect> get(net.minecraft.resources.Identifier id)
    • all

      public static Collection<ScreenEffect> all()
    • unregister

      public static boolean unregister(net.minecraft.resources.Identifier id)
      Removes an effect and frees its GPU resources.
    • disableAllInstantly

      public static void disableAllInstantly()
      Hides every effect at once, e.g. for a cutscene or a clean screenshot.
    • layers

      public static ScreenEffectLayers layers()
      The order effects are drawn in.
    • createScreen

      public static net.minecraft.client.gui.screens.Screen createScreen(@Nullable net.minecraft.client.gui.screens.Screen parent)
      The effects screen: every selectable effect with a switch, the active ones as reorderable layers with a strength slider each, previewed live over the game. Players' choices there are remembered across sessions. Also bound to a key (unset by default), and in a development environment /coolcatlib_client effects.
    • openScreen

      public static void openScreen()
      Opens createScreen(Screen) over the current screen.