Search Unity

Prepass Queue and Custom lighting

Discussion in 'Shaders' started by Phantomx, Jul 21, 2014.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hey!

    I want to have a texture for the specular color, so I made a shader for that, but I'm having some issues...
    It works fine as long as I'm in the transparent Queue, as soon as I switch to Geometry queue the lighting becomes all wrong on the model...

    here's the shader.

    Code (CSharp):
    1. Shader "Characters/DNSE" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _SpecTex("Specular Texture",2D) = "black" {}
    5.         _Shininess("Glossiness",Range(0,2)) = 1.0
    6.         _Bump("NormalMap",2D) = "bump" {}
    7.     }
    8.     SubShader {
    9.         Tags { "Queue" = "Geometry" "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         #pragma surface surf Characters nolightmap nodirlightmap
    14.  
    15.         sampler2D _MainTex;
    16.         sampler2D _Bump;
    17.         sampler2D _SpecTex;
    18.        
    19.         float _Shininess;
    20.        
    21.         struct CustomSurfaceOutput
    22.         {
    23.             fixed3 Albedo;
    24.             fixed3 Normal;
    25.             fixed3 Emission;
    26.             fixed3 GlossColor;
    27.             fixed Specular;
    28.             fixed Alpha;
    29.         };
    30.  
    31.         struct Input {
    32.             float2 uv_MainTex;
    33.         };
    34.        
    35.        
    36.         inline fixed4 LightingCharacters ( CustomSurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten){
    37.  
    38.             half3 h = normalize (lightDir + viewDir);
    39.  
    40.             fixed NdotL = max (0, dot (s.Normal, lightDir) * 0.5 + 0.5 );
    41.             fixed NdotH = max (0, dot (s.Normal, h));
    42.  
    43.             fixed3 spec = pow (NdotH, _Shininess*128.0);
    44.             fixed3 specCol = spec * s.GlossColor.rgb;
    45.            
    46.             fixed4 c;
    47.            
    48.             c.rgb = (s.Albedo * _LightColor0.rgb * NdotL + _LightColor0.rgb * specCol) * (atten * 2.0);
    49.             c.a = s.Alpha + _LightColor0.a * spec * atten;
    50.            
    51.             return c;
    52.  
    53.         }
    54.        
    55.         inline fixed4 LightingCharacters_PrePass (CustomSurfaceOutput s, half4 light)
    56.         {
    57.             fixed3 spec = light.a * s.GlossColor;
    58.          
    59.                fixed4 c;
    60.             c.rgb = s.Albedo * light.rgb + light.rgb * spec;
    61.             c.a = s.Alpha + spec;
    62.             return c;
    63.         }
    64.  
    65.         void surf (Input IN, inout CustomSurfaceOutput o) {
    66.             fixed3 tex = tex2D (_MainTex, IN.uv_MainTex);
    67.             fixed3 spec = tex2D (_SpecTex, IN.uv_MainTex);
    68.             fixed3 bump = UnpackNormal(tex2D(_Bump,IN.uv_MainTex));
    69.            
    70.             o.Albedo = tex;
    71.             o.Normal = bump;
    72.             o.GlossColor = spec;
    73.  
    74.         }
    75.         ENDCG
    76.     }
    77.     FallBack "Diffuse"
    78. }
    79.  
    the forward pass works fine, only the deferred pass gives me this problem...

    Thanks!
     
  2. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    nice question, i want to see a specular color shader, geometry shader is DX11 only ??!? do you have a geom shader there? i see that the CG isnt specified which version i.e. dx9 / dx11 , the version config line is missing. i dunno i am abit noob.
     
  3. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    no, that's not a geomerty shader, just a regular sufrace shader with custom lighting model.