Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.hasunemiku2015.signpic.TileEntityHandler;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import com.mojang.blaze3d.vertex.BufferBuilder;
- import com.mojang.blaze3d.vertex.DefaultVertexFormat;
- import com.mojang.blaze3d.vertex.PoseStack;
- import com.mojang.blaze3d.vertex.Tesselator;
- import com.mojang.blaze3d.vertex.VertexFormat.Mode;
- import com.mojang.math.Matrix4f;
- import com.mojang.math.Vector3f;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.renderer.texture.DynamicTexture;
- import net.minecraft.core.BlockPos;
- import net.minecraft.world.level.block.SignBlock;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraftforge.api.distmarker.OnlyIn;
- import net.minecraftforge.client.event.RenderLevelLastEvent;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- @OnlyIn(Dist.CLIENT)
- public class RenderEvent {
- static Minecraft mc = Minecraft.getInstance();
- protected static HashMap<BlockPos, RenderInfo> renderInfoMap = new HashMap<>();
- protected static HashMap<String, DynamicTexture> textureMap = new HashMap<>();
- /**
- * This event draws the image itself
- */
- @SubscribeEvent
- public void renderImage(RenderLevelLastEvent event) {
- if (!ModController.isEnabled) return;
- int renderDistance = (int) mc.gameRenderer.getRenderDistance();
- Vector3f projView = mc.gameRenderer.getMainCamera().getLookVector();
- PoseStack stack = event.getPoseStack();
- List<String> deadKeys = new ArrayList<>();
- for (RenderInfo info : renderInfoMap.values()) {
- if(!textureMap.containsKey(info.texture)) continue;
- if (Math
- .sqrt(Math.pow((info.x - mc.player.getX()), 2)
- + Math.pow((info.z - mc.player.getZ()), 2)) > renderDistance
- || !(mc.level.getBlockState(new BlockPos(info.x, info.y, info.z)).getBlock() instanceof SignBlock)) {
- renderInfoMap.values().remove(info);
- deadKeys.add(info.texture);
- return;
- }
- if(deadKeys.contains(info.texture)){
- deadKeys.remove(info.texture);
- }
- stack.pushPose();
- stack.translate(-projView.x(), -projView.y(), -projView.z());
- Matrix4f matrix = stack.last().pose();
- stack.mulPoseMatrix(matrix);
- // textureMap.get(info.texture).bind();
- textureMap.get(info.texture).upload();
- BufferBuilder buffer = Tesselator.getInstance().getBuilder();
- buffer.begin(Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
- renderWithState(buffer, info);
- Tesselator.getInstance().end();
- // Fixes the hand problem (So only the tessellator got multiplied)
- matrix.invert();
- stack.mulPoseMatrix(matrix);
- stack.popPose();
- }
- for(String key: deadKeys)
- textureMap.remove(key);
- }
- /**
- * Draws the image itself into the world, based on oritation code
- * @param buffer: The buffer build to draw the image
- * @param info: The data packet to be rendered
- */
- private void renderWithState(BufferBuilder buffer, RenderInfo info) {
- switch (info.state) {
- //0.45 not 0.5 to avoid culling
- default:
- case 0: {
- double wallSignOffset = info.isWallSign ? 0.45 : 0;
- // South, Vertical
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).uv(1.0F, 0.0F).endVertex();
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).uv(1.0F, 0.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).uv(0.0F, 0.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).uv(0.0F, 1.0F).endVertex();
- return;
- }
- case 1: {
- double wallSignOffset = info.isWallSign ? -0.45 : 0;
- // North, Vertical
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 +wallSignOffset)).uv(1.0F, 1.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 +wallSignOffset)).uv(1.0F, 0.0F).endVertex();
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 +wallSignOffset)).uv(0.0F, 0.0F).endVertex();
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 +wallSignOffset)).uv(0.0F, 1.0F).endVertex();
- return;
- }
- case 2: {
- double wallSignOffset = info.isWallSign ? 0.45 : 0;
- // East, Verical
- buffer.vertex((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 1.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), info.height / 2.0 + (info.y + 0.5) + info.offsetH, info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 0.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 0.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 1.0F).endVertex();
- return;
- }
- case 3: {
- double wallSignOffset = info.isWallSign ? -0.45 : 0;
- // West, Vertical
- buffer.vertex((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 1.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 0.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), info.height / 2.0 + (info.y + 0.5) + info.offsetH, info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 0.0F).endVertex();
- buffer.vertex((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 1.0F).endVertex();
- return;
- }
- //Added 0.01 to y (arbitary minimal amount) to avoid culling.
- case 4: {
- // North, Horizontal
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(0.0F, 0.0F).endVertex();
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(0.0F, 1.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(1.0F, 1.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(1.0F, 0.0F).endVertex();
- return;
- }
- case 5: {
- // East, Horizontal
- buffer.vertex( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 0.0F).endVertex();
- buffer.vertex( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 0.0F).endVertex();
- buffer.vertex(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 1.0F).endVertex();
- buffer.vertex(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 1.0F).endVertex();
- return;
- }
- case 6: {
- // South, Horizontal
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(1.0F, 1.0F).endVertex();
- buffer.vertex(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(1.0F, 0.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(0.0F, 0.0F).endVertex();
- buffer.vertex( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).uv(0.0F, 1.0F).endVertex();
- return;
- }
- case 7: {
- // West, Horizontal
- buffer.vertex( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 1.0F).endVertex();
- buffer.vertex( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 1.0F).endVertex();
- buffer.vertex(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(1.0F, 0.0F).endVertex();
- buffer.vertex(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).uv(0.0F, 0.0F).endVertex();
- return;
- }
- }
- }
- }
- class RenderInfo {
- int x, y, z, state;
- double height, width, offsetW, offsetH;
- String texture;
- boolean isWallSign;
- public RenderInfo(int x, int y, int z, double width, double height, double offSetW, double offSetH, int state, String texture,
- boolean isWallSign) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.width = width;
- this.height = height;
- this.offsetW= offSetW;
- this.offsetH = offSetH;
- this.state = state;
- this.texture = texture;
- this.isWallSign = isWallSign;
- }
- /**
- * Register the RenderInfo so it gets rendered.
- */
- public void register() {
- RenderEvent.renderInfoMap.put(new BlockPos(x, y, z), this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement