Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Fog Occlusion for particles #2766

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 shaderFog = RenderSystem.getShaderFog();
double shaderFogDistance = shaderFog.end();

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