Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UV adjustment based on the geometry normal

Discussion in 'General Graphics' started by Zimaell, Sep 25, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    Everyone has encountered stretching of the texture on the terrain due to a large difference in the heights of the vertices, as I noticed, you can calculate the geometric normal (in shader)

    Code (CSharp):
    1. float3 normalGeo = normalize(cross(ddy(i.positionWS), ddx(i.positionWS)));
    and, based on it, adjust the “uv”, but I still can’t find a formula with which it will adjust correctly.

    Any ideas, tips, hints on how to do this?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,354
    A lot of times this sort of thing is accomplished with triplanar projection. If you look up "Unity triplanar terrain shader" then you'll find lots of results and ready-made solutions.
     
    Zimaell likes this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    I've already done this, the results greatly affect performance...
    for example - a terrain with a basic shader of 170 fps, a terrain with a triplanar shader of 30-40 fps, a shader that I am currently writing (adjusting uv to heights) of 150 fps, the difference is significant.

    Now I have already understood how to correctly set a map for vertices, I now set it not in a fragment, in the vertex part,
    Code (CSharp):
    1. Varyings Vert(Attributes i){
    2.     Varyings o = (Varyings)0;
    3.     float4 dataTexUV = SAMPLE_TEXTURE2D_LOD(_TextureData, sampler_TextureData, i.texcoord, 0);
    4.     o.dataUV = TransformDataToHeight(dataTexUV);
    5.     return o;
    6.     }
    I like the results, I just need to understand how to make smooth transitions on one polygon with a sharp change in height.

    What do you think - is it possible to somehow set a smooth transition "uv" in a fragment?
     

    Attached Files:

  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    549
    Yes, triplanar sampling can be expensive, because you are sampling textures 3 times as often. However, you can improve performance a bit by relying on dynamic branching to skip directions for which the weight is (nearly) zero.

    Can't say for sure but I doubt it. It's like trying to wrap a texture around a sphere without a seam.
     
    Last edited: Sep 27, 2023
    Zimaell likes this.
  5. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    I think the idea with adjusting the “uv” would be more optimal, my problem was that I could not understand the structure of the scan and how to apply the necessary settings to the polygons needed, not to the vertices but to the polygons.

    But I finally understood how they work and now I can apply it to the polygons I need, in the screenshot I showed how I applied different textures to different polygons, now I’ll just do it with a scan, all that remains is to structure it all...
     

    Attached Files:

  6. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    By the way, how can you use one axis? for example, if I want the texture to be laid at an angle of 90 degrees, that is, on the side...
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,354
    Typically with triplanar projection you would calculate a weight for each axis to blend the three textures together. You can decide that If one of those weights is close enough to 1.0 then just ignore the other two.
     
    Zimaell likes this.
  8. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    338
    yes you are right, this is very useful...

    I just need to figure out how to make smooth transitions between different directions, although it’s still better than what I did with uv...

    experience, trial, error, results...