Minecraft Modding: Creating Colored "Fog" Overlay When In Custom Fluids

Background


In Minecraft when a player is submerged in water it puts a bit of a blue color overlay on the whole screen to give an "underwater" effect. Technically when underwater or in the clouds there is also a similar effect. This is called "fog".

Controlling Fog Color Depending On Forge Version

Note: From Build 1.12.2-14.23.0.2521 and beyond, the value of the Fluid.getColor() is used for the fog color so you can just @Override the function to return the value you want. You may still want to change the fog density, which you can do with the FogDensity.

For earlier versions you need to handle the FogColors event. This is explained below.

The Fog Density And Fog Color Event Handlers

Forge has provided the ability to hook using events into the fog rendering and create your own color and density of fog. This is really cool generally for interesting effects. In this tutorial we will use it for when the player is submerged in a custom fluid. 

I assume you have already created custom fluid blocks (see Jabelar's Custom Fluids Tutorial) and are familiar with event handling (see Jabelar's Event Handling Tutorial).

The fog density and fog color events are fairly easy to handle. There is only one trick related to the cancellation of the FogDensity event which can cause trouble, but I explain how to do that below.

It is probably easiest to just show an example:
  
    @SideOnly(Side.CLIENT)
    @SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
    public void onEvent(FogDensity event)
    {
if (event.getEntity().isInsideOfMaterial(ModMaterials.SLIME))
{
event.setDensity(0.5F);
}
else
{
event.setDensity(0.0001F);
}

event.setCanceled(true); // must cancel event for event handler to take effect
    }

    
    @SideOnly(Side.CLIENT)
    @SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
    public void onEvent(FogColors event)
    {
if (event.getEntity().isInsideOfMaterial(ModMaterials.SLIME))
{
Color theColor = Color.GREEN;
event.setRed(theColor.getRed());
     event.setGreen(theColor.getGreen());
     event.setBlue(theColor.getBlue());
}
    }

It is pretty simple. Basically in both events you check if the entity is inside of your material and then set the density and color according to what you want.

Key Point: The fog density ranges between 0 and 1 with 1 representing thick fog that you can't see through at all.

Warning: The FogDensity event must both be canceled (with the setCancelled(true) and must handle the case where the entity is not in your material by setting a very small (but not 0 or negative!) value. Otherwise, it will either not work at all, or it will create a fog always, or it will screw up other fog effects like underwater.


Conclusion


Creating fog effects are pretty easy and fun. I suggest you try using them for special ambience in custom biomes, as part of certain spell effects and so forth. Happy modding!

3 comments:

  1. isInsideOfMaterial can only be for vanilla blocks or material..

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. the onEvent methods have to be static, like 'pulic static void onEvent...'

    ReplyDelete