Add fog when under fluid?

Started by MartBarnav on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Add fog when under fluid?

Hello! I have managed to make a crude oil custom fluid for my mod. I've also made it so when the player is under it, they begin to drown and a dark, almost opaque screen overlay is shown. However, i have realized that the overlay doesn't really give the desired effect. I want an effect more similar to that of lava. For that, i need to make it so that a sort of fog appears when the player is under the fluid. 

For that, i have made a custom mod element, as i haven't really found a way to do this through the MCreator GUI. I am an experienced programmer, just not minecraft modding which is why i'm using MCreator in the first place. I've read through forge documentation trying to figure out how to achieve this, i can't say i really understand what i'm doing because i've never even touched forge or any other minecraft modding tool/library. My problem is that MCreator fails to compile my mod because it cannot find a certain forge package, even though according to the documentation i've read, it should exist.

 

Here is the package that can't be found:

net.minecraftforge.client.event.EntityViewRenderEvent

i need to import that package to do something like this:

@SubscribeEvent
public void onRenderFog(FogDensity event) {
	event.setDensity(1);
}

which according to forge documentation, should set a fog density. A similar method can be used to set a fog color, and from what i've read, i can add in an if statement to check if the player is under the liquid to make the fog only show if that's the case. 

When compiling, i get an error saying that net.minecraftforge.client.event.EntityViewRenderEvent  cannot be found and that by consequence, FogDensity cannot be found either. There is very little information about this on the web, so i'm hoping that someone in here has enough expertise to know what's going on. I've tried clearing my gradle cache and restarting MCreator but the problem persists.

Joined Jul 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
First of all: What Minecraft…
Wed, 07/12/2023 - 15:40

First of all: What Minecraft version are you making the mod for? I'm going to assume you're on 1.19 since I'm having the same problem there and since It's the latest supported version.

Anyways: I'm trying to do something similar, and after a lot of digging through events, it seems like EntityViewRenderEvent has been removed and changed to ViewportEvent.

I think I have mostly figured out the general way that this new system works, however none of the code below actually works, but I'll include it since maybe it's enough for you to figure out how to get it working.

Now, I'm pretty sure this is the general idea of how to change the fog with this:

import net.minecraftforge.client.event.ViewportEvent;
@SubscribeEvent
public void onFogRender(ViewportEvent.RenderFog event) {
event.setNearPlaneDistance(0);
event.setFarPlaneDistance(10);
}

I can't find anything in ViewportEvent for explicitly changing fog density, but the alpha value of fogColor might be what controls the density now:

import com.mojang.blaze3d.systems.RenderSystem;
@SubscribeEvent
public void onFogColor(ViewportEvent.ComputeFogColor event) {
RenderSystem.setShaderFogColor(255, 0, 0, 255); // Last value is alpha which might be the new density
}

Like I said though, I haven't managed to get it working since nothing about the fog changes, but maybe I'm just being stupid.

You can also change all these properties either with event.something() OR RenderSystem.something() (for example event.setNearPlaneDistance() or RenderSystem.setShaderFogStart()), and I'm not sure which one is better since neither of them have worked for me so far.

 

TL:DR, EntityViewRenderEvent is now ViewportEvent, but I have no idea how to use it so that's all I know.

Forge documentation:

ViewportEvent.RenderFog: https://nekoyue.github.io/ForgeJavaDocs-NG/javadoc/1.19.3/net/minecraftforge/client/event/ViewportEvent.RenderFog.html

ViewportEvent.ComputeFogColor: https://nekoyue.github.io/ForgeJavaDocs-NG/javadoc/1.19.3/net/minecraftforge/client/event/ViewportEvent.ComputeFogColor.html

 

Hopefully you can manage to figure it out, and let me know if you do since I need it for something similar lol.

Good luck!

Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hello! A bit late but…
Fri, 01/17/2025 - 01:22

Hello! A bit late but hopefully this helps people in the future. I got it to work with my custom crude oil fluid.

 

You have to make a custom element that listens to the RenderFog() event. This is my code that I copied from the vanilla Camera.java class after alot of reading of the vanilla code to try to understand how lava achieves it's fog effect :

@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT)
public class ClientEventHandler {

    public ClientEventHandler() {
    }

    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public static void onFogRender(ViewportEvent.RenderFog event) {
        Camera cam = event.getCamera();
        Minecraft minecraft = Minecraft.getInstance();

                
        Camera.NearPlane camera_nearplane = cam.getNearPlane();
        Vector3f lookVector = cam.getLookVector(); // Direction the camera is looking

        Vec3 forward = new Vec3(lookVector).scale(0.05F); //Copied from Camera.java
        for (Vec3 vec3 : Arrays.asList(
                    forward,
                    camera_nearplane.getTopLeft(),
                    camera_nearplane.getTopRight(),
                    camera_nearplane.getBottomLeft(),
                    camera_nearplane.getBottomRight()
                )) {
                    Vec3 vec31 = cam.getPosition().add(vec3);
                    BlockPos blockpos = BlockPos.containing(vec31);
                    FluidState fluidstate1 = minecraft.level.getFluidState(blockpos);
            if (fluidstate1.is(CrazyRandomModModFluids.CRUDE_OIL.get()) || fluidstate1.is(CrazyRandomModModFluids.FLOWING_CRUDE_OIL.get())) {
                 RenderSystem.setShaderFogStart(0.25F);
                RenderSystem.setShaderFogEnd(1.5F);
           } 
        }
    }
}

 

The default forge fluid fog functions don't work as nicely since they act more like water fog than lava fog, so the camera would be able to 'see through' the liquid if positioned very near to the surface which was really ugly. This code doesn't seem to have that same problem and acts very much like lava in vanilla.

 

It's just a matter of replacing certain things to get it to work with your fluids.