Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix canvas rendering bugs #12

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.fabricmc.fabric.api.client.rendering.v1.InvalidateRenderStateCallback;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.VertexBuffer;
Expand All @@ -16,13 +17,17 @@
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Quaternion;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3f;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public abstract class BakedBlockEntityRenderer<T extends BlockEntity> implements BlockEntityRenderer<T> {
private static final boolean USE_CANVAS_HACK = FabricLoader.getInstance().isModLoaded("canvas");

protected final BlockEntityRendererFactory.Context context;

protected BakedBlockEntityRenderer(BlockEntityRendererFactory.Context context) {
Expand Down Expand Up @@ -229,6 +234,14 @@ public void render(MatrixStack matrices, Matrix4f projectionMatrix, Camera camer
double camZ = vec3d.getZ();
RenderRegionPos centerRegion = new RenderRegionPos((int)camX >> REGION_SHIFT, (int)camZ >> REGION_SHIFT);

if (USE_CANVAS_HACK) {
float pitch = camera.getPitch();
float yaw = camera.getYaw();
matrices.push();
matrices.multiply(new Quaternion(Vec3f.POSITIVE_X, pitch, true));
matrices.multiply(new Quaternion(Vec3f.POSITIVE_Y, yaw + 180, true));
}

// Iterate over all RegionBuilders, render and upload to RegionBuffers
Set<RenderLayer> usedRenderLayers = new ObjectArraySet<>();
List<BlockEntity> blockEntities = new ArrayList<>();
Expand Down Expand Up @@ -293,6 +306,10 @@ public void render(MatrixStack matrices, Matrix4f projectionMatrix, Camera camer
}
layer.endDrawing();
}

if (USE_CANVAS_HACK) {
matrices.pop();
}
}

public void activateRegion(BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public void render(HyperlinkBlockEntity entity, float f, MatrixStack matrices, V
HitResult hitResult = MinecraftClient.getInstance().crosshairTarget;
if (hitResult instanceof BlockHitResult && ((BlockHitResult) hitResult).getBlockPos().equals(entity.getPos())) {
float scale = 0.025F;
matrices.scale(scale, scale, scale);
matrices.scale(scale, scale, -scale);
matrices.translate(-MinecraftClient.getInstance().textRenderer.getWidth(entity.url) / 2F, -4, 0);
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, entity.url, 0, 0, 0xFFFFFF);

MinecraftClient.getInstance().textRenderer.draw(entity.url, 0, 0, -1, true, matrices.peek().getModel(), vertexConsumers, false, 0, 0xF000F0);
}
matrices.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ public void render(ItemDisplayBlockEntity entity, float tickDelta, MatrixStack m
matrices.translate(0, 0, -0.4);

float scale = 0.025F;
matrices.scale(scale, scale, scale);
matrices.scale(scale, scale, -scale);

int color = name.getStyle().getColor() == null ? 0xFFFFFF : name.getStyle().getColor().getRgb();
matrices.translate(-MinecraftClient.getInstance().textRenderer.getWidth(name) / 2F, -4, 0);
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, name, 0, 0, color);

MinecraftClient.getInstance().textRenderer.draw(name, 0, 0, color, true, matrices.peek().getModel(), vertexConsumers, false, 0, 0xF000F0);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.hephaestus.glowcase.mixin.client;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(TextRenderer.class)
public class TextRendererMixin {
// These addToLastColumn calls are originally used for offsetting the text
// shadow, however simply adding to the last column of the matrix to produce
// an offset is wrong since it will ignore any previous transformations made
// and will only work correctly in special cases (the transformation matrix
// having no rotation).
// So since what's actually wanted here is a translation, that's what we do.
// Another option would be to modify addToLastColumn directly because these
// two lines of code are the only thing that ever calls that method, but
// then its name would be wrong.

@Redirect(
method = "drawInternal(Ljava/lang/String;FFIZLnet/minecraft/util/math/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;ZIIZ)I",
at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/Matrix4f;addToLastColumn(Lnet/minecraft/util/math/Vec3f;)V")
)
private void fixTranslation1(Matrix4f matrix4f, Vec3f vector) {
matrix4f.multiplyByTranslation(vector.getX(), vector.getY(), vector.getZ());
}

@Redirect(
method = "drawInternal(Lnet/minecraft/text/OrderedText;FFIZLnet/minecraft/util/math/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;ZII)I",
at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/Matrix4f;addToLastColumn(Lnet/minecraft/util/math/Vec3f;)V")
)
private void fixTranslation2(Matrix4f matrix4f, Vec3f vector) {
matrix4f.multiplyByTranslation(vector.getX(), vector.getY(), vector.getZ());
}
}
5 changes: 3 additions & 2 deletions src/main/resources/glowcase.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"package": "dev.hephaestus.glowcase.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"block.EntityShapeContextAccessor",
"block.EntityShapeContextAccessor"
],
"client": [
"client.MinecraftClientAccessor",
"client.render.ber.RenderPhaseAccessor",
"client.render.ber.WorldRendererMixin",
"client.render.entity.EntityRenderDispatcherAccessor"
"client.render.entity.EntityRenderDispatcherAccessor",
"client.TextRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down