Search Unity

Third layer of a 3-layer main texture is translucent when it shouldn't be

Discussion in 'Shaders' started by Scrillrock, Jun 13, 2019.

  1. Scrillrock

    Scrillrock

    Joined:
    Jun 13, 2019
    Posts:
    3
    So I'm very new to shaders and trying out something fairly ambitious for a beginner in this area which, surprisingly, has been going very well so far. I'm stuck up to a wall right now though and honestly can't tell what I'm doing wrong.

    What my shader is meant to do is basically stack several textures up and applies them as a single albedo texture to a model. Of course, there is an opaque base layer and everything else has an alpha layer. While I've figured out how to have only two layers, when i try and add one more, similar to the alpha one before it, it turns out translucent. Whatever i do to the rgb or alpha channels, it never seems to make the non-alpha area opaque. I'd appreciate any help, really weirded out by this one.

    Here's the shader:

    Code (CSharp):
    1. Shader "Custom/multLayers" {
    2.     Properties {
    3.         _Color ("Base Color", Color) = (1,1,1,1)
    4.         _BaseTex ("Main Texture (RGB)", 2D) = "" {}
    5.         _WebTex ("Web Texture (RGB)", 2D) = "" {}
    6.         _EyeTex ("Eye Texture (RGB)", 2D) = "" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.  
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.         sampler2D _WebTex;
    23.         sampler2D _EyeTex;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.         fixed4 _Color;
    32.  
    33.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    34.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    35.         // #pragma instancing_options assumeuniformscaling
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o) {
    41.             // Albedo comes from a texture tinted by color
    42.             float4 mainTex = tex2D (_MainTex, IN.uv_MainTex) ;
    43.             float4 webTex = tex2D (_WebTex, IN.uv_MainTex);
    44.             float4 eyeTex = tex2D (_EyeTex, IN.uv_MainTex);
    45.             half3 mainTexVisible = (mainTex.rgb*_Color) * (1-webTex.a);
    46.             half3 webTexVisible = webTex.rgb * (webTex.a/4);
    47.             half3 eyeTexVisible = eyeTex.rgb;
    48.             float3 finalColor = (mainTexVisible + webTexVisible + eyeTexVisible);
    49.             o.Albedo = finalColor.rgb;
    50.             // Metallic and smoothness come from slider variables
    51.             o.Metallic = _Metallic;
    52.             o.Smoothness = _Glossiness;
    53.             o.Alpha = 1 * (mainTex.a + webTex.a + eyeTex.a);
    54.         }
    55.         ENDCG
    56.     }
    57.     FallBack "Diffuse"
    58. }
     
  2. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    Code (CSharp):
    1. float3 finalColor = (mainTexVisible + webTexVisible + eyeTexVisible);
    The problem is you're adding the color values together together. Instead of adding them you could do a couple color lerps using the alpha to determine which color overlays which. Also there seem to be some calculations in their that you don't need. Try this perhaps:

    Code (CSharp):
    1.         void surf (Input IN, inout SurfaceOutputStandard o) {
    2.             // Albedo comes from a texture tinted by color
    3.             float4 mainTex = tex2D (_MainTex, IN.uv_MainTex) ;
    4.             float4 webTex = tex2D (_WebTex, IN.uv_MainTex);
    5.             float4 eyeTex = tex2D (_EyeTex, IN.uv_MainTex);
    6.  
    7.             float3 finalColor = lerp(mainTex.rgb * _Color.rgb,  webTex.rgb, webTex.a);
    8.             finalColor = lerp(finalColor, eyeTex.rgb, eyeTex.a);
    9.             o.Albedo = finalColor;
    10.  
    11.             // Metallic and smoothness come from slider variables
    12.             o.Metallic = _Metallic;
    13.             o.Smoothness = _Glossiness;
    14.             o.Alpha = saturate(mainTex.a + webTex.a + eyeTex.a);
    15.         }
     
    Scrillrock and jvo3dc like this.
  3. Scrillrock

    Scrillrock

    Joined:
    Jun 13, 2019
    Posts:
    3
    Yeah, so, this was an old late night desperation post and I've figured it out since then. Sorry to have not checked the for replies or closed the thread recently. Thank you for the suggestion nonetheless, really appreciate it!