Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Solved] [1.20.2] How do I draw a line using BufferBuilder and Tesselator?

Featured Replies

Posted

I'd like to visualize a ray that I cast from a player. To archieve this, I need to be able to draw a line.

 

Currently, I am using this code for testing purposes, but I cannot see any line drawn around x=0 y=0 z=0:

    @SubscribeEvent
    public static void onClientTick(TickEvent.ClientTickEvent event) {
        // Only call code once as the tick event is called twice every tick
        if (event.phase == TickEvent.Phase.END) {
			
            if(MEASUREMENT_MANAGER.isCurrentlyMeasuring()) {

                Tesselator tesselator = Tesselator.getInstance();
                BufferBuilder bufferBuilder = tesselator.getBuilder();

                Color c = new Color(1, 0, 0, 0.4f);

                bufferBuilder.begin(VertexFormat.Mode.LINES, DefaultVertexFormat.POSITION_COLOR);

                bufferBuilder.vertex(0, 0, 0).color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()).endVertex();
                bufferBuilder.vertex(0, 2, 2).color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()).endVertex();

                tesselator.end();
            }
        }
    }

 

There have been many implementations in lower versions, but I could not find any for 1.20.2. What I've found, however, is this implementation on GitHub: https://github.com/cabaletta/baritone/blob/e183dcba1707c991fea1ba55be052dc3ee5d71a6/src/main/java/baritone/utils/IRenderer.java#L149

This also did not work for me.

 

Any help would be appreciated.

Edited by TopSnek

  • Author
10 minutes ago, Luis_ST said:

Why do you not use a IGuiOverlay to draw the line? You can there use GuiGraphics.

Every other implementation I've found used BufferBuilder. I'm not sure if it would be a clean solution to project 3D line(s) that are bound to world coordinates to a 2D GUI. If you still suggest using IGuiOverlay for that I'll give it a try.

 

This would also create problems when using the 3rd person cam (F5), because the ray could be theoretically be casted from the back of the player.

Edited by TopSnek
grammar

  • Author
On 11/13/2023 at 9:08 PM, Luis_ST said:

Have you checkout the StructureBlockRenderer to take a look at how vanilla renders lines?

Thanks, yes I did - it brought me to LevelRenderer.java, which has some great examples. I tried applying some of what Mojang did there, but without success. Here is my code:

Spoiler
    @SubscribeEvent
    public static void onClientTick(TickEvent.ClientTickEvent event) {
        // Only call code once as the tick event is called twice every tick
        if (event.phase == TickEvent.Phase.END) {

            if (MEASUREMENT_CONTROLLER.isCurrentlyMeasuring()) {

                Tesselator tesselator = Tesselator.getInstance();
                BufferBuilder bufferBuilder = tesselator.getBuilder();

                RenderSystem.enableBlend();
                RenderSystem.enableDepthTest();
                RenderSystem.disableCull();

                bufferBuilder.begin(VertexFormat.Mode.LINES, DefaultVertexFormat.POSITION_COLOR);

                PoseStack poseStack = RenderSystem.getModelViewStack();
                poseStack.pushPose();
                RenderSystem.applyModelViewMatrix();
                Matrix4f matrix4f = poseStack.last().pose();

                bufferBuilder.vertex( matrix4f, 0, 1, 0).color(0, 255, 0, 255).endVertex();
                bufferBuilder.vertex( matrix4f, 0, 3, 0).color(0, 255, 0, 255).endVertex();

                BufferUploader.drawWithShader(bufferBuilder.end());

                poseStack.popPose();
                RenderSystem.applyModelViewMatrix();
                RenderSystem.enableCull();
                RenderSystem.disableBlend();
                RenderSystem.defaultBlendFunc();
                RenderSystem.depthMask(true);

                System.out.println("rendering");
            }

        }
    }

 

I try to plot a vertical line close to world coordinates 0, 0, 0 but cannot see it in game, "rendering" is being printed inside the console.

I really don't know what I'm missing, I've been looking at code examples for days now - slowly it becomes a bit frustrating.

  • Author

I've found the solution. My problem(s) were an invalid translation + I called it inside the wrong event.

Working code for 1.20.2:

    SubscribeEvent
    public static void onRenderLayerPost(RenderLevelStageEvent event) {
        Vec3 view = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();

        var tesselator = Tesselator.getInstance();
        var buffer = tesselator.getBuilder();
        VertexBuffer vertexBuffer = new VertexBuffer(VertexBuffer.Usage.STATIC);
        buffer.begin(VertexFormat.Mode.DEBUG_LINES, DefaultVertexFormat.POSITION_COLOR);
        buffer.vertex(0, 0, 0).color(1f, 1f, 1f, 1f).endVertex();
        buffer.vertex(10, 10, 10).color(1f, 1f, 1f, 1f).endVertex();
        vertexBuffer.bind();
        vertexBuffer.upload(buffer.end());

        PoseStack matrix = event.getPoseStack();
        matrix.pushPose();
        matrix.translate(-view.x, -view.y, -view.z);
        var shader = GameRenderer.getPositionColorShader();
        vertexBuffer.drawWithShader(matrix.last().pose(), event.getProjectionMatrix(), shader);
        matrix.popPose();

        VertexBuffer.unbind();
    }

Most of the code is from this post:

 

  • TopSnek changed the title to [Solved] [1.20.2] How do I draw a line using BufferBuilder and Tesselator?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.