From 396e6b73765783c6e141848f5329ca56da0785e7 Mon Sep 17 00:00:00 2001 From: Srendi Date: Mon, 4 Nov 2024 16:06:47 +0100 Subject: [PATCH] Sphere renderer, very cursed --- .../client/RenderUtil.java | 205 ++++++++++++------ .../OverlayModuleLevelRenderer.java | 17 +- .../smartglasses/OverlayObjectHolder.java | 2 + .../objects/threedim/BoxRenderer.java | 4 +- .../objects/threedim/SphereRenderer.java | 49 +++++ .../overlay/OverlayGlassesFunctions.java | 9 + .../objects/three_dim/SphereObject.java | 88 ++++++++ 7 files changed, 306 insertions(+), 68 deletions(-) create mode 100644 src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/SphereRenderer.java create mode 100644 src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/objects/three_dim/SphereObject.java diff --git a/src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java b/src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java index 8232f1c90..47333264a 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java @@ -1,5 +1,6 @@ package de.srendi.advancedperipherals.client; +import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.math.Matrix4f; @@ -48,66 +49,139 @@ public static void drawBox(PoseStack stack, VertexConsumer buffer, float r, floa public static void drawPlane(PoseStack stack, VertexConsumer buffer, float r, float g, float b, float a, Direction perspective, float pX, float pY, float pZ, float sX, float sY, float sZ) { stack.pushPose(); + + pX = pX + 0.5f; + pY = pY + 0.5f; + pZ = pZ + 0.5f; + + stack.translate(pX, pY, pZ); + Matrix4f matrix4f = stack.last().pose(); sX = sX / 2; sY = sY / 2; sZ = sZ / 2; - pX = pX + 0.5f; - pY = pY + 0.5f; - pZ = pZ + 0.5f; - if (perspective == Direction.UP) { - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } if (perspective == Direction.DOWN) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); } if (perspective == Direction.SOUTH) { - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); } if (perspective == Direction.NORTH) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); } if (perspective == Direction.EAST) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } if (perspective == Direction.WEST) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } stack.popPose(); } + public static void drawSphere(PoseStack poseStack, BufferBuilder consumer, float radius, double pX, double pY, double pZ, float r, float g, float b, float a, int sectors, int stacks) { + poseStack.pushPose(); + + poseStack.translate(pX, pY, pZ); + + Matrix4f matrix4f = poseStack.last().pose(); + + float z, xy; + float nx1, ny1, nz1, nx2, ny2, nz2, nx3, ny3, nz3, nx4, ny4, nz4, lengthInv = (1.0f / radius); // vertex normal + float s, t; //TODO vertex texCoord + + float sectorStep = (float) (2 * Math.PI / sectors); + float stackStep = (float) (Math.PI / stacks); + float sectorAngle, stackAngle; + + for (int i = 1; i <= stacks; ++i) { + stackAngle = (float) (Math.PI / 2 - i * stackStep); + + xy = (float) (radius * Math.cos(stackAngle)); + z = (float) (radius * Math.sin(stackAngle)); + + for (int j = 0; j < sectors; ++j) { + + sectorAngle = j * sectorStep; + + float x1 = (float) (xy * Math.cos(sectorAngle)); + float y1 = (float) (xy * Math.sin(sectorAngle)); + + float x2 = (float) (xy * Math.cos(sectorAngle + sectorStep)); + float y2 = (float) (xy * Math.sin(sectorAngle + sectorStep)); + + float x3 = (float) (radius * Math.cos(stackAngle + stackStep) * Math.cos(sectorAngle + sectorStep)); + float y3 = (float) (radius * Math.cos(stackAngle + stackStep) * Math.sin(sectorAngle + sectorStep)); + float z3 = (float) (radius * Math.sin(stackAngle + stackStep)); + + float x4 = (float) (radius * Math.cos(stackAngle + stackStep) * Math.cos(sectorAngle)); + float y4 = (float) (radius * Math.cos(stackAngle + stackStep) * Math.sin(sectorAngle)); + float z4 = (float) (radius * Math.sin(stackAngle + stackStep)); + + nx1 = x1 * lengthInv; + ny1 = y1 * lengthInv; + nz1 = z * lengthInv; + + nx2 = x2 * lengthInv; + ny2 = y2 * lengthInv; + nz2 = z * lengthInv; + + nx3 = x3 * lengthInv; + ny3 = y3 * lengthInv; + nz3 = z3 * lengthInv; + + nx4 = x4 * lengthInv; + ny4 = y4 * lengthInv; + nz4 = z4 * lengthInv; + + // Triangle 1 + consumer.vertex(matrix4f, x1, y1, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx1, ny1, nz1).endVertex(); + consumer.vertex(matrix4f, x3, y3, z3).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx3, ny3, nz3).endVertex(); + consumer.vertex(matrix4f, x4, y4, z4).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx4, ny4, nz4).endVertex(); + + // Triangle 2 + consumer.vertex(matrix4f, x1, y1, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx1, ny1, nz1).endVertex(); + consumer.vertex(matrix4f, x2, y2, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx2, ny2, nz2).endVertex(); + consumer.vertex(matrix4f, x3, y3, z3).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx3, ny3, nz3).endVertex(); + } + } + poseStack.popPose(); + + } + public static void drawVoxelShape(PoseStack pPoseStack, VertexConsumer pConsumer, VoxelShape pShape, double pX, double pY, double pZ, float r, float g, float b, float a) { for (Direction direction : Direction.values()) { pShape.calculateFace(direction).forAllBoxes((minX, minY, minZ, maxX, maxY, maxZ) -> { // Get the vertices for the face - float x1 = (float) (minX + pX); - float y1 = (float) (minY + pY); - float z1 = (float) (minZ + pZ); - float x2 = (float) (maxX + pX); - float y2 = (float) (maxY + pY); - float z2 = (float) (maxZ + pZ); + float x1 = (float) (minX); + float y1 = (float) (minY); + float z1 = (float) (minZ); + float x2 = (float) (maxX); + float y2 = (float) (maxY); + float z2 = (float) (maxZ); // Calculate the normal for the face Vec3i normalVec = direction.getNormal(); @@ -138,8 +212,8 @@ public static void drawShape(PoseStack pPoseStack, VertexConsumer pConsumer, Vox f /= f3; f1 /= f3; f2 /= f3; - pConsumer.vertex(pose.pose(), (float) (minX + pX), (float) (minY + pY), (float) (minZ + pZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex(); - pConsumer.vertex(pose.pose(), (float) (maxX + pX), (float) (maxY + pY), (float) (maxZ + pZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex(); + pConsumer.vertex(pose.pose(), (float) (minX), (float) (minY), (float) (minZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex(); + pConsumer.vertex(pose.pose(), (float) (maxX), (float) (maxY), (float) (maxZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex(); }); } @@ -171,6 +245,13 @@ public static void drawBox(PoseStack stack, VertexConsumer buffer, ResourceLocat public static void drawPlane(PoseStack stack, VertexConsumer buffer, ResourceLocation texture, Direction perspective, float pX, float pY, float pZ, float sX, float sY, float sZ, float pUOffset, float pVOffset, float pWidth, float pHeight) { stack.pushPose(); + + pX = pX + 0.5f; + pY = pY + 0.5f; + pZ = pZ + 0.5f; + + stack.translate(pX, pY, pZ); + Matrix4f matrix4f = stack.last().pose(); TextureAtlasSprite stillTexture = Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(texture); @@ -179,50 +260,46 @@ public static void drawPlane(PoseStack stack, VertexConsumer buffer, ResourceLoc sY = sY / 2; sZ = sZ / 2; - pX = pX + 0.5f; - pY = pY + 0.5f; - pZ = pZ + 0.5f; - float u1 = stillTexture.getU(pUOffset); float u2 = stillTexture.getU(pWidth); float v1 = stillTexture.getV(pVOffset); float v2 = stillTexture.getV(pHeight); if (perspective == Direction.UP) { - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } if (perspective == Direction.DOWN) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, -1f, 0f).endVertex(); } if (perspective == Direction.SOUTH) { - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); } if (perspective == Direction.NORTH) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(1f, 0f, 0f).endVertex(); } if (perspective == Direction.EAST) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, -sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, -sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, -sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } if (perspective == Direction.WEST) { - buffer.vertex(matrix4f, -sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, -sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); - buffer.vertex(matrix4f, -sX + pX, sY + pY, sZ + pZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, -sY, sZ).color(1, 1, 1, 1f).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, sX, sY, sZ).color(1, 1, 1, 1f).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); + buffer.vertex(matrix4f, -sX, sY, sZ).color(1, 1, 1, 1f).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(0f, 1f, 0f).endVertex(); } stack.popPose(); } diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleLevelRenderer.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleLevelRenderer.java index 1e325bd08..8a0fbac98 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleLevelRenderer.java +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleLevelRenderer.java @@ -65,7 +65,9 @@ public static void renderLevelState(RenderLevelStageEvent event) { ((IThreeDObjectRenderer) batch.get(0).getRenderObject()).renderBatch(batch, event, posestack, view, bufferbuilder); } - BlockPos blockPos = new BlockPos(2, 190, 0); + //TODO Everything below here is just for debugging and testing. Will be removed before we push to production + + BlockPos blockPos = new BlockPos(2, 10, 0); float[] colors = EnumColor.DARK_PURPLE.getRgb(); @@ -85,7 +87,7 @@ public static void renderLevelState(RenderLevelStageEvent event) { bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL); colors = EnumColor.LIGHT_PURPLE.getRgb(); - blockPos = new BlockPos(0, 190, 2); + blockPos = new BlockPos(0, 10, 2); posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ()); VoxelShape shape = Block.box(0.0, 0.0, 0.0, 16.0, 18.0, 16.0); @@ -97,6 +99,17 @@ public static void renderLevelState(RenderLevelStageEvent event) { BufferUploader.drawWithShader(bufferbuilder.end()); posestack.popPose(); + bufferbuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL); + posestack.pushPose(); + + blockPos = new BlockPos(0, 10, 0); + posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ()); + + RenderUtil.drawSphere(posestack, bufferbuilder, 0.5f, 0f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 128, 48); + + BufferUploader.drawWithShader(bufferbuilder.end()); + posestack.popPose(); + } } diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java index 7bd0888ce..54da6ea07 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java @@ -3,6 +3,7 @@ import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject; @@ -50,5 +51,6 @@ public static void registerDecodeObjects() { ObjectDecodeRegistry.register(BoxObject.TYPE_ID, BoxObject::decode); ObjectDecodeRegistry.register(BlockObject.TYPE_ID, BlockObject::decode); + ObjectDecodeRegistry.register(SphereObject.TYPE_ID, SphereObject::decode); } } diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/BoxRenderer.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/BoxRenderer.java index 235ac2227..873c1e7ea 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/BoxRenderer.java +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/BoxRenderer.java @@ -5,10 +5,10 @@ import com.mojang.blaze3d.vertex.BufferUploader; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexFormat; import de.srendi.advancedperipherals.client.RenderUtil; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject; import net.minecraft.client.renderer.GameRenderer; -import net.minecraft.client.renderer.RenderType; import net.minecraft.world.phys.Vec3; import net.minecraftforge.client.event.RenderLevelStageEvent; @@ -23,7 +23,7 @@ public void renderBatch(List batch, RenderLevelStageEven for (ThreeDimensionalObject renderableObject : batch) { poseStack.pushPose(); onPreRender(renderableObject); - bufferBuilder.begin(RenderType.translucent().mode(), DefaultVertexFormat.POSITION_COLOR_NORMAL); + bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL); RenderSystem.setShader(GameRenderer::getPositionColorShader); float alpha = renderableObject.opacity; diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/SphereRenderer.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/SphereRenderer.java new file mode 100644 index 000000000..e94071302 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/threedim/SphereRenderer.java @@ -0,0 +1,49 @@ +package de.srendi.advancedperipherals.client.smartglasses.objects.threedim; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.BufferUploader; +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexFormat; +import de.srendi.advancedperipherals.client.RenderUtil; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.world.phys.Vec3; +import net.minecraftforge.client.event.RenderLevelStageEvent; + +import java.util.List; + +public class SphereRenderer implements IThreeDObjectRenderer { + + @Override + public void renderBatch(List batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) { + poseStack.pushPose(); + + for (ThreeDimensionalObject renderableObject : batch) { + poseStack.pushPose(); + onPreRender(renderableObject); + bufferBuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL); + + SphereObject sphere = (SphereObject) renderableObject; + + RenderSystem.setShader(GameRenderer::getPositionColorShader); + float alpha = renderableObject.opacity; + float red = (float) (renderableObject.color >> 16 & 255) / 255.0F; + float green = (float) (renderableObject.color >> 8 & 255) / 255.0F; + float blue = (float) (renderableObject.color & 255) / 255.0F; + + poseStack.translate(-view.x + renderableObject.getX(), -view.y + renderableObject.getY(), -view.z + renderableObject.getZ()); + RenderUtil.drawSphere(poseStack, bufferBuilder, 1f, 0f, 0f, 0f, red, green, blue, alpha, sphere.sectors, sphere.stacks); + BufferUploader.drawWithShader(bufferBuilder.end()); + onPostRender(renderableObject); + + poseStack.popPose(); + } + + + poseStack.popPose(); + + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java index 2314856b7..ccb6ecc75 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java @@ -8,6 +8,7 @@ import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject; @@ -73,6 +74,14 @@ public final MethodResult createBox(IArguments arguments) throws LuaException { return MethodResult.of(object, "SUCCESS"); } + @LuaFunction + public final MethodResult createSphere(IArguments arguments) throws LuaException { + SphereObject block = new SphereObject(overlayModule, arguments); + RenderableObject object = overlayModule.addObject(block); + + return MethodResult.of(object, "SUCCESS"); + } + @LuaFunction public final MethodResult getObject(IArguments arguments) throws LuaException { int id = arguments.getInt(0); diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/objects/three_dim/SphereObject.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/objects/three_dim/SphereObject.java new file mode 100644 index 000000000..daa9f6bfe --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/objects/three_dim/SphereObject.java @@ -0,0 +1,88 @@ +package de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim; + +import dan200.computercraft.api.lua.IArguments; +import dan200.computercraft.api.lua.LuaException; +import de.srendi.advancedperipherals.AdvancedPeripherals; +import de.srendi.advancedperipherals.client.smartglasses.objects.IObjectRenderer; +import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.SphereRenderer; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.OverlayModule; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes.FixedPointNumberProperty; +import net.minecraft.network.FriendlyByteBuf; + +import java.util.UUID; + +public class SphereObject extends ThreeDimensionalObject { + public static final int TYPE_ID = 6; + + private final IObjectRenderer renderer = new SphereRenderer(); + + @FixedPointNumberProperty(min = 1, max = 1024) + public int sectors = 16; + + @FixedPointNumberProperty(min = 1, max = 1024) + public int stacks = 16; + + public SphereObject(OverlayModule module, IArguments arguments) throws LuaException { + super(module, arguments); + reflectivelyMapProperties(arguments); + } + + public SphereObject(UUID player) { + super(player); + } + + @Override + public void encode(FriendlyByteBuf buffer) { + buffer.writeInt(TYPE_ID); + super.encode(buffer); + buffer.writeInt(sectors); + buffer.writeInt(stacks); + } + + public static SphereObject decode(FriendlyByteBuf buffer) { + int objectId = buffer.readInt(); + boolean hasValidUUID = buffer.readBoolean(); + if (!hasValidUUID) { + AdvancedPeripherals.exception("Tried to decode a buffer for an OverlayObject but without a valid player as target.", new IllegalArgumentException()); + return null; + } + UUID player = buffer.readUUID(); + int color = buffer.readInt(); + float opacity = buffer.readFloat(); + + int x = buffer.readInt(); + int y = buffer.readInt(); + int z = buffer.readInt(); + int maxX = buffer.readInt(); + int maxY = buffer.readInt(); + int maxZ = buffer.readInt(); + + boolean disableDepthTest = buffer.readBoolean(); + boolean disableCulling = buffer.readBoolean(); + + int sectors = buffer.readInt(); + int stacks = buffer.readInt(); + + SphereObject clientObject = new SphereObject(player); + clientObject.setId(objectId); + clientObject.color = color; + clientObject.opacity = opacity; + clientObject.x = x; + clientObject.y = y; + clientObject.z = z; + clientObject.maxX = maxX; + clientObject.maxY = maxY; + clientObject.maxZ = maxZ; + clientObject.disableDepthTest = disableDepthTest; + clientObject.disableCulling = disableCulling; + clientObject.sectors = sectors; + clientObject.stacks = stacks; + + return clientObject; + } + + @Override + public IObjectRenderer getRenderObject() { + return renderer; + } +}