Search Unity

16-bit texture lost a lot of values when used in shader

Discussion in 'Shaders' started by zoran404, Jun 25, 2022.

  1. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Hi guys. I'm trying trying to render earth from a 16-bit heightmap, but for some reason I lost a lot of detail.
    In these screenshots the red pixels all get the same value from the texture, even though the texture is very detailed.
    This is a big problem since I need to clearly distinguish land from sea.

    Unity_Th2ELn5x6B.png Unity_f4Nbs7LWvO.png

    I suspect image import settings might be the problem, but I've tried all values and I never get the correct result.

    Unity_p8ASkY8Sdv.png

    My shader code:

    Code (HLSL):
    1. void vert(inout appdata_full v, out Input o)
    2. {
    3.     v.vertex.xyz = normalize(v.vertex.xyz);
    4.     o.position = v.vertex.xyz;
    5. }
    6.  
    7. void surf(Input IN, inout SurfaceOutputStandard o)
    8. {
    9.     float2 uv = sphere2map(normalize(IN.position));
    10.     float h = tex2D(_Heightmap, uv);
    11.     static float waterLevel = 0.57254907;
    12.     o.Albedo = h == waterLevel ?
    13.         float3(1, 0, 0) : // red at water level
    14.         h > waterLevel ?
    15.             float3(0, (h - waterLevel) / (1 - waterLevel), 0) : // green above
    16.             float3(0, 0, h / waterLevel); // blue below
    17. }
    Any tips would be really appreciated!
     
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, have you tried ticking "Ignore PNG file gamma"?

    If not working, can you try disabling "Generate Mip Maps"?

    Or try both of them.
     
  3. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Thanks for the tip, but I'm afraid it didn't help.

    "Ignore PNG file gamma" doesn't change anything, probably because this is a tif file.

    Disabling "Generate Mip Maps" still leaves me with the same red areas. Since I'd primarily display earth up close, I don't actually use mip maps anyway, so I can leave them disabled.
     
    wwWwwwW1 likes this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    AFAIK Unity doesn't support 16 bit tiff files in the base image importer. You must use jpg png, exr, or hdr file formats for importing > 8 bit data formats. A 16 bit tiff gets quantized to 8 bit on import regardless of the target image format you use.

    edit: png, not jpg. jpg doesn’t support 16 bit at all.
     
    Last edited: Jun 29, 2022
    wwWwwwW1 likes this.