From c578970a66183fa461db6f81ccdb708f8240cc2a Mon Sep 17 00:00:00 2001 From: Eclipse-Sol Date: Sun, 20 Sep 2026 13:16:03 -0400 Subject: [PATCH] Fix edge cases with cloud wraps (#2122) --- .../angelica/render/CloudDisc.java | 18 ++++++- .../angelica/render/CloudRenderer.java | 27 ++++------ .../angelica/render/CloudUniforms.java | 11 ++-- .../angelica/render/CloudVertexMesh.java | 52 ++++++++++--------- .../assets/angelica/shaders/cloud_faces.vert | 3 +- 5 files changed, 62 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/gtnewhorizons/angelica/render/CloudDisc.java b/src/main/java/com/gtnewhorizons/angelica/render/CloudDisc.java index 9cfebb1de..965316f66 100644 --- a/src/main/java/com/gtnewhorizons/angelica/render/CloudDisc.java +++ b/src/main/java/com/gtnewhorizons/angelica/render/CloudDisc.java @@ -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); diff --git a/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java b/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java index 17a141469..d80e5b994 100644 --- a/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java @@ -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; } diff --git a/src/main/java/com/gtnewhorizons/angelica/render/CloudUniforms.java b/src/main/java/com/gtnewhorizons/angelica/render/CloudUniforms.java index 56960dc43..f3004c1f9 100644 --- a/src/main/java/com/gtnewhorizons/angelica/render/CloudUniforms.java +++ b/src/main/java/com/gtnewhorizons/angelica/render/CloudUniforms.java @@ -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); } diff --git a/src/main/java/com/gtnewhorizons/angelica/render/CloudVertexMesh.java b/src/main/java/com/gtnewhorizons/angelica/render/CloudVertexMesh.java index ecd215cce..7889d5b0d 100644 --- a/src/main/java/com/gtnewhorizons/angelica/render/CloudVertexMesh.java +++ b/src/main/java/com/gtnewhorizons/angelica/render/CloudVertexMesh.java @@ -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) { diff --git a/src/main/resources/assets/angelica/shaders/cloud_faces.vert b/src/main/resources/assets/angelica/shaders/cloud_faces.vert index 2dec14732..2c5755f3c 100644 --- a/src/main/resources/assets/angelica/shaders/cloud_faces.vert +++ b/src/main/resources/assets/angelica/shaders/cloud_faces.vert @@ -12,7 +12,6 @@ flat out float v_Shade; #ifndef UNTEXTURED uniform vec4 u_Scroll; out vec2 v_UV; -const float SCROLL_SPEED = 1.0 / 256.0; const int DIR_NORTH = 2; const int DIR_WEST = 4; #endif @@ -50,7 +49,7 @@ void main() { vec2 uvCell = vec2(float(cellX), float(cellZ)) + unit.xz; if (dir >= DIR_WEST) uvCell.x = float(cellX) + 0.5; else if (dir >= DIR_NORTH) uvCell.y = float(cellZ) + 0.5; - v_UV = uvCell * SCROLL_SPEED + u_Scroll.xy; + v_UV = uvCell * u_Scroll.zw + u_Scroll.xy; #endif gl_Position = u_MVPMatrix * vec4(pos, 1.0);