Search Unity

Tiling the tesselation heightmap atlas for terrain

Discussion in 'Shaders' started by IgorAherne, Sep 17, 2014.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Hey, dear community :)

    I've made a surface shader for the terrain and have a problem on how to make use of tiling.

    The shader samples a diffuse texture atlas, consisting of four textures. Inside of the fragment program, I make use of frac function and that way - specify which quarter of the texture has to be sampled. So far so good, tiling works.

    But then, I had a look at the unity's tesselation shader example here

    It is working inside of the vertex program. I can still make use of the texture2Dlod(), and sample the correct quarter of the heightmap (also an atlass).

    However, the way I used to tile the texture before I advanced to tesselation, was externally, from unity's inspector of material. My selected quarter from the atlas was extracted and then tiled by unity (I didn't see other 3 parts, so indeed, unity's interface managed to tile the correct quarter, not the atlass, which is excellent).

    But now, when I try to tile the heightmap from the inspector, I cannot do anything, that is the numbers don't change the tiling for the quarter of the height map specifically :(
    multiplying the modified uv coordinates inside the shader grabs other 3 quarters of atlas and tiles the whole heightmap thing, which is bad. Diffuse is still working fine in its fragment program.

    May it be the case that unity can tile the selected quarter only if the Tex2D was used inside the fragment program? Why can't my quarter of the hightmap tile via the unity's interface when it's sampled inside the vert program, using tex2Dlod? Is there a way to tell unity that I want the selected heightmap quarter to be tiled the way it did tile my quarter for diffuse texture? Maybe screw the inspector tiling and somehow manage to replicate it manually, but of course only upon a chosen quarter?

    Thank you guys
     
    Last edited: Sep 17, 2014
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Do this with your disp function
    Code (CSharp):
    1.  
    2. void disp (inout appdata v)
    3.         {
    4.             float2 uv = TRANSFORM_TEX(v.texcoord,_HeightMap);
    5.             fixed d = tex2Dlod(_HeightMap, float4(uv,0,0)).r * _Displacement;
    6.  
    7.             v.vertex.xyz += v.normal * d;
    8.         }
    9.  
    don't forget to include UnityCG.cginc
     
    chadiik and IgorAherne like this.
  3. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    It's working excellent, thank you so much :)

    Just had to declare a float4 _HeightMap_ST before the function and all worked fine.

    Could you please tell me what happened with the TRANSFORM_TEX function?
    It seems that TRANSFORM_TEX searches for the pre-defined _ST float4 variable, where the x/y tiling and offset were stored, and applies them to the current vertex uv coordinates? Or does it affect the _Heightmap sampler? Why then the result of TRANSFORM_TEX is a float2, how do those 2 arguments form it?

    Thank you very much!