Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RenderBeeHive extends TileEntityRenderer<TieredBeehiveTileEntity> {
- public static final ResourceLocation TIER_1_OVERLAY = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_1.png");
- public static final ResourceLocation TIER_2_OVERLAY = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_2.png");
- public static final ResourceLocation TIER_3_OVERLAY = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_3.png");
- public static final ResourceLocation TIER_4_OVERLAY = new ResourceLocation(ResourcefulBees.MOD_ID, "textures/block/bee_nest/tier_overlay/tier_4.png");
- public RenderBeeHive(TileEntityRendererDispatcher dispatcher) {
- super(dispatcher);
- Minecraft.getInstance().textureManager.register(PlayerContainer.BLOCK_ATLAS, new SimpleTexture(RenderBeeHive.TIER_1_OVERLAY));
- Minecraft.getInstance().textureManager.register(PlayerContainer.BLOCK_ATLAS, new SimpleTexture(RenderBeeHive.TIER_2_OVERLAY));
- Minecraft.getInstance().textureManager.register(PlayerContainer.BLOCK_ATLAS, new SimpleTexture(RenderBeeHive.TIER_3_OVERLAY));
- Minecraft.getInstance().textureManager.register(PlayerContainer.BLOCK_ATLAS, new SimpleTexture(RenderBeeHive.TIER_4_OVERLAY));
- }
- @Override
- public void render(TieredBeehiveTileEntity tile, float partialTick, @NotNull MatrixStack matrix, @NotNull IRenderTypeBuffer renderer, int light, int overlayLight) {
- ResourceLocation location = getLocation(tile);
- if (location == null) return;
- IVertexBuilder builder = renderer.getBuffer(Atlases.translucentCullBlockSheet());
- Vector3f start = new Vector3f(-0.0001f, -0.0001f, -0.0001f);
- Vector3f end = new Vector3f(1.0001f, 1.0001f, 1.0001f);
- CubeModel model = new CubeModel(start, end);
- model.setTextures(location);
- RenderCuboid.INSTANCE.renderCube(model, matrix, builder, -1, light, overlayLight);
- }
- private static ResourceLocation getLocation(TieredBeehiveTileEntity tile) {
- switch (tile.getTier()) {
- case 1:
- return TIER_1_OVERLAY;
- case 2:
- return TIER_2_OVERLAY;
- case 3:
- return TIER_3_OVERLAY;
- case 4:
- return TIER_4_OVERLAY;
- default:
- return null;
- }
- }
- }
- public class CubeModel {
- private final Vector3f start;
- private final Vector3f end;
- public final TextureAtlasSprite[] sprites = new TextureAtlasSprite[6];
- public CubeModel(Vector3f start, Vector3f end) {
- this.start = start;
- this.end = end;
- }
- public void setTextures(ResourceLocation texture) {
- TextureAtlasSprite sprite = getSprite(texture);
- this.sprites[0] = sprite;
- this.sprites[1] = sprite;
- this.sprites[2] = sprite;
- this.sprites[3] = sprite;
- this.sprites[4] = sprite;
- this.sprites[5] = sprite;
- }
- public static TextureAtlasSprite getSprite(ResourceLocation spriteLocation) {
- return Minecraft.getInstance().getTextureAtlas(PlayerContainer.BLOCK_ATLAS).apply(spriteLocation);
- }
- public Vector3f getSize() {
- return new Vector3f(this.getEnd().x()- this.getStart().x(), this.getEnd().y()- this.getStart().y(), this.getEnd().z()- this.getStart().z());
- }
- public Vector3f getStart() {
- return start;
- }
- public Vector3f getEnd() {
- return end;
- }
- }
- public class RenderCuboid {
- public static final RenderCuboid INSTANCE = new RenderCuboid();
- private static final Vector3f VEC_ZERO = new Vector3f(0.0F, 0.0F, 0.0F);
- private static Vector3f withValue(Vector3f vector, Axis axis, float value) {
- if (axis == Axis.X) {
- return new Vector3f(value, vector.y(), vector.z());
- } else if (axis == Axis.Y) {
- return new Vector3f(vector.x(), value, vector.z());
- } else if (axis == Axis.Z) {
- return new Vector3f(vector.x(), vector.y(), value);
- } else {
- throw new CustomException("Was given a null axis! That was probably not intentional, consider this a bug! (Vector = " + vector + ")");
- }
- }
- public static double getValue(Vector3f vector, Axis axis) {
- if (axis == Axis.X) {
- return vector.x();
- } else if (axis == Axis.Y) {
- return vector.y();
- } else if (axis == Axis.Z) {
- return vector.z();
- } else {
- throw new CustomException("Was given a null axis! That was probably not intentional, consider this a bug! (Vector = " + vector + ")");
- }
- }
- public static float getRed(int color) {
- return (float) (color >> 16 & 255) / 255.0F;
- }
- public static float getGreen(int color) {
- return (float) (color >> 8 & 255) / 255.0F;
- }
- public static float getBlue(int color) {
- return (float) (color & 255) / 255.0F;
- }
- public static float getAlpha(int color) {
- return (float) (color >> 24 & 255) / 255.0F;
- }
- public void renderCube(CubeModel cube, MatrixStack matrix, IVertexBuilder buffer, int argb, int light, int overlay) {
- float red = getRed(argb);
- float green = getGreen(argb);
- float blue = getBlue(argb);
- float alpha = getAlpha(argb);
- Vector3f size = cube.getSize();
- matrix.pushPose();
- matrix.translate(cube.getStart().x(), cube.getStart().y(), cube.getStart().z());
- MatrixStack.Entry lastMatrix = matrix.last();
- Matrix4f matrix4f = lastMatrix.pose();
- Matrix3f normal = lastMatrix.normal();
- Direction[] directions = Direction.values();
- for (Direction direction : directions) {
- Direction face = direction;
- int ordinal = face.ordinal();
- TextureAtlasSprite sprite = cube.sprites[ordinal];
- if (sprite != null) {
- Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
- Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
- float other = face.getAxisDirection() == Direction.AxisDirection.POSITIVE ? (float) getValue(size, face.getAxis()) : 0.0F;
- face = face.getAxisDirection() == Direction.AxisDirection.NEGATIVE ? face : face.getOpposite();
- Direction opposite = face.getOpposite();
- float minU = sprite.getU0();
- float maxU = sprite.getU1();
- float minV = sprite.getV1();
- float maxV = sprite.getV0();
- double sizeU = getValue(size, u);
- double sizeV = getValue(size, v);
- for (int uIndex = 0; (double) uIndex < sizeU; ++uIndex) {
- float[] baseUV = new float[]{minU, maxU, minV, maxV};
- double addU = 1.0D;
- if ((double) uIndex + addU > sizeU) {
- addU = sizeU - (double) uIndex;
- baseUV[1] = baseUV[0] + (baseUV[1] - baseUV[0]) * (float) addU;
- }
- for (int vIndex = 0; (double) vIndex < sizeV; ++vIndex) {
- float[] uv = Arrays.copyOf(baseUV, 4);
- double addV = 1.0D;
- if ((double) vIndex + addV > sizeV) {
- addV = sizeV - (double) vIndex;
- uv[3] = uv[2] + (uv[3] - uv[2]) * (float) addV;
- }
- float[] xyz = new float[]{(float) uIndex, (float) ((double) uIndex + addU), (float) vIndex, (float) ((double) vIndex + addV)};
- this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, true, false, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, true, true, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, false, true, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, face, u, v, other, uv, xyz, false, false, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, false, false, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, false, true, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, true, true, red, green, blue, alpha, light, overlay);
- this.renderPoint(matrix4f, normal, buffer, opposite, u, v, other, uv, xyz, true, false, red, green, blue, alpha, light, overlay);
- }
- }
- }
- }
- matrix.popPose();
- }
- 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) {
- int uArray = minU ? 0 : 1;
- int vArray = minV ? 2 : 3;
- Vector3f vertex = withValue(VEC_ZERO, u, xyz[uArray]);
- vertex = withValue(vertex, v, xyz[vArray]);
- vertex = withValue(vertex, face.getAxis(), other);
- Vector3i normalForFace = face.getNormal();
- float adjustment = 2.5F;
- Vector3f norm = new Vector3f((float) normalForFace.getX() + adjustment, (float) normalForFace.getY() + adjustment, (float) normalForFace.getZ() + adjustment);
- norm.normalize();
- 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();
- }
- private static class CustomException extends RuntimeException {
- public CustomException(String message) {
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement