Search Unity

Tinted Shader + Alpha = reversed normals?

Discussion in 'Shaders' started by chdrvn, Nov 28, 2014.

  1. chdrvn

    chdrvn

    Joined:
    Dec 4, 2013
    Posts:
    23
    I have a version of my regular weapon material with an added blue glow custom shader (mostly cut and pasted, im a shader troglodyte). I use it for the weapon pickups, as opposed to the weapon in the players hand.

    I have a new weapon now that relies on the alpha channel for a transparent/cutout/specular bumped material. When I try and add an alpha channel to my custom glow shader the normals are reversed!!

    Ive read about an issue involving mirrored symetrical UVs on meshes imported via fbx from maya also. Not sure if this is my problem or its my custom shader. All meshes and materials/normals looked kosher. just the addition of the alpha channel on my shader that reversed the normals.

    Code (CSharp):
    1.  
    2. Shader "Custom/ItemGlow" {
    3.     Properties {
    4.         _ColorTint("Colour Tint",Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB) Alpha(A)", 2D) = "white" {}
    6.         _BumpMap ("Normal Map" ,2D)="bump"{}
    7.         _RimColor("Rim Color",Color)=(1,1,1,1)
    8.         //_RimPower("Rim Power",Range(0.1,6.0))=3.0
    9.      
    10.     }
    11.     SubShader {
    12.         Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    13.         ZWrite Off
    14.          Blend SrcAlpha OneMinusSrcAlpha
    15.      
    16.         CGPROGRAM
    17.         #pragma surface surf Lambert alpha
    18.  
    19.      
    20.  
    21.         struct Input
    22.         {
    23.             float4 color : Color;
    24.             float2 uv_MainTex;
    25.             float2 uv_BumpMap;
    26.             float3 viewDir;
    27.         };
    28.         float4 _ColorTint;
    29.         sampler2D _MainTex;
    30.         sampler2D _BumpMap;
    31.         float4 _RimColor;
    32.         float _RimPower;
    33.      
    34.         void surf (Input IN, inout SurfaceOutput o)
    35.         {
    36.      
    37.             IN.color = _ColorTint;
    38.          
    39.             o.Albedo = tex2D (_MainTex,IN.uv_MainTex).rgb * IN.color;
    40.             o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
    41.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    42.             //o.Normal.y *= sign(unity_Scale.y);
    43.             half rim = 1.0 - saturate(dot(normalize(IN.viewDir),o.Normal));
    44.          
    45.             o.Emission = _RimColor.rgb * pow(rim,0.7);//<---_RimPower goes there,
    46.          
    47.         }
    48.         ENDCG
    49.     }
    50.     FallBack "Diffuse"
    51. }
    52.  
    53.  
    this was my glow shader that had correct normals before i needed the alpha channel
    Code (CSharp):
    1.  
    2. Shader "Custom/ItemGlow" {
    3.     Properties {
    4.         _ColorTint("Colour Tint",Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _BumpMap ("Normal Map",2D)="bump"{}
    7.         _RimColor("Rim Color",Color)=(1,1,1,1)
    8.         //_RimPower("Rim Power",Range(0.1,6.0))=3.0
    9.      
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.      
    14.      
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert
    17.  
    18.      
    19.  
    20.         struct Input
    21.         {
    22.             float4 color : Color;
    23.             float2 uv_MainTex;
    24.             float2 uv_BumpMap;
    25.             float3 viewDir;
    26.         };
    27.         float4 _ColorTint;
    28.         sampler2D _MainTex;
    29.         sampler2D _BumpMap;
    30.         float4 _RimColor;
    31.         float _RimPower;
    32.      
    33.         void surf (Input IN, inout SurfaceOutput o)
    34.         {
    35.      
    36.             IN.color = _ColorTint;
    37.             o.Albedo = tex2D (_MainTex,IN.uv_MainTex).rgb * IN.color;
    38.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    39.             half rim = 1.0 - saturate(dot(normalize(IN.viewDir),o.Normal));
    40.             o.Emission = _RimColor.rgb * pow(rim,0.7);//<---_RimPower goes there,
    41.          
    42.         }
    43.         ENDCG
    44.     }
    45.     FallBack "Diffuse"
    46. }
    47.  
    Thanks for any help fellow game creators
     
    Last edited: Nov 28, 2014
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    A screenshot could help to identify the problem. When using alpha blending with complex objects, depth sorting is what causes headache usually. Tim described the problem and a solution in the Transparent Depth Shader (good for ghosts!) thread, is this what you mean with reversed normals?
     
  3. chdrvn

    chdrvn

    Joined:
    Dec 4, 2013
    Posts:
    23

    Not sure that depth sorting is the problem. The top image is before I added the alpha channel, bottom afterwards.
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    It is. With z writing disabled, you're rendering the inside parts over the outside parts. Normals are fine.
     
  5. chdrvn

    chdrvn

    Joined:
    Dec 4, 2013
    Posts:
    23
    hmmmm, I commented out the ZWrite line, still looks the same. Appreciate the feedback though, thanks.