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;
   }

2014年7月1日 星期二

Get Selected AssetPath


常常寫EditorWindow但又常常忘了怎寫
把寫好的放上來,以後要改比較方便

順便提供一些小工具

當專案比較大時,資料夾結構很深,有時常需要知道asset的path。所以寫了一個小工具可以看Asset的path


using UnityEngine;
using System.Collections;
using UnityEditor;

public class GetSelectedAssetPath : EditorWindow {

 [MenuItem ("Window/GetSelectedAssetPath")]
 static void Init () {
  // Get existing open window or if none, make a new one:
  GetSelectedAssetPath window = (GetSelectedAssetPath)EditorWindow.GetWindow (typeof (GetSelectedAssetPath));
 }
 void OnGUI () {
  if (GUILayout.Button("Select", GUILayout.Width(60)))
  {
   Debug.Log(AssetDatabase.GetAssetPath(Selection.activeObject));
  }
 }

}