Skip to content

Commit

Permalink
Fix fast cloud rendering (#2648)
Browse files Browse the repository at this point in the history
* make it possible to have no vertex buffer, and to simply not render clouds if there is none. Disable view-dependent culling on fast clouds. Resolves the crash and makes them visible from the top and bottom.

* add alpha test to only render clouds for non-transparent pixels

* avoid cloud geometry rebuilds when moving vertically on fast clouds
  • Loading branch information
douira authored Aug 10, 2024
1 parent 2468a72 commit bad3763
Showing 1 changed file with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ public void render(Camera camera,
int centerCellZ = (int) (Math.floor(cloudCenterZ / 12.0));

// -1 if below clouds, +1 if above clouds
int orientation = (int) Math.signum(pos.y() - cloudHeight);
var parameters = new CloudGeometryParameters(centerCellX, centerCellZ, cloudDistance, orientation, Minecraft.getInstance().options.getCloudsType());
var cloudType = Minecraft.getInstance().options.getCloudsType();
int orientation = cloudType == CloudStatus.FANCY ? (int) Math.signum(pos.y() - cloudHeight) : 0;
var parameters = new CloudGeometryParameters(centerCellX, centerCellZ, cloudDistance, orientation, cloudType);

CloudGeometry geometry = this.cachedGeometry;

if (geometry == null || !Objects.equals(geometry.params(), parameters)) {
this.cachedGeometry = (geometry = rebuildGeometry(geometry, parameters, this.textureData));
}

VertexBuffer vertexBuffer = geometry.vertexBuffer();
if (vertexBuffer == null) {
return;
}

final float translateX = (float) (cloudCenterX - (centerCellX * 12));
final float translateZ = (float) (cloudCenterZ - (centerCellZ * 12));

Expand Down Expand Up @@ -114,7 +120,6 @@ public void render(Camera camera,
Vec3 colorModulator = level.getCloudColor(tickDelta);
RenderSystem.setShaderColor((float) colorModulator.x, (float) colorModulator.y, (float) colorModulator.z, 0.8f);

VertexBuffer vertexBuffer = geometry.vertexBuffer();
vertexBuffer.bind();

RenderSystem.enableBlend();
Expand Down Expand Up @@ -201,17 +206,20 @@ public void render(Camera camera,
}
}

MeshData builtBuffer = bufferBuilder.buildOrThrow();
MeshData builtBuffer = bufferBuilder.build();

VertexBuffer vertexBuffer;
VertexBuffer vertexBuffer = null;

if (existingGeometry != null) {
vertexBuffer = existingGeometry.vertexBuffer();
} else {
vertexBuffer = new VertexBuffer(VertexBuffer.Usage.DYNAMIC);
}
if (builtBuffer != null) {
if (existingGeometry != null) {
vertexBuffer = existingGeometry.vertexBuffer();
}
if (vertexBuffer == null) {
vertexBuffer = new VertexBuffer(VertexBuffer.Usage.DYNAMIC);
}

uploadToVertexBuffer(vertexBuffer, builtBuffer);
uploadToVertexBuffer(vertexBuffer, builtBuffer);
}

Tesselator.getInstance().clear();

Expand All @@ -238,6 +246,10 @@ private static void addCellGeometryToBuffer(VertexBufferWriter writer,

int cellColor = textureData.getCellColor(cellIndex);

if (ColorABGR.unpackAlpha(cellColor) == 0) {
return;
}

float x = offsetX * 12;
float z = offsetZ * 12;

Expand Down Expand Up @@ -337,21 +349,16 @@ private static void emitCellGeometry2D(VertexBufferWriter writer, int faces, int
long ptr = buffer;
int count = 0;

// -Y
if (CloudFaceSet.contains(faces, CloudFace.NEG_Y)) {
int mixedColor = ColorMixer.mul(color, CloudFace.POS_Y.getColor());
int mixedColor = ColorMixer.mul(color, CloudFace.POS_Y.getColor());

ptr = writeVertex(ptr, x + 12.0f, 0.0f, z + 12.0f, mixedColor);
ptr = writeVertex(ptr, x + 0.0f, 0.0f, z + 12.0f, mixedColor);
ptr = writeVertex(ptr, x + 0.0f, 0.0f, z + 0.0f, mixedColor);
ptr = writeVertex(ptr, x + 12.0f, 0.0f, z + 0.0f, mixedColor);
ptr = writeVertex(ptr, x + 12.0f, 0.0f, z + 12.0f, mixedColor);
ptr = writeVertex(ptr, x + 0.0f, 0.0f, z + 12.0f, mixedColor);
ptr = writeVertex(ptr, x + 0.0f, 0.0f, z + 0.0f, mixedColor);
ptr = writeVertex(ptr, x + 12.0f, 0.0f, z + 0.0f, mixedColor);

count += 4;
}
count += 4;

if (count > 0) {
writer.push(stack, buffer, count, ColorVertex.FORMAT);
}
writer.push(stack, buffer, count, ColorVertex.FORMAT);
}
}

Expand Down

0 comments on commit bad3763

Please sign in to comment.