Skip to content

Commit 5308665

Browse files
authored
Fix opacity for Wallhack and Xray with Sodium (#6039)
1 parent 08771f0 commit 5308665

6 files changed

Lines changed: 142 additions & 11 deletions

File tree

src/main/java/meteordevelopment/meteorclient/mixin/sodium/SodiumBlockRendererMixin.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,49 @@
77

88
import meteordevelopment.meteorclient.systems.modules.render.Xray;
99
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
10+
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.DefaultMaterials;
11+
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.Material;
12+
import net.caffeinemc.mods.sodium.client.render.model.MutableQuadViewImpl;
1013
import net.minecraft.block.BlockState;
1114
import net.minecraft.client.render.model.BlockStateModel;
1215
import net.minecraft.util.math.BlockPos;
1316
import org.spongepowered.asm.mixin.Mixin;
17+
import org.spongepowered.asm.mixin.Unique;
1418
import org.spongepowered.asm.mixin.injection.At;
1519
import org.spongepowered.asm.mixin.injection.Inject;
20+
import org.spongepowered.asm.mixin.injection.ModifyArg;
1621
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1722

18-
@Mixin(value = BlockRenderer.class)
19-
public abstract class SodiumBlockRendererMixin {
20-
@Inject(method = "renderModel", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/model/color/ColorProviderRegistry;getColorProvider(Lnet/minecraft/block/Block;)Lnet/caffeinemc/mods/sodium/client/model/color/ColorProvider;", shift = At.Shift.AFTER), cancellable = true)
21-
private void onRenderModel(BlockStateModel model, BlockState state, BlockPos pos, BlockPos origin, CallbackInfo ci) {
22-
int alpha = Xray.getAlpha(state, pos);
23+
@Mixin(value = BlockRenderer.class, remap = false)
24+
public class SodiumBlockRendererMixin {
25+
@Unique
26+
private int xrayAlpha;
2327

24-
if (alpha == 0) ci.cancel();
28+
@Inject(method = "renderModel", at = @At("HEAD"), cancellable = true)
29+
private void onRenderModel(BlockStateModel model, BlockState state, BlockPos pos, BlockPos origin, CallbackInfo info) {
30+
xrayAlpha = Xray.getAlpha(state, pos);
31+
32+
// Cancel block rendering when alpha is 0, required for Iris support but unnecessary to check for shaders, we already force be disabled when Xray is enabled
33+
if (xrayAlpha == 0) {
34+
info.cancel();
35+
}
36+
}
37+
38+
@Inject(method = "bufferQuad", at = @At("HEAD"))
39+
private void onBufferQuad(MutableQuadViewImpl quad, float[] brightnesses, Material material, CallbackInfo info) {
40+
if (xrayAlpha != -1) {
41+
for (int i = 0; i < 4; i++) {
42+
int color = quad.baseColor(i);
43+
quad.setColor(i, ((xrayAlpha & 0xFF) << 24) | (color & 0x00FFFFFF));
44+
}
45+
}
46+
}
47+
48+
@ModifyArg(method = "processQuad", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/BlockRenderer;bufferQuad(Lnet/caffeinemc/mods/sodium/client/render/model/MutableQuadViewImpl;[FLnet/caffeinemc/mods/sodium/client/render/chunk/terrain/material/Material;)V"), index = 2)
49+
private Material modifyMaterial(Material material) {
50+
if (xrayAlpha != -1) {
51+
return DefaultMaterials.TRANSLUCENT;
52+
}
53+
return material;
2554
}
2655
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixin.sodium;
7+
8+
import meteordevelopment.meteorclient.systems.modules.render.Xray;
9+
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
10+
import net.caffeinemc.mods.sodium.client.model.light.LightPipeline;
11+
import net.caffeinemc.mods.sodium.client.model.quad.ModelQuadViewMutable;
12+
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
13+
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
14+
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.DefaultFluidRenderer;
15+
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.DefaultMaterials;
16+
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.Material;
17+
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.TranslucentGeometryCollector;
18+
import net.caffeinemc.mods.sodium.client.world.LevelSlice;
19+
import net.minecraft.client.texture.Sprite;
20+
import net.minecraft.fluid.FluidState;
21+
import net.minecraft.util.math.BlockPos;
22+
import net.minecraft.util.math.Direction;
23+
import org.spongepowered.asm.mixin.Final;
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.Shadow;
26+
import org.spongepowered.asm.mixin.Unique;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Inject;
29+
import org.spongepowered.asm.mixin.injection.ModifyArg;
30+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
31+
32+
@Mixin(value = DefaultFluidRenderer.class, remap = false)
33+
public abstract class SodiumDefaultFluidRendererMixin {
34+
@Final
35+
@Shadow
36+
private int[] quadColors;
37+
38+
@Unique
39+
private int xrayAlpha;
40+
41+
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
42+
private void onRender(LevelSlice level, net.minecraft.block.BlockState blockState, FluidState fluidState, BlockPos blockPos, BlockPos offset, TranslucentGeometryCollector collector, ChunkModelBuilder meshBuilder, Material material, ColorProvider<FluidState> colorProvider, Sprite[] sprites, CallbackInfo ci) {
43+
xrayAlpha = Xray.getAlpha(fluidState.getBlockState(), blockPos);
44+
45+
// Cancel block rendering when alpha is 0, required for Iris support but unnecessary to check for shaders, we already force be disabled when Xray is enabled
46+
if (xrayAlpha == 0) {
47+
ci.cancel();
48+
}
49+
}
50+
51+
@Inject(method = "updateQuad", at = @At("TAIL"))
52+
private void onUpdateQuad(ModelQuadViewMutable quad, LevelSlice level, BlockPos pos, LightPipeline lighter, Direction dir, ModelQuadFacing facing, float brightness, ColorProvider<FluidState> colorProvider, FluidState fluidState, CallbackInfo ci) {
53+
if (xrayAlpha != -1) {
54+
for (int i = 0; i < 4; i++) {
55+
quadColors[i] = (quadColors[i] & 0x00FFFFFF) | (xrayAlpha << 24);
56+
}
57+
}
58+
}
59+
60+
@ModifyArg(method = "render", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/DefaultFluidRenderer;writeQuad(Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/buffers/ChunkModelBuilder;Lnet/caffeinemc/mods/sodium/client/render/chunk/translucent_sorting/TranslucentGeometryCollector;Lnet/caffeinemc/mods/sodium/client/render/chunk/terrain/material/Material;Lnet/minecraft/util/math/BlockPos;Lnet/caffeinemc/mods/sodium/client/model/quad/ModelQuadView;Lnet/caffeinemc/mods/sodium/client/model/quad/properties/ModelQuadFacing;Z)V"), index = 2)
61+
private Material modifyMaterial(Material material) {
62+
if (xrayAlpha != -1) {
63+
return DefaultMaterials.TRANSLUCENT;
64+
}
65+
return material;
66+
}
67+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixin.sodium;
7+
8+
import com.llamalad7.mixinextras.expression.Definition;
9+
import com.llamalad7.mixinextras.expression.Expression;
10+
import com.llamalad7.mixinextras.sugar.Local;
11+
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkVertexEncoder;
12+
import org.joml.Vector3fc;
13+
import org.spongepowered.asm.mixin.Mixin;
14+
import org.spongepowered.asm.mixin.injection.At;
15+
import org.spongepowered.asm.mixin.injection.Inject;
16+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
17+
18+
@Mixin(targets = "net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.bsp_tree.InnerPartitionBSPNode", remap = false)
19+
public class SodiumInnerPartitionBSPNodeMixin {
20+
// https://github.com/LlamaLad7/MixinExtras/wiki/Expressions#things-to-watch-out-for
21+
// 'Be particularly careful when using == 0 or != 0'
22+
@Definition(id = "splitPlaneEdgeDot", local = @Local(type = float.class, name = "splitPlaneEdgeDot"))
23+
@Expression("splitPlaneEdgeDot == 0.0")
24+
@Inject(method = "interpolateAttributes(FLorg/joml/Vector3fc;Lnet/caffeinemc/mods/sodium/client/render/chunk/vertex/format/ChunkVertexEncoder$Vertex;Lnet/caffeinemc/mods/sodium/client/render/chunk/vertex/format/ChunkVertexEncoder$Vertex;Lnet/caffeinemc/mods/sodium/client/render/chunk/vertex/format/ChunkVertexEncoder$Vertex;Lnet/caffeinemc/mods/sodium/client/render/chunk/vertex/format/ChunkVertexEncoder$Vertex;Lnet/caffeinemc/mods/sodium/client/render/chunk/vertex/format/ChunkVertexEncoder$Vertex;)V", at = @At("MIXINEXTRAS:EXPRESSION"), cancellable = true)
25+
private static void onInterpolateAttributes(float splitDistance, Vector3fc splitPlane,
26+
ChunkVertexEncoder.Vertex inside, ChunkVertexEncoder.Vertex outside, ChunkVertexEncoder.Vertex targetA,
27+
ChunkVertexEncoder.Vertex targetB, ChunkVertexEncoder.Vertex targetC, CallbackInfo ci,
28+
@Local(name = "splitPlaneEdgeDot") float splitPlaneEdgeDot) {
29+
if (splitPlaneEdgeDot == 0.0f) ci.cancel();
30+
}
31+
}

src/main/java/meteordevelopment/meteorclient/systems/modules/render/WallHack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public void onDeactivate() {
6868

6969
@Override
7070
public WWidget getWidget(GuiTheme theme) {
71-
if (MixinPlugin.isSodiumPresent) return theme.label("Warning: Due to Sodium in use, opacity is overridden to 0.");
72-
if (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse()) return theme.label("Warning: Due to shaders in use, opacity is overridden to 0.");
71+
if (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse())
72+
return theme.label("Warning: Due to shaders in use, opacity is overridden to 0.");
7373

7474
return null;
7575
}

src/main/java/meteordevelopment/meteorclient/systems/modules/render/Xray.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public void onDeactivate() {
8080

8181
@Override
8282
public WWidget getWidget(GuiTheme theme) {
83-
if (MixinPlugin.isSodiumPresent) return theme.label("Warning: Due to Sodium in use, opacity is overridden to 0.");
8483
if (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse()) return theme.label("Warning: Due to shaders in use, opacity is overridden to 0.");
8584

8685
return null;
@@ -120,7 +119,7 @@ public static int getAlpha(BlockState state, BlockPos pos) {
120119
Xray xray = Modules.get().get(Xray.class);
121120

122121
if (wallHack.isActive() && wallHack.blocks.get().contains(state.getBlock())) {
123-
if (MixinPlugin.isSodiumPresent || (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse())) return 0;
122+
if (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse()) return 0;
124123

125124
int alpha;
126125

@@ -130,7 +129,7 @@ public static int getAlpha(BlockState state, BlockPos pos) {
130129
return alpha;
131130
}
132131
else if (xray.isActive() && !wallHack.isActive() && xray.isBlocked(state.getBlock(), pos)) {
133-
return (MixinPlugin.isSodiumPresent || (MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse())) ? 0 : xray.opacity.get();
132+
return ((MixinPlugin.isIrisPresent && IrisApi.getInstance().isShaderPackInUse())) ? 0 : xray.opacity.get();
134133
}
135134

136135
return -1;

src/main/resources/meteor-client-sodium.mixins.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
"MeshVertexConsumerMixin",
88
"SodiumBlockOcclusionCacheMixin",
99
"SodiumBlockRendererMixin",
10+
"SodiumDefaultFluidRendererMixin",
1011
"SodiumFluidRendererImplDefaultRenderContextMixin",
1112
"SodiumFluidRendererImplMixin",
13+
"SodiumInnerPartitionBSPNodeMixin",
1214
"SodiumLightDataAccessMixin",
1315
"SodiumWorldRendererMixin"
1416
],
1517
"injectors": {
1618
"defaultRequire": 1
19+
},
20+
"mixinextras": {
21+
"minVersion": "0.5.0"
1722
}
1823
}

0 commit comments

Comments
 (0)