Skip to content

Commit

Permalink
Add fog occlusion for particles
Browse files Browse the repository at this point in the history
occlusion check based off `RenderSectionManager.getEffectiveRenderDistance`
  • Loading branch information
AnOpenSauceDev committed Sep 21, 2024
1 parent 888d922 commit efdc207
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
1 change: 1 addition & 0 deletions common/src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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",
Expand Down

0 comments on commit efdc207

Please sign in to comment.