Search Unity

What is wrong with my shader? Lossing detail in the distance

Discussion in 'Shaders' started by RaymingW, Nov 28, 2019.

  1. RaymingW

    RaymingW

    Joined:
    Nov 11, 2017
    Posts:
    26
    Hi all,

    I wrote a very simple shader for prototyping a game. The goal is to swap the color of a car on the fly without editing texture every time.

    It is achieved by a color exclude map that told shader when to use the alternate color.



    You can notice that the model starts to lose detail when the camera moves away, which did not happen when there is no exclude map applied.

    Here is my code:
    Code (CSharp):
    1. void surf (Input IN, inout SurfaceOutputStandard o)
    2.         {
    3.             // Albedo comes from a texture tinted by color
    4.             fixed4 exclude = tex2D(_ExcludeTex, IN.uv_MainTex);
    5.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    6.             if(exclude.r>0){
    7.                 c.rgb = _AlterColor.rgb;
    8.             }
    9.          
    10.             o.Albedo = c.rgb;
    11.             // Metallic and smoothness come from slider variables
    12.             o.Metallic = _Metallic;
    13.             o.Smoothness = _Glossiness;
    14.             o.Alpha = c.a;
    15.         }
    Can someone help me out, please?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,683
    Because your exclude texture is showing a smaller mip map as you get further away, and you’ve got your shader code setup to check for anything over 0, which is going to eventually expand to the entire texture as the mip gets smaller.

    You can try using > 0.5, but you’ll get some color fringing. The best solution would be something like this:
    https://medium.com/@bgolus/the-team-color-problem-b70ec69d109f
     
    Last edited: Nov 28, 2019
    RaymingW likes this.
  3. RaymingW

    RaymingW

    Joined:
    Nov 11, 2017
    Posts:
    26
    Wow, your blog is really eye-opening!

    In the blog, you also mentioned the bright fringes issue, which is super helpful!

    Thank you for the amazing blog!