Search Unity

Can't capture high quality heightmap with Unity Camera ?

Discussion in 'General Graphics' started by VincentAbert, Nov 17, 2021.

  1. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    Hello !

    I'll try to explain my situation, which hopefully makes sense : I am trying to create heightmaps to be used with the unity terrain system by rendering a top-down view of some objects with an orthographic camera. In order to render the object's height, I use this extremely simple unlit shader :

    It simply remaps the height of every pixel from [0, 600] to [0, 1]. I then create an orthographic camera, render my scene in a top-down perspective to a render texture, and set the terrain heightmap to that texture using
    Code (CSharp):
    1. terrainData.CopyActiveRenderTextureToHeightmap(new RectInt(0, 0, resolution, resolution), new Vector2Int(0, 0), TerrainHeightmapSyncControl.HeightAndLod);
    Here the scene :


    The resulting heightmap :



    And the terrain :


    This ALMOST works: the terrain is very clearly shaped by the render texture. The issue is the following : although the results are slightly different depending on the render texture format, I can't seem to get rid of some ugly stepping, typically caused by a lack of bit depth, even when I use RGFloat textures...

    Is there any way to get around this ?

    I'm quite confused and after trying different things for hours, I'm getting pretty hopeless... I tried using a different way to get the height of the pixels in the shader by instead reconstructing it from the camera depth map, but I get the exact same stepped results, which makes me think it does not come from there. I know that when I use the exact same method to set the terrain heightmap to a prerendered high quality stamp, using the same render texture formats, I get a flawless result... So I can't quite put my finger in what seems to be the issue, but I really need this.

    One thing I noticed is that when the render texture format is set to default, the stepping is occuring throught the height range (as you would expect from a texture lacking the proper bit depth), but when setting to RG32 or RGFloat, it's quite smooth near the 0 value, and it gets more and more stepped as the values increase.

    Thanks a lot !

    Vincent