Jump to content

(1.18.2) Line rendering bug (ghost line)


ElTotisPro50

Recommended Posts

Im trying to render a simple line using the "RenderLevelStageEvent", for testing, i put the start position above a diamond block, and the end position 5 blocks above the diamond block; The line rendering is almost good, but for some reason, apart from the original line, its rendering a second line that kind of looks "fake", it moves along with the camera, its like a ghost line that should not exist. Here is an example video: https://imgur.com/a/GRkSLVX

 

Renderer.java

@SubscribeEvent
    public static void test(RenderLevelStageEvent event) {
        if(Minecraft.getInstance().player == null) return;
        Player player = Minecraft.getInstance().player;

        Vec3 proj = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
        double vpX = proj.x;
        double vpY = proj.y;
        double vpZ = proj.z;
        drawLine(event.getPoseStack(),
                new Vec3(11 - vpX, -60 - vpY, 12 - vpZ),
                new Vec3(11 - vpX, -55 - vpY, 12 - vpZ),
                new Color(255,0,0), 255);
    }

    public static void drawLine(PoseStack stack, Vec3 start, Vec3 end, Color color, int alpha) {
        RenderType rtype = new NoDepthWrappedRenderLayer(TotisRenderType.createLinesRenderType(3.0D, 0));
        Matrix4f m = stack.last().pose();
        Matrix3f norm = stack.last().normal();
        MultiBufferSource.BufferSource buffer = Minecraft.getInstance().renderBuffers().bufferSource();
        VertexConsumer bb = buffer.getBuffer(rtype);
        bb.vertex(m,(float)start.x,(float)start.y,(float)start.z).color(color.getRed(),color.getGreen(),color.getBlue(), alpha).normal(norm, 1.0F, 0.0F, 0.0F).endVertex();
        bb.vertex(m,(float)end.x,(float)start.y,(float)start.z).color(color.getRed(),color.getGreen(),color.getBlue(), alpha).normal(norm, 1.0F, 0.0F, 0.0F).endVertex();
        bb.vertex(m,(float)start.x,(float)start.y,(float)start.z).color(color.getRed(),color.getGreen(),color.getBlue(), alpha).normal(norm, 0.0F, 1.0F, 0.0F).endVertex();
        bb.vertex(m,(float)start.x,(float)end.y,(float)start.z).color(color.getRed(),color.getGreen(),color.getBlue(), alpha).normal(norm, 0.0F, 1.0F, 0.0F).endVertex();
        buffer.endBatch(rtype);
      
      //Btw im rendering 4 vertex because only two draws a 2D line, which looks good from one side, but thinner from the other, so this renders a 3D line that looks fine from any perspective
    }

 

TotisRenderType.java:

public class TotisRenderType extends RenderType {

    public TotisRenderType(String nameIn, VertexFormat formatIn, VertexFormat.Mode drawModeIn, int bufferSizeIn, boolean useDelegateIn, boolean needsSortingIn, Runnable setupTaskIn, Runnable clearTaskIn) {
        super(nameIn, formatIn, drawModeIn, bufferSizeIn, useDelegateIn, needsSortingIn, setupTaskIn, clearTaskIn);
    }

    public static RenderType createLinesRenderType(double width, int index) {
        return create("totis_lines" + index, DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.LINES, 256, false, false, CompositeState.builder().setLayeringState(VIEW_OFFSET_Z_LAYERING).setLineState(new LineStateShard(OptionalDouble.of(width))).setShaderState(RENDERTYPE_LINES_SHADER).setTextureState(RenderStateShard.NO_TEXTURE).setTransparencyState(TRANSLUCENT_TRANSPARENCY).setDepthTestState(RenderStateShard.NO_DEPTH_TEST).setCullState(NO_CULL).setWriteMaskState(RenderStateShard.COLOR_WRITE).setLightmapState(NO_LIGHTMAP).createCompositeState(false));
    }
}

 

NoDepthWrappedRenderLayer.java:

public class NoDepthWrappedRenderLayer extends RenderType {

    private final RenderType delegate;

    public NoDepthWrappedRenderLayer(RenderType delegate) {
        super(Constants.MOD_ID + "no_depth", delegate.format(), delegate.mode(), delegate.bufferSize(), true, delegate.isOutline(), () -> {
            delegate.setupRenderState();
            RenderSystem.disableDepthTest();
        }, () -> {
            RenderSystem.enableDepthTest();
            delegate.clearRenderState();
        });
        this.delegate = delegate;
    }

    public Optional<RenderType> m_7280_() {
        return this.delegate.outline();
    }

    public boolean equals(@Nullable Object other) {
        return other instanceof NoDepthWrappedRenderLayer && this.delegate.equals(((NoDepthWrappedRenderLayer)other).delegate);
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.delegate});
    }
}
Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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