Skip to content

Commit

Permalink
fix: Don't set the direction of diagonal fluid faces (CaffeineMC#476)
Browse files Browse the repository at this point in the history
Prevents improper culling. Fixes #171 more thoroughly.
  • Loading branch information
mrmangohands authored and spoorn committed Jun 29, 2021
1 parent 1139eda commit dd63252
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ public ChunkRenderBounds build(SectionPos origin) {
int z1 = origin.getWorldStartZ() + leftBound(this.z);
int z2 = origin.getWorldEndZ() + rightBound(this.z);

// Expand the bounding box by 8 blocks (half a chunk) in order to deal with diagonal surfaces
return new ChunkRenderBounds(
Math.max(x1, origin.getWorldStartX()) - 8.0f,
Math.max(y1, origin.getWorldStartY()) - 8.0f,
Math.max(z1, origin.getWorldStartZ()) - 8.0f,
Math.max(x1, origin.getWorldStartX()) - 0.5f,
Math.max(y1, origin.getWorldStartY()) - 0.5f,
Math.max(z1, origin.getWorldStartZ()) - 0.5f,

Math.min(x2, origin.getWorldEndX()) + 8.0f,
Math.min(y2, origin.getWorldEndY()) + 8.0f,
Math.min(z2, origin.getWorldEndZ()) + 8.0f
Math.min(x2, origin.getWorldEndX()) + 0.5f,
Math.min(y2, origin.getWorldEndY()) + 0.5f,
Math.min(z2, origin.getWorldEndZ()) + 0.5f
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,36 @@ public boolean render(IBlockDisplayReader world, FluidState fluidState, BlockPos

Vector3d velocity = fluidState.getFlow(world, pos);

TextureAtlasSprite TextureAtlasSprite;
TextureAtlasSprite sprite;
ModelQuadFacing facing;
float u1, u2, u3, u4;
float v1, v2, v3, v4;

if (velocity.x == 0.0D && velocity.z == 0.0D) {
TextureAtlasSprite = sprites[0];
u1 = TextureAtlasSprite.getInterpolatedU(0.0D);
v1 = TextureAtlasSprite.getInterpolatedV(0.0D);
sprite = sprites[0];
facing = ModelQuadFacing.UP;
u1 = sprite.getInterpolatedU(0.0D);
v1 = sprite.getInterpolatedV(0.0D);
u2 = u1;
v2 = TextureAtlasSprite.getInterpolatedV(16.0D);
u3 = TextureAtlasSprite.getInterpolatedU(16.0D);
v2 = sprite.getInterpolatedV(16.0D);
u3 = sprite.getInterpolatedU(16.0D);
v3 = v2;
u4 = u3;
v4 = v1;
} else {
TextureAtlasSprite = sprites[1];
sprite = sprites[1];
facing = ModelQuadFacing.UNASSIGNED;
float dir = (float) MathHelper.atan2(velocity.z, velocity.x) - (1.5707964f);
float sin = MathHelper.sin(dir) * 0.25F;
float cos = MathHelper.cos(dir) * 0.25F;
u1 = TextureAtlasSprite.getInterpolatedU(8.0F + (-cos - sin) * 16.0F);
v1 = TextureAtlasSprite.getInterpolatedV(8.0F + (-cos + sin) * 16.0F);
u2 = TextureAtlasSprite.getInterpolatedU(8.0F + (-cos + sin) * 16.0F);
v2 = TextureAtlasSprite.getInterpolatedV(8.0F + (cos + sin) * 16.0F);
u3 = TextureAtlasSprite.getInterpolatedU(8.0F + (cos + sin) * 16.0F);
v3 = TextureAtlasSprite.getInterpolatedV(8.0F + (cos - sin) * 16.0F);
u4 = TextureAtlasSprite.getInterpolatedU(8.0F + (cos - sin) * 16.0F);
v4 = TextureAtlasSprite.getInterpolatedV(8.0F + (-cos - sin) * 16.0F);
u1 = sprite.getInterpolatedU(8.0F + (-cos - sin) * 16.0F);
v1 = sprite.getInterpolatedV(8.0F + (-cos + sin) * 16.0F);
u2 = sprite.getInterpolatedU(8.0F + (-cos + sin) * 16.0F);
v2 = sprite.getInterpolatedV(8.0F + (cos + sin) * 16.0F);
u3 = sprite.getInterpolatedU(8.0F + (cos + sin) * 16.0F);
v3 = sprite.getInterpolatedV(8.0F + (cos - sin) * 16.0F);
u4 = sprite.getInterpolatedU(8.0F + (cos - sin) * 16.0F);
v4 = sprite.getInterpolatedV(8.0F + (-cos - sin) * 16.0F);
}

float uAvg = (u1 + u2 + u3 + u4) / 4.0F;
Expand All @@ -186,23 +189,23 @@ public boolean render(IBlockDisplayReader world, FluidState fluidState, BlockPos
v3 = MathHelper.lerp(s3, v3, vAvg);
v4 = MathHelper.lerp(s3, v4, vAvg);

quad.setSprite(TextureAtlasSprite);
quad.setSprite(sprite);

this.setVertex(quad, 0, 0.0f, h1, 0.0f, u1, v1);
this.setVertex(quad, 1, 0.0f, h2, 1.0F, u2, v2);
this.setVertex(quad, 2, 1.0F, h3, 1.0F, u3, v3);
this.setVertex(quad, 3, 1.0F, h4, 0.0f, u4, v4);

this.calculateQuadColors(quad, world, pos, lighter, Direction.UP, 1.0F, !lava);
this.flushQuad(buffers, quad, Direction.UP, false);
this.flushQuad(buffers, quad, facing, false);

if (fluidState.shouldRenderSides(world, this.scratchPos.setPos(posX, posY + 1, posZ))) {
this.setVertex(quad, 3, 0.0f, h1, 0.0f, u1, v1);
this.setVertex(quad, 2, 0.0f, h2, 1.0F, u2, v2);
this.setVertex(quad, 1, 1.0F, h3, 1.0F, u3, v3);
this.setVertex(quad, 0, 1.0F, h4, 0.0f, u4, v4);

this.flushQuad(buffers, quad, Direction.DOWN, true);
this.flushQuad(buffers, quad, ModelQuadFacing.DOWN, true);
}

rendered = true;
Expand All @@ -223,7 +226,7 @@ public boolean render(IBlockDisplayReader world, FluidState fluidState, BlockPos
this.setVertex(quad, 3, 1.0F, yOffset, 1.0F, maxU, maxV);

this.calculateQuadColors(quad, world, pos, lighter, Direction.DOWN, 1.0F, !lava);
this.flushQuad(buffers, quad, Direction.DOWN, false);
this.flushQuad(buffers, quad, ModelQuadFacing.DOWN, false);

rendered = true;
}
Expand Down Expand Up @@ -323,15 +326,15 @@ public boolean render(IBlockDisplayReader world, FluidState fluidState, BlockPos
float br = dir.getAxis() == Direction.Axis.Z ? 0.8F : 0.6F;

this.calculateQuadColors(quad, world, pos, lighter, dir, br, !lava);
this.flushQuad(buffers, quad, dir, false);
this.flushQuad(buffers, quad, ModelQuadFacing.fromDirection(dir), false);

if (TextureAtlasSprite != this.waterOverlaySprite) {
this.setVertex(quad, 0, x1, c1, z1, u1, v1);
this.setVertex(quad, 1, x1, yOffset, z1, u1, v3);
this.setVertex(quad, 2, x2, yOffset, z2, u2, v3);
this.setVertex(quad, 3, x2, c2, z2, u2, v2);

this.flushQuad(buffers, quad, dir, true);
this.flushQuad(buffers, quad, ModelQuadFacing.fromDirection(dir), true);
}

rendered = true;
Expand All @@ -356,7 +359,7 @@ private void calculateQuadColors(ModelQuadView quad, IBlockDisplayReader world,
}
}

private void flushQuad(ChunkModelBuffers buffers, ModelQuadView quad, Direction dir, boolean flip) {
private void flushQuad(ChunkModelBuffers buffers, ModelQuadView quad, ModelQuadFacing facing, boolean flip) {
int vertexIdx, lightOrder;

if (flip) {
Expand All @@ -367,7 +370,7 @@ private void flushQuad(ChunkModelBuffers buffers, ModelQuadView quad, Direction
lightOrder = 1;
}

ModelVertexSink sink = buffers.getSink(ModelQuadFacing.fromDirection(dir));
ModelVertexSink sink = buffers.getSink(facing);
sink.ensureCapacity(4);

for (int i = 0; i < 4; i++) {
Expand Down

0 comments on commit dd63252

Please sign in to comment.