Skip to content

Commit

Permalink
Reorient water quads according to the corner heights (CaffeineMC#2467)
Browse files Browse the repository at this point in the history
The seam is placed such that it touches either the highest or lowest corner.

Closes CaffeineMC#609
  • Loading branch information
douira authored and IMS212 committed Jun 23, 2024
1 parent 3d4cf6a commit 9b937ff
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FluidRenderer {
// TODO: allow this to be changed by vertex format
// TODO: move fluid rendering to a separate render pass and control glPolygonOffset and glDepthFunc to fix this properly
private static final float EPSILON = 0.001f;
private static final float ALIGNED_EQUALS_EPSILON = 0.011f;

private final BlockPos.Mutable scratchPos = new BlockPos.Mutable();
private final MutableFloat scratchHeight = new MutableFloat(0);
Expand Down Expand Up @@ -96,6 +97,10 @@ private boolean isSideExposed(BlockRenderView world, int x, int y, int z, Direct
return true;
}

private static boolean isAlignedEquals(float a, float b) {
return Math.abs(a - b) <= ALIGNED_EQUALS_EPSILON;
}

public void render(WorldSlice world, FluidState fluidState, BlockPos blockPos, BlockPos offset, ChunkBuildBuffers buffers) {
var material = DefaultMaterials.forFluidState(fluidState);
var meshBuilder = buffers.get(material);
Expand Down Expand Up @@ -215,10 +220,30 @@ public void render(WorldSlice world, FluidState fluidState, BlockPos blockPos, B

quad.setSprite(sprite);

setVertex(quad, 0, 0.0f, northWestHeight, 0.0f, u1, v1);
setVertex(quad, 1, 0.0f, southWestHeight, 1.0F, u2, v2);
setVertex(quad, 2, 1.0F, southEastHeight, 1.0F, u3, v3);
setVertex(quad, 3, 1.0F, northEastHeight, 0.0f, u4, v4);

// top surface alignedness is calculated with a more relaxed epsilon
boolean aligned = isAlignedEquals(northEastHeight, northWestHeight)
&& isAlignedEquals(northWestHeight, southEastHeight)
&& isAlignedEquals(southEastHeight, southWestHeight)
&& isAlignedEquals(southWestHeight, northEastHeight);

boolean creaseNorthEastSouthWest = aligned
|| northEastHeight > northWestHeight && northEastHeight > southEastHeight
|| northEastHeight < northWestHeight && northEastHeight < southEastHeight
|| southWestHeight > northWestHeight && southWestHeight > southEastHeight
|| southWestHeight < northWestHeight && southWestHeight < southEastHeight;

if (creaseNorthEastSouthWest) {
setVertex(quad, 1, 0.0f, northWestHeight, 0.0f, u1, v1);
setVertex(quad, 2, 0.0f, southWestHeight, 1.0F, u2, v2);
setVertex(quad, 3, 1.0F, southEastHeight, 1.0F, u3, v3);
setVertex(quad, 0, 1.0F, northEastHeight, 0.0f, u4, v4);
} else {
setVertex(quad, 0, 0.0f, northWestHeight, 0.0f, u1, v1);
setVertex(quad, 1, 0.0f, southWestHeight, 1.0F, u2, v2);
setVertex(quad, 2, 1.0F, southEastHeight, 1.0F, u3, v3);
setVertex(quad, 3, 1.0F, northEastHeight, 0.0f, u4, v4);
}

this.updateQuad(quad, world, blockPos, lighter, Direction.UP, 1.0F, colorProvider, fluidState);
this.writeQuad(meshBuilder, material, offset, quad, facing, false);
Expand All @@ -228,7 +253,6 @@ public void render(WorldSlice world, FluidState fluidState, BlockPos blockPos, B
ModelQuadFacing.NEG_Y, true);

}

}

if (!sfDown) {
Expand Down Expand Up @@ -363,7 +387,7 @@ private ColorProvider<FluidState> getColorProvider(Fluid fluid, FluidRenderHandl
if (override != null) {
return override;
}

return DefaultColorProviders.adapt(handler);
}

Expand Down Expand Up @@ -397,7 +421,7 @@ private void writeQuad(ChunkModelBuilder builder, Material material, BlockPos of
var vertices = this.vertices;

for (int i = 0; i < 4; i++) {
var out = vertices[flip ? 3 - i : i];
var out = vertices[flip ? (3 - i + 1) & 0b11 : i];
out.x = offset.getX() + quad.getX(i);
out.y = offset.getY() + quad.getY(i);
out.z = offset.getZ() + quad.getZ(i);
Expand Down

0 comments on commit 9b937ff

Please sign in to comment.