Advertisement
Guest User

Untitled

a guest
Jan 11th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.29 KB | None | 0 0
  1. package me.hasunemiku2015.signpic.TileEntityHandler;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import com.mojang.blaze3d.vertex.BufferBuilder;
  8. import com.mojang.blaze3d.vertex.DefaultVertexFormat;
  9. import com.mojang.blaze3d.vertex.PoseStack;
  10. import com.mojang.blaze3d.vertex.Tesselator;
  11. import com.mojang.blaze3d.vertex.VertexFormat.Mode;
  12. import com.mojang.math.Matrix4f;
  13. import com.mojang.math.Vector3f;
  14.  
  15. import net.minecraft.client.Minecraft;
  16. import net.minecraft.client.renderer.texture.DynamicTexture;
  17. import net.minecraft.core.BlockPos;
  18. import net.minecraft.world.level.block.SignBlock;
  19. import net.minecraftforge.api.distmarker.Dist;
  20. import net.minecraftforge.api.distmarker.OnlyIn;
  21. import net.minecraftforge.client.event.RenderLevelLastEvent;
  22. import net.minecraftforge.eventbus.api.SubscribeEvent;
  23.  
  24. @OnlyIn(Dist.CLIENT)
  25. public class RenderEvent {
  26.   static Minecraft mc = Minecraft.getInstance();
  27.   protected static HashMap<BlockPos, RenderInfo> renderInfoMap = new HashMap<>();
  28.   protected static HashMap<String, DynamicTexture> textureMap = new HashMap<>();
  29.  
  30.   /**
  31.    * This event draws the image itself
  32.    */
  33.   @SubscribeEvent
  34.   public void renderImage(RenderLevelLastEvent event) {
  35.     if (!ModController.isEnabled) return;
  36.  
  37.     int renderDistance = (int) mc.gameRenderer.getRenderDistance();
  38.  
  39.     Vector3f projView = mc.gameRenderer.getMainCamera().getLookVector();
  40.  
  41.     PoseStack stack = event.getPoseStack();
  42.  
  43.     List<String> deadKeys = new ArrayList<>();
  44.     for (RenderInfo info : renderInfoMap.values()) {
  45.       if(!textureMap.containsKey(info.texture)) continue;
  46.  
  47.       if (Math
  48.           .sqrt(Math.pow((info.x - mc.player.getX()), 2)
  49.               + Math.pow((info.z - mc.player.getZ()), 2)) > renderDistance
  50.           || !(mc.level.getBlockState(new BlockPos(info.x, info.y, info.z)).getBlock() instanceof SignBlock)) {
  51.  
  52.         renderInfoMap.values().remove(info);
  53.         deadKeys.add(info.texture);
  54.         return;
  55.       }
  56.  
  57.       if(deadKeys.contains(info.texture)){
  58.         deadKeys.remove(info.texture);
  59.       }
  60.  
  61.       stack.pushPose();
  62.       stack.translate(-projView.x(), -projView.y(), -projView.z());
  63.       Matrix4f matrix = stack.last().pose();
  64.       stack.mulPoseMatrix(matrix);
  65.  
  66.       // textureMap.get(info.texture).bind();
  67.       textureMap.get(info.texture).upload();
  68.  
  69.       BufferBuilder buffer = Tesselator.getInstance().getBuilder();
  70.       buffer.begin(Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
  71.       renderWithState(buffer, info);
  72.       Tesselator.getInstance().end();
  73.  
  74.       // Fixes the hand problem (So only the tessellator got multiplied)
  75.       matrix.invert();
  76.       stack.mulPoseMatrix(matrix);
  77.  
  78.       stack.popPose();
  79.     }
  80.  
  81.     for(String key: deadKeys)
  82.       textureMap.remove(key);
  83.   }
  84.  
  85.   /**
  86.    *  Draws the image itself into the world, based on oritation code
  87.    *  @param buffer: The buffer build to draw the image
  88.    *  @param info: The data packet to be rendered
  89.    */
  90.   private void renderWithState(BufferBuilder buffer, RenderInfo info) {
  91.     switch (info.state) {
  92.       //0.45 not 0.5 to avoid culling
  93.       default:
  94.       case 0: {
  95.         double wallSignOffset = info.isWallSign ? 0.45 : 0;
  96.  
  97.         // South, Vertical
  98.         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();
  99.         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();
  100.         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();
  101.         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();
  102.         return;
  103.       }
  104.  
  105.       case 1: {
  106.         double wallSignOffset = info.isWallSign ? -0.45 : 0;
  107.  
  108.         // North, Vertical
  109.         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();
  110.         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();
  111.         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();
  112.         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();
  113.         return;
  114.       }
  115.  
  116.       case 2: {
  117.         double wallSignOffset = info.isWallSign ? 0.45 : 0;
  118.  
  119.         // East, Verical
  120.         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();
  121.         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();
  122.         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();
  123.         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();
  124.         return;
  125.       }
  126.  
  127.       case 3: {
  128.         double wallSignOffset = info.isWallSign ? -0.45 : 0;
  129.  
  130.         // West, Vertical
  131.         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();
  132.         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();
  133.         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();
  134.         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();
  135.         return;
  136.       }
  137.  
  138.       //Added 0.01 to y (arbitary minimal amount) to avoid culling.
  139.       case 4: {
  140.         // North, Horizontal
  141.         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();
  142.         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();
  143.         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();
  144.         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();
  145.         return;
  146.       }
  147.  
  148.       case 5: {
  149.         // East, Horizontal
  150.         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();
  151.         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();
  152.         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();
  153.         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();
  154.         return;
  155.       }
  156.  
  157.       case 6: {
  158.         // South, Horizontal
  159.         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();
  160.         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();
  161.         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();
  162.         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();
  163.         return;
  164.       }
  165.  
  166.       case 7: {
  167.         // West, Horizontal
  168.         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();
  169.         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();
  170.         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();
  171.         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();
  172.         return;
  173.       }
  174.     }
  175.   }
  176. }
  177.  
  178. class RenderInfo {
  179.   int x, y, z, state;
  180.   double height, width, offsetW, offsetH;
  181.   String texture;
  182.   boolean isWallSign;
  183.  
  184.   public RenderInfo(int x, int y, int z, double width, double height, double offSetW, double offSetH, int state, String texture,
  185.       boolean isWallSign) {
  186.     this.x = x;
  187.     this.y = y;
  188.     this.z = z;
  189.     this.width = width;
  190.     this.height = height;
  191.     this.offsetW= offSetW;
  192.     this.offsetH = offSetH;
  193.     this.state = state;
  194.     this.texture = texture;
  195.  
  196.     this.isWallSign = isWallSign;
  197.   }
  198.  
  199.   /**
  200.    * Register the RenderInfo so it gets rendered.
  201.    */
  202.   public void register() {
  203.     RenderEvent.renderInfoMap.put(new BlockPos(x, y, z), this);
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement