| 1 | package com.jellypudding.offlineclient.render;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 7 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 8 | import net.minecraft.client.Camera;
|
| 9 | import net.minecraft.client.renderer.StagedVertexBuffer;
|
| 10 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.Direction;
|
| 13 | import net.minecraft.world.phys.AABB;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 | import org.joml.Vector3f;
|
| 16 |
|
| 17 | import java.util.ArrayList;
|
| 18 | import java.util.List;
|
| 19 |
|
| 20 | // Collects lines and boxes from every Render3DEvent handler and draws
|
| 21 | // them in a single upload. All coordinates are world coordinates.
|
| 22 | public final class DrawBatch {
|
| 23 |
|
| 24 | // Thinner lines vanish against terrain at any distance.
|
| 25 | private static final float LINE_WIDTH = 2;
|
| 26 |
|
| 27 | // Direction.values allocates a fresh array on every call.
|
| 28 | private static final Direction[] SIDES = Direction.values();
|
| 29 |
|
| 30 | // A batch is built every frame and most have nothing to draw.
|
| 31 | private StagedVertexBuffer buffer;
|
| 32 | private final List<StagedVertexBuffer.Draw> draws = new ArrayList<>();
|
| 33 | private final List<RenderType> types = new ArrayList<>();
|
| 34 |
|
| 35 | private final PoseStack.Pose pose;
|
| 36 | private final Vec3 camera;
|
| 37 |
|
| 38 | public DrawBatch(PoseStack poseStack) {
|
| 39 | this.pose = poseStack.last();
|
| 40 | this.camera = cameraPos();
|
| 41 | }
|
| 42 |
|
| 43 | public static Vec3 cameraPos() {
|
| 44 | Camera camera = OfflineClient.MC.gameRenderer.mainCamera();
|
| 45 | return camera == null ? Vec3.ZERO : camera.position();
|
| 46 | }
|
| 47 |
|
| 48 | // A point in front of the camera that tracer lines start from.
|
| 49 | private static Vec3 tracerOrigin() {
|
| 50 | Camera camera = OfflineClient.MC.gameRenderer.mainCamera();
|
| 51 | if (camera == null) {
|
| 52 | return Vec3.ZERO;
|
| 53 | }
|
| 54 | double yaw = Math.toRadians(camera.yRot());
|
| 55 | double pitch = Math.toRadians(camera.xRot());
|
| 56 | return new Vec3(-Math.sin(yaw) * Math.cos(pitch), -Math.sin(pitch),
|
| 57 | Math.cos(yaw) * Math.cos(pitch)).scale(10);
|
| 58 | }
|
| 59 |
|
| 60 | public void line(Vec3 from, Vec3 to, int color, boolean throughWalls) {
|
| 61 | lineRelative(from.subtract(camera), to.subtract(camera), color, throughWalls);
|
| 62 | }
|
| 63 |
|
| 64 | public void tracer(Vec3 to, int color, boolean throughWalls) {
|
| 65 | lineRelative(tracerOrigin(), to.subtract(camera), color, throughWalls);
|
| 66 | }
|
| 67 |
|
| 68 | // Pulled in off the faces. A box flush with a block fights it for depth.
|
| 69 | public static final double BLOCK_INSET = 0.002;
|
| 70 |
|
| 71 | // The bounds of a block pulled in off its faces.
|
| 72 | public static AABB blockBox(BlockPos pos) {
|
| 73 | return new AABB(pos).deflate(BLOCK_INSET);
|
| 74 | }
|
| 75 |
|
| 76 | // Outlines a single block.
|
| 77 | public void outlineBlock(BlockPos pos, int color, boolean throughWalls) {
|
| 78 | outlineBox(blockBox(pos), color, throughWalls);
|
| 79 | }
|
| 80 |
|
| 81 | // A bit per side for the joined box calls.
|
| 82 | public static int sideBit(Direction side) {
|
| 83 | return 1 << side.ordinal();
|
| 84 | }
|
| 85 |
|
| 86 | // A bit for every horizontal side on which the set holds a neighbour of the key.
|
| 87 | public static int sharedSides(LongSet keys, long key) {
|
| 88 | int hidden = 0;
|
| 89 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 90 | if (keys.contains(BlockPos.offset(key, side))) {
|
| 91 | hidden |= sideBit(side);
|
| 92 | }
|
| 93 | }
|
| 94 | return hidden;
|
| 95 | }
|
| 96 |
|
| 97 | // The faces of a box that the mask does not hide.
|
| 98 | public void solidBoxPart(AABB box, int hidden, int color, boolean throughWalls) {
|
| 99 | if (hidden == 0) {
|
| 100 | solidBox(box, color, throughWalls);
|
| 101 | return;
|
| 102 | }
|
| 103 | for (Direction side : SIDES) {
|
| 104 | if ((hidden & sideBit(side)) == 0) {
|
| 105 | solidFace(box, side, color, throughWalls);
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | // The edges of a box leaving out every edge that lies on a hidden face.
|
| 111 | public void outlineBoxPart(AABB box, int hidden, int color, boolean throughWalls) {
|
| 112 | if (hidden == 0) {
|
| 113 | outlineBox(box, color, throughWalls);
|
| 114 | return;
|
| 115 | }
|
| 116 | AABB b = box.move(camera.reverse());
|
| 117 | float x1 = (float) b.minX;
|
| 118 | float y1 = (float) b.minY;
|
| 119 | float z1 = (float) b.minZ;
|
| 120 | float x2 = (float) b.maxX;
|
| 121 | float y2 = (float) b.maxY;
|
| 122 | float z2 = (float) b.maxZ;
|
| 123 |
|
| 124 | if (shown(hidden, Direction.DOWN, Direction.NORTH)) {
|
| 125 | edge(x1, y1, z1, x2, y1, z1, color, throughWalls);
|
| 126 | }
|
| 127 | if (shown(hidden, Direction.DOWN, Direction.EAST)) {
|
| 128 | edge(x2, y1, z1, x2, y1, z2, color, throughWalls);
|
| 129 | }
|
| 130 | if (shown(hidden, Direction.DOWN, Direction.SOUTH)) {
|
| 131 | edge(x2, y1, z2, x1, y1, z2, color, throughWalls);
|
| 132 | }
|
| 133 | if (shown(hidden, Direction.DOWN, Direction.WEST)) {
|
| 134 | edge(x1, y1, z2, x1, y1, z1, color, throughWalls);
|
| 135 | }
|
| 136 | if (shown(hidden, Direction.UP, Direction.NORTH)) {
|
| 137 | edge(x1, y2, z1, x2, y2, z1, color, throughWalls);
|
| 138 | }
|
| 139 | if (shown(hidden, Direction.UP, Direction.EAST)) {
|
| 140 | edge(x2, y2, z1, x2, y2, z2, color, throughWalls);
|
| 141 | }
|
| 142 | if (shown(hidden, Direction.UP, Direction.SOUTH)) {
|
| 143 | edge(x2, y2, z2, x1, y2, z2, color, throughWalls);
|
| 144 | }
|
| 145 | if (shown(hidden, Direction.UP, Direction.WEST)) {
|
| 146 | edge(x1, y2, z2, x1, y2, z1, color, throughWalls);
|
| 147 | }
|
| 148 | if (shown(hidden, Direction.NORTH, Direction.WEST)) {
|
| 149 | edge(x1, y1, z1, x1, y2, z1, color, throughWalls);
|
| 150 | }
|
| 151 | if (shown(hidden, Direction.NORTH, Direction.EAST)) {
|
| 152 | edge(x2, y1, z1, x2, y2, z1, color, throughWalls);
|
| 153 | }
|
| 154 | if (shown(hidden, Direction.SOUTH, Direction.EAST)) {
|
| 155 | edge(x2, y1, z2, x2, y2, z2, color, throughWalls);
|
| 156 | }
|
| 157 | if (shown(hidden, Direction.SOUTH, Direction.WEST)) {
|
| 158 | edge(x1, y1, z2, x1, y2, z2, color, throughWalls);
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | // An edge shows only whilst both of the faces it lies on do.
|
| 163 | private static boolean shown(int hidden, Direction a, Direction b) {
|
| 164 | return (hidden & (sideBit(a) | sideBit(b))) == 0;
|
| 165 | }
|
| 166 |
|
| 167 | public void outlineBox(AABB box, int color, boolean throughWalls) {
|
| 168 | AABB b = box.move(camera.reverse());
|
| 169 | float x1 = (float) b.minX;
|
| 170 | float y1 = (float) b.minY;
|
| 171 | float z1 = (float) b.minZ;
|
| 172 | float x2 = (float) b.maxX;
|
| 173 | float y2 = (float) b.maxY;
|
| 174 | float z2 = (float) b.maxZ;
|
| 175 |
|
| 176 | edge(x1, y1, z1, x2, y1, z1, color, throughWalls);
|
| 177 | edge(x2, y1, z1, x2, y1, z2, color, throughWalls);
|
| 178 | edge(x2, y1, z2, x1, y1, z2, color, throughWalls);
|
| 179 | edge(x1, y1, z2, x1, y1, z1, color, throughWalls);
|
| 180 | edge(x1, y2, z1, x2, y2, z1, color, throughWalls);
|
| 181 | edge(x2, y2, z1, x2, y2, z2, color, throughWalls);
|
| 182 | edge(x2, y2, z2, x1, y2, z2, color, throughWalls);
|
| 183 | edge(x1, y2, z2, x1, y2, z1, color, throughWalls);
|
| 184 | edge(x1, y1, z1, x1, y2, z1, color, throughWalls);
|
| 185 | edge(x2, y1, z1, x2, y2, z1, color, throughWalls);
|
| 186 | edge(x2, y1, z2, x2, y2, z2, color, throughWalls);
|
| 187 | edge(x1, y1, z2, x1, y2, z2, color, throughWalls);
|
| 188 | }
|
| 189 |
|
| 190 | // The four edges of a rectangle lying flat at one height.
|
| 191 | public void flatRect(double x1, double z1, double x2, double z2, double y,
|
| 192 | int color, boolean throughWalls) {
|
| 193 | line(new Vec3(x1, y, z1), new Vec3(x2, y, z1), color, throughWalls);
|
| 194 | line(new Vec3(x2, y, z1), new Vec3(x2, y, z2), color, throughWalls);
|
| 195 | line(new Vec3(x2, y, z2), new Vec3(x1, y, z2), color, throughWalls);
|
| 196 | line(new Vec3(x1, y, z2), new Vec3(x1, y, z1), color, throughWalls);
|
| 197 | }
|
| 198 |
|
| 199 | public void solidBox(AABB box, int color, boolean throughWalls) {
|
| 200 | VertexConsumer vc = buffer(Pipelines.fill(throughWalls));
|
| 201 | AABB b = box.move(camera.reverse());
|
| 202 | float x1 = (float) b.minX;
|
| 203 | float y1 = (float) b.minY;
|
| 204 | float z1 = (float) b.minZ;
|
| 205 | float x2 = (float) b.maxX;
|
| 206 | float y2 = (float) b.maxY;
|
| 207 | float z2 = (float) b.maxZ;
|
| 208 |
|
| 209 | quad(vc, color, x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2);
|
| 210 | quad(vc, color, x1, y2, z1, x1, y2, z2, x2, y2, z2, x2, y2, z1);
|
| 211 | quad(vc, color, x1, y1, z1, x1, y2, z1, x2, y2, z1, x2, y1, z1);
|
| 212 | quad(vc, color, x2, y1, z1, x2, y2, z1, x2, y2, z2, x2, y1, z2);
|
| 213 | quad(vc, color, x1, y1, z2, x2, y1, z2, x2, y2, z2, x1, y2, z2);
|
| 214 | quad(vc, color, x1, y1, z1, x1, y1, z2, x1, y2, z2, x1, y2, z1);
|
| 215 | }
|
| 216 |
|
| 217 | // One face of a box as a tinted quad.
|
| 218 | public void solidFace(AABB box, Direction side, int color, boolean throughWalls) {
|
| 219 | float[] c = faceCorners(box, side);
|
| 220 | quad(buffer(Pipelines.fill(throughWalls)), color,
|
| 221 | c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11]);
|
| 222 | }
|
| 223 |
|
| 224 | // The four edges of one face of a box.
|
| 225 | public void outlineFace(AABB box, Direction side, int color, boolean throughWalls) {
|
| 226 | float[] c = faceCorners(box, side);
|
| 227 | edge(c[0], c[1], c[2], c[3], c[4], c[5], color, throughWalls);
|
| 228 | edge(c[3], c[4], c[5], c[6], c[7], c[8], color, throughWalls);
|
| 229 | edge(c[6], c[7], c[8], c[9], c[10], c[11], color, throughWalls);
|
| 230 | edge(c[9], c[10], c[11], c[0], c[1], c[2], color, throughWalls);
|
| 231 | }
|
| 232 |
|
| 233 | // The corners of one face in order round its edge relative to the camera.
|
| 234 | private float[] faceCorners(AABB box, Direction side) {
|
| 235 | AABB b = box.move(camera.reverse());
|
| 236 | float x1 = (float) b.minX;
|
| 237 | float y1 = (float) b.minY;
|
| 238 | float z1 = (float) b.minZ;
|
| 239 | float x2 = (float) b.maxX;
|
| 240 | float y2 = (float) b.maxY;
|
| 241 | float z2 = (float) b.maxZ;
|
| 242 | return switch (side) {
|
| 243 | case DOWN -> new float[] {x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2};
|
| 244 | case UP -> new float[] {x1, y2, z1, x1, y2, z2, x2, y2, z2, x2, y2, z1};
|
| 245 | case NORTH -> new float[] {x1, y1, z1, x1, y2, z1, x2, y2, z1, x2, y1, z1};
|
| 246 | case SOUTH -> new float[] {x1, y1, z2, x2, y1, z2, x2, y2, z2, x1, y2, z2};
|
| 247 | case WEST -> new float[] {x1, y1, z1, x1, y1, z2, x1, y2, z2, x1, y2, z1};
|
| 248 | case EAST -> new float[] {x2, y1, z1, x2, y2, z1, x2, y2, z2, x2, y1, z2};
|
| 249 | };
|
| 250 | }
|
| 251 |
|
| 252 | // One filled face between four world points in order around its edge.
|
| 253 | public void quad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color, boolean throughWalls) {
|
| 254 | VertexConsumer vc = buffer(Pipelines.fill(throughWalls));
|
| 255 | Vec3 ra = a.subtract(camera);
|
| 256 | Vec3 rb = b.subtract(camera);
|
| 257 | Vec3 rc = c.subtract(camera);
|
| 258 | Vec3 rd = d.subtract(camera);
|
| 259 | quad(vc, color, (float) ra.x, (float) ra.y, (float) ra.z, (float) rb.x, (float) rb.y, (float) rb.z,
|
| 260 | (float) rc.x, (float) rc.y, (float) rc.z, (float) rd.x, (float) rd.y, (float) rd.z);
|
| 261 | }
|
| 262 |
|
| 263 | // The four edges of a face between four world points.
|
| 264 | public void outlineQuad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color, boolean throughWalls) {
|
| 265 | line(a, b, color, throughWalls);
|
| 266 | line(b, c, color, throughWalls);
|
| 267 | line(c, d, color, throughWalls);
|
| 268 | line(d, a, color, throughWalls);
|
| 269 | }
|
| 270 |
|
| 271 | // A box whose sides blend from one colour at the bottom to another at the top.
|
| 272 | public void gradientBox(AABB box, int bottom, int top, boolean throughWalls) {
|
| 273 | solidFace(box, Direction.DOWN, bottom, throughWalls);
|
| 274 | solidFace(box, Direction.UP, top, throughWalls);
|
| 275 | gradientSides(box, bottom, top, throughWalls);
|
| 276 | }
|
| 277 |
|
| 278 | // The four upright faces of a box blending from bottom colour to top colour.
|
| 279 | public void gradientSides(AABB box, int bottom, int top, boolean throughWalls) {
|
| 280 | VertexConsumer vc = buffer(Pipelines.fill(throughWalls));
|
| 281 | AABB b = box.move(camera.reverse());
|
| 282 | float x1 = (float) b.minX;
|
| 283 | float y1 = (float) b.minY;
|
| 284 | float z1 = (float) b.minZ;
|
| 285 | float x2 = (float) b.maxX;
|
| 286 | float y2 = (float) b.maxY;
|
| 287 | float z2 = (float) b.maxZ;
|
| 288 |
|
| 289 | wall(vc, bottom, top, x1, z1, x2, z1, y1, y2);
|
| 290 | wall(vc, bottom, top, x2, z1, x2, z2, y1, y2);
|
| 291 | wall(vc, bottom, top, x2, z2, x1, z2, y1, y2);
|
| 292 | wall(vc, bottom, top, x1, z2, x1, z1, y1, y2);
|
| 293 | }
|
| 294 |
|
| 295 | // One upright face between two corners with its own colour at each height.
|
| 296 | private void wall(VertexConsumer vc, int bottom, int top,
|
| 297 | float ax, float az, float bx, float bz, float y1, float y2) {
|
| 298 | vc.addVertex(pose, ax, y1, az).setColor(bottom);
|
| 299 | vc.addVertex(pose, ax, y2, az).setColor(top);
|
| 300 | vc.addVertex(pose, bx, y2, bz).setColor(top);
|
| 301 | vc.addVertex(pose, bx, y1, bz).setColor(bottom);
|
| 302 | }
|
| 303 |
|
| 304 | public void draw() {
|
| 305 | if (buffer == null) {
|
| 306 | return;
|
| 307 | }
|
| 308 | try {
|
| 309 | if (draws.isEmpty()) {
|
| 310 | return;
|
| 311 | }
|
| 312 | buffer.upload();
|
| 313 | for (int i = 0; i < draws.size(); i++) {
|
| 314 | StagedVertexBuffer.ExecuteInfo info = buffer.getExecuteInfo(draws.get(i));
|
| 315 | if (info != null) {
|
| 316 | types.get(i).prepare().drawFromBuffer(info);
|
| 317 | }
|
| 318 | }
|
| 319 | buffer.endDraw();
|
| 320 | } finally {
|
| 321 | draws.clear();
|
| 322 | types.clear();
|
| 323 | buffer.close();
|
| 324 | buffer = null;
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | private void lineRelative(Vec3 from, Vec3 to, int color, boolean throughWalls) {
|
| 329 | edge((float) from.x, (float) from.y, (float) from.z,
|
| 330 | (float) to.x, (float) to.y, (float) to.z, color, throughWalls);
|
| 331 | }
|
| 332 |
|
| 333 | private void edge(float x1, float y1, float z1, float x2, float y2, float z2,
|
| 334 | int color, boolean throughWalls) {
|
| 335 | VertexConsumer vc = buffer(Pipelines.lines(throughWalls));
|
| 336 | Vector3f normal = new Vector3f(x2, y2, z2).sub(x1, y1, z1).normalize();
|
| 337 | vc.addVertex(pose, x1, y1, z1).setColor(color).setNormal(pose, normal).setLineWidth(LINE_WIDTH);
|
| 338 |
|
| 339 | // The vanilla line shader glitches when a line crosses the near plane.
|
| 340 | float t = new Vector3f(x1, y1, z1).negate().dot(normal);
|
| 341 | float length = new Vector3f(x2, y2, z2).sub(x1, y1, z1).length();
|
| 342 | if (t > 0 && t < length) {
|
| 343 | Vector3f mid = new Vector3f(normal).mul(t).add(x1, y1, z1);
|
| 344 | vc.addVertex(pose, mid).setColor(color).setNormal(pose, normal).setLineWidth(LINE_WIDTH);
|
| 345 | vc.addVertex(pose, mid).setColor(color).setNormal(pose, normal).setLineWidth(LINE_WIDTH);
|
| 346 | }
|
| 347 |
|
| 348 | vc.addVertex(pose, x2, y2, z2).setColor(color).setNormal(pose, normal).setLineWidth(LINE_WIDTH);
|
| 349 | }
|
| 350 |
|
| 351 | private void quad(VertexConsumer vc, int color,
|
| 352 | float ax, float ay, float az, float bx, float by, float bz,
|
| 353 | float cx, float cy, float cz, float dx, float dy, float dz) {
|
| 354 | vc.addVertex(pose, ax, ay, az).setColor(color);
|
| 355 | vc.addVertex(pose, bx, by, bz).setColor(color);
|
| 356 | vc.addVertex(pose, cx, cy, cz).setColor(color);
|
| 357 | vc.addVertex(pose, dx, dy, dz).setColor(color);
|
| 358 | }
|
| 359 |
|
| 360 | private VertexConsumer buffer(RenderType type) {
|
| 361 | if (buffer == null) {
|
| 362 | buffer = new StagedVertexBuffer(() -> "OfflineClient DrawBatch", RenderType.BIG_BUFFER_SIZE);
|
| 363 | }
|
| 364 | if (!types.isEmpty() && types.getLast() == type
|
| 365 | && type.canConsolidateConsecutiveGeometry()) {
|
| 366 | return buffer.getVertexBuilder(draws.getLast());
|
| 367 | }
|
| 368 | StagedVertexBuffer.Draw draw = buffer.appendDraw(type.format(),
|
| 369 | type.primitiveTopology(), type.sortOnUpload()
|
| 370 | ? RenderSystem.getProjectionType().vertexSorting() : null);
|
| 371 | draws.add(draw);
|
| 372 | types.add(type);
|
| 373 | return buffer.getVertexBuilder(draw);
|
| 374 | }
|
| 375 | }
|
| 376 |
|