0

I am trying to figure out a technique for drawing a 3D grid in a 3D viewport like in Unreal Engine. Drawing a grid on its own is simple, you decide what the size of the grid should be and then you either draw a quad of that size and apply a texture to it (with or without transparency) or just draw quads for the lines if you don't want to use a texture.

Simple enough.

But the thing in UE is that the grid is infinite (well it looks like so). And that's what I'd like to understand: how does this work? I can't figure it out, so I thought I'd asked for help.

I know the code is available, and I looked at it, but it's a monster). I have managed to find Engine/Source/Editor/UnrealEd/Private/EditorComponents.cpp in which DrawNewGrid seemed like a good starting point. Beside calling a function called DrawPlane10x10 I wasn't really able to understand how it worked overall.

  • How does it make look like the grid is tiled to infinity?
  • Does it use a texture (materials seemed involved, but I could find how) or do they draw the individual lines as quads?
  • Does it create one grid (say 10x10) and duplicate it?

I understand it obviously doesn't draw/duplicate the grid to infinity. So I assume it has some mechanism in which only grids that would be visible through the frustum are drawn, but again couldn't find code related to that. And how would you do it? So, to sum up my questions:

  • I'd like to draw a 3D grid like in UE where the grid is always visible through the frustum regardless of how far you move away from the origin.
  • How do you draw that grid? Geometry or quad + texture?
  • How do you make it so that eventually a small grid like (10x10) is repeated so that it looks like the grid goes to infinity by being smart, e.g. only rendering what's visible through the frustum (and updating dynamically as the camera moves)?
  • how do you deal with aliasing (as I guess as lines become small, they do alias)?

I have been Googling this for a while and couldn't find good resources. I guess it's a tile-based technique, but I couldn't find much info about it either.

Edit:

I was given this solution:

  • Create a basic plane vertex and fragment shader with the coordinates of the grid
  • Unproject the plane coordinates so that the points are always at infinity
  • Draw the plane only when it intersects the floor (ie y=0)
  • Draw the grid and the axis highlight (red for x and blue for z)
  • Output the grid depth for every fragment
  • Add a fade out effect when the grid is far away
user18490
  • 3,546
  • 4
  • 33
  • 52

0 Answers0