Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to correctly read vertex data for a terrain?

Discussion in 'Shaders' started by Zimaell, Sep 26, 2023.

  1. Zimaell


    May 7, 2020
    I have a terrain measuring 1025 by 1025 vertices, I create a file to store its heights
    Code (CSharp):
    1. Texture2D tex = new Texture2D(1025, 1025, TextureFormat.RGBAFloat, false);
    2. tex.wrapMode = TextureWrapMode.Clamp;
    3. tex.filterMode = FilterMode.Point;
    for accuracy I separate the values and put them in different channels
    height 126 => Color(0.1, 0.2, 0.6, 0)
    since accuracy may be lost when transferring data to the shader, but this way I get accurate data in the shader

    Next I need to read this data to apply it to the required vertices
    Code (CSharp):
    1. //vert
    2. o.uv = i.texcoord;
    4. //frag
    5. float4 dataTex = SAMPLE_TEXTURE2D(_TextureData, sampler_TextureData, i.uv);
    the problem is that the texture is displaced (example in the screenshot).

    How to correctly indicate the coordinates?

    Attached Files: