2014年7月7日 星期一

GGX BRDF

最近很流行 也來試作一下
float G_Smith(float roughness, float NoV, float NoL)
   {
    //Schlich model
    //G(l,v,h) = G1(l)G1(v)
     float  k = (roughness + 1) * (roughness + 1) /8;
    return  (NoV / (NoV * (1 - k) + k)) *  (NoL / (NoL * (1 - k) + k));
   }
   
   fixed4 GGX_BRDF(float roughness, half3 lightDir, half3 viewDir, float3 Normal, fixed3 specularColor, fixed3 diffuseColor, fixed3 lightColor)
   {
      float pi = 3.14159;
      viewDir = normalize(viewDir);
      lightDir = normalize(lightDir);
      half3 h = normalize (lightDir + viewDir);
      float NdotL = max(0, dot ( Normal, lightDir));
      float NdotH = max (0, dot (Normal, h));
      float NdotV = max(0, dot(Normal, viewDir));
      float LdotH = max(0, dot(lightDir, h));
      float VdotH = max(0 , dot(viewDir, h));
      float3 Kd = 0;
          lightColor.rgb *= NdotL;
          Kd = diffuseColor * _MainColor / pi;
          half4 c;
          c.rgb = Kd * lightColor;
          
          //GGX NDF          
          float alpha =  roughness *  roughness;
          float beta = ((NdotH * NdotH) * (alpha*alpha -1.0) + 1.0);
          float Specular_D =  alpha * alpha/ (pi * beta * beta);
          fixed3 f0 = specularColor;
          float G = G_Smith(roughness, NdotV, NdotL);          
          float Specular_G = G * VdotH / (NdotH , NdotV);
          fixed3 Fschlick =  f0 + (fixed3(1,1,1) - f0)* pow(1 - LdotH, 5);
          c.rgb += Specular_D*Specular_G*Fschlick * lightColor.rgb;
          c.a = 1;
          return c;
   }

沒有留言 :

張貼留言