Search Unity

How to read textures in a 4x4 texture atlas?

Discussion in 'Shaders' started by drudiverse, Aug 15, 2014.

  1. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    For a single texture, i can read the colors with the line:

    Code (csharp):
    1. color = tex2D (textureName , uv);
    So for a 4x4 texture atlas, i want to read just one of the textures with:

    Code (csharp):
    1. color = tex2D (textureName , uv * 0.5);    //the entire shader goes black  
    If i have three textures on a terrain and i change the uv reading value of one of them, all the scenery goes black as if i had changed uv for all three, isn't it very strange?
     
    Last edited: Aug 15, 2014
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's hard to tell what's going on without the rest of the shader there.
     
  3. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    That should work fine, however I'd recommend setting the tiling/offset in the material instead, this way you can use the same shader for all parts of the atlas. For a 4x4 atlas, set tiling to 0.5, and offset to 0/1 as needed, and just use tex2D(texture, uv) in the shader.
     
  4. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Thankyou. i found that it's the if condition, if i write

    Code (csharp):
    1.   if (IN.worldPos.y >=1){clr = tex2D (_TexTop,  IN.uv_TexTop*.5);}// it goes black
    and

    Code (csharp):
    1. clr = tex2D (_TexTop,  IN.uv_TexTop*.5);// works fine
    So it's very difficult to write a texture atlas shader that uses different textures for different parts of landscapes because the shader literally won't run anything with "if" condition where the uv is multiplied, it's crazy. If anyone knows a workaround, i will be very happy.

    writing an additive version of the code i wanted to do is scary difficult. it would be like a 2d parametric version of "if condition" , there has to be an easy way to make the texture atlas into simple discreet variables?
     
    Last edited: Aug 15, 2014
  5. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    i've asked aras if he knows why that line bugs. Hope he will reply.

    The solution, may be to make a table of multiply/offset values using if condition, and multiply the same instances of the sampler2d's using an offset etc control value.