Fabric API

Fabric API

106M Downloads

Render a string from a world position

Pseudow opened this issue ยท 2 comments

commented

Hi I am currently trying to render a String in the world position to the player's view. I have done that for now:

    @Override
    public void onHudRender(MatrixStack matrixStack, float tickDelta) {
        this.loadEntity();

        if(this.entity == null || this.player == null)
            return;

        final Vec3d targetPosition = new Vec3d(this.entity.getX(), this.entity.getY(), this.entity.getZ());
        final Camera camera = MinecraftClient.getInstance().gameRenderer.getCamera();
        final Vec3d viewPosition = targetPosition.subtract(camera.getPos());

        matrixStack.translate(
                viewPosition.x,
                viewPosition.y,
                viewPosition.z
        );
        matrixStack.multiplyPositionMatrix(RenderSystem.getProjectionMatrix());
        matrixStack.multiplyPositionMatrix(RenderSystem.getModelViewMatrix());

        MinecraftClient.getInstance().textRenderer.draw(matrixStack, "Salut", 0, 0, 0xffffff);

        matrixStack.pop();
    }

But it doesn't work and throws this error:

	at java.base/java.util.ArrayDeque.getLast(ArrayDeque.java:413)
	at net.minecraft.client.util.math.MatrixStack.peek(MatrixStack.java:71)
	at net.minecraft.client.gui.DrawableHelper.drawTexture(DrawableHelper.java:255)
	at net.minecraft.client.gui.DrawableHelper.drawTexture(DrawableHelper.java:227)
	at net.minecraft.client.gui.screen.Screen.renderBackgroundTexture(Screen.java:472)
	at net.minecraft.client.gui.screen.DownloadingTerrainScreen.render(DownloadingTerrainScreen.java:32)
	at net.minecraft.client.gui.screen.Screen.renderWithTooltip(Screen.java:121)
commented

You need to push the matrix stack before you start to use it. (To match the pop call from later).

commented

Thank you for your answer, but I actually forgot it.
My code wasn't working even if I specified matrixStack.push() at the begininng.
Do you know how can I render a String from a 3d world dimension into the view space?

I've been stuck so many time trying to make it happen.