GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add Fog Occlusion for particles by AnOpenSauceDev · Pull Request #2766 · CaffeineMC/sodium

Conversation

@AnOpenSauceDev

@AnOpenSauceDev AnOpenSauceDev commented Sep 21, 2024

Copy link
Copy Markdown

I'm not sure if this is best suited for Sodium, but this PR fixes Minecraft's behavior of rendering particles regardless of fog occlusion. This PR makes Sodium skip particle rendering if a particle falls behind opaque fog and additionally has Fog Occlusion enabled (essentially just re-using the existing fog occlusion logic).

(Render Distance is set to 2.)

Sodium with Fog No Fog No Fog + Particle Fog Occlusion
2024-09-21_23 46 04 despite the fog occluding the particles, they are still having render called. 2024-09-21_23 46 24 disabling terrain fog shows the particles are still being rendered. 2024-09-21_23 46 55 with Fog Occlusion enabled, the particles no longer render past fog distance, or in some cases, outside of render distance

Let me know if I need to fix/rework anything!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

GoatTech-42 reacted with heart emoji Felix14-v2 reacted with eyes emoji
All reactions
  • ❤️ 1 reaction
  • 👀 1 reaction
@jellysquid3

jellysquid3 commented Sep 23, 2024

Copy link
Copy Markdown
Member

I don't see a problem with the proposed changes as they are written. But has this been measured to be a problem? I've suspected that some servers will spawn many particles outside the render distance, but have not actually observed this to significantly impact performance.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Sep 23, 2024

Copy link
Copy Markdown
Author

Particle rendering on thousands of particles can take up a few milliseconds every frame even on my PC, and I assume the time penalty from all the unnecessary rendering would have a worse effect on a lower-end computer. I do have some older hardware lying around, so I might benchmark the performance thoroughly there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Sep 23, 2024

Copy link
Copy Markdown
Author

On a slower laptop I have (AMD 7530U), a somewhat empty Hypixel Skyblock lobby in the End at 2 Render Distance takes about 1.5ms (lowest was 0.8ms, highest was 2.1ms) to render a frame that stays around 3000 particles.

Turning on fog occlusion drops that to the range of 0.4 to 0.6ms, while skipping 28% to 34% of all particles. It seems that at the very least particle-heavy servers do get a noticeable performance increase.

debug class used

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@douira

douira commented Sep 23, 2024

Copy link
Copy Markdown
Contributor

Interesting that servers send so many particles outside of the fog distance

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Sep 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

The worst-case scenario is that after joining a LAN server hosted on my home PC, the particles I'm spawning with a command block still render at over 100 blocks outside of the render distance (2 again) on the laptop.

No Fog, No Fog Occlusion

2024-09-23_15 01 58

command: particle minecraft:scrape ~ ~15 ~ 0 0 0 30 500 force (with normal, particles won't be sent past 32 blocks regardless of render distance.)

Fog, No Fog Occlusion

2024-09-23_15 02 52

It also seems that particles are rendering over the stars when in fog. This behavior also happens in Vanilla.

Fog + Fog Occlusion

2024-09-23_15 02 40

This is fixed with Particle Fog Occlusion.

Felix14-v2 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Sep 26, 2024

Copy link
Copy Markdown
Author

It seems that particles actually do render over the fog itself (w/o Fog Occlusion), but since they use the sky fog color it's not visible until sunrise/sunset. This then causes some ugly transitions with long-distance particles like campfire smoke.

2024-09-26_14 16 38

Side note: particles don't render behind translucent blocks/fluids, but I think this is intended behavior.

Benchmarking

In the somewhat plausible situation someone has buildings outside of render distance with lots of campfires going, performance can increase by almost 2X. (Rendering the same setup from the first image, a 15x15 fill of campfires)

2024-09-26_14 46 49

Rendering the scene inside of the fog distance looks like it has the same performance, so at the very least the occlusion check has a negligible impact.

The performance gains from this are going to be extremely situational, probably only having an effect on large multiplayer servers, some large builds, and mods/datapacks that make use of long-distance effects (i.e. some biomes from Terralith and Better End.), but it could fix some existing visual issues with particles.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Felix14-v2

Felix14-v2 commented Sep 26, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

probably only having an effect on large multiplayer servers, some large builds, and mods/datapacks that make use of long-distance effects

Will underwater fog and fog from blindness effect also affect particle culling? This should significantly improve performance in many mini-games related to these effects.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Sep 26, 2024

Copy link
Copy Markdown
Author

As of that latest commit, Darkness + Blindness now have an effect on culling. (I forgot to use the fog end distance 😔)

Water fog didn't drop the particle count by much after entering it, but I need to test that properly later.

Felix14-v2 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AnOpenSauceDev

AnOpenSauceDev commented Oct 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

It looks like water fog isn't actually getting rid of particles because of fog, and I have made a few observations:

  1. it looks like after the water fog transition stops, almost all of the world is still visible (at least at 4 chunks), but the fog + overlay just makes it much harder to see. Because of this, the particle count won't really change from being in water past the first few seconds after entering it.

  2. despite this, no particles will be visible, as particles currently don't appear through translucent blocks/fluids after rendering. This actually causes thousands of particles to be waste time rendering when not submerged in an ocean. I have a few (probably too slow) ideas on how to stop the rendering anyways, but that doesn't apply to Fog Occlusion anymore.

Because of that being an unrelated problem, this feature is complete and working properly. The only extra thing I could do is also factor in height for distance to cull particles in a sphere instead of a cylinder (This didn't really provide much of a boost in render time in my survival world though).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@douira

douira commented Dec 1, 2024

Copy link
Copy Markdown
Contributor

Can this be rebased to dev?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AnOpenSauceDev added 2 commits December 2, 2024 11:26
@AnOpenSauceDev
Add fog occlusion for particles
032ead8
occlusion check based off `RenderSectionManager.getEffectiveRenderDistance`
@AnOpenSauceDev
factor in fog distance correctly
fe212af
@AnOpenSauceDev
AnOpenSauceDev force-pushed the dev branch from 54e7e9e to fe212af Compare December 2, 2024 01:32
@AnOpenSauceDev
Fix ParticleEngineMixin on 1.21.2+
16c0c83
It seems that fog now has information stored within its own class, so I've just quickly updated it to match.
@douira

douira commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Sorry for taking so long on this. Is this workable on 1.21.11/26.1?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@douira douira modified the milestone: Upcoming Minor Mar 19, 2026
@AnOpenSauceDev

AnOpenSauceDev commented Mar 20, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

My setup is a mess so I currently can't make any changes outside ruining history with the web editor, but a direct port of the code is:

@Mixin(QuadParticleGroup.class)
public abstract class QuadParticleGroupMixin {

    @Redirect(method = "extractRenderState",at = @At(value = "INVOKE", target = "Lnet/minecraft/client/particle/SingleQuadParticle;extract(Lnet/minecraft/client/renderer/state/QuadParticleRenderState;Lnet/minecraft/client/Camera;F)V"))
    public void sodium$checkFogOcclusion(SingleQuadParticle instance, QuadParticleRenderState particleTypeRenderState, Camera camera, float f){
        if(SodiumClientMod.options().performance.useFogOcclusion ? sodium$isParticleFogOccluded(camera.position(),instance.getBoundingBox().getMaxPosition()) : true){
            instance.extract(particleTypeRenderState,camera,f);
        }
    }

    @Unique
    public boolean sodium$isParticleFogOccluded(Vec3 pointA, Vec3 pointB){
        double dx = pointA.x - pointB.x;
        double dz = pointA.z - pointB.z;
        double distance = (dx * dx + dz * dz);

        var shaderFog = ((FogStorage) Minecraft.getInstance().gameRenderer).sodium$getFogParameters();
        double shaderFogDistance = shaderFog.environmentalEnd();

        var renderDistance = Minecraft.getInstance().gameRenderer.getRenderDistance();

        if(!Mth.equal(shaderFog.alpha(),1.0f)){
            return false;
        }

        var fogDist = Math.min(renderDistance,shaderFogDistance);

        return distance < fogDist * fogDist;
    }

}

The environment fog's end seems to be safe to use (and also has better fade-in), but can look a tiny bit weird in the sunset, less so than vanilla though. I took these screenshots just using the render end because of this.

This doesn't change the particle (P:) count, but as you can see by the performance graphs, the CPU is definitely not trying to render them.

image

Fog occlusion off (particles visible against skybox like before)

image

Fog occlusion on (particles hidden correctly)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AnOpenSauceDev added 2 commits March 21, 2026 00:20
@AnOpenSauceDev
Update Fog Particle Culling to 1.21.11
8800e13
@AnOpenSauceDev
Merge branch 'dev' into dev
e9d0e40
@AnOpenSauceDev

AnOpenSauceDev commented Oct 20, 2026

Copy link
Copy Markdown
Author

Not entirely sure if I'll be able to keep 'bumping' this PR into the future, so I'm wondering if this can be merged (possibly into an older version), or closed if the need for this has become obselete.

I haven't touched MC seriously in ages so this may have become broken with the new renderer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

Upcoming Minor

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@AnOpenSauceDev @jellysquid3 @douira @Felix14-v2
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.