Search Unity

Resolved Difficulties with texture sampling. (Converting old surface shader to Shader Graph)

Discussion in 'Shader Graph' started by RabbitTortoise, Sep 6, 2020.

  1. RabbitTortoise

    RabbitTortoise

    Joined:
    Aug 11, 2018
    Posts:
    2
    Hi, I'm trying to convert my old Surface Shader to Shader Graph. The version I'm using is HDRP 9.0.0-preview.54. Shader is configured as HDRP-Lit.
    The functionality I'm after is the following: I'm using the red channel values from texture as indexes to sample my texture array. The problem is that the method I used in the surface shader doesn't seem to work in Shader Graph. And I'm having a feeling that I'm missing something obvious.

    This is the part I'm having difficulties in converting to Shader Graph:

    I'm using the following line in my surface shader to calculate the index for the texture array (indexes are from 0-255). (_MaxTileIndex == 255). It takes the red channel value from array and multiplies it by 255 to get the index for texture array.
    Code (CSharp):
    1.  
    2. //Calculate the tile index in the texture array.
    3. //Takes the red value (0.0-1.0) from current pixel(tile) and multiplies it by 255 to get the index of the tile.
    4. int index = tex2D(_TilemapTexture, calculatedUV).r * _MaxTileIndex;
    5.  
    The same in Shader Graph. TileMapTexture is is full of pixels that have values (3,0,0,0) [scale 0-255], and is configured as RGBA32, Point, Clamp, Read/Write in import settings. The expected index would be 3 (Rock-texture) but I'm getting 0 for the index (Water-texture). (Array sampling not done in the screenshot, but if I manually give it an index I get the correct texture as result.) Is my texture configured wrong so the sampling doesn't give good values? Or am I missing something else?
    upload_2020-9-6_20-39-25.png

    So I started investigating:

    I created a color that has red value of 0,1 (scale: 0-1). Then I compared it to Vector with value of 0,1. They are not equal according to the Comparison node. So does Shader Graph use range 0-1 or 0-255 when doing calculations with colors? When I use the Split-node do I get 0,1 or 25,5 for the red channel? What is going on behind the scenes?
    upload_2020-9-6_20-48-30.png


    Then I compared the color of that has red value of 3 (scale: 0-255) to the sampled texture (that is full of color (3,0,0,0). And they are not same. Sampled texture in different format than the color node or something funny happening in sampling?
    upload_2020-9-6_20-53-46.png


    So what I am missing? Why are all of these different? How can I use the red channel of a texture to index an texture array?
     

    Attached Files:

  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you should just be able to grab the texture layer from the red channel of another texture.
    the same goes for color or vector input of course.

    but what is the difference between a color (rgba) and a float4?

    actually i have never used a color node inside shader graph. colors from the inspector however are treated as color in gamma color space. so i guess this is the same when you use a color node within shader graph. this means that an input value of 0.1 for the color may result in SRGBToLinear(0.1) if you are linear color space while 0.1 will always be 0.1 when set using float4.
    when it comes to texture input you have to make sure that "sRGB (color texture)" is unchecked in the texture's import settings.
     
  3. RabbitTortoise

    RabbitTortoise

    Joined:
    Aug 11, 2018
    Posts:
    2
    Just noticed my screenshot has two mistakes that were fixed in the project. (Type was set to Normal instead of Default and it was taking RGBA instead of R as output)
    upload_2020-9-7_7-50-34.png


    Anyway, I unchecked "sRGB (color texture)" and it seems to be working now.

    I went to the docs to see what it does:
    "This setting is only relevant when Linear color space is used, and controls whether sRGB->Linear color space conversion is done when sampling the texture in shaders.
    Non-color textures are typically stored as linear values, and the GPU should not do any color space conversions. This flag is set to false in this case."
    And yeah, makes sense that this would mess up my shader as I store data in a texture and I don't it to be modified in any way other than what I do in my own scripts.
     

    Attached Files: