From 032ead86b32d063addf19d85d26dd142384c85cc Mon Sep 17 00:00:00 2001 From: AnOpenSauceDev <119765865+AnOpenSauceDev@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:14:36 +1000 Subject: [PATCH 1/3] Add fog occlusion for particles occlusion check based off `RenderSectionManager.getEffectiveRenderDistance` --- .../render/particle/ParticleEngineMixin.java | 54 +++++++++++++++++++ .../main/resources/sodium-common.mixins.json | 1 + 2 files changed, 55 insertions(+) create mode 100644 common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java new file mode 100644 index 0000000000..1b15cdd5c5 --- /dev/null +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java @@ -0,0 +1,54 @@ +package net.caffeinemc.mods.sodium.mixin.features.render.particle; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.VertexConsumer; +import net.caffeinemc.mods.sodium.client.SodiumClientMod; +import net.minecraft.client.Camera; +import net.minecraft.client.Minecraft; +import net.minecraft.client.particle.Particle; +import net.minecraft.client.particle.ParticleEngine; +import net.minecraft.util.Mth; +import net.minecraft.world.phys.Vec3; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(ParticleEngine.class) +public class ParticleEngineMixin { + + /** + * @author AnOpenSauceDev + * @reason Prevent Minecraft from rendering particles that are fog occluded. + */ + @Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/particle/Particle;render(Lcom/mojang/blaze3d/vertex/VertexConsumer;Lnet/minecraft/client/Camera;F)V")) + public void particleFogOcclusion(Particle instance, VertexConsumer vertexConsumer, Camera camera, float v){ + if(!SodiumClientMod.options().performance.useFogOcclusion){ + instance.render(vertexConsumer,camera,v); + return; + } + + if(sodium$isParticleFogOccluded(camera.getPosition(),instance.getBoundingBox().getCenter())){ + instance.render(vertexConsumer,camera,v); + } + } + + @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 renderDistance = Minecraft.getInstance().gameRenderer.getRenderDistance(); + var color = RenderSystem.getShaderFogColor(); + + if(!Mth.equal(color[3],1.0f)){ + return false; + } + + var fogDist = Math.min(renderDistance,distance); + + return distance < fogDist * fogDist; + } + +} diff --git a/common/src/main/resources/sodium-common.mixins.json b/common/src/main/resources/sodium-common.mixins.json index 9cce3b84b6..f8a88499f3 100644 --- a/common/src/main/resources/sodium-common.mixins.json +++ b/common/src/main/resources/sodium-common.mixins.json @@ -65,6 +65,7 @@ "features.render.model.ItemBlockRenderTypesMixin", "features.render.model.item.ItemRendererMixin", "features.render.particle.SingleQuadParticleMixin", + "features.render.particle.ParticleEngineMixin", "features.render.world.clouds.LevelRendererMixin", "features.render.world.sky.FogRendererMixin", "features.render.world.sky.ClientLevelMixin", From fe212af099ccedd19abcfb47140fa2d30189dbac Mon Sep 17 00:00:00 2001 From: AnOpenSauceDev <119765865+AnOpenSauceDev@users.noreply.github.com> Date: Fri, 27 Sep 2024 00:59:27 +1000 Subject: [PATCH 2/3] factor in fog distance correctly --- .../mixin/features/render/particle/ParticleEngineMixin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java index 1b15cdd5c5..03cdaa3452 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java @@ -38,6 +38,7 @@ public void particleFogOcclusion(Particle instance, VertexConsumer vertexConsume double dx = pointA.x - pointB.x; double dz = pointA.z - pointB.z; double distance = (dx * dx + dz * dz); + double shaderFogDistance = RenderSystem.getShaderFogEnd(); var renderDistance = Minecraft.getInstance().gameRenderer.getRenderDistance(); var color = RenderSystem.getShaderFogColor(); @@ -46,9 +47,8 @@ public void particleFogOcclusion(Particle instance, VertexConsumer vertexConsume return false; } - var fogDist = Math.min(renderDistance,distance); + var fogDist = Math.min(renderDistance,shaderFogDistance); return distance < fogDist * fogDist; } - } From 16c0c83cdbd702caf2350d69090dbd00f62fcc2d Mon Sep 17 00:00:00 2001 From: AnOpenSauceDev <119765865+AnOpenSauceDev@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:54:53 +1000 Subject: [PATCH 3/3] Fix `ParticleEngineMixin` on 1.21.2+ It seems that fog now has information stored within its own class, so I've just quickly updated it to match. --- .../features/render/particle/ParticleEngineMixin.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java index 03cdaa3452..62d91cf9cc 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/ParticleEngineMixin.java @@ -38,12 +38,13 @@ public void particleFogOcclusion(Particle instance, VertexConsumer vertexConsume double dx = pointA.x - pointB.x; double dz = pointA.z - pointB.z; double distance = (dx * dx + dz * dz); - double shaderFogDistance = RenderSystem.getShaderFogEnd(); + + var shaderFog = RenderSystem.getShaderFog(); + double shaderFogDistance = shaderFog.end(); var renderDistance = Minecraft.getInstance().gameRenderer.getRenderDistance(); - var color = RenderSystem.getShaderFogColor(); - if(!Mth.equal(color[3],1.0f)){ + if(!Mth.equal(shaderFog.alpha(),1.0f)){ return false; }