|
| 1 | +package net.tcmfatbird.tutorialmod.feature; |
| 2 | + |
| 3 | +import com.mojang.blaze3d.systems.RenderSystem; |
| 4 | +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; |
| 5 | +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; |
| 6 | +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; |
| 7 | +import net.minecraft.block.BlockState; |
| 8 | +import net.minecraft.client.MinecraftClient; |
| 9 | +import net.minecraft.client.gui.DrawContext; |
| 10 | +import net.minecraft.client.render.*; |
| 11 | +import net.minecraft.client.render.RenderTickCounter; |
| 12 | +import net.minecraft.entity.EquipmentSlot; |
| 13 | +import net.minecraft.util.math.BlockPos; |
| 14 | +import net.minecraft.util.math.Vec3d; |
| 15 | +import net.tcmfatbird.tutorialmod.item.ModItems; |
| 16 | +import org.joml.Matrix4f; |
| 17 | + |
| 18 | +public final class QuantumVisionClient { |
| 19 | + private static final int RADIUS = 16; |
| 20 | + private static final int XZ_STEP = 4; |
| 21 | + private static final int Y_STEP = 3; |
| 22 | + |
| 23 | + private QuantumVisionClient() {} |
| 24 | + |
| 25 | + public static void register() { |
| 26 | + HudRenderCallback.EVENT.register(QuantumVisionClient::renderOverlay); |
| 27 | + WorldRenderEvents.AFTER_ENTITIES.register(QuantumVisionClient::renderOreClouds); |
| 28 | + } |
| 29 | + |
| 30 | + private static boolean active(MinecraftClient client) { |
| 31 | + return client.player != null |
| 32 | + && client.world != null |
| 33 | + && client.player.getEquippedStack(EquipmentSlot.HEAD).isOf(ModItems.WAVEFUNCTION_GOGGLES); |
| 34 | + } |
| 35 | + |
| 36 | + private static void renderOverlay(DrawContext context, RenderTickCounter tickCounter) { |
| 37 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 38 | + if (!active(client)) return; |
| 39 | + |
| 40 | + int width = context.getScaledWindowWidth(); |
| 41 | + int height = context.getScaledWindowHeight(); |
| 42 | + context.fill(0, 0, width, height, 0x3311FF44); |
| 43 | + context.drawText(client.textRenderer, "~ WAVEFUNCTION VISION ~", width / 2 - 58, 8, 0xCCFFCC, true); |
| 44 | + } |
| 45 | + |
| 46 | + private static void renderOreClouds(WorldRenderContext context) { |
| 47 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 48 | + if (!active(client)) return; |
| 49 | + |
| 50 | + Vec3d cam = context.camera().getPos(); |
| 51 | + Matrix4f matrix = context.matrixStack().peek().getPositionMatrix(); |
| 52 | + |
| 53 | + RenderSystem.disableDepthTest(); |
| 54 | + RenderSystem.enableBlend(); |
| 55 | + RenderSystem.defaultBlendFunc(); |
| 56 | + RenderSystem.setShader(GameRenderer::getPositionColorProgram); |
| 57 | + |
| 58 | + Tessellator tessellator = Tessellator.getInstance(); |
| 59 | + BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); |
| 60 | + |
| 61 | + BlockPos center = client.player.getBlockPos(); |
| 62 | + boolean wroteGeometry = false; |
| 63 | + |
| 64 | + for (int x = center.getX() - RADIUS; x <= center.getX() + RADIUS; x += XZ_STEP) { |
| 65 | + for (int z = center.getZ() - RADIUS; z <= center.getZ() + RADIUS; z += XZ_STEP) { |
| 66 | + for (int y = center.getY() - 10; y <= center.getY() + 6; y += Y_STEP) { |
| 67 | + BlockPos anchor = new BlockPos(x, y, z); |
| 68 | + BlockState state = client.world.getBlockState(anchor); |
| 69 | + float chance = QuantumWavefunctionHandler.getCollapseChance(state, y); |
| 70 | + if (chance <= 0f) continue; |
| 71 | + |
| 72 | + float patchRoll = QuantumWavefunctionHandler.positionRoll(anchor); |
| 73 | + if (patchRoll > chance * 2.2f) continue; |
| 74 | + |
| 75 | + wroteGeometry = drawFluffyPatch(buffer, matrix, cam, anchor) || wroteGeometry; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (wroteGeometry) { |
| 81 | + BufferRenderer.drawWithGlobalProgram(buffer.end()); |
| 82 | + } |
| 83 | + |
| 84 | + RenderSystem.enableDepthTest(); |
| 85 | + RenderSystem.disableBlend(); |
| 86 | + } |
| 87 | + |
| 88 | + private static boolean drawFluffyPatch(BufferBuilder buffer, Matrix4f matrix, Vec3d cam, BlockPos anchor) { |
| 89 | + boolean wrote = false; |
| 90 | + int blobs = 7 + Math.floorMod(anchor.getX() + anchor.getY() + anchor.getZ(), 5); |
| 91 | + |
| 92 | + for (int i = 0; i < blobs; i++) { |
| 93 | + int sx = spread(anchor, i * 13 + 3); |
| 94 | + int sy = spread(anchor, i * 17 + 7) / 2; |
| 95 | + int sz = spread(anchor, i * 19 + 11); |
| 96 | + |
| 97 | + float ox = sx * 0.22f; |
| 98 | + float oy = sy * 0.18f; |
| 99 | + float oz = sz * 0.22f; |
| 100 | + |
| 101 | + float cx = anchor.getX() + 0.5f + ox; |
| 102 | + float cy = anchor.getY() + 0.45f + oy; |
| 103 | + float cz = anchor.getZ() + 0.5f + oz; |
| 104 | + |
| 105 | + float size = 0.22f + (Math.abs(spread(anchor, i * 23 + 5)) * 0.015f); |
| 106 | + float alpha = 0.10f + (0.03f * i); |
| 107 | + |
| 108 | + addCube(buffer, matrix, |
| 109 | + cx - (float) cam.x, |
| 110 | + cy - (float) cam.y, |
| 111 | + cz - (float) cam.z, |
| 112 | + size, |
| 113 | + 1.0f, 1.0f, 1.0f, Math.min(alpha, 0.28f)); |
| 114 | + wrote = true; |
| 115 | + } |
| 116 | + |
| 117 | + return wrote; |
| 118 | + } |
| 119 | + |
| 120 | + private static int spread(BlockPos pos, int salt) { |
| 121 | + int hash = pos.getX() * 73428767 ^ pos.getY() * 9127837 ^ pos.getZ() * 43828921 ^ salt; |
| 122 | + hash ^= (hash >>> 13); |
| 123 | + return Math.floorMod(hash, 9) - 4; |
| 124 | + } |
| 125 | + |
| 126 | + private static void addCube(BufferBuilder buffer, Matrix4f matrix, |
| 127 | + float cx, float cy, float cz, |
| 128 | + float half, |
| 129 | + float r, float g, float b, float a) { |
| 130 | + float minX = cx - half; |
| 131 | + float maxX = cx + half; |
| 132 | + float minY = cy - half; |
| 133 | + float maxY = cy + half; |
| 134 | + float minZ = cz - half; |
| 135 | + float maxZ = cz + half; |
| 136 | + |
| 137 | + quad(buffer, matrix, minX, minY, minZ, maxX, minY, minZ, maxX, maxY, minZ, minX, maxY, minZ, r, g, b, a); |
| 138 | + quad(buffer, matrix, minX, minY, maxZ, minX, maxY, maxZ, maxX, maxY, maxZ, maxX, minY, maxZ, r, g, b, a); |
| 139 | + |
| 140 | + quad(buffer, matrix, minX, minY, minZ, minX, maxY, minZ, minX, maxY, maxZ, minX, minY, maxZ, r, g, b, a); |
| 141 | + quad(buffer, matrix, maxX, minY, minZ, maxX, minY, maxZ, maxX, maxY, maxZ, maxX, maxY, minZ, r, g, b, a); |
| 142 | + |
| 143 | + quad(buffer, matrix, minX, maxY, minZ, maxX, maxY, minZ, maxX, maxY, maxZ, minX, maxY, maxZ, r, g, b, a); |
| 144 | + quad(buffer, matrix, minX, minY, minZ, minX, minY, maxZ, maxX, minY, maxZ, maxX, minY, minZ, r, g, b, a); |
| 145 | + } |
| 146 | + |
| 147 | + private static void quad(BufferBuilder buffer, Matrix4f matrix, |
| 148 | + float x1, float y1, float z1, |
| 149 | + float x2, float y2, float z2, |
| 150 | + float x3, float y3, float z3, |
| 151 | + float x4, float y4, float z4, |
| 152 | + float r, float g, float b, float a) { |
| 153 | + buffer.vertex(matrix, x1, y1, z1).color(r, g, b, a); |
| 154 | + buffer.vertex(matrix, x2, y2, z2).color(r, g, b, a); |
| 155 | + buffer.vertex(matrix, x3, y3, z3).color(r, g, b, a); |
| 156 | + buffer.vertex(matrix, x4, y4, z4).color(r, g, b, a); |
| 157 | + } |
| 158 | +} |
0 commit comments