Skip to content

Commit

Permalink
Fix cull bitmask ordering in entity rendering
Browse files Browse the repository at this point in the history
Closes #2788
  • Loading branch information
jellysquid3 committed Oct 12, 2024
1 parent a864415 commit adad380
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

public class EntityRenderer {
import static net.caffeinemc.mods.sodium.client.render.immediate.model.ModelCuboid.*;

public class EntityRenderer {
private static final int NUM_CUBE_VERTICES = 8;
private static final int NUM_CUBE_FACES = 6;
private static final int NUM_FACE_VERTICES = 4;

// The ordering needs to be the same as Minecraft, otherwise some core shader replacements
// will be unable to identify the facing.
private static final int
FACE_NEG_Y = 0, // DOWN
FACE_POS_Y = 1, // UP
FACE_NEG_X = 2, // WEST
FACE_NEG_Z = 3, // NORTH
FACE_POS_X = 4, // EAST
FACE_POS_Z = 5; // SOUTH

private static final int
VERTEX_X1_Y1_Z1 = 0,
VERTEX_X2_Y1_Z1 = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

import java.util.Set;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.NotNull;

public class ModelCuboid {
// The ordering needs to be the same as Minecraft, otherwise some core shader replacements
// will be unable to identify the facing.
public static final int
FACE_NEG_Y = 0, // DOWN
FACE_POS_Y = 1, // UP
FACE_NEG_X = 2, // WEST
FACE_NEG_Z = 3, // NORTH
FACE_POS_X = 4, // EAST
FACE_POS_Z = 5; // SOUTH

public final float x1, y1, z1;
public final float x2, y2, z2;

public final float u0, u1, u2, u3, u4, u5;
public final float v0, v1, v2;

private final int faces;
private final int cullBitmask;

public final boolean mirror;

Expand Down Expand Up @@ -63,16 +74,27 @@ public ModelCuboid(int u, int v,

this.mirror = mirror;

int faces = 0;
int cullBitmask = 0;

for (var dir : renderDirections) {
faces |= 1 << dir.ordinal();
for (var direction : renderDirections) {
cullBitmask |= 1 << getFaceIndex(direction);
}

this.faces = faces;
this.cullBitmask = cullBitmask;
}

public boolean shouldDrawFace(int faceIndex) {
return (this.cullBitmask & (1 << faceIndex)) != 0;
}

public boolean shouldDrawFace(int quadIndex) {
return (this.faces & (1 << quadIndex)) != 0;
public static int getFaceIndex(@NotNull Direction dir) {
return switch (dir) {
case DOWN -> FACE_NEG_Y;
case UP -> FACE_POS_Y;
case NORTH -> FACE_NEG_Z;
case SOUTH -> FACE_POS_Z;
case WEST -> FACE_NEG_X;
case EAST -> FACE_POS_X;
};
}
}

0 comments on commit adad380

Please sign in to comment.