Search code examples
3dlibgdxrenderingrenderrectangles

LibGDX nearby rectangles in 3D renders blocky/incorrectly


I am using LibGDX, constructing rectangles in 3D space with ModelBuilder#createRect, placing them in a 3D environment, and rendering them with ModelBatch. Everything works almost right. The issue arises when two rectangles are near each other. It seems to have ranges of depth where some of the rectangles that are actually behind are rendered in front.

To help illustrate what I mean, here are some examples:

Even though we have 2 rectangles (the bush and the figure) both vertically angled the same, when the model instances are close to each other, parts of one is rendered in front even though the rectangle is flat and one should fully be in front of the other instead of a small stripe of the bush in front of the figure.

enter image description here

Here we have 2 intersecting rectangles (the wall and the figure). I would expect to see a clean cut between the 2 rectangles, but instead I get a staircase. This would lead me to believe it's keeping ranges of areas where one rectangle is always in front of another in that range even though there might be an intersection in that range, or it's omitting parts of the rectangle thinking that it doesn't need to render it.

enter image description here

I believe these two examples stem from the same issue.

When I render the rectangle, here is the code for the material.

material.set(BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
                FloatAttribute(AlphaTest, 0.5f))

Adding a FloatAttribute with an arbitrary float seems to mitigate the issue, but not entirely. (The examples above are with the float attribute.)

I am using Environment, ModelBatch, etc. from com.badlogic.gdx.graphics.g3d to render the rectangles.

How can I use LibGDX to render the rectangles in 3D properly?


Solution

  • Props to @Tenfour04 in the comments for thinking about the camera planes being the issue. I had a viewing frustrum with a viewpoint at the origin, near of 0.1F, a far of 4000F. Changing the near to 50F fixed the issues, and the depth steps became more reasonable.

    Here's what it looks like with the near at 50F instead of 0.1F:

    enter image description here