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.

A tint, with no assets

The one effect CoolCatLib ships. Call tint(net.minecraft.resources.ResourceLocation, 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()
 

Your own shader

A mod brings its own post effect JSON and shaders (or uses vanilla's, like minecraft:invert). Register effects once, from client code (a ClientModConstructor is a good place), then turn them on and off as the game goes:

 // assets/mymod/shaders/post/desaturate.json, drawing assets/mymod/shaders/program/desaturate.json
 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.ResourceLocation
    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()
     
    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.ResourceLocation id)
     
    The order effects are drawn in.
    static void
    register(net.minecraft.resources.ResourceLocation id, ScreenEffectDefinition definition)
    Registers an effect from a definition built in code.
    register(net.minecraft.resources.ResourceLocation id, net.minecraft.resources.ResourceLocation definition)
    Registers an effect loaded from assets/<namespace>/shaders/post/<path>.json, a vanilla one included (such as minecraft:creeper).
    tint(net.minecraft.resources.ResourceLocation 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.ResourceLocation id)
    Removes an effect and frees its GPU resources.

    Methods inherited from class java.lang.Object

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

    • TINT

      public static final net.minecraft.resources.ResourceLocation 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(net.minecraft.resources.ResourceLocation, int).
    • TINT_COLOR

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

    • tint

      public static ScreenEffect tint(net.minecraft.resources.ResourceLocation 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.ResourceLocation id, net.minecraft.resources.ResourceLocation definition)
      Registers an effect loaded from assets/<namespace>/shaders/post/<path>.json, a vanilla one included (such as minecraft:creeper). 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.ResourceLocation id, ScreenEffectDefinition definition)
      Registers an effect from a definition built in code. Its programs and shaders still come from resources.
      Throws:
      IllegalArgumentException - if the id is taken
    • get

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

      public static Collection<ScreenEffect> all()
    • unregister

      public static boolean unregister(net.minecraft.resources.ResourceLocation 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 @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 /coolcatcanvas_client effects.
    • openScreen

      public static void openScreen()