Skip to content

Commit

Permalink
simplify rendering some more
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Feb 16, 2025
1 parent 494e2e6 commit c4f6fb6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/main/java/folk/sisby/antique_atlas/MarkerTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void draw(DrawContext context, double markerX, double markerY, float mark
context.getMatrices().pop();
}

public void draw(MatrixStack matrices, VertexConsumerProvider vertexConsumers, double markerX, double markerY, float markerScale, int tileChunks, float[] accent, float tint, float alpha, int light) {
public void draw(MatrixStack matrices, VertexConsumerProvider vertexConsumers, double markerX, double markerY, float z, float markerScale, int tileChunks, float[] accent, float tint, float alpha, int light) {
if (alpha == 0) return;
matrices.push();
matrices.translate(markerX, markerY, 0.0);
Expand All @@ -104,20 +104,20 @@ public void draw(MatrixStack matrices, VertexConsumerProvider vertexConsumers, d
if (tileChunks > 1 && mipLevels > 0) {
int mipLevel = MathHelper.clamp(MathHelper.ceilLog2(tileChunks), 0, mipLevels);
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, id, fullTextureWidth(), textureHeight, light)) {
batcher.add(offsetX / (1 << mipLevel), offsetY / (1 << mipLevel), textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), getU(mipLevel), 0, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), mainArgb);
batcher.add(offsetX / (1 << mipLevel), offsetY / (1 << mipLevel), z, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), getU(mipLevel), 0, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), mainArgb);
}
if (accentId != null && accent != null) {
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, accentId, fullTextureWidth(), textureHeight, light)) {
batcher.add(offsetX / (1 << mipLevel), offsetY / (1 << mipLevel), textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), getU(mipLevel), 0, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), accentArgb);
batcher.add(offsetX / (1 << mipLevel), offsetY / (1 << mipLevel), z, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), getU(mipLevel), 0, textureWidth / (1 << mipLevel), textureHeight / (1 << mipLevel), accentArgb);
}
}
} else {
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, id, fullTextureWidth(), textureHeight, light)) {
batcher.add(offsetX, offsetY, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, mainArgb);
batcher.add(offsetX, offsetY, z, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, mainArgb);
}
if (accentId != null && accent != null) {
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, accentId, fullTextureWidth(), textureHeight, light)) {
batcher.add(offsetX, offsetY, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, accentArgb);
batcher.add(offsetX, offsetY, z, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, accentArgb);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/folk/sisby/antique_atlas/gui/AtlasScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float par3) {
tiles.setStep(tileChunks);

RenderSystem.setShaderColor(1, 1, 1, state.is(DELETING_MARKER) ? 0.5f : 1.0f);
renderTiles(context.getMatrices(), null, getGuiX() + MAP_BORDER_WIDTH, getGuiY() + MAP_BORDER_HEIGHT, mapWidth, mapHeight, mapStartScreenX, mapStartScreenY, mapScale, tilePixels, guiScale, 15728640, tiles);
renderTiles(context.getMatrices(), null, getGuiX() + MAP_BORDER_WIDTH, getGuiY() + MAP_BORDER_HEIGHT, 0, mapWidth, mapHeight, mapStartScreenX, mapStartScreenY, mapScale, tilePixels, guiScale, 15728640, tiles);
RenderSystem.setShaderColor(1, 1, 1, 1);

// Overlay the frame so that edges of the map are smooth:
Expand Down Expand Up @@ -700,7 +700,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float par3) {
}
}

public static void renderTiles(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int mapX, int mapY, int mapWidth, int mapHeight, double mapStartScreenX, double mapStartScreenY, double mapScale, int pixelsPerTile, double guiScale, int light, TileRenderIterator tiles) {
public static void renderTiles(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int mapX, int mapY, int z, int mapWidth, int mapHeight, double mapStartScreenX, double mapStartScreenY, double mapScale, int pixelsPerTile, double guiScale, int light, TileRenderIterator tiles) {
matrices.push();
matrices.translate(mapStartScreenX, mapStartScreenY, 0);
matrices.scale((float) (mapScale / guiScale), (float) (mapScale / guiScale), 1.0F);
Expand All @@ -720,7 +720,7 @@ public static void renderTiles(MatrixStack matrices, VertexConsumerProvider vert
int drawY = subtile.y * subTilePixels;
// a non-scope bounds check allows subtile-level accuracy, and keeps border tiling accurate.
if (drawX * (guiScale / mapScale) > mapX + mapWidth - mapStartScreenX || drawY * (guiScale / mapScale) > mapY + mapHeight - mapStartScreenY || (drawX + subTilePixels) * (guiScale / mapScale) < mapX - mapStartScreenX || (drawY + subTilePixels) * (guiScale / mapScale) < mapY - mapStartScreenY) continue;
batcher.add(drawX, drawY, subTilePixels, subTilePixels, subtile.getTextureU() * 8, subtile.getTextureV() * 8, 8, 8, 0xFFFFFFFF);
batcher.add(drawX, drawY, z, subTilePixels, subTilePixels, subtile.getTextureU() * 8, subtile.getTextureV() * 8, 8, 8, 0xFFFFFFFF);
}
}
});
Expand All @@ -741,7 +741,7 @@ private void renderPlayer(DrawContext context, PlayerSummary player, float iconS
int argb = ColorHelper.Argb.getArgb(state.is(PLACING_MARKER) ? 127 : 255, (int) (tint * 255), (int) (tint * greenTint * 255), (int) (tint * 255));
float playerRotation = ((float) Math.round(player.yaw() / 360f * PLAYER_ROTATION_STEPS) / PLAYER_ROTATION_STEPS) * 360f;

DrawUtil.drawCenteredWithRotation(context.getMatrices(), null, PLAYER, playerOffsetX, playerOffsetY, iconScale, PLAYER_ICON_WIDTH, PLAYER_ICON_HEIGHT, playerRotation, 15728640, argb);
DrawUtil.drawCenteredWithRotation(context.getMatrices(), null, PLAYER, playerOffsetX, playerOffsetY, 0, iconScale, PLAYER_ICON_WIDTH, PLAYER_ICON_HEIGHT, playerRotation, 15728640, argb);

if (hovering && !self) {
context.drawTooltip(textRenderer, Text.literal(player.username()).formatted(player.online() ? Formatting.LIGHT_PURPLE : Formatting.GRAY), (int) getMouseX() - getGuiX(), (int) getMouseY() - getGuiY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ void renderFirstPersonAtlas(MatrixStack matrices, VertexConsumerProvider vertexC
int mapHeight = bookHeight - MAP_BORDER_HEIGHT * 2;
int tileChunks = 1;

matrices.push();
matrices.translate(0, 0, 0.01);
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, AtlasScreen.BOOK, bookWidth, bookHeight, light)) {
batcher.add(bookX, bookY, bookWidth, bookHeight, 0, 0, bookWidth, bookHeight, 0xFFFFFFFF);
batcher.add(bookX, bookY, 0.01F, bookWidth, bookHeight, 0, 0, bookWidth, bookHeight, 0xFFFFFFFF);
}
matrices.pop();

if (MinecraftClient.getInstance().currentScreen instanceof AtlasScreen) {
matrices.pop();
Expand All @@ -86,25 +83,18 @@ void renderFirstPersonAtlas(MatrixStack matrices, VertexConsumerProvider vertexC
tiles.setScope(new Rect(mapStartChunkX, mapStartChunkZ, mapEndChunkX, mapEndChunkZ));
tiles.setStep(tileChunks);

matrices.push();
AtlasScreen.renderTiles(matrices, vertexConsumers, bookX + MAP_BORDER_WIDTH, bookY + MAP_BORDER_HEIGHT, mapWidth, mapHeight, mapStartScreenX, mapStartScreenY, 1, 16, 1, light, tiles);
matrices.pop();
AtlasScreen.renderTiles(matrices, vertexConsumers, bookX + MAP_BORDER_WIDTH, bookY + MAP_BORDER_HEIGHT, 0, mapWidth, mapHeight, mapStartScreenX, mapStartScreenY, 1, 16, 1, light, tiles);

matrices.push();
matrices.translate(0, 0, -0.02);
Rect2i mapArea = new Rect2i(bookX + MAP_BORDER_WIDTH, bookY + MAP_BORDER_HEIGHT, mapWidth, mapHeight);
worldAtlasData.getAllMarkers(tileChunks).forEach((landmark, texture) -> {
double markerX = worldXToScreenX(landmark.pos().getX(), bookX, mapOffsetX, mapWidth, 1) - bookX;
double markerY = worldZToScreenY(landmark.pos().getZ(), bookY, mapOffsetY, mapHeight, 1) - bookY;
DyeColor color = landmark.color();
Vector2d markerPoint = new Vector2d(markerX, markerY);
float alpha = (float) MathHelper.clamp(MathUtil.innerDistanceToEdge(mapArea, markerPoint) / 32.0, 0, 1);
texture.draw(matrices, vertexConsumers, markerX, markerY, 1, tileChunks, color == null ? null : color.getColorComponents(), 1F, alpha, light);
texture.draw(matrices, vertexConsumers, markerX, markerY, -0.02F, 1, tileChunks, color == null ? null : color.getColorComponents(), 1F, alpha, light);
});
matrices.pop();

matrices.push();
matrices.translate(0, 0, -0.04);
Map<UUID, PlayerSummary> friends = SurveyorClient.getFriends();
PlayerSummary playerSummary = friends.remove(SurveyorClient.getClientUuid());
Map<UUID, PlayerSummary> orderedFriends = new LinkedHashMap<>(friends);
Expand All @@ -116,17 +106,13 @@ void renderFirstPersonAtlas(MatrixStack matrices, VertexConsumerProvider vertexC
double playerOffsetX = worldXToScreenX(MinecraftClient.getInstance().player.getPos().getX(), bookX, mapOffsetX, mapWidth, 1) - bookX;
double playerOffsetY = worldZToScreenY(MinecraftClient.getInstance().player.getPos().getZ(), bookY, mapOffsetY, mapHeight, 1) - bookY;
float playerRotation = ((float) Math.round(MinecraftClient.getInstance().player.getHeadYaw() / 360f * PLAYER_ROTATION_STEPS) / PLAYER_ROTATION_STEPS) * 360f;
DrawUtil.drawCenteredWithRotation(matrices, vertexConsumers, PLAYER, playerOffsetX, playerOffsetY, 1, PLAYER_ICON_WIDTH, PLAYER_ICON_HEIGHT, playerRotation, light, argb);
DrawUtil.drawCenteredWithRotation(matrices, vertexConsumers, PLAYER, playerOffsetX, playerOffsetY, -0.04F, 1, PLAYER_ICON_WIDTH, PLAYER_ICON_HEIGHT, playerRotation, light, argb);
});

matrices.pop();
matrices.push();
// Overlay the frame so that edges of the map are smooth:
matrices.translate(0, 0, -0.03);
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, BOOK_FRAME, bookWidth, bookHeight, light)) {
batcher.add(bookX, bookY, bookWidth, bookHeight, 0, 0, bookWidth, bookHeight, 0xFFFFFFFF);
batcher.add(bookX, bookY, -0.03F, bookWidth, bookHeight, 0, 0, bookWidth, bookHeight, 0xFFFFFFFF);
}
matrices.pop();

matrices.pop();
ci.cancel();
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/folk/sisby/antique_atlas/util/DrawBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,25 @@ public class DrawBatcher implements AutoCloseable {
private final float textureHeight;
private final int light;

public DrawBatcher(MatrixStack matrices, Identifier texture, int textureWidth, int textureHeight, int light) {
RenderSystem.setShaderTexture(0, texture);
RenderSystem.setShader(GameRenderer::getPositionColorTexLightmapProgram);
this.matrix4f = matrices.peek().getPositionMatrix();
this.bufferBuilder = Tessellator.getInstance().getBuffer();
this.bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_LIGHT);
this.vertexConsumer = bufferBuilder;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
this.light = light;
}

public DrawBatcher(MatrixStack matrices, VertexConsumerProvider vertexConsumers, Identifier texture, int textureWidth, int textureHeight, int light) {
if (vertexConsumers == null) {
RenderSystem.setShaderTexture(0, texture);
RenderSystem.setShader(GameRenderer::getPositionColorTexLightmapProgram);
this.bufferBuilder = Tessellator.getInstance().getBuffer();
this.bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_LIGHT);
this.vertexConsumer = bufferBuilder;
} else {
this.bufferBuilder = null;
this.vertexConsumer = vertexConsumers.getBuffer(RenderLayer.getText(texture));
}
this.matrix4f = matrices.peek().getPositionMatrix();
this.bufferBuilder = null;
this.vertexConsumer = vertexConsumers.getBuffer(RenderLayer.getText(texture));
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
this.light = light;
}

public void add(int x, int y, int width, int height, int u, int v, int regionWidth, int regionHeight, int argb) {
this.innerAdd(x, x + width, y, y + height, 0,
public void add(int x, int y, float z, int width, int height, int u, int v, int regionWidth, int regionHeight, int argb) {
this.innerAdd(x, x + width, y, y + height, z,
(u + 0.0F) / textureWidth,
(u + (float) regionWidth) / textureWidth,
(v + 0.0F) / textureHeight,
Expand All @@ -53,7 +49,7 @@ public void add(int x, int y, int width, int height, int u, int v, int regionWid
);
}

private void innerAdd(int x1, int x2, int y1, int y2, int z, float u1, float u2, float v1, float v2, int argb) {
private void innerAdd(float x1, float x2, float y1, float y2, float z, float u1, float u2, float v1, float v2, int argb) {
vertexConsumer.vertex(matrix4f, x1, y1, z).color(argb).texture(u1, v1).light(light).next();
vertexConsumer.vertex(matrix4f, x1, y2, z).color(argb).texture(u1, v2).light(light).next();
vertexConsumer.vertex(matrix4f, x2, y2, z).color(argb).texture(u2, v2).light(light).next();
Expand All @@ -62,6 +58,6 @@ private void innerAdd(int x1, int x2, int y1, int y2, int z, float u1, float u2,

@Override
public void close() {
if (bufferBuilder != null) BufferRenderer.draw(bufferBuilder.end());
if (bufferBuilder != null) BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
}
}
4 changes: 2 additions & 2 deletions src/main/java/folk/sisby/antique_atlas/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import net.minecraft.util.math.RotationAxis;

public class DrawUtil {
public static void drawCenteredWithRotation(MatrixStack matrices, VertexConsumerProvider vertexConsumers, Identifier texture, double x, double y, float scale, int textureWidth, int textureHeight, float rotation, int light, int argb) {
public static void drawCenteredWithRotation(MatrixStack matrices, VertexConsumerProvider vertexConsumers, Identifier texture, double x, double y, float z, float scale, int textureWidth, int textureHeight, float rotation, int light, int argb) {
matrices.push();
matrices.translate(x, y, 0.0);
matrices.scale(scale, scale, 1.0F);
matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(180 + rotation));
matrices.translate(-textureWidth / 2f, -textureHeight / 2f, 0f);
try (DrawBatcher batcher = new DrawBatcher(matrices, vertexConsumers, texture, textureWidth, textureHeight, light)) {
batcher.add(0, 0, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, argb);
batcher.add(0, 0, z, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, argb);
}
matrices.pop();
}
Expand Down

0 comments on commit c4f6fb6

Please sign in to comment.