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

Change smoothness of shader with map

Discussion in 'General Graphics' started by freerun, Jul 19, 2016.

  1. freerun

    freerun

    Joined:
    Dec 16, 2013
    Posts:
    29
    Hi, Ive seen that there is no option to use a custom roughness map in the default shader. Is it possible to do so?
     
  2. Buhlaine

    Buhlaine

    Community Manager

    Joined:
    Feb 5, 2016
    Posts:
    348
    Hiya!

    If you're trying to use a texture as your smoothness (roughness) map, you can do so by saving your smoothness map in the alpha channel of your Metallic/Roughness map. We read it like this because the smoothness map only uses black and white data so it's easily stored/read in the alpha. You can find more information in our manual here.

    Hope this helps!
     
    HunterAhlquist and theANMATOR2b like this.
  3. Deleted User

    Deleted User

    Guest

    is there a reason for it to be in alpha channel? Since that one texture will have twice the memory footprint, and you could get a new set of RGB channels for the same cost as one alpha channel. Metallic, smoothness and occlusion could be in one texture. Specular could still use a RGB texture without alpha , then have a new texture with Height, Smoothness and occlusion in the RGB Channels. It just seems a bit wrong to use alpha, with that memory overhead
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    There's a cost to sampling a texture, and there's a limit to the number of textures you can sampler from. On some low end platforms the standard shader gets very close to (and in some cases hits) that limit.

    Why aren't metallic, smoothness, and occlusion in a single RGB texture? It seems like it was for parity with the Standard Specular shader which actually does use all 4 channels of the RGBA texture.

    Now why not have height and occlusion in the same texture? Probably because on platforms that the parallax mapping is turned on for on the cost of sampling a texture and the texture sampling limits are higher so they didn't care as much.
     
    Peter_123 likes this.
  5. Deleted User

    Deleted User

    Guest

    if you want your game to run on a 32 bit system, the problem will be the memory and not necessary the sample cost, its just that a smarter packing could reduce the amount of textures and sample cost for the same amount of memory usage.It just feels like waste when you utilize multiple inputs on the metallic standard shader
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    The Alloy shader framework does this kind of packing, and you could do it yourself with surface shaders. One thing to be wary of is the alpha component of textures are always linear whereas the colors default to sRGB so just copying the alpha to one of your color channels won't get the same results unless you set the texture to be linear, in which case then metallic and occlusion will be off as they are both assumed to be in sRGB.
     
  7. Deleted User

    Deleted User

    Guest

    Yes, I know you can make your own shaders etc, I just wondered What unity's though on this was, since its default, it should promote best practice. And when it comes to sRGB, I'm sure you can toggle that off in the texture importer - https://docs.unity3d.com/Manual/class-TextureImporter.html . If unity would have a Default textue type for those kind of textures (like they do with normal map, lightmap, etc etc) They could set those settings on texture import/when it detects that you use a texture in the Metal/Roughness/AO slot.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    You can't disable sRGB on a single color channel, it's a toggle for the entire texture. You're packing 3 values (metalness, occlusion, and smoothness) and two want to be sRGB and one wants to be linear. You can get away with glossiness / smoothness being sRGB, but it means you lose some precision around the high smoothness areas, or you can approximately remap it back to linear from sRGB using a simple power. Doing either can cause problems if you want to switch between Standard and Standard Specular as the smoothness won't match if you just copy the smoothness between the color channel and alpha.

    And when you do pack textures like this you tend to get a lot of extra noise from compression which will show up in the metallic and smoothness.

    The real problem is no matter which path you take there's always a case where it feels like they "should have done it differently".
     
  9. Deleted User

    Deleted User

    Guest

    hmm, are you sure unity USE sRGB values in shaders for Roughness, metallic etc? This seems a bit weird, If you are on linear rendering, all values should be linear? If you have any resources on this, I would be interested in reading them
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Metallic is expecting sRGB, it uses a normal texture which defaults to sRGB for the color values, and if you look at the standard shader source the _Metallic property (the slider) has [Gamma] in front of it which actually applies the appropriate sRGB correction to the curve before the shader gets it.
    Smoothness is expecting linear, as it comes from the alpha of the specular, metallic, or in 5.4 optionally the albedo texture. In the standard shader the _Glossiness property has nothing before it meaning it'll be treated as a linear.

    If you take the values from the sliders * 255 and put them into a texture the results will match.

    Also there's always some confusion about gamma and linear space rendering vs gamma and linear textures.
    In gamma space rendering all textures are treated as "linear" in the shader; a color or alpha value of 128 in an sRGB or linear texture in gamma space rendering will evaluate to ~0.5 (actually closer to 0.502 because 128/255 != 1/2).
    In linear space rendering sRGB textures and linear textures are different; a color or alpha value of 128 for a linear texture is still ~0.5, but a color of 128 in an sRGB texture is ~0.2 in the shader.
     
  11. Deleted User

    Deleted User

    Guest

    okay, ye have not been looking into the shader code for the standard shader, so thanks for this information. But if the setup was different, say they did pack smarter, then would the smart thing not to be, to alter so all the inputs use the same approach? I have not been using the standard shader, cause of this texture setup, to save memory, where possible,