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

Surface Shader not blending two samples correctly

Discussion in 'Shaders' started by afuzzyllama, Aug 7, 2016.

  1. afuzzyllama

    afuzzyllama

    Joined:
    May 19, 2010
    Posts:
    20
    When sampling the same texture twice in a surface shader, I have the following:

    At the end of my surf() method, when I get my main sample from the following code:

    Code (CG):
    1. float3 mainTextureSample = tex2D(_TextureMap, textureUV);
    2. o.Albedo = mainTextureSample.rgb;
    I get the following output:

    main.png

    If I use the following code:
    Code (CG):
    1. float3 detailTextureSample = tex2D(_TextureMap, detailUV);
    2. o.Albedo = detailTextureSample.rgb;
    I get the following output:
    detail.png

    If I use this code to try to combine the two:
    Code (CG):
    1. float3 mainTextureSample = tex2D(_TextureMap, textureUV);
    2. float3 detailTextureSample = tex2D(_TextureMap, detailUV);
    3. o.Albedo = mainTextureSample.rgb - detailTextureSample.rgb;
    I get the following:
    combo.png

    Since the detail sample is returning black, I'd expect to see something like:
    expected.png

    Am I doing something wrong? My shader was working in 5.3, but in 5.4 it seems that it isn't calculating things the same way?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    You might want to look at the generated shader code, there have been some bugs with the surface shader generation code in Unity 5.

    Mainly it's quite possible the surface shader is using the same UV for both the main and detail UVs when trying to use them both which is clearly wrong.
     
    Last edited: Aug 7, 2016
  3. afuzzyllama

    afuzzyllama

    Joined:
    May 19, 2010
    Posts:
    20
    As far as I can tell, the shader seems to be packing the texture uvs correctly.

    If i move all of my surface shader code to a finalcolor shader, I get the expected output. Unfortunately, that also strips the lighting information off of the final color which I need.

    I am running the code on my Mac, so it is running OpenGL 4. When I get back to my Windows machine, I'm going to see if the DirectX shader also produces the same result.