Advertisement
Vaerys_Dawn

Render tile entity block overlay

Mar 12th, 2021
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.33 KB | None | 0 0
  1. public class RenderBeeHive extends TileEntityRenderer<TieredBeehiveTileEntity> {
  2.  
  3.     public RenderBeeHive(TileEntityRendererDispatcher dispatcher) {
  4.         super(dispatcher);
  5.     }
  6.  
  7.     @Override
  8.     public void render(TieredBeehiveTileEntity tile, float partialTick, @NotNull MatrixStack matrix, @NotNull IRenderTypeBuffer renderer, int light, int overlayLight) {
  9.         ResourceLocation location = null;
  10.         switch (tile.getTier()) {
  11.             case 1:
  12.                 location = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_1.png");
  13.                 break;
  14.             case 2:
  15.                 location = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_2.png");
  16.                 break;
  17.             case 3:
  18.                 location = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_3.png");
  19.                 break;
  20.             case 4:
  21.                 location = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_4.png");
  22.                 break;
  23.         }
  24.         if (location == null) return;
  25.         IVertexBuilder builder = renderer.getBuffer(Atlases.translucentCullBlockSheet());
  26.         Vector3f start = new Vector3f(-0.001f, -0.001f, -0.001f);
  27.         Vector3f end = new Vector3f(1.001f, 1.001f, 1.001f);
  28.         Minecraft.getInstance().textureManager.bind(location);
  29.         CubeModel model = new CubeModel(start, end);
  30.         model.setTextures(location);
  31.         RenderCuboid.INSTANCE.renderCube(model, matrix, builder, -1, light, overlayLight);
  32.     }
  33. }
  34.  
  35. public class CubeModel {
  36.  
  37.     private final Vector3f start;
  38.     private final Vector3f end;
  39.     public final TextureAtlasSprite[] sprites = new TextureAtlasSprite[6];
  40.  
  41.     public CubeModel(Vector3f start, Vector3f end) {
  42.         this.start = start;
  43.         this.end = end;
  44.     }
  45.  
  46.     public void setTextures(ResourceLocation texture) {
  47.         TextureAtlasSprite sprite = getSprite(texture);
  48.         this.sprites[0] = sprite;
  49.         this.sprites[1] = sprite;
  50.         this.sprites[2] = sprite;
  51.         this.sprites[3] = sprite;
  52.         this.sprites[4] = sprite;
  53.         this.sprites[5] = sprite;
  54.     }
  55.  
  56.     public static TextureAtlasSprite getSprite(ResourceLocation spriteLocation) {
  57.         return Minecraft.getInstance().getTextureAtlas(PlayerContainer.BLOCK_ATLAS).apply(spriteLocation);
  58.     }
  59.  
  60.     public Vector3f getSize() {
  61.         return new Vector3f(this.getEnd().x()- this.getStart().x(), this.getEnd().y()- this.getStart().y(), this.getEnd().z()- this.getStart().z());
  62.     }
  63.  
  64.     public Vector3f getStart() {
  65.         return start;
  66.     }
  67.  
  68.     public Vector3f getEnd() {
  69.         return end;
  70.     }
  71. }
  72.  
  73. public class RenderCuboid {
  74.     public static final RenderCuboid INSTANCE = new RenderCuboid();
  75.     private static final Vector3f VEC_ZERO = new Vector3f(0.0F, 0.0F, 0.0F);
  76.  
  77.  
  78.     private static Vector3f withValue(Vector3f vector, Axis axis, float value) {
  79.         if (axis == Axis.X) {
  80.             return new Vector3f(value, vector.y(), vector.z());
  81.         } else if (axis == Axis.Y) {
  82.             return new Vector3f(vector.x(), value, vector.z());
  83.         } else if (axis == Axis.Z) {
  84.             return new Vector3f(vector.x(), vector.y(), value);
  85.         } else {
  86.             throw new CustomException("Was given a null axis! That was probably not intentional, consider this a bug! (Vector = " + vector + ")");
  87.         }
  88.     }
  89.  
  90.     public static double getValue(Vector3f vector, Axis axis) {
  91.         if (axis == Axis.X) {
  92.             return vector.x();
  93.         } else if (axis == Axis.Y) {
  94.             return vector.y();
  95.         } else if (axis == Axis.Z) {
  96.             return vector.z();
  97.         } else {
  98.             throw new CustomException("Was given a null axis! That was probably not intentional, consider this a bug! (Vector = " + vector + ")");
  99.         }
  100.     }
  101.  
  102.     public static float getRed(int color) {
  103.         return (float) (color >> 16 & 255) / 255.0F;
  104.     }
  105.  
  106.     public static float getGreen(int color) {
  107.         return (float) (color >> 8 & 255) / 255.0F;
  108.     }
  109.  
  110.     public static float getBlue(int color) {
  111.         return (float) (color & 255) / 255.0F;
  112.     }
  113.  
  114.     public static float getAlpha(int color) {
  115.         return (float) (color >> 24 & 255) / 255.0F;
  116.     }
  117.  
  118.     public void renderCube(CubeModel cube, MatrixStack matrix, IVertexBuilder buffer, int argb, int light, int overlay) {
  119.         float red = getRed(argb);
  120.         float green = getGreen(argb);
  121.         float blue = getBlue(argb);
  122.         float alpha = getAlpha(argb);
  123.         Vector3f size = cube.getSize();
  124.         matrix.pushPose();
  125.         matrix.translate(cube.getStart().x(), cube.getStart().y(), cube.getStart().z());
  126.         MatrixStack.Entry lastMatrix = matrix.last();
  127.         Matrix4f matrix4f = lastMatrix.pose();
  128.         Matrix3f normal = lastMatrix.normal();
  129.         Direction[] directions = Direction.values();
  130.  
  131.         for (Direction direction : directions) {
  132.             Direction face = direction;
  133.             int ordinal = face.ordinal();
  134.             TextureAtlasSprite sprite = cube.sprites[ordinal];
  135.             if (sprite != null) {
  136.                 Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
  137.                 Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
  138.                 float other = face.getAxisDirection() == Direction.AxisDirection.POSITIVE ? (float) getValue(size, face.getAxis()) : 0.0F;
  139.                 face = face.getAxisDirection() == Direction.AxisDirection.NEGATIVE ? face : face.getOpposite();
  140.                 Direction opposite = face.getOpposite();
  141.                 float minU = sprite.getU0();
  142.                 float maxU = sprite.getU1();
  143.                 float minV = sprite.getV1();
  144.                 float maxV = sprite.getV0();
  145.                 double sizeU = getValue(size, u);
  146.                 double sizeV = getValue(size, v);
  147.                 for (int uIndex = 0; (double) uIndex < sizeU; ++uIndex) {
  148.                     float[] baseUV = new float[]{minU, maxU, minV, maxV};
  149.                     double addU = 1.0D;
  150.                     if ((double) uIndex + addU > sizeU) {
  151.                         addU = sizeU - (double) uIndex;
  152.                         baseUV[1] = baseUV[0] + (baseUV[1] - baseUV[0]) * (float) addU;
  153.                     }
  154.                     for (int vIndex = 0; (double) vIndex < sizeV; ++vIndex) {
  155.                         float[] uv = Arrays.copyOf(baseUV, 4);
  156.                         double addV = 1.0D;
  157.                         if ((double) vIndex + addV > sizeV) {
  158.                             addV = sizeV - (double) vIndex;
  159.                             uv[3] = uv[2] + (uv[3] - uv[2]) * (float) addV;
  160.                         }
  161.                         float[] xyz = new float[]{(float) uIndex, (float) ((double) uIndex + addU), (float) vIndex, (float) ((double) vIndex + addV)};
  162.                         this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, true, false, red, green, blue, alpha, light, overlay);
  163.                         this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, true, true, red, green, blue, alpha, light, overlay);
  164.                         this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, false, true, red, green, blue, alpha, light, overlay);
  165.                         this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, false, false, red, green, blue, alpha, light, overlay);
  166.                         this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, false, false, red, green, blue, alpha, light, overlay);
  167.                         this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, false, true, red, green, blue, alpha, light, overlay);
  168.                         this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, true, true, red, green, blue, alpha, light, overlay);
  169.                         this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, true, false, red, green, blue, alpha, light, overlay);
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.  
  175.         matrix.popPose();
  176.     }
  177.  
  178.     private void renderPoint(Matrix4f matrix4f, Matrix3f normal, IVertexBuilder buffer, Direction face, Axis u, Axis v, float other, float[] uv, float[] xyz, boolean minU, boolean minV, float red, float green, float blue, float alpha, int light, int overlay) {
  179.         int uArray = minU ? 0 : 1;
  180.         int vArray = minV ? 2 : 3;
  181.         Vector3f vertex = withValue(VEC_ZERO, u, xyz[uArray]);
  182.         vertex = withValue(vertex, v, xyz[vArray]);
  183.         vertex = withValue(vertex, face.getAxis(), other);
  184.         Vector3i normalForFace = face.getNormal();
  185.         float adjustment = 2.5F;
  186.         Vector3f norm = new Vector3f((float) normalForFace.getX() + adjustment, (float) normalForFace.getY() + adjustment, (float) normalForFace.getZ() + adjustment);
  187.         norm.normalize();
  188.         buffer.vertex(matrix4f, vertex.x(), vertex.y(), vertex.z()).color(red, green, blue, alpha).uv(uv[uArray], uv[vArray]).overlayCoords(overlay).uv2(light).normal(normal, norm.x(), norm.y(), norm.z()).endVertex();
  189.     }
  190.  
  191.     private static class CustomException extends RuntimeException {
  192.         public CustomException(String message) {
  193.             super(message);
  194.         }
  195.     }
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement