Search Unity

Simplified Cook-Torrance

Discussion in 'Shaders' started by dirtybassett, Nov 8, 2014.

  1. dirtybassett

    dirtybassett

    Joined:
    Oct 3, 2012
    Posts:
    59
    I have tried to make a fast pbr shader using simplified cook-torrance but it still seems slow, Was wondering if anyone knows if there is a faster way?
    Code (CSharp):
    1. Shader "Custom/simple AO " {
    2.     Properties {
    3.        
    4.         _MainTex ("Base (RGB) Rough (A)", 2D) = "grey" {}
    5.    
    6.      
    7.         _BumpMap ("Normalmap", 2D) = "bump" {}
    8.      
    9.      
    10.  
    11.  
    12.  
    13.     }
    14.     SubShader {
    15.         Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" "IgnoreProjector"="True" }
    16.         LOD 400
    17.         CGPROGRAM
    18.  
    19.     #pragma surface surf Physical    exclude_path:prepass    novertexlights noforwardadd nolightmap halfasview
    20.         sampler2D _MainTex;
    21.  
    22.         sampler2D _BumpMap;
    23.    
    24.  
    25.  
    26.      
    27.      
    28. struct Input {
    29.         float2 uv_MainTex;
    30.         half nv;
    31.         float4 ambcolor;
    32.      
    33.      
    34.         };
    35.  
    36.     inline    fixed4 LightingPhysical( SurfaceOutput s, half3 lightDir, half3 halfDir, half atten )
    37.         {
    38.      
    39.     half diff=  max(0,dot(s.Normal,lightDir));
    40.  
    41.     half nh = saturate(dot (s.Normal, halfDir));
    42.  
    43.     half lh =  saturate(dot (halfDir,lightDir));
    44.     half A=s.Alpha;
    45.  
    46.  
    47.  
    48.     half spec = ((A+1)*exp2(A*nh-A))/(1+(10*(lh*lh*lh)));
    49.  
    50.     fixed3 main =(s.Albedo+spec) * _LightColor0.rgb* (diff *atten);
    51.  
    52.     fixed4 c;
    53.  
    54.     c.rgb =main;
    55.     c.a = 0;
    56.     return c;
    57.         }
    58.    
    59.         void surf (Input IN, inout SurfaceOutput o) {
    60.          
    61.          
    62.          
    63.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    64.          
    65.             fixed3 main = c;
    66.             o.Albedo = main;
    67.      
    68.             o.Alpha =1*exp2(8* (c.a)+1) ;
    69.             o.Normal =UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    70.          
    71.  
    72.          
    73.         }
    74.         ENDCG
    75.     }
    76.     FallBack "Mobile/Diffuse"
    77. }