Search Unity

Bumped anisotropic lighting

Discussion in 'Shaders' started by metervara, May 11, 2011.

  1. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    I've ported the anisotropic lighting shader from the wiki to a surface shader - works fine. Wanted to create a bumped version, but I get a lot of artifacts on the surface, anyone know why?



    Code (csharp):
    1. Shader "Metervara/Anisotropic Bump" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}
    6.         _BumpMap ("Normalmap", 2D) = "bump" {}
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType" = "Opaque" }
    10.         CGPROGRAM
    11.         #pragma surface surf Anisotropic
    12.        
    13.         sampler2D _AnisoTex;
    14.      
    15.         half4 LightingAnisotropic (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
    16.            
    17.             half3 halfAngle = normalize(viewDir + lightDir);
    18.            
    19.             float2 uvA;
    20.             uvA.x = dot(lightDir,s.Normal);
    21.             uvA.y = dot(halfAngle,s.Normal);
    22.            
    23.             half4 aniso = tex2D( _AnisoTex, uvA );
    24.            
    25.             half4 c;
    26.             c.rgb = s.Albedo * _LightColor0.rgb * aniso.rgb * (aniso.a * atten * 2);
    27.             c.a = s.Alpha;
    28.             return c;
    29.     }
    30.  
    31.     struct Input {
    32.         float2 uv_MainTex;
    33.         float2 uv_BumpMap;
    34.     };
    35.      
    36.     sampler2D _MainTex;
    37.     sampler2D _BumpMap;
    38.     fixed4 _Color;
    39.      
    40.     void surf (Input IN, inout SurfaceOutput o) {
    41.         o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
    42.         o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    43.     }
    44.     ENDCG
    45.   }
    46.   Fallback "Diffuse"
    47. }
     

    Attached Files:

  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I've encountered those little squares with image-based lighting, too. I don't know what causes it (other than the texture lookup), and would definitely like to know how to get rid of it.
     
  3. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Turning mip-maps off for the Anisotropic Lookup texture fixed it. Yay!
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I originally suspected that that was the problem, but I thought I tried disabling mipmaps and it did not fix the issue. I will check again.
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I just checked and that fixes the problem for the matcap shader as well. I must have been misremembering.

    What I would like to know now is whether there is a way to tell the graphics card to use mipmaps but not be insane about it. I assume it's similar to this problem: http://aras-p.info/blog/2010/01/07/screenspace-vs-mip-mapping/