Jump to content

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


Recommended Posts

Posted (edited)

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
Posted (edited)
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
Posted
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.

Posted

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • May this question can't be answered but is for a project. The question is more of how Forge load and manage mods, it's not required code or anything, just an explanation about how do it works on the load/manage of mods internally.
    • I myself am quite new to the modding scene, I started modding in February but have been in development hell a lot and have struggled to focus on one mod though I have been developing the current mod I've been working on since September and have made a lot of progress; during this time I have found a lot of helpful resources and have learnt a lot of things.    1. You can, I have had no Java experience previously and since modding have managed to be quite knowledgeable in Java.   2. Modding does not require massive teams, I and many others work alone, though having a team is certainly helpful in the modding process to get things done in a quicker and more organised way.   3. I absolutely have to recommend ModdingByKaupenjoe, he makes loads of tutorials for Minecraft from 1.16.5 and above with support for Forge, Fabric, and as of his 1.21 tutorials, Neoforge. These tutorials are really well made, covering almost every modding topic, (such as items, blocks, mobs, worldgen, etc.) and are pretty easy to follow, and Kaupenjoe always leaves the link to his GitHub repositories where you can view the code of that tutorial at your own pace as well as linking textures in the description of his videos for you to use. These forums are also quite good if you need help, though I have found that it sometimes takes a little while for a response but it is always worth the wait; from my experience the people on these forums have always been kind and helpful. There are also user-submitted tutorials that may be helpful as well.  Using GitHub to search up a certain class that you are wanting to use in your mod is also quite helpful, the video I have linked goes into more detail. I would also recommend planning out your mod before you make it to have a clearer idea of how you want your mod to be, including sketches and annotations are also a good idea for this. It has helped me make progress a lot quicker when programming as I already know how I want things to look/act.   I hope all this information helps!
    • I don't know what I did differently, but the error code changed to "Invalid signature for profile public key" so I set "enforce-secure-profile" to false since only my friend will be allowed to join anyway and then everything worked properly. Here's the logs anyway, but I hope the problem doesn't come back. https://paste.ee/p/Ri44L
  • Topics

×
×
  • Create New...

Important Information

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