Search Unity

Shaders not working at all, Custom Lighting looks like Vertex Illum

Discussion in 'Shaders' started by Gistix, Jan 23, 2015.

  1. Gistix

    Gistix

    Joined:
    Jul 21, 2013
    Posts:
    29
    How it looks (Custom Lighting function):

    How it should look (Unity Specular):


    My Shader:
    Code (CSharp):
    1.  
    2. Shader "Custom/Nose_dsf" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     //_FresnelThickness ("Fresnel Thickness", Range(0, 10)) = 1      
    9. }
    10.  
    11. SubShader {
    12.     Tags { "RenderType"="Opaque" }
    13.     LOD 300
    14.  
    15. CGPROGRAM
    16. #pragma surface surf CustomBlinnPhong
    17.  
    18. sampler2D _MainTex;
    19. fixed4 _Color;
    20. half _Shininess;
    21. //half _FresnelThickness;
    22.  
    23. struct Input {
    24.     float2 uv_MainTex;
    25.     //float3 viewDir;
    26. };
    27.  
    28. inline fixed4 LightingCustomBlinnPhong (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
    29. {
    30.     half3 h = normalize (lightDir + viewDir);
    31.  
    32.     fixed diff = max (0, dot (s.Normal, lightDir));
    33.  
    34.     float nh = max (0, dot (s.Normal, h));
    35.     float spec = pow (nh, s.Specular*128.0) * s.Gloss;
    36.  
    37.     fixed4 c;
    38.     c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
    39.     c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
    40.     return c;
    41. }
    42.  
    43. void surf (Input IN, inout SurfaceOutput o) {
    44.     //half fresnel = pow(max(0,dot(o.Normal, normalize(IN.viewDir))),_FresnelThickness);
    45.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    46.     o.Albedo = (tex.rgb * _Color.rgb) /*+ fresnel*/;
    47.     o.Gloss = tex.a;
    48.     o.Alpha = tex.a * _Color.a;
    49.     o.Specular = _Shininess;
    50. }
    51. ENDCG
    52. }
    53.  
    54. Fallback "Diffuse"
    55. }
    56.  
    Trying to add "fresnel" will also give unexpected results, will only work on the Editor preview, won't work at all on Scene/Game tab.
    I also tried with #pragma target 3.0

    Unity 4.6.1
    Windows 7 64 bits
    ATI Mobility Radeon HD 4650, DX 10.1
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh dear... this weird issue again! :D

    I get that, even with Unity's specular shader! the fix, believe it or not, is to use Normal Mapping in your shader, as that, for some reason, gives a decent highlight with no bump-map applied... ;)

    This also occurs with any custom lighting models, including the Cook-Torrance BRDF I wrote once. The fix was using normal maps, of which can be a pest if you end up blowing the interpolator limit with surface shaders. On the other hand though, if you do need normal mapping anyway, then such a problem won't be there.

    EDIT: Oh, and another note as well: the Vertex-Lit Specular looks far worse than that: on a low-poly mesh, the highlight flickers, rather than doing what the screenshots above are doing, when rotating the regular sphere mesh at various angles.

    A sample comparison: Rotate two spheres, one with your shader, one with vertex-lit specular, and you will see what I mean. (The highlights blend completely differently to each other)
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    First things first, clamp your specular and diffuse
    Code (CSharp):
    1. fixed diff = max (0, saturate(dot (s.Normal, lightDir)));
    Code (CSharp):
    1.  float nh = max (0, saturate(dot(s.Normal, h)));
    Now its been a while and i never had that issue for a long time, but if i remeber correctly you should normalize your normal direction

    Not necessarily, normal maps could work but most of the time they are not the solution to fixing the issue.

    Also, make sure you are in forward or deffered (deffered is for pro but you can use forward to), when using vertex lit rendering path youll get that vertex lighting which is ugly.
     
    Last edited: Jan 27, 2015