Skip to content

Instantly share code, notes, and snippets.

@Haven-King
Created December 15, 2020 04:20
Show Gist options
  • Save Haven-King/48f5ebe6e86c982791e8ef757a393afe to your computer and use it in GitHub Desktop.
Save Haven-King/48f5ebe6e86c982791e8ef757a393afe to your computer and use it in GitHub Desktop.
package net.gudenau.minecraft.food.renderer.block;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.gudenau.minecraft.food.block.entity.JarBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.*;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.util.math.*;
import net.minecraft.world.World;
@Environment(EnvType.CLIENT)
public class JarBlockEntityRenderer implements BlockEntityRenderer<JarBlockEntity>{
public JarBlockEntityRenderer(BlockEntityRendererFactory.Context context){}
@Override
public void render(JarBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay){
renderEntity(entity, tickDelta, matrices, vertexConsumers, light);
renderFluid(entity, matrices, vertexConsumers, light);
}
private void renderEntity(JarBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light){
Entity jarredEntity = entity.getEntity();
if(!(jarredEntity instanceof LivingEntity)){
return;
}
LivingEntity livingEntity = (LivingEntity)jarredEntity;
float scale = livingEntity.getHeight() > livingEntity.getWidth()
? (0.35F / livingEntity.getHeight())
: (0.35F / livingEntity.getWidth());
matrices.push();
matrices.translate(-0.5F, -0.5F, -0.5F);
matrices.scale(scale, scale, scale);
matrices.translate(1 / scale, 0.625 / scale, 1 / scale);
MinecraftClient.getInstance().getEntityRenderDispatcher().render(
jarredEntity, 0, 0, 0, 0, 1, matrices, vertexConsumers, light
);
matrices.pop();
}
// FIXME Scale the UVs
private void renderFluid(JarBlockEntity entity, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light){
Fluid fluid = entity.getFluid();
float amount = entity.getFillAmount();
if(fluid == Fluids.EMPTY || amount == 0){
return;
}
MinecraftClient.getInstance().getTextureManager().bindTexture(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);
World world = entity.getWorld();
BlockPos pos = entity.getPos();
FluidState fluidState = fluid.getDefaultState();
FluidRenderHandler fluidRenderHandler = FluidRenderHandlerRegistry.INSTANCE.get(fluid);
Sprite[] sprites = fluidRenderHandler.getFluidSprites(world, pos, fluidState);
int color = fluidRenderHandler.getFluidColor(world, pos, fluidState);
RenderLayer renderLayer = RenderLayers.getFluidLayer(fluidState);
VertexConsumer vertexConsumer = vertexConsumers.getBuffer(renderLayer);
Sprite stillSprite = sprites[0];
float red = ((color >>> 16) & 0xFF) * 0.003921569f;
float green = ((color >>> 8) & 0xFF) * 0.003921569f;
float blue = (color & 0xFF) * 0.003921569f;
float u1 = stillSprite.getMinU();
float v1 = stillSprite.getMinV();
float u2 = stillSprite.getMaxU();
float v2 = stillSprite.getMaxV();
Matrix4f modelMatrix = matrices.peek().getModel();
Matrix3f normalMatrix = matrices.peek().getNormal();
float height = amount * (7 / 16F) + 0.125F;
float a = 0.26f;
float b = 0.74F;
// Top
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.26F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.74F, red, green, blue, u1, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.74F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.26F, red, green, blue, u2, v1, light, normalMatrix);
// Bottom
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.26F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.26F, red, green, blue, u2, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.74F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.74F, red, green, blue, u1, v2, light, normalMatrix);
// North
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.26F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.26F, red, green, blue, u1, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.26F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.26F, red, green, blue, u2, v1, light, normalMatrix);
// South
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.74F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.74F, red, green, blue, u2, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.74F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.74F, red, green, blue, u1, v2, light, normalMatrix);
// West
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.26F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, 0.0625F, 0.74F, red, green, blue, u1, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.74F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.26F, height, 0.26F, red, green, blue, u2, v1, light, normalMatrix);
// East
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.26F, red, green, blue, u1, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.26F, red, green, blue, u2, v1, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, height, 0.74F, red, green, blue, u2, v2, light, normalMatrix);
vertex(vertexConsumer, modelMatrix, 0.74F, 0.0625F, 0.74F, red, green, blue, u1, v2, light, normalMatrix);
}
private void vertex(VertexConsumer vertexConsumer, Matrix4f modelMatrix, float x, float y, float z, float red, float green, float blue, float u, float v, int light, Matrix3f normalMatrix) {
vertexConsumer.vertex(modelMatrix, x, y, z).color(red, green, blue, 1.0F).texture(u, v).light(light).normal(normalMatrix, 0.0F, 1.0F, 0.0F).next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment