Fix edge cases with cloud wraps (#2122)

This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-09-20 17:16:03 +00:00
1 parent ef68af5850
commit c578970a66
5 files changed
+62 -49

No files matched your search

@@ -11,7 +11,6 @@ final class CloudDisc {
static final int WEDGE_COUNT = 32;
static final double WEDGES_PER_RADIAN = WEDGE_COUNT / (2.0 * Math.PI);
static final int ALWAYS_DRAWN_CELLS = Math.max(CELLS_PER_CHUNK, 4 * MARGIN_CELLS);
static final float SCROLL_SPEED = 1.0f / 256.0f;
private static final double WALL_CUT_PIXELS = 0.625;
private static final double PLATE_LOD_PIXELS = 0.35;
private static final double TAN_11_25_DEG = 0.19891236737965800; // √(4+2√2)−√2−1
@@ -28,6 +27,23 @@ final class CloudDisc {
private CloudDisc() {
}
static double cellCoordinate(double worldPosition, float cellWidthBlocks, double offset) {
return worldPosition / cellWidthBlocks + offset;
}
static float textureOffset(int anchor, int textureSize) {
return Math.floorMod(anchor, textureSize) * (1.0f / textureSize);
}
static boolean withinMargin(int driftX, int driftZ) {
// Reject distant anchors before squaring
return driftX >= -MARGIN_CELLS && driftX <= MARGIN_CELLS && driftZ >= -MARGIN_CELLS && driftZ <= MARGIN_CELLS && driftX * driftX + driftZ * driftZ <= MARGIN_CELLS * MARGIN_CELLS;
}
static boolean withinDisc(long driftX, long driftZ, int radiusCells) {
return driftX >= -radiusCells && driftX <= radiusCells && driftZ >= -radiusCells && driftZ <= radiusCells && driftX * driftX + driftZ * driftZ <= (long) radiusCells * radiusCells;
}
static double pixelsPerRadian(int displayHeight, float fovDegrees) {
if (!(fovDegrees > 0.0f) || fovDegrees >= 180.0f) return 0.0;
return (displayHeight * 0.5) / Math.tan(Math.toRadians(fovDegrees) * 0.5);
@@ -59,9 +59,10 @@ import java.util.concurrent.Executors;
import static com.gtnewhorizons.angelica.render.CloudDisc.CELLS_PER_CHUNK;
import static com.gtnewhorizons.angelica.render.CloudDisc.MARGIN_CELLS;
import static com.gtnewhorizons.angelica.render.CloudDisc.SCROLL_SPEED;
import static com.gtnewhorizons.angelica.render.CloudDisc.WEDGE_COUNT;
import static com.gtnewhorizons.angelica.render.CloudDisc.WEDGES_PER_RADIAN;
import static com.gtnewhorizons.angelica.render.CloudDisc.textureOffset;
import static com.gtnewhorizons.angelica.render.CloudDisc.withinMargin;
/**
* Draws the cloud layer.
@@ -217,10 +218,6 @@ public class CloudRenderer implements IResourceManagerReloadListener {
return Math.min(radiusBlocks + verticalSlack, MAX_FAR_PLANE_DISTANCE);
}
private static boolean withinMargin(int driftX, int driftZ) {
return driftX * driftX + driftZ * driftZ <= MARGIN_CELLS * MARGIN_CELLS;
}
private void invalidateGeometry() {
anchorCellX = Integer.MIN_VALUE;
anchorCellZ = Integer.MIN_VALUE;
@@ -339,11 +336,8 @@ public class CloudRenderer implements IResourceManagerReloadListener {
final double cloudTick = cloudTicks + (double) partialTicks;
final float cameraY = (float) (viewEntity.lastTickPosY + (viewEntity.posY - viewEntity.lastTickPosY) * partialTicks);
double cameraCellX = (viewEntity.prevPosX + (viewEntity.posX - viewEntity.prevPosX) * partialTicks + cloudTick * 0.03D) / cellWidthBlocks;
double cameraCellZ = (viewEntity.prevPosZ + (viewEntity.posZ - viewEntity.prevPosZ) * partialTicks) / cellWidthBlocks
+ 0.33000001311302185D;
cameraCellX -= MathHelper.floor_double(cameraCellX / 2048.0D) * 2048;
cameraCellZ -= MathHelper.floor_double(cameraCellZ / 2048.0D) * 2048;
final double cameraCellX = CloudDisc.cellCoordinate(viewEntity.prevPosX + (viewEntity.posX - viewEntity.prevPosX) * partialTicks + cloudTick * 0.03D, cellWidthBlocks, 0);
final double cameraCellZ = CloudDisc.cellCoordinate(viewEntity.prevPosZ + (viewEntity.posZ - viewEntity.prevPosZ) * partialTicks, cellWidthBlocks, 0.33000001311302185D);
final float cloudBaseRelativeY = cloudElevation - cameraY + 0.33F;
final float cellFractionX = (float) (cameraCellX - MathHelper.floor_double(cameraCellX));
@@ -393,7 +387,7 @@ public class CloudRenderer implements IResourceManagerReloadListener {
&& (faceMeshActive || cachedPlateLodCells == plateLodCells);
final boolean geomCacheValid = anchorInRange && settingsUnchanged;
final boolean canBuildAsync = settingsUnchanged && !faceMeshActive && vertexMesh.built();
final boolean canBuildAsync = settingsUnchanged && !faceMeshActive && vertexMesh.built() && CloudDisc.withinDisc((long) anchorX - anchorCellX, (long) anchorZ - anchorCellZ, radiusCells);
final boolean builderBusy = buildInFlight != null || uploadParams != null;
if (!geomCacheValid && !(canBuildAsync && builderBusy)) {
@@ -419,8 +413,7 @@ public class CloudRenderer implements IResourceManagerReloadListener {
if (wantInterior && (interiorAnchorX != anchorX || interiorAnchorZ != anchorZ
|| interiorShapeGeneration != shapeGeneration || interiorCellHeight != cellHeightBlocks
|| interiorShadersActive != shadersActive)) {
interiorMesh.buildInterior(shape, anchorX, anchorZ, cellHeightBlocks,
anchorX * SCROLL_SPEED, anchorZ * SCROLL_SPEED, untextured() && !shadersActive, shadersActive);
interiorMesh.buildInterior(shape, anchorX, anchorZ, cellHeightBlocks, textureOffset(anchorX, shape.width), textureOffset(anchorZ, shape.height), untextured() && !shadersActive, shadersActive);
interiorMesh.uploadBuilt();
interiorAnchorX = anchorX;
interiorAnchorZ = anchorZ;
@@ -517,7 +510,7 @@ public class CloudRenderer implements IResourceManagerReloadListener {
uniforms.modelView.set(modelViewScratch);
if (faceMeshActive) {
uniforms.setCellHeight(cellHeightBlocks);
uniforms.setScroll(faceMesh.buildAnchorX() * SCROLL_SPEED, faceMesh.buildAnchorZ() * SCROLL_SPEED);
uniforms.setScroll(textureOffset(faceMesh.buildAnchorX(), shape.width), textureOffset(faceMesh.buildAnchorZ(), shape.height), 1.0f / shape.width, 1.0f / shape.height);
}
uploadFogUniforms(uniforms, fogStart, fogEnd);
drawClouds(r, g, b, false);
@@ -552,11 +545,11 @@ public class CloudRenderer implements IResourceManagerReloadListener {
}
private static void buildVertexMesh(CloudVertexMesh mesh, BuildParams p) {
final float scrollX = p.anchorX() * SCROLL_SPEED;
final float scrollZ = p.anchorZ() * SCROLL_SPEED;
final float scrollX = textureOffset(p.anchorX(), p.shape().width);
final float scrollZ = textureOffset(p.anchorZ(), p.shape().height);
if (!p.fancy()) {
mesh.buildFast(p.radiusCells(), scrollX, scrollZ, p.shadersActive());
mesh.buildFast(p.shape(), p.radiusCells(), scrollX, scrollZ, p.shadersActive());
return;
}
@@ -23,6 +23,7 @@ final class CloudUniforms {
final GlUniformFloat4v scroll;
private final float[] vec4Buf = new float[4];
private float lastScrollX = Float.NaN, lastScrollZ = Float.NaN;
private float lastTextureScaleX = Float.NaN, lastTextureScaleZ = Float.NaN;
private int lastFogEnabled = -1;
private float lastFogR = Float.NaN, lastFogG = Float.NaN, lastFogB = Float.NaN;
private float lastColorR = Float.NaN, lastColorG = Float.NaN, lastColorB = Float.NaN;
@@ -41,14 +42,16 @@ final class CloudUniforms {
scroll = context.bindUniformIfPresent("u_Scroll", GlUniformFloat4v::new);
}
void setScroll(float x, float z) {
if (scroll == null || (x == lastScrollX && z == lastScrollZ)) return;
void setScroll(float x, float z, float textureScaleX, float textureScaleZ) {
if (scroll == null || (x == lastScrollX && z == lastScrollZ && textureScaleX == lastTextureScaleX && textureScaleZ == lastTextureScaleZ)) return;
lastScrollX = x;
lastScrollZ = z;
lastTextureScaleX = textureScaleX;
lastTextureScaleZ = textureScaleZ;
vec4Buf[0] = x;
vec4Buf[1] = z;
vec4Buf[2] = 0.0f;
vec4Buf[3] = 0.0f;
vec4Buf[2] = textureScaleX;
vec4Buf[3] = textureScaleZ;
scroll.set(vec4Buf);
}
@@ -16,7 +16,6 @@ import java.nio.IntBuffer;
import java.util.Arrays;
import static com.gtnewhorizons.angelica.render.CloudDisc.ALWAYS_DRAWN_CELLS;
import static com.gtnewhorizons.angelica.render.CloudDisc.SCROLL_SPEED;
import static com.gtnewhorizons.angelica.render.CloudDisc.WEDGE_COUNT;
import static com.gtnewhorizons.angelica.render.CloudDisc.wedgeOf;
import static org.joml.Math.clamp;
@@ -120,6 +119,7 @@ final class CloudVertexMesh {
private long[] wallSortKey = new long[4096];
private long[] wallSortScratch = new long[4096];
private final int[] radixCounts = new int[RADIX_SIZE];
private float textureScaleX, textureScaleZ;
private boolean forPackShader;
private int wallCutCells = Integer.MAX_VALUE;
private int wallClipRadiusSq;
@@ -160,8 +160,8 @@ final class CloudVertexMesh {
return wallStart;
}
void buildFast(int radiusCells, float scrollX, float scrollZ, boolean forPackShader) {
beginBuild(false, forPackShader);
void buildFast(CloudShape shape, int radiusCells, float scrollX, float scrollZ, boolean forPackShader) {
beginBuild(shape, false, forPackShader);
nextQuadDir = DIR_NONE;
nextQuadGroup = GROUP_PLATE_ALWAYS;
@@ -170,10 +170,10 @@ final class CloudVertexMesh {
nextQuadCellZ = 0;
final double minCell = -radiusCells;
final double maxCell = radiusCells;
final float uWest = -radiusCells * SCROLL_SPEED + scrollX;
final float uEast = radiusCells * SCROLL_SPEED + scrollX;
final float vNorth = -radiusCells * SCROLL_SPEED + scrollZ;
final float vSouth = radiusCells * SCROLL_SPEED + scrollZ;
final float uWest = -radiusCells * textureScaleX + scrollX;
final float uEast = radiusCells * textureScaleX + scrollX;
final float vNorth = -radiusCells * textureScaleZ + scrollZ;
final float vSouth = radiusCells * textureScaleZ + scrollZ;
ensureEmitCapacity(quadBytes);
emitQuad(minCell, 0.0, minCell, uWest, vNorth, maxCell, 0.0, minCell, uEast, vNorth,
maxCell, 0.0, maxCell, uEast, vSouth, minCell, 0.0, maxCell, uWest, vSouth, FACE_UP, NORMAL_POS_Y);
@@ -185,7 +185,7 @@ final class CloudVertexMesh {
double cellHeightBlocks, float scrollX, float scrollZ,
boolean emitUnderside, boolean emitTopSurface, boolean emitPlates,
int wallCut, int plateLodCells, boolean untextured, boolean forPack) {
beginBuild(untextured, forPack);
beginBuild(shape, untextured, forPack);
this.wallCutCells = wallCut;
buildAnchorX = anchorX;
buildAnchorZ = anchorZ;
@@ -211,12 +211,14 @@ final class CloudVertexMesh {
void buildInterior(CloudShape shape, int anchorX, int anchorZ, double deckTopY,
float scrollX, float scrollZ, boolean untextured, boolean forPack) {
beginBuild(untextured, forPack);
beginBuild(shape, untextured, forPack);
addInteriorFaces(shape, anchorX, anchorZ, deckTopY, scrollX, scrollZ);
finishGeometry();
}
private void beginBuild(boolean untextured, boolean forPack) {
private void beginBuild(CloudShape shape, boolean untextured, boolean forPack) {
textureScaleX = 1.0f / shape.width;
textureScaleZ = 1.0f / shape.height;
forPackShader = forPack;
if (!forPack && quadCell.length < quadFace.length) {
quadCell = new int[quadFace.length];
@@ -455,9 +457,9 @@ final class CloudVertexMesh {
if (dir == DIR_WEST || dir == DIR_EAST) {
final double z1 = relZ + span;
final float uCentre = (relX + 0.5F) * SCROLL_SPEED + scrollX;
final float vNorth = relZ * SCROLL_SPEED + scrollZ;
final float vSouth = (relZ + span) * SCROLL_SPEED + scrollZ;
final float uCentre = (relX + 0.5F) * textureScaleX + scrollX;
final float vNorth = relZ * textureScaleZ + scrollZ;
final float vSouth = (relZ + span) * textureScaleZ + scrollZ;
if (dir == DIR_WEST) {
emitQuad(x0, 0.0, z1, uCentre, vSouth, x0, deckTopY, z1, uCentre, vSouth,
x0, deckTopY, z0, uCentre, vNorth, x0, 0.0, z0, uCentre, vNorth, FACE_SIDE_X, NORMAL_NEG_X);
@@ -468,9 +470,9 @@ final class CloudVertexMesh {
}
} else {
final double x1 = relX + span;
final float uLeft = relX * SCROLL_SPEED + scrollX;
final float uRight = (relX + span) * SCROLL_SPEED + scrollX;
final float vCentre = (relZ + 0.5F) * SCROLL_SPEED + scrollZ;
final float uLeft = relX * textureScaleX + scrollX;
final float uRight = (relX + span) * textureScaleX + scrollX;
final float vCentre = (relZ + 0.5F) * textureScaleZ + scrollZ;
if (dir == DIR_NORTH) {
emitQuad(x0, deckTopY, z0, uLeft, vCentre, x1, deckTopY, z0, uRight, vCentre,
x1, 0.0, z0, uRight, vCentre, x0, 0.0, z0, uLeft, vCentre, FACE_SIDE_Z, NORMAL_NEG_Z);
@@ -485,10 +487,10 @@ final class CloudVertexMesh {
private void emitPlateRect(int relX0, int relZ0, int relX1, int relZ1, boolean emitUnderside, boolean emitTopSurface,
double plateTopY, float scrollX, float scrollZ, int bytesPerRect) {
final double x0 = relX0, x1 = relX1, z0 = relZ0, z1 = relZ1;
final float uLeft = relX0 * SCROLL_SPEED + scrollX;
final float uRight = relX1 * SCROLL_SPEED + scrollX;
final float vNorth = relZ0 * SCROLL_SPEED + scrollZ;
final float vSouth = relZ1 * SCROLL_SPEED + scrollZ;
final float uLeft = relX0 * textureScaleX + scrollX;
final float uRight = relX1 * textureScaleX + scrollX;
final float vNorth = relZ0 * textureScaleZ + scrollZ;
final float vSouth = relZ1 * textureScaleZ + scrollZ;
ensureEmitCapacity(bytesPerRect);
if (emitUnderside) {
@@ -511,11 +513,11 @@ final class CloudVertexMesh {
nextQuadCellZ = 0;
final float uLeft = scrollX;
final float uRight = SCROLL_SPEED + scrollX;
final float uCentre = 0.5F * SCROLL_SPEED + scrollX;
final float uRight = textureScaleX + scrollX;
final float uCentre = 0.5F * textureScaleX + scrollX;
final float vNorth = scrollZ;
final float vSouth = SCROLL_SPEED + scrollZ;
final float vCentre = 0.5F * SCROLL_SPEED + scrollZ;
final float vSouth = textureScaleZ + scrollZ;
final float vCentre = 0.5F * textureScaleZ + scrollZ;
final double x0 = 0.0;
final double x1 = 1.0;
@@ -689,7 +691,7 @@ final class CloudVertexMesh {
default -> false;
};
int band = (int) (Math.sqrt(cx * cx + cz * cz) * ORDER_BANDS_PER_CELL);
int band = (int) (Math.sqrt((double) cx * cx + (double) cz * cz) * ORDER_BANDS_PER_CELL);
if (band >= bands) band = bands - 1;
final int groupSlot = facingAway ? 2 + 2 * WEDGE_COUNT : switch (group) {