Search Unity

Modify mesh vertex with heightmap

Discussion in 'Shaders' started by Rotary-Heart, Sep 2, 2015.

  1. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    I have been working with a way to modify a mesh vertex depending on the heightmap specified. So far it works, but it doesn't give any smooth vertex positioning. Here's what I mean:

    Capture.PNG

    Here's the output of my shader code. As you can see the vertex are not smooth they look like stairs. This is how it should look:

    Capture2.PNG

    This one is a terrain with the same imported heightmap. As you can see the vertex positions are not in a stairs way like mine.

    Finally here's my code:

    Code (CSharp):
    1. v2f vert(appdata_full v)
    2. {
    3.     v2f o;
    4.     v.vertex.y += tex2Dlod(_MainTex, float4(v.texcoord.xy, 0.0, 0.0)) * _ScaleY;
    5.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    6.     o.vpos = v.vertex.xyz;
    7.     o.uv = TRANSFORM_UV(0);
    8.     return o;
    9. }
    Any help on what to change to make the hill more smooth?
     
  2. jistyles

    jistyles

    Joined:
    Nov 6, 2013
    Posts:
    34
    Off the top of my head, one of two possible things are happening:
    1) the texture is being point sampled -- if it's an editor resource, double check your filter mode is at least bilinear. If it's generated at run time as a texture2D OR rendertexture, then set it to be x.filterMode = FilterMode.Bilinear;

    2) the bit depth isn't sufficient to reproduce the forms (eg, it's quantized). Make sure it's of a format that represents the bit depth you need.

    There's one last possibility, but it's a real edge case - quantized UV's from compressed UV packing or floating point innaccuracy. This can occur when UV's are super far away from 0,0.
     
    Rotary-Heart likes this.
  3. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    #1 fixed my issue. My texture filter mode is set to bilinear in the editor, but since I make some changes to the texture within the code it seems that it changes back to point. Re setting it to bilinear after the changes are made fixed my issue.

    Thanks for the suggestion.