From b29896858ef1241b6e12b7ff1d552137cfc0b76b Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 31 May 2020 14:29:51 -0300 Subject: [PATCH] much better, fixes #301 --- .../carpet/script/utils/ShapesRenderer.java | 111 +++++++++++------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/main/java/carpet/script/utils/ShapesRenderer.java b/src/main/java/carpet/script/utils/ShapesRenderer.java index 99de0c31..a4590182 100644 --- a/src/main/java/carpet/script/utils/ShapesRenderer.java +++ b/src/main/java/carpet/script/utils/ShapesRenderer.java @@ -1,15 +1,18 @@ package carpet.script.utils; import carpet.CarpetSettings; +import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.render.RenderLayer; -import net.minecraft.client.render.VertexConsumer; +import net.minecraft.client.render.BufferBuilder; +import net.minecraft.client.render.Tessellator; import net.minecraft.client.render.VertexConsumerProvider; -import net.minecraft.client.util.math.Matrix4f; +import net.minecraft.client.render.VertexFormats; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.Entity; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.IWorld; import net.minecraft.world.dimension.DimensionType; +import org.lwjgl.opengl.GL11; import java.util.ArrayList; import java.util.HashMap; @@ -41,14 +44,26 @@ public class ShapesRenderer public void render(MatrixStack matrices, VertexConsumerProvider vertexConsumers, double cameraX, double cameraY, double cameraZ) { - //Camera camera = this.client.gameRenderer.getCamera(); IWorld iWorld = this.client.world; DimensionType dimensionType = iWorld.getDimension().getType(); if (shapes.get(dimensionType).isEmpty()) return; - //BlockPos blockPos = new BlockPos(camera.getPos().x, 0.0D, camera.getPos().z); - VertexConsumer vertexConsumer = vertexConsumers.getBuffer(RenderLayer.getLines()); long currentTime = client.world.getTime(); + RenderSystem.enableDepthTest(); + RenderSystem.shadeModel(7425); + RenderSystem.enableAlphaTest(); + RenderSystem.defaultAlphaFunc(); + Entity entity = this.client.gameRenderer.getCamera().getFocusedEntity(); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder bufferBuilder = tessellator.getBuffer(); + double d = 0.0D - cameraY; + double e = 256.0D - cameraY; + RenderSystem.disableTexture(); + RenderSystem.disableBlend(); + double f = (double)(entity.chunkX << 4) - cameraX; + double g = (double)(entity.chunkZ << 4) - cameraZ; + + // render synchronized (shapes) { Iterator> it = shapes.get(dimensionType).iterator(); @@ -56,10 +71,16 @@ public class ShapesRenderer { RenderedShape shape = it.next(); if (shape.isExpired(currentTime)) it.remove(); - shape.render(matrices, vertexConsumer, (float) cameraX, (float) cameraY, (float) cameraZ); + shape.render(tessellator, bufferBuilder, (float) cameraX, (float) cameraY, (float) cameraZ); } } + + RenderSystem.lineWidth(1.0F); + RenderSystem.enableBlend(); + RenderSystem.enableTexture(); + RenderSystem.shadeModel(7424); } + public void addShape(String type, DimensionType dim, CompoundTag tag) { BiFunction> shapeFactory; @@ -86,7 +107,7 @@ public class ShapesRenderer { T shape; long expiryTick; - public abstract void render(MatrixStack matrices, VertexConsumer vertexConsumer, float cx, float cy, float cz ); + public abstract void render(Tessellator tessellator, BufferBuilder builder, float cx, float cy, float cz ); protected RenderedShape(MinecraftClient client, T shape) { this.shape = shape; @@ -106,14 +127,18 @@ public class ShapesRenderer super(client, (ShapeDispatcher.Box)ShapeDispatcher.Box.fromTag(boxData)); } @Override - public void render(MatrixStack matrices, VertexConsumer vertexConsumer, float cx, float cy, float cz) + public void render(Tessellator tessellator, BufferBuilder bufferBuilder, float cx, float cy, float cz) { - drawBox(matrices, vertexConsumer, + RenderSystem.lineWidth(2.0F); + bufferBuilder.begin(GL11.GL_LINES, VertexFormats.POSITION_COLOR); // 3 + drawBox(bufferBuilder, shape.x1-cx, shape.y1-cy, shape.z1-cz, shape.x2-cx, shape.y2-cy, shape.z2-cz, shape.r, shape.g, shape.b, shape.a, shape.r, shape.g, shape.b ); + tessellator.draw(); } + } public static class RenderedLine extends RenderedShape @@ -123,49 +148,51 @@ public class ShapesRenderer super(client, (ShapeDispatcher.Line)ShapeDispatcher.Line.fromTag(boxData)); } @Override - public void render(MatrixStack matrices, VertexConsumer vertexConsumer, float cx, float cy, float cz) + public void render(Tessellator tessellator, BufferBuilder bufferBuilder, float cx, float cy, float cz) { - drawLine(matrices, vertexConsumer, + RenderSystem.lineWidth(2.0F); + bufferBuilder.begin(GL11.GL_LINES, VertexFormats.POSITION_COLOR); // 3 + drawLine(bufferBuilder, shape.x1-cx, shape.y1-cy, shape.z1-cz, shape.x2-cx, shape.y2-cy, shape.z2-cz, shape.r, shape.g, shape.b, shape.a ); + tessellator.draw(); } } // some raw shit - public static void drawLine(MatrixStack matrix, VertexConsumer vertexConsumer, float x1, float y1, float z1, float x2, float y2, float z2, float red1, float grn1, float blu1, float alpha) { - Matrix4f matrix4f = matrix.peek().getModel(); - vertexConsumer.vertex(matrix4f, x1, y1, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z2).color(red1, grn1, blu1, alpha).next(); + public static void drawLine(BufferBuilder builder, float x1, float y1, float z1, float x2, float y2, float z2, float red1, float grn1, float blu1, float alpha) { + + builder.vertex(x1, y1, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z2).color(red1, grn1, blu1, alpha).next(); } - public static void drawBox(MatrixStack matrix, VertexConsumer vertexConsumer, float x1, float y1, float z1, float x2, float y2, float z2, float red1, float grn1, float blu1, float alpha, float red2, float grn2, float blu2) { - Matrix4f matrix4f = matrix.peek().getModel(); - vertexConsumer.vertex(matrix4f, x1, y1, z1).color(red1, grn2, blu2, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z1).color(red1, grn2, blu2, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y1, z1).color(red2, grn1, blu2, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z1).color(red2, grn1, blu2, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y1, z1).color(red2, grn2, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y1, z2).color(red2, grn2, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y1, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y1, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x1, y2, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y1, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z2).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z1).color(red1, grn1, blu1, alpha).next(); - vertexConsumer.vertex(matrix4f, x2, y2, z2).color(red1, grn1, blu1, alpha).next(); + public static void drawBox(BufferBuilder builder, float x1, float y1, float z1, float x2, float y2, float z2, float red1, float grn1, float blu1, float alpha, float red2, float grn2, float blu2) { + builder.vertex(x1, y1, z1).color(red1, grn2, blu2, alpha).next(); + builder.vertex(x2, y1, z1).color(red1, grn2, blu2, alpha).next(); + builder.vertex(x1, y1, z1).color(red2, grn1, blu2, alpha).next(); + builder.vertex(x1, y2, z1).color(red2, grn1, blu2, alpha).next(); + builder.vertex(x1, y1, z1).color(red2, grn2, blu1, alpha).next(); + builder.vertex(x1, y1, z2).color(red2, grn2, blu1, alpha).next(); + builder.vertex(x2, y1, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y2, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y2, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y2, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y2, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y1, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y1, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y1, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y1, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y1, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x1, y2, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y1, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z2).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z1).color(red1, grn1, blu1, alpha).next(); + builder.vertex(x2, y2, z2).color(red1, grn1, blu1, alpha).next(); } }