3

i'm trying to render geometrical shapes over uneven terrain (loaded from heightmap / shapes geometry is also generated based on averaged heights across the heightmap however they do not fit it exactly). I have the following problem - somethimes the terrain shows through the shape like showed on the picture.

Open Image

I need to draw both terrain and shapes with depth testing enabled so they do not obstruct other objects in the scene.. Could someone suggest a solution to make sure the shapes are always rendered on top ? Lifting them up is not really feasible... i need to replace the colors of actual pixel on the terrain and doing this in pixel shader seems too expensive..

thanks in advance

Pawel Klapuch
  • 380
  • 4
  • 15
  • 2
    You can use either `DepthBias` (probably better `SlopeScaledDepthBias`) or a stencil buffer to mask the terrain. Look up *Decaling*. – Nico Schertler Mar 21 '14 at 12:20
  • You mention that you need the depth test to avoid an obstruction with other objects of the scene. However, if you can just depth sort the shapes you want to draw on the terrain, disable the depth test/write, render them DIRECTLY after the terrain and re-enable depth testing/writing to render all other objects you should achieve the desired effect. – Elvithari Jun 10 '14 at 10:04

1 Answers1

1

I had a similar problem and this is how I solved it:

  • You first render the terrain and keep the depth buffer. Do not render any objects
  • Render solid bounding box of the shape you want to put on the terrain.
  • You need to make sure that your bounding box covers all the height range the shape covers
  • An over-conservative estimation is to use the global minimum and maximum elevation of the entire terrain
  • In the pixel shader, you read depth buffer and reconstructs world space position
  • You check if this position is inside your shape
  • In your case you can check if its xy (xz) projection is within the given distance from the center of your given circle
  • Transform this position into your shape's local coordinate system and compute the desired color
  • Alpha-blend over the render target

This method results in shapes perfectly aligned with the terrain surface. It also does not produce any artifacts and works with any terrain. The possible drawback is that it requires using deferred-style shading and I do not know if you can do this. Still, I hope this might be helpful for you.

Egor
  • 779
  • 5
  • 20